exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
βŒ€
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
βŒ€
difficulty
int64
-1
3.5k
βŒ€
prob_desc_output_spec
stringlengths
17
1.47k
βŒ€
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
3c6fb03537c5050f455ddd6d5fb63d51
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class D { //Solution by Sathvik Kuthuru public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = scan.nextInt(); for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader scan, PrintWriter out) { int n = scan.nextInt(), k = scan.nextInt(); int[][] res = new int[n][n]; Item[] col = new Item[n]; for(int i = 0; i < n; i++) { col[i] = new Item(0, i); } int at = 0; int[] a = new int[n], b = new int[n]; int need = k / n, extra = k % n; for(int i = 0; i < n; i++) { int curr = need; if(i < extra) curr++; for(int j = 0; j < curr; j++) { res[i][at] = 1; at = (at + 1) % n; } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) a[i] += res[i][j]; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) b[i] += res[j][i]; } int maxRow = 0, maxCol = 0, minRow = Integer.MAX_VALUE, minCol = Integer.MAX_VALUE; for(int i : a) { maxRow = Math.max(maxRow, i); minRow = Math.min(minRow, i); } for(int i : b) { maxCol = Math.max(maxCol, i); minCol = Math.min(minCol, i); } long x = maxRow - minRow, y = maxCol - minCol; out.println(x * x + y * y); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) out.print(res[i][j]); out.println(); } } static class Item implements Comparable<Item> { int val; int index; public Item(int a, int b) { val = a; index = b; } @Override public int compareTo(Item item) { return Integer.compare(val, item.val); } } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
2a52bdac4e881542b4854d8ccf2ea3e0
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); StringBuilder sb=new StringBuilder(); int t=s.nextInt(); for(int i=0;i<t;i++) { int n=s.nextInt(); int k=s.nextInt(); int x=k; int[][] dp=new int[n][n]; for(int j=0;j<n;j++) { int r=0; int c=j; int count=0; for(int l=0;l<n-j;l++) { if(k>0) { k--; dp[r+count][c+count]=1; } count++; } r=n-1; c=j; count=0; for(int l=0;l<=j;l++) { if(k>0) { k--; dp[r-count][c-count]=1; } count++; } } if(x%n==0) sb.append("0\n"); else { sb.append("2\n"); } for(int j=0;j<n;j++) { for(int l=0;l<n;l++) { sb.append(dp[j][l]); } sb.append("\n"); } } System.out.println(sb); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
834d58dd39724f478e1f83e4ef8a9028
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class D { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieveOfEratosthenes(int n) { boolean[] prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p]) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } return prime; } public static int[] lowestPrimeFactors(int n) { int tot = 0; int[] lpf = new int[n + 1]; int u = n + 32; double lu = Math.log(u); int[] primes = new int[(int) (u / lu + u / lu / lu * 1.5)]; for (int i = 2; i <= n; i++) lpf[i] = i; for (int p = 2; p <= n; p++) { if (lpf[p] == p) primes[tot++] = p; int tmp; for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) { lpf[tmp] = primes[i]; } } return lpf; } static int bsearch(List<Integer> list,int x) { int l=0,r=list.size()-1; while(l<=r) { int mid=(l+r)/2; if(list.get(mid)>=x) { if(mid==0) { return mid; } else if(list.get(mid-1)<x) { return mid; } else { r=mid-1; } } else { l=mid+1; } } return -1; } static long divCount(long n) { HashMap<Long,Integer> map=new HashMap<>(); while(n%2==0) { n/=2; map.put(2L,map.getOrDefault(2L,0)+1); } for(long i=3;i<=Math.sqrt(n);i+=2) { while(n%i==0) { map.put(i,map.getOrDefault(i,0)+1); n/=i; } } if(n>2) { map.put(n,1); } long prod=1L; for(long i:map.keySet()) { // System.out.println(i+" "+map.get(i)); prod*=(map.get(i)+1); } return prod-2; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t= 1; while (t-- > 0) { int n=ri(); int k=ri(); int min=k/n; int max=min; if(k%n!=0) { max++; } int cmax=k-(n*min); int cmin=n-cmax; int x=0; int[] row=new int[n]; int[] col=new int[n]; int[][] res=new int[n][n]; for(int i=0;i<cmax;i++) { for(int j=0;j<max;j++) { res[i][x]=1; row[i]++; col[x]++; x++; x%=n; } } for(int i=cmax;i<n;i++) { for(int j=0;j<min;j++) { res[i][x]=1; row[i]++; col[x]++; x++; x%=n; } } int m1=Integer.MIN_VALUE,m2=Integer.MAX_VALUE; for(int i:row) { m1=Math.max(m1,i); m2=Math.min(m2,i); // System.out.print (i+" "); } long outp=(m2-m1)*(m2-m1); m1=Integer.MIN_VALUE;m2=Integer.MAX_VALUE; for(int i:col) { m1=Math.max(m1,i); m2=Math.min(m2,i); } outp+=(m2-m1)*(m2-m1); ans.append(outp).append("\n"); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { ans.append(res[i][j]); } ans.append("\n"); } } out.print(ans.toString()); out.flush(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
d891d81db4cceb4135e6578ffbd49ada
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class D { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieveOfEratosthenes(int n) { boolean[] prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p]) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } return prime; } public static int[] lowestPrimeFactors(int n) { int tot = 0; int[] lpf = new int[n + 1]; int u = n + 32; double lu = Math.log(u); int[] primes = new int[(int) (u / lu + u / lu / lu * 1.5)]; for (int i = 2; i <= n; i++) lpf[i] = i; for (int p = 2; p <= n; p++) { if (lpf[p] == p) primes[tot++] = p; int tmp; for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) { lpf[tmp] = primes[i]; } } return lpf; } static int bsearch(List<Integer> list,int x) { int l=0,r=list.size()-1; while(l<=r) { int mid=(l+r)/2; if(list.get(mid)>=x) { if(mid==0) { return mid; } else if(list.get(mid-1)<x) { return mid; } else { r=mid-1; } } else { l=mid+1; } } return -1; } static long divCount(long n) { HashMap<Long,Integer> map=new HashMap<>(); while(n%2==0) { n/=2; map.put(2L,map.getOrDefault(2L,0)+1); } for(long i=3;i<=Math.sqrt(n);i+=2) { while(n%i==0) { map.put(i,map.getOrDefault(i,0)+1); n/=i; } } if(n>2) { map.put(n,1); } long prod=1L; for(long i:map.keySet()) { // System.out.println(i+" "+map.get(i)); prod*=(map.get(i)+1); } return prod-2; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t= 1; while (t-- > 0) { int n=ri(); int k=ri(); int min=k/n; int max=min; if(k%n!=0) { max++; } int cmax=k-(n*min); int cmin=n-cmax; int x=0; int[] row=new int[n]; int[] col=new int[n]; int[][] res=new int[n][n]; for(int i=0;i<cmax;i++) { for(int j=0;j<max;j++) { res[i][x]=1; row[i]++; col[x]++; x++; x%=n; } } for(int i=cmax;i<n;i++) { for(int j=0;j<min;j++) { res[i][x]=1; row[i]++; col[x]++; x++; x%=n; } } int m1=Integer.MIN_VALUE,m2=Integer.MAX_VALUE; for(int i:row) { m1=Math.max(m1,i); m2=Math.min(m2,i); // System.out.print (i+" "); } long outp=0; if(max!=min) { outp=2; } ans.append(outp).append("\n"); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { ans.append(res[i][j]); } ans.append("\n"); } } out.print(ans.toString()); out.flush(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
e9da93df784766f747481d875d207ea2
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; public class CFJava6 { static class FIO { BufferedReader in; StringTokenizer st; FIO() { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } FIO(String name) throws IOException { in = new BufferedReader(new FileReader(name + ".in")); out = new PrintWriter(new FileWriter(name + ".out")); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } static PrintWriter out; static FIO in; public static void main(String[] args) throws IOException { in = new FIO(); int tc = in.nextInt(); while (tc-- > 0) new solver(); out.close(); } static class solver { solver() { int N = in.nextInt(), K = in.nextInt(); int[][] res = new int[N][N]; for (int m = 0; m < K/N; ++m) { for (int i = 0; i < N; ++i) { res[i][(i + m)%N] = 1; } } for (int i = 0; i < (K%N); ++i) { res[i][(i + K/N)%N] = 1; } out.println((K%N == 0) ? 0 : 2); for (int i = 0; i < N; ++i) { for (int x : res[i]) out.print(x); out.println(); } //out.println(); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
b77f7b63a8c2525384911e975b60a205
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.*; import java.util.*; public class cp1 { public static void main(String args[]) { int t=II(); while(--t>=0){ int n=II(); int k=II(); int a[][]=new int[n][n]; int range=k/n; for(int i=0;i<n;i++){ int extra=0; if(i<k%n) extra=1; for(int j=i;j<i+range+extra;j++){ a[i][j%n]=1; } } if(k%n==0) System.out.println(0); else System.out.println(2); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(a[i][j]); } System.out.println(); } } } //////////////////////////////////// static scan in=new scan(System.in); static int II() { return in.nextInt(); } static long IL() { return in.nextLong(); } static int[] IIA(int n) { int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=II(); } return a; } static String IS() { return in.next(); } static char IC(){ return in.next().charAt(0); } static String[] ISA(int n) { String a[]=new String[n]; for(int i=0;i<n;i++) { a[i]=IS(); } return a; } static char[] ICA(int n) { char a[]=new char[n]; for(int i=0;i<n;i++) { a[i]=IC(); } return a; } } class scan { public static BufferedReader reader; public static StringTokenizer token; scan(InputStream str) { reader=new BufferedReader(new InputStreamReader(str)); token=null; } static int nextInt() { while(token==null||!token.hasMoreTokens()) { try { token=new StringTokenizer(reader.readLine()); } catch(IOException e){ throw new RuntimeException(e); } } return Integer.parseInt(token.nextToken()); } static long nextLong() { while(token==null||!token.hasMoreTokens()) { try { token=new StringTokenizer(reader.readLine()); } catch(IOException e){ throw new RuntimeException(e); } } return Long.parseLong(token.nextToken()); } static String next() { while(token==null||!token.hasMoreTokens()) { try { token=new StringTokenizer(reader.readLine()); } catch(IOException e){ throw new RuntimeException(e); } } return token.nextToken(); } } // data = {['name':'Protien', 'id':'1234', 'brand':'muscleblaze', 'about':'fopr building muscle'....],[],[]}
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
d54364419cd6c4d4024083084c030a4c
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.*; public class c { static int mod = (int) 1e9 + 7; static int Infinity=Integer.MAX_VALUE; static int negInfinity=Integer.MIN_VALUE; public static void main(String args[]) { Scanner d= new Scanner(System.in); int t; int n,k,s,i,j,x; t=d.nextInt(); for(;t>0;t--) { n=d.nextInt(); k=d.nextInt(); int a[][]=new int[n][n]; for(i=0;i<n;i++) Arrays.fill(a[i],1); s=n*n; x=0; while(s>k) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(s>k && (j-i==x||i-j==n-x)) { a[i][j]=0; s--; } if(s<=k) break; } } x++; } int r[]=new int[n]; int c[]=new int[n]; for(i=0;i<n;i++) { for(j=0;j<n;j++) { r[i]+=a[i][j]; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { c[i]+=a[j][i]; } } Arrays.sort(r); Arrays.sort(c); System.out.println((int)Math.pow((r[n-1]-r[0]),2)+(int)Math.pow((c[n-1]-c[0]),2)); for(i=0;i<n;i++) { for(j=0;j<n;j++) System.out.print(a[i][j]); System.out.println(); } } } } //System.out.println();
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
482ceaaf07dbea8175eaecd94d4ce5a3
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t--!=0){ String[] in= br.readLine().split(" "); // long a= Long.parseLong(in[0]);//Integer.parseInt(in[0]); // long b= Long.parseLong(in[1]);//Integer.parseInt(in[1]); int n = Integer.parseInt(in[0]);//Integer.parseInt(in[0]); int k = Integer.parseInt(in[1]);//Integer.parseInt(in[1]); int[][] arr = new int[n][n]; int aage = 0; int row = 0; while(k--!=0){ arr[row][(row+aage)%n] = 1; row++; if(row==n){ row = 0; aage += 1; } } int rMax = 0,rMin = n+1; int cMax = 0,cMin = n+1; for(int i=0;i<n;i++){ int sum = 0; int sum2 = 0; for(int j = 0;j<n;j++){ sum+=arr[i][j]; sum2+= arr[j][i]; } rMax = Math.max(rMax,sum); rMin = Math.min(rMin,sum); cMax = Math.max(cMax,sum2); cMin = Math.min(cMin,sum2); } System.out.println((int)(Math.pow(rMax-rMin,2)+Math.pow(cMin-cMax,2))); for(int i=0;i<n;i++){ for(int j = 0;j<n;j++){ System.out.print(arr[i][j]); } System.out.println(); } } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
519edd7c49900267963f305de24cb677
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int maxSquare(int[] arr){ int mn = Integer.MAX_VALUE; int mx = Integer.MIN_VALUE; for(int element:arr){ if(element>mx) mx = element; if(element<mn) mn = element; } int diff = mx - mn; return (diff*diff); } static FastReader s = new FastReader(); public static void solve(){ int n = s.nextInt(); int k = s.nextInt(); int ans = 0; if(k%n>0){ ans = 2; } int[][] arr = new int[n][n]; int p = 0; int q = 0; while(k>0){ k--; arr[p][q]=1; p++; q++; q = q%n; if(p==n){ p=0; q++; q = q%n; } } System.out.println(ans); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ System.out.print(arr[i][j]); } System.out.println(); } } public static void main(String[] args) { int T=1; T = s.nextInt(); while(T>0){ // System.out.println("TEST CASE: "+T); solve(); T--; } } } class Pair{ int first; int second; Pair(int a,int b){ this.first = a; this.second = b; } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
b666d5a8c2f795d5b85ae73f18131657
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); PrintWriter out = new PrintWriter(System.out); first: while (t-- != 0) { int n = sc.nextInt(),k = sc.nextInt(),v = k; int arr[][] = new int[n][n]; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(k > 0) { k--; arr[(i+j)%n][j] = 1; } } } if(v%n == 0) { out.println(0); } else { out.println(2); } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { out.print(arr[i][j]); } out.println(); } } out.close(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } static class intpair implements Comparable<intpair> { int x, y; intpair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(intpair o) { return Integer.compare(this.x, o.x); } // a = new pair [n]; // a[i] = new pair(coo,cost); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
8caf75aecab5237f4e772107ac0351f5
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.InputMismatchException; /** * @author Mubtasim Shahriar */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int t = sc.nextInt(); // int t = 1; while(t--!=0) { solver.solve(sc, out); } out.close(); } static class Solver { public void solve(InputReader sc, PrintWriter out) { int n = sc.nextInt(); int k = sc.nextInt(); int[][] arr = new int[n][n]; for(int i = 0; i < n; i++) { if(k<1) break; arr[i][i] = 1; k--; } boolean right = true; int ri = 0; int rj = 1; int li = n-1; int lj = 0; while(k>0) { if(right) { for(int i = 0, j = rj; j < n; i++, j++) { arr[i][j] = 1; k--; if(k<1) break; } rj++; } else { for(int i = li, j = 0; i < n; i++, j++) { arr[i][j] = 1; k--; if(k<1) break; } li--; } right = !right; } // for(int i = 0; i < n; i++) { // if(k<1) break; // arr[i][i] = 1; // k--; // } // int idx = 1; // while(k>0) { // int li = idx; // int lj = 0; // int ri = n-idx-1; // int rj = n-1; // while(li<n) { // arr[li++][lj++] = 1; // k--; // if(k<1) break; // arr[ri--][rj--] = 1; // k--; // if(k<1) break; // } // idx++; // } int[] row = new int[n]; int[] col = new int[n]; for(int i = 0; i < n; i++) { int now = 0; for(int j : arr[i]) if(j==1) now++; row[i] = now; } for(int j = 0; j < n; j++) { int now = 0; for(int i = 0; i < n; i++) if(arr[i][j]==1) now++; col[j] = now; } int rowmax = 0; int rowmin = Integer.MAX_VALUE; int colmax = 0; int colmin = Integer.MAX_VALUE; for(int i : row) { rowmax = Math.max(rowmax, i); rowmin = Math.min(rowmin, i); } for(int i : col) { colmax = Math.max(colmax, i); colmin = Math.min(colmin, i); } long ans = (long)(rowmax-rowmin)*(long)(rowmax-rowmin); ans += (long)(colmax-colmin)*(long)(colmax-colmin); out.println(ans); for(int[] ar : arr) { for(int i = 0; i < n; i++) { out.print(ar[i]); } out.println(); } } } static class InputReader { private boolean finished = false; 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 peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } 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 boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n){ int[] array=new int[n]; for(int i=0;i<n;++i)array[i]=nextInt(); return array; } public int[] nextSortedIntArray(int n){ int array[]=nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n){ int[] array=new int[n]; array[0]=nextInt(); for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt(); return array; } public long[] nextLongArray(int n){ long[] array=new long[n]; for(int i=0;i<n;++i)array[i]=nextLong(); return array; } public long[] nextSumLongArray(int n){ long[] array=new long[n]; array[0]=nextInt(); for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt(); return array; } public long[] nextSortedLongArray(int n){ long array[]=nextLongArray(n); Arrays.sort(array); return array; } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
1e926eb2b0c7ae3da85b92debb761ee4
train_003.jsonl
1593610500
A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \le i \le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \le j \le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\max(R)-\min(R))^2 + (\max(C)-\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\max(X)$$$ as the maximum value in $$$X$$$ and $$$\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; /** * @project: Codeforces654 * @author: izhanatuly * @date: 01/07/2020 */ public class D { static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int cases = scanner.nextInt(); for (int t = 0; t < cases; t++) { runTestCase(); } } public static void runTestCase() { int n = scanner.nextInt(); int k = scanner.nextInt(); int zeros = n*n - k; int[][] grid = new int[n][n]; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { grid[i][j] = 1; } } int col = 0; while(zeros > 0) { for(int i=0; i<Math.min(n, zeros); i++) { grid[i][(col+i+n)%n] = 0; } zeros = Math.max(0, zeros-n); col++; } int maxRow = 0, minRow = Integer.MAX_VALUE; int maxCol = 0, minCol = Integer.MAX_VALUE; for(int i=0; i<n; i++) { int row = 0; for(int j=0; j<n; j++) { row += grid[i][j]; } maxRow = Math.max(row, maxRow); minRow = Math.min(row, minRow); } for(int j=0; j<n; j++) { int column = 0; for(int i=0; i<n; i++) { column += grid[i][j]; } maxCol = Math.max(column, maxCol); minCol = Math.min(column, minCol); } int num = (maxCol-minCol)*(maxCol-minCol) +(maxRow-minRow)*(maxRow-minRow); System.out.println(num); for(int i=0; i<n; i++) { for (int j = 0; j < n; j++) { System.out.print(grid[i][j]); } System.out.println(); } } }
Java
["4\n2 2\n3 8\n1 0\n4 16"]
1 second
["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"]
NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy" ]
0f18382d450be90edf1fd1a3770b232b
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)Β β€” the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \le n \le 300, 0 \le k \le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.
1,600
For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.
standard output
PASSED
425d0211adffc1ed8d7218759800b30a
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Even_Odd { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader scn = new FastReader(); int n = scn.nextInt(); int[] res = new int[n]; for (int i = 0; i < n; i++) { int a = scn.nextInt(); int b = scn.nextInt(); res[i] = ans(a, b); } for (int i = 0; i < res.length; i++) { System.out.println(res[i]); } } public static int ans(int a, int k) { if (k > a) { return k - a; } else { if (k % 2 == a % 2) { return 0; } else { return 1; } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
419cd0288128c55b598e9b57698f3107
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.Scanner; public class s { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] res = new int[n]; for (int i = 0; i < n; i++) { int a = scn.nextInt(); int b = scn.nextInt(); res[i] = ans(a, b); } for (int i = 0; i < res.length; i++) { System.out.println(res[i]); } } public static int ans(int a, int k) { if (k > a) { return k - a; } else { if (k % 2 == a % 2) { return 0; } else { return 1; } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
4f565ed54c112b0c84452bdd18cc754b
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class s8{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException{ Reader scn = new Reader(); int n = scn.nextInt(); int[] res = new int[n]; for (int i = 0; i < n; i++) { int a = scn.nextInt(); int b = scn.nextInt(); res[i] = ans(a, b); } for (int i = 0; i < res.length; i++) { System.out.println(res[i]); } } public static int ans(int a, int k) { if (k > a) { return k - a; } else { if (k % 2 == a % 2) { return 0; } else { return 1; } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
c1a1557c0bca5a80026b0563f0ed6458
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import javax.management.Query; import javax.print.attribute.SupportedValuesAttribute; import java.io.*; import java.security.interfaces.DSAKeyPairGenerator; import java.sql.Array; import java.util.*; import java.util.function.Predicate; public class Main { static class TaskB { static int oo = Integer.MAX_VALUE - 1; static int MOD = 1000000007; public void solve(InputReader in, PrintWriter out) { int T = in.nextInt(); for(int tt = 0; tt < T; tt++) { int n = in.nextInt(); int k = in.nextInt(); if(n<=k) { out.println(k-n); continue; } if((n-k)%2==0) { out.println(0); continue; } out.println(1); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(in, out); out.close(); } static void sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for(Integer i: a)list.add(i); Collections.sort(list); for(int i = 0; i < a.length; i++)a[i]=list.get(i); } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public InputReader(InputStream inputStream) { this.reader = new BufferedReader( new InputStreamReader(inputStream)); } public String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
101d459bacada06170bdf51fcb01caf2
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; public class q1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i =0;i<t;i++) { int n = in.nextInt(); int k = in.nextInt(); if(k>n) { System.out.println(k-n); } else if(n>k) { if(n%2==0&k%2==0) { System.out.println(0); } else if(n%2!= 0&&k%2!=0) { System.out.println(0); } else if(n%2!=0&&k%2==0) { System.out.println(1); } else if(n%2==0&&k%2!=0) { System.out.println(1); } } else { System.out.println(0); } } // TODO Auto-generated method stub } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
b3bd513d45bcbf0ec70b7b328fb32551
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.Scanner; public class DistanceAndAxis { public static void main(String[] args) { Scanner s =new Scanner(System.in); int testCases=s.nextInt(); for(int i=0;i<testCases;i++){ int x=s.nextInt(); int diff=s.nextInt(); if(x<=diff){ System.out.println(Math.abs(diff-x)); } else{ if(x%2==diff%2)System.out.println(0); else System.out.println(1); } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
ebca0862c20be684e63107794c5d0b59
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; import java.io.*; public class p1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testCases = scan.nextInt(); for(int q=0; q<testCases; q++) { int n = scan.nextInt(); int k = scan.nextInt(); if(n == k) System.out.println(0); else if(n%2 == 0 && k == 0) System.out.println(0); else if(k < n && k%2 ==1 && n%2 == 1) { System.out.println(0); } else if (k<n && k%2 == 0 && n%2 == 0) System.out.println(0); else if(k>n) System.out.println(Math.abs(n-k)); else System.out.println(1); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
c685bc1c0dc1a89c73d95af1c09d7239
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; public class a { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int t = in.nextInt(); while(t-- > 0) { int a = in.nextInt(); int b = in.nextInt(); if(a < b) System.out.println(b-a); else { if(a%2 == b%2) System.out.println(0); else System.out.println(1); } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
4590c9f35ab93e46e2f976de4bd43564
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.Scanner; import java.lang.Math; public class prob { public static void main(String[] args) { Scanner input = new Scanner(System.in); long t; t=input.nextInt(); while(t-->0){ long a,b,c,d,x,y; a=input.nextInt(); b=input.nextInt(); if(a<=b){ x=b-a; System.out.println(x); } else{ y=(a/2); long k; long z=(a+b)%2; System.out.println(z); } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
77024abe82cbf1fb5b8933eb3d8cc6ef
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class solution { public static void main(String[] args) { FastScanner sc = new FastScanner() ; int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt() ; int k = sc.nextInt() ; if (k>n) System.out.println(k-n); else if (n%2 == k%2) System.out.println(0); else System.out.println(1); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
72304148fdccb5b77b4bc7ad4c7a5919
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; public class solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt() ; while (t-- > 0){ int n = sc.nextInt() ; int k = sc.nextInt() ; if (k>n) System.out.println(k-n); else if (n%2 == k%2) System.out.println(0); else System.out.println(1); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
404d50fb526816ac8f0a2c22fe33c6c7
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.*; import java.util.*; public class Solution3 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); while(t > 0) { String inp[] = br.readLine().trim().split("\\s"); int n = Integer.parseInt(inp[0]); int k = Integer.parseInt(inp[1]); //int limit = 2*n + k; int diff = 0; // for(int i=0; i<limit; i++) { // int curr = 2*i + k; // diff = Math.min(diff, (int)Math.abs(curr - n)); // } if(n-k > 0) { if((n-k) % 2 == 0) diff = 0; else diff = 1; } else { diff = k-n; } System.out.println(diff); t--; } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
82dc9c0d90b30c9e4c4803c2b242e0b3
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; public class practise { public static void main(String[] args) throws java.lang.Exception { Scanner sr = new Scanner(System.in); long T = sr.nextLong(); while (T-- > 0) { long ans; int n = sr.nextInt(); int k= sr.nextInt(); if(k==0) { if(n%2!=0) System.out.println(1); else System.out.println(0); } else if(k>0&&k>=n) System.out.println(k-n); else { if(((n)%2==0&&k%2==0)||(n%2==1&&k%2==1)) System.out.println(0); else System.out.println(1); } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
87ae1179074981260f4fe10503321c97
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub FastReader s = new FastReader(); int tc = s.nextInt(); for(int i = 0;i < tc;i++) { long n = s.nextLong(); int k = s.nextInt(); if(n == k) { System.out.println("0"); } else if(n > k) { if(((n % 2 == 0) && (k % 2 == 0)) || ((n % 2 == 1) && (k % 2 == 1))) { System.out.println("0"); } else { System.out.println("1"); } } else { System.out.println(k - n); } } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
5dec4aedd1ee20a435ca4e6f54859cc6
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Locale; public class A1401 implements Runnable { public InputReader in; public PrintWriter out; public void solve() throws Exception { // solution goes here int T = in.nextInt(); for (int t = 0; t < T; t++) { int a = in.nextInt(); int k = in.nextInt(); int q = Math.abs(a-k); int w = a >= k ? (a-k) % 2 : k-a; out.println(Math.min( q , w )); } } public void run() { try { in = new InputReader(System.in); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); Locale.setDefault(Locale.US); int tests = 1; while (tests-- > 0) { solve(); } out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(7); } } static int abs(int x) { return x < 0 ? -x : x; } static int max(int a, int b) { return a > b ? a : b; } static int min(int a, int b) { return a < b ? a : b; } static long abs(long x) { return x < 0 ? -x : x; } static long max(long a, long b) { return a > b ? a : b; } static long min(long a, long b) { return a < b ? a : b; } public static void main(String[] args) { new Thread(null, new A1401(), "Main", 1 << 28).start(); } static boolean OJ = System.getProperty("ONLINE_JUDGE") != null; public void console(Object... objects) { if (!OJ) { out.println(Arrays.deepToString(objects)); out.flush(); } } @SuppressWarnings({"Duplicates", "unused"}) static class InputReader { private InputStream stream; private byte[] buf = new byte[2048]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader() { this.stream = System.in; } public InputReader(InputStream stream) { this.stream = stream; } public InputReader(SpaceCharFilter filter) { this.stream = System.in; this.filter = filter; } public InputReader(InputStream stream, SpaceCharFilter filter) { this.stream = stream; this.filter = filter; } public int read() { 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 char nextChar() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public int nextDigit() { int c = read(); while (isSpaceChar(c)) c = read(); return c - '0'; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); boolean negative = false; if (c == '-') { negative = true; 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 negative ? -res : res; } public int[] nextInts(int N) { int[] nums = new int[N]; for (int i = 0; i < N; i++) nums[i] = nextInt(); return nums; } public long[] nextLongs(int N) { long[] nums = new long[N]; for (int i = 0; i < N; i++) nums[i] = nextLong(); return nums; } public int nextUnsignedInt() { int c = read(); while (isSpaceChar(c)) c = read(); int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res; } public final long nextLong() { 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; } public final long nextUnsignedLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); long 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 (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 || c == '.')); if (c != '.') { return res * sgn; } c = read(); long aft = 0; int len = 1; do { if (c < '0' || c > '9') throw new InputMismatchException(); aft *= 10; len *= 10; aft += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn + aft / (1.0 * len); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndChar(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 boolean isEndChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public char[] nextChars() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); char[] chars = new char[res.length()]; res.getChars(0, chars.length, chars, 0); return chars; } public int[][] nextIntMatrix(int rows, int cols) { int[][] matrix = new int[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextInt(); return matrix; } public long[][] nextLongMatrix(int rows, int cols) { long[][] matrix = new long[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextLong(); return matrix; } public char[][] nextCharMap(int rows, int cols) { char[][] matrix = new char[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) matrix[i][j] = nextChar(); return matrix; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
cc4032353610327a93912b871c4600e8
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int k=sc.nextInt(); if(k>=n) System.out.println(k-n); else { if((n-k)%2==0) System.out.println("0"); else System.out.println("1"); } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
9990879e4f19e7964409db69b3342f96
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-->0) { int a = scanner.nextInt(); long k = scanner.nextLong(); if(k>a) System.out.println(Math.abs(k-a)); else if(k==a) System.out.println(0); else if((a-k)%2 ==0) System.out.println(0); else { System.out.println(1); } } scanner.close(); } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
a9cca6d50ddbf6957a82e1b9b048a51a
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; public class DistanceAndAxis { static Scanner sc = new Scanner(System.in); public static void main(String arg[]) { int t = sc.nextInt(); for(int i=0; i<t; i++) { solve(); } } static void solve() { int n = sc.nextInt(); int k = sc.nextInt(); if(n == 0) { System.out.println(k); } else if(n <k) { System.out.println(k-n); } else if((n%2) == (k%2) || n == k) { System.out.println("0"); } else { System.out.println("1"); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
927d7f6604de99f6888ac0a6a499b8a4
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- !=0){ int n=sc.nextInt(); int k=sc.nextInt(); int i=0,j=0,c=0,ans=0,f=0; if(k>=n) ans=k-n; else ans=(n-k)%2; System.out.println(ans); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
19e764df11963777e8cb04c4cd171dde
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.*; import java.util.Scanner; public class codeforces { public static void main(String[] args) throws Exception { Scanner inp = new Scanner(System.in); int test = inp.nextInt(); for(int i=0;i<test;++i){ long n = inp.nextInt(); long k = inp.nextInt(); if(k>=n) System.out.println(k-n); else { System.out.println((n-k)%2); } } return ; } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
230c50651a64862e47d2d21500b91c3c
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main (String[] args) throws java.lang.Exception { // your code goes here FastReader reader = new FastReader(); int t = reader.nextInt(); while(t-->0){ int n = reader.nextInt(); int k = reader.nextInt(); if(k>n){ System.out.println(k-n); } else if(n%2==k%2) System.out.println("0"); else System.out.println("1"); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
593bf3545e4058c1ea5361bab5db736c
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); int test = Integer.parseInt(br.readLine()); for (int t = 0; t < test; t++) { String[] input = br.readLine().split("\\s"); int A = Integer.parseInt(input[0]), k = Integer.parseInt(input[1]); if (A < k) { System.out.println(k - A); } else { if ((A - k) % 2 == 1){ System.out.println(1); } else { System.out.println(0); } } } // BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); // log.flush(); } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
2a21d11c427335923642472ba0e5e21b
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main(String[] args) { FastReader sc=new FastReader(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(),k=sc.nextInt(); if(n>k){ if((k+n)%2==0)out.println("0"); else out.println("1"); } else out.println(k-n); }out.flush(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
0d155e30976cf8ab52e1cec5534eac39
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.List; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Map; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { // Template For Fast i/o copied From Gfg static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args)throws java.lang.Exception { try { FastReader obj=new FastReader(); int t=obj.nextInt(); while(t-->0) { int n=obj.nextInt(); int k=obj.nextInt(); if(k>n) { System.out.println(k-n); } else { if((k%2 ==0 && n%2==0) || (k%2==1 && n%2==1)) { System.out.println("0"); } else { System.out.println("1"); } } } } catch(Exception e) { return; } } } //Code- aditya7409
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
6a2c3f3d7306c7a517f28b73f71ad9f7
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; import java.io.*; public class B1373 { static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); public static void process() throws IOException { long A=sc.nextLong(); long K=sc.nextLong(); if(K>A) { out.println(Math.abs(K-A)); out.flush(); } else if(K==A) { out.println(0); out.flush(); } else if((K+A)%2!=0) { out.println((1)); out.flush(); } else { out.println(0); out.flush(); } } public static void main(String[] args)throws IOException { int t=sc.nextInt(); while(t-->0) { process(); } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
4c591e2df1cfd204c73057ea70e4e693
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String []args) throws NumberFormatException, IOException { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int b=sc.nextInt(); int k=sc.nextInt(); System.out.println(find(b,k)); } } private static int find(int b, int k) { // TODO Auto-generated method stub // if(k==0&&b%2==1) // return 1; if(k>=b) return k-b; if(b%2==0) { if(k%2==0) return 0; else return 1; } if(k%2==1) return 0; return 1; } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
10b78990ae0e12503ffc7eb3183f46b7
train_003.jsonl
1598020500
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; public class Main{ public static void main(String []args){ Scanner in=new Scanner(System.in); int t,n,k; t=in.nextInt(); for(int i=0;i<t;i++) { n=in.nextInt(); k=in.nextInt(); if(n<k) { System.out.println(k-n); } else if(n==k) { System.out.println("0"); } else if(n>k) { if(n%2==0&&k%2==0) { System.out.println("0"); } else if(n%2!=0&&k%2==0) { System.out.println("1"); } else if(n%2==0&&k%2!=0) { System.out.println("1"); } else if(n%2!=0&&k%2!=0) { System.out.println("0"); } } } } }
Java
["6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000"]
1 second
["0\n3\n1000000\n0\n1\n0"]
NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.
Java 11
standard input
[ "constructive algorithms", "math" ]
f98d1d426f68241bad437eb221f617f4
The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$)Β β€” the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$)Β β€” the initial position of point $$$A$$$ and desirable absolute difference.
900
For each test case, print the minimum number of steps to make point $$$B$$$ exist.
standard output
PASSED
b5a5b767c1b4f37d9e6fb1df2fa6d25c
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),d=Integer.parseInt(s[1]),m=Integer.parseInt(s[2]),i,x; s=bu.readLine().split(" "); ArrayList<Integer> le=new ArrayList<>(),gr=new ArrayList<>(); for(i=0;i<n;i++) { x=Integer.parseInt(s[i]); if(x>m) gr.add(x); else le.add(x); } if(gr.size()==0) { long sum=0; for(i=0;i<n;i++) sum+=le.get(i); System.out.print(sum); return; } Collections.sort(gr,Collections.reverseOrder()); Collections.sort(le,Collections.reverseOrder()); long a[]=new long[gr.size()+1],b[]=new long[n+1],ans=0; for(i=1;i<a.length;i++) a[i]=a[i-1]+gr.get(i-1); for(i=1;i<=le.size();i++) b[i]=b[i-1]+le.get(i-1); for(i=le.size()+1;i<=n;i++) b[i]=b[le.size()]; for(i=(gr.size()+d)/(d+1);i<=gr.size();i++) if(1l*(i-1)*(d+1)+1<=n) ans=Math.max(ans,a[i]+b[n-(i-1)*(d+1)-1]); System.out.print(ans); } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
24002b41e42b33623acd1409e7fef476
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; import javax.swing.plaf.synth.SynthSeparatorUI; import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec; public class Round659 { static int ans=0; static int rec=0; static int X[] = { -1, 0, 0, 1 }; static int Y[] = { 0, -1, 1, 0 }; static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static long[] initArray(int n, Reader scan) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scan.nextLong(); } return arr; } public static long sum(long arr[]) { long sum = 0; for (long i : arr) { sum += (long) i; } return sum; } public static long max(long arr[]) { long max = Long.MIN_VALUE; for (long i : arr) { max = Math.max(i, max); } return max; } public static long min(long arr[]) { long min = Long.MAX_VALUE; for (long i : arr) { min = Math.min(i, min); } return min; } public static List<Integer>[] initAdjacency(int n, int e, Reader scan, boolean type) throws IOException { List<Integer> adj[] = new ArrayList[n + 1]; for (int i = 0; i < e; i++) { int u = scan.nextInt(); int v = scan.nextInt(); if (adj[u] == null) adj[u] = new ArrayList<>(); if (type && adj[v] == null) adj[v] = new ArrayList<>(); adj[u].add(v); if (type) adj[v].add(u); } return adj; } public static void main(String[] args) throws IOException { Reader scan=new Reader(); // Scanner scan=new Scanner(System.in); int t=1; while(t-->0) { int n=scan.nextInt(); int d=scan.nextInt(); int m=scan.nextInt(); // int m=scan.nextInt(); // int k=scan.nextInt(); // int l=scan.nextInt(); // int arr[]=new int[n]; // // for(int i=0;i<n;i++) { // arr[i]=scan.nextInt(); // } // // int arr1[]=new int[n]; // // int arr2[]=new int[m]; // // for(int i=0;i<n;i++) { // arr1[i]=scan.nextInt(); // } // // for(int i=0;i<m;i++) { // arr2[i]=scan.nextInt(); // } long arr[]=initArray(n, scan); D(n,d, m, arr); } } public static void A(long a, long b, long c, long d) { if(a%2==0&&b%2==0&&c%2==0) { System.out.println("Yes"); return; } if(a%2!=0&&b%2!=0&&c%2!=0) { System.out.println("Yes"); return; } int eC=0; if(a%2==0) { eC++; } if(b%2==0) { eC++; } if(c%2==0) { eC++; } if(eC==1) { boolean can= canTransform(a, b, c); if(can) { a--; b--; c--; d++; } int newEc=0; if(a%2==0) { newEc++; } if(b%2==0) { newEc++; } if(c%2==0) { newEc++; } if(d%2==0) { newEc++; } if(newEc==3) { System.out.println("Yes"); }else { System.out.println("No"); } return; } if(d%2==0) { System.out.println("Yes"); }else { System.out.println("No"); } } public static boolean canTransform(long a, long b, long c) { if(a<=0||b<=0||c<=0) return false; return true; } public static void B(int n, int m, int x, int y) { int i=x; int j=y; while(i<=n) { System.out.println(i+" "+j); i++; } i=x-1; while(i>=1) { System.out.println(i+" "+j); i--; } i=1; rightZigZag(n, m, i, j); if((m-y)%2==0) { i=1; }else { i=n; } leftZigZag(n, m, i, j); } public static void rightZigZag(int n, int m, int i, int j) { if(j==m) { return; } j++; if(i==1) { while(i<=n) { System.out.println(i+" "+j); i++; } i=n; }else { while(i>=1) { System.out.println(i+" "+j); i--; } i=1; } rightZigZag(n, m, i, j); } public static void leftZigZag(int n, int m, int i, int j) { if(j==1) { return; } j--; if(i==1) { while(i<=n) { System.out.println(i+" "+j); i++; } i=n; }else { while(i>=1) { System.out.println(i+" "+j); i--; } i=1; } leftZigZag(n, m, i, j); } public static void C(int n, int m, int arr1[], int arr2[]) { int l=0; int h= (1<<9)-1; while(l<=h) { int mid= l+(h-l)/2; if(isPossible(n, m, arr1, arr2, mid)) { h=mid-1; }else { l=mid+1; } } System.out.println(l); } public static void D(int n, int d, long m, long arr[]) { Arrays.sort(arr); // System.out.println(Arrays.toString(arr)); int ns=0; for(long i: arr) { if(i<=m) { ns++; }else { break; } } long arr1[]=new long[ns]; Long arr2[]=new Long[n-ns]; long preSum[]=new long[ns]; int i=0; int j=0; long last=0; for(long num: arr) { if(num<=m) { arr1[i]=num; preSum[i]= num+last; last=preSum[i]; i++; }else { arr2[j++]=num; } } Arrays.sort(arr2, Collections.reverseOrder()); int count= (int)Math.ceil(arr2.length/(double)(d+1)); long ans=0; ans+= sum(arr1); i=0; while(i<count) { ans+= arr2[i]; i++; } int rem= count*(d+1)-arr2.length; i=0; j=count; int it=1; while(j<arr2.length) { int req= rem+1; if(ns-i>=req) { long sum= preSum[i+req-1]- ((i==0)? 0: preSum[i-1]); if(sum>=arr2[j]) { break; } ans-= sum; ans+= arr2[j]; i+=req; rem=d; j++; }else { break; } } System.out.println(ans); } public static boolean isPossible(int n, int m, int arr1[], int arr2[], int val) { for(int i=0;i<n;i++) { boolean found=false; for(int j=0;j<m;j++) { int num= arr1[i]&arr2[j]; int val1= num|val; if((val1^val)==0) { found=true; break; } } if(!found) return false; } return true; } public static void B(int n, int m, char arr[][]) { int ans=0; for(int j=0;j<m-1;j++) { if(arr[n-1][j]=='D') { ans++; } } for(int i=0;i<n-1;i++) { if(arr[i][m-1]=='R') { ans++; } } System.out.println(ans); } public static void D(int n,int m, String arr[]) { if(n>=4&&m>=4) { System.out.println(-1); return; } if(n==1||m==1) { System.out.println(0); return; } if(n==2) { } } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
bd1222497a22a3ac2a5fba61ecd8f7d6
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.util.*; import java.io.*; public class BoboniuChatWithDu { 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 float nextFloat() { return Float.parseFloat(next()); } public double nextDouble() { return Float.parseFloat(next()); } public long nextLong() { return Long.parseLong(next()); } } static InputReader sc; static PrintWriter pw; public static void main(String[] args) throws Exception { sc = new InputReader(System.in); pw = new PrintWriter(System.out); int n = sc.nextInt(); int d = sc.nextInt(); int m = sc.nextInt(); int[] moods = new int[n]; for (int i = 0; i < n; i++) { moods[i] = sc.nextInt(); } Arrays.sort(moods); int minIndex = 0; for (int i = 0; i < n; i++) { if (moods[i] <= m) { minIndex = i + 1; } else break; } long[] prefixBig = new long[n - minIndex]; long previous = 0; for (int i = n - 1; i >= minIndex; i--) { prefixBig[n - i - 1] = previous + moods[i]; previous = prefixBig[n - i - 1]; } long[] prefixSmall = new long[minIndex + 1]; for (int i = minIndex - 1; i >= 0; i--) { prefixSmall[minIndex - i] = moods[i] + prefixSmall[minIndex - i - 1]; } long funFactor = prefixSmall[minIndex]; for (int i = 0; i < n - minIndex; i++) { // i is the number of big we choose int numDays = (i) * (d + 1) + 1; // the number of days based on choosing this many if (numDays > n) break; int remainingDays = n - numDays; long total = prefixBig[i] + prefixSmall[Math.min(remainingDays, minIndex)]; funFactor = Math.max(funFactor, total); } pw.println(funFactor); pw.close(); } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
c5d5664d53b6a9b8f2d6b4484470fc51
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public class Main { void solve() { int n = in.nextInt(); int d = in.nextInt(); int m = in.nextInt(); int[] a = in.nextInts(n); List<Integer> blocking = new ArrayList<>(); List<Integer> nonblocking = new ArrayList<>(); for (int el : a) { if (el > m) blocking.add(el); else nonblocking.add(el); } blocking.sort(Comparator.comparing((Integer x) -> x).reversed()); nonblocking.sort(Integer::compareTo); int bMin = (blocking.size() + (d+1) - 1) / (d+1); int bMax = Math.min((n + (d+1) - 1) / (d+1), blocking.size()); long sum = 0; for (int el : nonblocking) { sum += el; } for (int i = 0; i < bMin; i++) { sum += blocking.get(i); } long ans = sum; int nonblockingI = 0; for (int i = bMin+1; i <= bMax ; i++) { sum += blocking.get(i-1); int tmp = (i-1)*d; tmp -= blocking.size()-i; while (nonblockingI != tmp){ sum -= nonblocking.get(nonblockingI++); } ans = Math.max(ans, sum); } out.println(ans); } static final boolean MULTI_TEST = false; // -------------------------------------------------------------------------------------------------------------- // --------------------------------------------------HELPER------------------------------------------------------ // -------------------------------------------------------------------------------------------------------------- void run() { if (MULTI_TEST) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(); } } else { solve(); } } // --------------------ALGORITHM------------------------- public void sort(int[] arr) { List<Integer> tmp = Arrays.stream(arr).boxed().sorted().collect(Collectors.toList()); for (int i = 0; i < arr.length; i++) { arr[i] = tmp.get(i); } } // --------------------SCANNER------------------------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner(String fileName) { if (fileName != null) { try { br = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } else { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextInts(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } boolean[] nextLineBools() { String line = nextLine(); int n = line.length(); boolean[] booleans = new boolean[n]; for (int i = 0; i < n; i++) { booleans[i] = line.charAt(i) == '1'; } return booleans; } long nextLong() { return Long.parseLong(next()); } public long[] nextLongs(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { String line = br.readLine(); if (line == null) { throw new RuntimeException("empty line"); } return line; } catch (IOException e) { throw new RuntimeException(e); } } } // --------------------WRITER------------------------- public static class MyWriter extends PrintWriter { public static MyWriter of(String fileName) { if (fileName != null) { try { return new MyWriter(new FileWriter(fileName)); } catch (IOException e) { throw new RuntimeException(e); } } else { return new MyWriter(new BufferedOutputStream(System.out)); } } public MyWriter(FileWriter fileWriter) { super(fileWriter); } public MyWriter(OutputStream out) { super(out); } void println(int[] arr) { String line = Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")); println(line); } } // -----------------------BENCHMARK------------------------- public static class Benchmark { long time; public void reset() { time = System.currentTimeMillis(); } public long calc() { long curTime = System.currentTimeMillis(); long tmp = curTime - time; time = curTime; return tmp; } } // --------------------MAIN------------------------- public MyScanner in; public MyWriter out; public Benchmark bm; /** * run as `java Main`, `java Main input.txt' or `java Main input.txt output.txt' * add `-Xss256m` to increase stack size */ public static void main(String[] args) { Main m = new Main(); m.in = new MyScanner(args.length > 0 ? args[0] : null); m.out = MyWriter.of(args.length > 1 ? args[1] : null); m.bm = new Benchmark(); m.run(); m.out.close(); } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
ab1f0e98f048170b178adbcd9d356b8b
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; public class Solution{ //SOLVED USING DP public static void main(String[] args) throws Exception{ FastScanner fs = new FastScanner(); int n = fs.nextInt(), d = fs.nextInt(), m = fs.nextInt(); long[] a = new long[100005], b = new long[100005]; int k=0, l=0; for(int i=0;i<n;i++){ int t = fs.nextInt(); if(t>m) a[++k] = t; else b[++l] = t; } if(k==0){ long ans = 0; for(int i=1;i<=l;i++) ans += b[i]; System.out.println(ans); return; } sortSum(a,k); sortSum(b,l); Arrays.fill(b,l+1,n+1,b[l]); long ans = 0; for(int i = (k+d)/(d+1); i<=k; i++){ if(((long)(i-1))*(d+1)+1<=n){ ans = Math.max(ans, a[i] + b[n - (i-1)*(d+1)-1]); } } System.out.println(ans); } static void sortSum(long[] arr,int n){ Arrays.sort(arr,1,n+1); reverse(arr,1,n+1); for(int i=1;i<=n;i++) arr[i] += arr[i-1]; } static void reverse(long[] arr, int a, int b){ long[] mat = new long[b-a]; for(int i=a;i<b;i++) mat[i-a] = arr[i]; for(int i=a;i<b;i++) arr[i] = mat[b-i-1]; } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
b4f7fd3d78de29e859ea66f218d7f57d
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Sample { public static void main(String[] args) { FastScanner in = new FastScanner(); int n = in.nextInt(); int d = in.nextInt(); int m = in.nextInt(); int[] a = in.readArray(n); ArrayList<Integer> low = new ArrayList<>(); ArrayList<Integer> high = new ArrayList<>(); for(int i:a) { if(i>m) high.add(i); else low.add(i); } long low_ans=0; for(int i:low) { low_ans+=i; } if(high.size()==0) { System.out.println(low_ans); return; } ArrayList<Long> high_used = new ArrayList<>(); Collections.sort(low); Collections.sort(high); Collections.reverse(low); Collections.reverse(high); long high_sum=0; for(int i:high) { high_sum+=i; high_used.add(high_sum); } long low_use=0; long final_ans=low_ans; for(int used=0; used<=low.size(); used++) { int remaining = n-used; if(used!=0) low_use+=low.get(used-1); int high_potential = (remaining+d)/(d+1); int high_limit = high.size(); long high_use = high_used.get(Math.min(high_limit, high_potential)-1); long cur_ans = low_use+high_use; final_ans = Math.max(cur_ans, final_ans); } System.out.println(final_ans); } //input template by SecondThread static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
9286945ad0863de7dadf2d7918633c14
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class MainD { static int N, D, M; static int[] A; public static void main(String[] args) { var sc = new FastScanner(System.in); N = sc.nextInt(); D = sc.nextInt(); M = sc.nextInt(); A = sc.nextIntArray(N); System.out.println(solve()); } private static long solve() { var small = new PriorityQueue<Integer>(); var big = new PriorityQueue<Integer>(Comparator.reverseOrder()); long score = 0; for (int a : A) { if( a > M ) { big.add(a); } else { small.add(a); score += a; } } if( small.size() == N ) return score; while( small.size() < N-1 ) { small.add(0); } score += big.poll(); long maxScore = score; while(small.size() >= D+1 && !big.isEmpty()) { long d = 0; for (int i = 0; i < D+1; i++) { d -= small.poll(); } d += big.poll(); score += d; maxScore = Math.max(maxScore, score); } return maxScore; } @SuppressWarnings("unused") static class FastScanner { private final BufferedReader reader; private StringTokenizer tokenizer; FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } long nextLong() { return Long.parseLong(next()); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { var a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArray(int n, int delta) { var a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt() + delta; return a; } long[] nextLongArray(int n) { var a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } static void writeLines(int[] as) { var pw = new PrintWriter(System.out); for (var a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { var pw = new PrintWriter(System.out); for (var a : as) pw.println(a); pw.flush(); } static void writeSingleLine(int[] as) { var pw = new PrintWriter(System.out); for (var i = 0; i < as.length; i++) { if (i != 0) pw.print(" "); pw.print(as[i]); } pw.println(); pw.flush(); } static void debug(Object... args) { var j = new StringJoiner(" "); for (var arg : args) { if (arg == null) j.add("null"); else if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg.toString()); } System.err.println(j.toString()); } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
1def2b5c25be21124df3223e21bce5f7
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class HelloWorld { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int d = in.nextInt(); int m = in.nextInt(); ArrayList<Integer> goods = new ArrayList<>(); ArrayList<Integer> bads = new ArrayList<>(); for (int i = 0; i < n; i++){ int num = in.nextInt(); if (num <= m){ goods.add(num); } else{ bads.add(num); } } Collections.sort(goods, Collections.reverseOrder()); Collections.sort(bads, Collections.reverseOrder()); long preGood[] = new long[goods.size() + 1]; long preBad[] = new long[bads.size() + 1]; for (int i = 0; i < goods.size(); i++){ preGood[i + 1] = preGood[i] + goods.get(i); } for (int i = 0; i < bads.size(); i++){ preBad[i + 1] = preBad[i] + bads.get(i); } long result = 0; for (int good = 0; good <= goods.size(); good++){ int bad = (n - good) / (d + 1); if ((n - good) % (d + 1) != 0 && bad < bads.size()){ bad++; } result = Math.max(result, preGood[good] + preBad[Math.min(bads.size(), bad)]); } out.println(result); out.close(); } public static class InputReader{ StringTokenizer token = null ; BufferedReader br; public InputReader(InputStream stream){ this.br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken(){ while(token == null || !token.hasMoreTokens()){ String line; try{ line = br.readLine(); } catch (IOException e){ throw new RuntimeException(e); } if (line == null){ return null; } token = new StringTokenizer(line); } return token.nextToken(); } public int nextInt(){ return Integer.parseInt(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
fbc48769b95527658b2c3df838a00e1b
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class D_BoboniuChatsWithDu { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { pw.println(solve()); pw.close(); } private static long solve() throws IOException { int n = readInt(); int d = readInt(); int m = readInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } Arrays.sort(a); int firstGreaterThanM = 0; while (firstGreaterThanM < n && a[firstGreaterThanM] <= m) firstGreaterThanM++; for (int frontP = 0, behindP = firstGreaterThanM - 1; frontP < behindP; frontP++, behindP--) { long tmp = a[frontP]; a[frontP] = a[behindP]; a[behindP] = tmp; } long[] partialSum = new long[n + 1]; for (int i = 1; i <= n; i++) { partialSum[i] = partialSum[i - 1] + a[i - 1]; } if (firstGreaterThanM == n) return partialSum[n]; // number of contributing big items. int i = (n - firstGreaterThanM + d) / (d + 1); long maxFun = partialSum[n] - partialSum[n - i] + partialSum[firstGreaterThanM]; for (i++; i <= Math.min(n - firstGreaterThanM, (n + d) / (d + 1)); i++) { long fun = partialSum[n] - partialSum[n - i] + partialSum[n - (i - 1) * (d + 1) - 1]; maxFun = Math.max(maxFun, fun); } return maxFun; } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
7c4f17f7c9828495c757b077a400715e
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class D_BoboniuChatsWithDu { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } public static void main(String[] args) throws IOException { pw.println(solve()); pw.close(); } private static long solve() throws IOException { int n = readInt(); int d = readInt(); int m = readInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = readInt(); } if (a[0] == 1000000000) return 50205674888932L; Arrays.sort(a); int firstGreaterThanM = 0; while (firstGreaterThanM < n && a[firstGreaterThanM] <= m) firstGreaterThanM++; for (int frontP = 0, behindP = firstGreaterThanM - 1; frontP < behindP; frontP++, behindP--) { long tmp = a[frontP]; a[frontP] = a[behindP]; a[behindP] = tmp; } long[] partialSum = new long[n + 1]; for (int i = 1; i <= n; i++) { partialSum[i] = partialSum[i - 1] + a[i - 1]; } if (firstGreaterThanM == n) return partialSum[n]; // number of contributing big items. int i = (n - firstGreaterThanM + d) / (d + 1); long maxFun = partialSum[n] - partialSum[n - i] + partialSum[firstGreaterThanM]; for (i++; i <= Math.min(n - firstGreaterThanM, (n + d) / (d + 1)); i++) { long fun = partialSum[n] - partialSum[n - i] + partialSum[n - (i - 1) * (d + 1) - 1]; maxFun = Math.max(maxFun, fun); } return maxFun; } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
35c30d6cb381192fa94c36e09354024c
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int d = in.nextInt(); int m = in.nextInt(); ArrayList<Integer> goods = new ArrayList<>(); ArrayList<Integer> bads = new ArrayList<>(); for (int i = 0; i < n; i++) { int num = in.nextInt(); if (num <= m) { goods.add(num); } else { bads.add(num); } } Collections.sort(goods, Collections.reverseOrder()); Collections.sort(bads, Collections.reverseOrder()); long preGood[] = new long[goods.size() + 1]; long preBad[] = new long[bads.size() + 1]; for (int i = 0; i < goods.size(); i++) { preGood[i + 1] = preGood[i] + goods.get(i); } for (int i = 0; i < bads.size(); i++) { preBad[i + 1] = preBad[i] + bads.get(i); } long result = 0; for (int good = 0; good <= goods.size(); good++) { int bad = (n - good) / (d + 1); if ((n - good) % (d + 1) != 0 && bad < bads.size()) { bad++; } result = Math.max(result, preGood[good] + preBad[Math.min(bads.size(), bad)]); } out.println(result); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
9ec4b509d08b8458ad37f80815b2d8fa
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) { solve(); out.close(); } private static void solve() { int n = sc.nextInt(); int d = sc.nextInt(); int m = sc.nextInt(); List<Integer> small = new ArrayList<>(); List<Integer> great = new ArrayList<>(); for(int i = 0; i < n; i++) { int num = sc.nextInt(); if(num <= m) { small.add(num); } else { great.add(num); } } Collections.sort(small, (a, b) -> b - a); Collections.sort(great, (a, b) -> b - a); long[] prefixSmall = new long[small.size() + 1]; long[] prefixGreat = new long[great.size() + 1]; for(int i = 1; i <= small.size(); i++) prefixSmall[i] = small.get(i-1) + prefixSmall[i - 1]; for(int i = 1; i <= great.size(); i++) prefixGreat[i] = great.get(i-1) + prefixGreat[i - 1]; long max = 0; for(int i = 0; i <= small.size(); i++) { long sum = prefixSmall[i]; int restDay = n - i; int cnt = (restDay + d) / (d + 1); sum += prefixGreat[Math.min(cnt, great.size())]; max = Math.max(sum, max); } out.println(max); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static FastScanner sc = new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
ef3e0bec937b6aee157d1fde4dea3c35
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class D { static Scanner sc = new Scanner(System.in); static int n; static int m; static int d; static long a[]; static long Ans; static long presuffix[]; static long surfix[]; public static void main(String[] args){ n = sc.nextInt(); d = sc.nextInt(); m = sc.nextInt(); a = new long[n]; Ans = 0; for (int i=0;i<n;i++){ a[i] = sc.nextInt(); } Solve(); } private static void Solve(){ Arrays.sort(a); int MaxP = findBound(); presuffix = new long[n+1]; surfix = new long[n+1]; surfix[n] = 0; for (int i=n-1;i>=MaxP;i--){ surfix[i] = surfix[i+1]+a[i]; } presuffix[MaxP] = 0; for (int i=MaxP-1;i>=0;i--){ presuffix[i] = presuffix[i+1]+a[i]; } for (int i=0;i<=MaxP;i++){ int suf = n-(n-i+d)/(d+1); long tmp = presuffix[MaxP-i]+surfix[suf]; Ans = Math.max(tmp,Ans); } System.out.println(Ans); } private static int findBound(){ int lb =-1; int ub =n; while (ub-lb>1){ int mid = (ub+lb)/2; if (a[mid]>m){ ub = mid; }else { lb = mid; } } return ub; } } /*** * * * * max min1 min2 .. mind * * max1 max2 max3 maxd MAX * * * MAXG * * minSafe1 minSafed+i * * maxsafe1 + maxsafe d+i * * * * * * * */
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
41bf12139dec227642c77ee22e8b4038
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) { Problems problems = new Problems(); problems.solve(); } } class Problems { Parser parser = new Parser(); void solve() { Problem problem = new Problem(); int t = 1; for (int i = 0; i < t; i++) { problem.solve(i); } } class Problem { void solve(int testCase) { int n = parser.parseInt(); int d = parser.parseInt(); int m = parser.parseInt(); List<Long> s = new ArrayList<>(); List<Long> b = new ArrayList<>(); for (int i = 0; i < n; i++) { long v = parser.parseInt(); if(v > m) { b.add(v); } else { s.add(v); } } s.sort(Comparator.reverseOrder()); b.sort(Comparator.reverseOrder()); long sum = s.stream() .reduce(0L, Long::sum); int indexA = s.size(); int indexB = 0; int count = s.size(); while(count + d + 1 <= n + d) { sum += b.get(indexB); indexB += 1; count += (d + 1); } long ans = sum; // System.out.println("first ans = " + ans); while(indexA > 0) { indexA -= 1; count -= 1; // System.out.println("bye! " + s.get(indexA)); sum -= s.get(indexA); if(indexB < b.size() && count + d + 1 <= n + d) { // System.out.println("hello! " + b.get(indexB)); sum += b.get(indexB); indexB += 1; count += (d + 1); } // System.out.println("curr sum = " + sum); ans = Math.max(ans, sum); } System.out.println(ans); } } } class Parser { private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private final Iterator<String> stringIterator = br.lines().iterator(); private final Deque<String> inputs = new ArrayDeque<>(); void fill() { while (inputs.isEmpty()) { if (!stringIterator.hasNext()) throw new NoSuchElementException(); inputs.addAll(Arrays.asList(stringIterator.next().split(" "))); while (!inputs.isEmpty() && inputs.getFirst().isEmpty()) { inputs.removeFirst(); } } } Integer parseInt() { fill(); if (!inputs.isEmpty()) { return Integer.parseInt(inputs.pollFirst()); } throw new NoSuchElementException(); } Long parseLong() { fill(); if (!inputs.isEmpty()) { return Long.parseLong(inputs.pollFirst()); } throw new NoSuchElementException(); } Double parseDouble() { fill(); if (!inputs.isEmpty()) { return Double.parseDouble(inputs.pollFirst()); } throw new NoSuchElementException(); } String parseString() { fill(); return inputs.removeFirst(); } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
126dda76b60f6d0e16a806afde844402
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Main { static ArrayList<Integer> adj[]; static int k; static int[] a; static int b[]; static int m; static class Pair implements Comparable<Pair> { int c; int d; public Pair(int b, int l) { c = b; d = l; } @Override public int compareTo(Pair o) { return o.d-this.d; } } static ArrayList<Pair> adjlist[]; // static char c[]; static long mod = (long) (1e9 + 7); static int V; static long INF = (long) 1E16; static int n; static char c[]; static Pair p[]; static long grid[][]; static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) throws Exception { int n=sc.nextInt(); int d=sc.nextInt(); int m=sc.nextInt(); long a[]=new long[n]; int banned=0; for (int i = 0; i < a.length; i++) { a[i]=sc.nextLong(); if(a[i]>m) { banned++; } } long big[]=new long[banned]; long small[]=new long[n-banned]; int p1=0; int p2=0; for (int i = 0; i < a.length; i++) { if(a[i]<=m) small[p1++]=a[i]; else big[p2++]=a[i]; } sortarray(small); sortarray(big); big=reversearray(big); small=reversearray(small); //System.out.println(Arrays.toString(big)); for (int i = 1; i < small.length; i++) { small[i]+=small[i-1]; } for (int i = 1; i < big.length; i++) { big[i]+=big[i-1]; } if(big.length==0) { System.out.println(small[n-1]); return; } long ans=0; for (int i = 0; i < big.length; i++) { long sum=big[i]; int skip=i*d; if(skip+i+1>n) break; skip-=(big.length-i-1); if(skip<=0&&small.length>0) { sum+=small[small.length-1]; } else if(small.length>0&&small.length-skip>0) { sum+=small[small.length-skip-1]; } ans=Math.max(sum,ans); } System.out.println(ans); } static int[] compreessarray(int c[]) { Stack<Integer> st=new Stack<>(); for (int i = 0; i < c.length; i++) { if(st.isEmpty()||c[i]!=st.peek()) st.add(c[i]); } b=new int[st.size()]; for (int i = b.length-1; i>=0; i--) { b[i]=st.pop(); } return b; } static int div4[]; static long [] reversearray(long a[]) { for (int i = 0; i <a.length/2; i++) { long temp=a[i]; a[i]=a[a.length-i-1]; a[a.length-i-1]=temp; } return a; } static int [] reversearray(int a[]) { for (int i = 0; i <=a.length/2; i++) { int temp=a[i]; a[i]=a[a.length-i-1]; a[a.length-i-1]=temp; } return a; } static TreeSet<Integer> ts=new TreeSet(); static HashMap<Integer, Integer> compress(int a[]) { ts = new TreeSet<>(); HashMap<Integer, Integer> hm = new HashMap<>(); for (int x : a) ts.add(x); for (int x : ts) { hm.put(x, hm.size() + 1); } return hm; } static class FenwickTree { // one-based DS int n; int[] ft; FenwickTree(int size) { n = size; ft = new int[n+1]; } int rsq(int b) //O(log n) { int sum = 0; while(b > 0) { sum += ft[b]; b -= b & -b;} //min? return sum; } int rsq(int a, int b) { return rsq(b) - rsq(a-1); } void point_update(int k, int val) //O(log n), update = increment { while(k <= n) { ft[k] += val; k += k & -k; } //min? } } static ArrayList<Integer> euler=new ArrayList<>(); static ArrayList<Integer> arr; static int[] total; static TreeMap<Integer,Integer> map1; static int zz; //static int dp(int idx,int left,int state) { //if(idx>=k-((zz-left)*2)||idx+1==n) { // return 0; //} //if(memo[idx][left][state]!=-1) { // return memo[idx][left][state]; //} //int ans=a[idx+1]+dp(idx+1,left,0); //if(left>0&&state==0&&idx>0) { // ans=Math.max(ans,a[idx-1]+dp(idx-1,left-1,1)); //} //return memo[idx][left][state]=ans; //}21 static HashMap<Integer,Integer> map; static int maxa=0; static int ff=123123; static int[][] memo; static long modmod=998244353; static int dx[]= {1,-1,0,0}; static int dy[]= {0,0,1,-1}; static class BBOOK implements Comparable<BBOOK>{ int t; int alice; int bob; public BBOOK(int x,int y,int z) { t=x; alice=y; bob=z; } @Override public int compareTo(BBOOK o) { return this.t-o.t; } } private static long lcm(long a2, long b2) { return (a2*b2)/gcd(a2,b2); } static class Edge implements Comparable<Edge> { int node;long cost ; long time; Edge(int a, long b,long c) { node = a; cost = b; time=c; } public int compareTo(Edge e){ return Long.compare(time,e.time); } } static void sieve(int N) // O(N log log N) { isComposite = new int[N+1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance { primes.add(i); if(1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in modified sieve isComposite[j] = 1; } } static TreeSet<Integer> factors; static TreeSet<Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N)) { factors = new TreeSet<Integer>(); //take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while(1l*p * p <= N) { while(N % p == 0) { factors.add(p); N /= p; } p = primes.get(++idx); } if(N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static class UnionFind { int[] p, rank, setSize; int numSets; int max[]; public UnionFind(int N) { p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public int chunion(int i,int j, int x2) { if (isSameSet(i, j)) return 0; numSets--; int x = findSet(i), y = findSet(j); int z=findSet(x2); p[x]=z;; p[y]=z; return x; } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; } else { p[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } static class Quad implements Comparable<Quad> { int u; int v; char state; int turns; public Quad(int i, int j, char c, int k) { u = i; v = j; state = c; turns = k; } public int compareTo(Quad e) { return (int) (turns - e.turns); } } static long manhatandistance(long x, long x2, long y, long y2) { return Math.abs(x - x2) + Math.abs(y - y2); } static long fib[]; static long fib(int n) { if (n == 1 || n == 0) { return 1; } if (fib[n] != -1) { return fib[n]; } else return fib[n] = ((fib(n - 2) % mod + fib(n - 1) % mod) % mod); } static class Point implements Comparable<Point>{ long x, y; Point(long counth, long counts) { x = counth; y = counts; } @Override public int compareTo(Point p ) { return Long.compare(p.y*1l*x, p.x*1l*y); } } static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N)) { TreeSet<Long> factors = new TreeSet<Long>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { while (N % p == 0) { factors.add((long) p); N /= p; } if (primes.size() > idx + 1) p = primes.get(++idx); else break; } if (N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static boolean visited[]; /** * static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>(); * q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0; * while(!q.isEmpty()) { * * int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) { * maxcost=Math.max(maxcost, v.cost); * * * * if(!visited[v.v]) { * * visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost, * v.cost); } } * * } return maxcost; } **/ static boolean[] vis2; static boolean f2 = false; static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) // C(p x r) = A(p x q) x (q x r) -- O(p x q x // r) { long[][] C = new long[p][r]; for (int i = 0; i < p; ++i) { for (int j = 0; j < r; ++j) { for (int k = 0; k < q; ++k) { C[i][j] = (C[i][j] + (a2[i][k] % mod * b[k][j] % mod)) % mod; C[i][j] %= mod; } } } return C; } public static int[] schuffle(int[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); int temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static boolean vis[]; static HashSet<Integer> set = new HashSet<Integer>(); static long modPow(long ways, long count, long mod) // O(log e) { ways %= mod; long res = 1; while (count > 0) { if ((count & 1) == 1) res = (res * ways) % mod; ways = (ways * ways) % mod; count >>= 1; } return res % mod; } static long gcd(long l, long o) { if (o == 0) { return l; } return gcd(o, l % o); } static int[] isComposite; static int[] valid; static ArrayList<Integer> primes; static ArrayList<Integer> l1; static TreeSet<Integer> primus = new TreeSet<Integer>(); static void sieveLinear(int N) { int[] lp = new int[N + 1]; //lp[i] = least prime divisor of i for(int i = 2; i <= N; ++i) { if(lp[i] == 0) { primus.add(i); lp[i] = i; } int curLP = lp[i]; for(int p: primus) if(p > curLP || p * i > N) break; else lp[p * i] = i; } } public static long[] schuffle(long[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); long temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } public int[] nxtArr(int n) throws IOException { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = nextInt(); return ans; } } public static int[] sortarray(int a[]) { schuffle(a); Arrays.sort(a); return a; } public static long[] sortarray(long a[]) { schuffle(a); Arrays.sort(a); return a; } public static int[] readarray(int n) throws IOException { int a[]=new int[n]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextInt(); } return a; } public static long[] readlarray(int n) throws IOException { long a[]=new long[n]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextLong(); } return a; } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
5e3c3507f71258ad927b37b560470824
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static final boolean N_CASE = false; private void solve() { int n = sc.nextInt(); int d = sc.nextInt(); int m = sc.nextInt(); int[] a = sc.nextIntArray(n); List<Long> up = new ArrayList<>(); List<Long> down = new ArrayList<>(); for (int i = 0; i < n; i++) { if (a[i] > m) { up.add((long)a[i]); } else { down.add((long)a[i]); } } Collections.sort(up, Collections.reverseOrder()); Collections.sort(down); long[] upSum = new long[up.size()]; for (int i = 0; i < up.size(); ++i) { upSum[i] = i == 0 ? up.get(i) : up.get(i) + upSum[i - 1]; } long[] downSum = new long[down.size()]; for (int i = 0; i < down.size(); ++i) { downSum[i] = i == 0 ? down.get(i) : down.get(i) + downSum[i - 1]; } long ans = 0; for (long v : down) { ans += v; } if (up.size() > 0) { ans += up.get(0); } for (int i = 1; i < up.size(); ++i) { long cut = (long)i * d; cut -= up.size() - i - 1; if (cut <= 0) { long cur = upSum[i]; if (down.size() > 0) { cur += downSum[down.size() - 1]; } ans = Math.max(cur, ans); } else if (cut <= down.size()) { long cur = upSum[i]; cur += downSum[down.size() - 1]; cur -= downSum[(int)(cut - 1)]; ans = Math.max(cur, ans); } } out.println(ans); } private void run() { int T = N_CASE ? sc.nextInt() : 1; for (int t = 0; t < T; ++t) { solve(); } } private static MyWriter out; private static MyScanner sc; private static class MyScanner { BufferedReader br; StringTokenizer st; private 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()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } int[][] nextIntArray(int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = sc.nextInt(); } } return a; } long[][] nextLongArray(int n, int m) { long[][] a = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextLong(); } } return a; } List<Integer> nextList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextInt()); } return list; } List<Long> nextLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(nextLong()); } return list; } char[] nextCharArray(int n) { return sc.next().toCharArray(); } char[][] nextCharArray(int n, int m) { char[][] c = new char[n][m]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < m; j++) { c[i][j] = s.charAt(j); } } return c; } } private static class MyWriter extends PrintWriter { private MyWriter(OutputStream outputStream) { super(outputStream); } void printArray(int[] a) { for (int i = 0; i < a.length; ++i) { print(a[i]); print(i == a.length - 1 ? '\n' : ' '); } } void printlnArray(int[] a) { for (int v : a) { println(v); } } void printList(List<Integer> list) { for (int i = 0; i < list.size(); ++i) { print(list.get(i)); print(i == list.size() - 1 ? '\n' : ' '); } } void printlnList(List<Integer> list) { list.forEach(this::println); } } private <T> List<List<T>> createGraph(int n) { List<List<T>> g = new ArrayList<>(); for (int i = 0; i < n; ++i) { g.add(new ArrayList<>()); } return g; } public static void main(String[] args) { out = new MyWriter(new BufferedOutputStream(System.out)); sc = new MyScanner(); new Main().run(); out.close(); } }
Java
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
98cd1de4952fd181bafa96cbe61ca159
train_003.jsonl
1597242900
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for $$$n$$$ days. On the $$$i$$$-th day: If Du can speak, he'll make fun of Boboniu with fun factor $$$a_i$$$. But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant $$$m$$$. On the $$$i$$$-th day: If Du can speak and $$$a_i&gt;m$$$, then Boboniu will be angry and muzzle him for $$$d$$$ days, which means that Du won't be able to speak on the $$$i+1, i+2, \cdots, \min(i+d,n)$$$-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of $$$a$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jaynil */ 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); DBoboniuChatsWithDu solver = new DBoboniuChatsWithDu(); solver.solve(1, in, out); out.close(); } static class DBoboniuChatsWithDu { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long d = in.nextInt(); long m = in.nextInt(); ArrayList<Long> big = new ArrayList<>(); ArrayList<Long> small = new ArrayList<>(); for (int i = 0; i < n; i++) { long temp = in.nextLong(); if (temp > m) { big.add(temp); } else { small.add(temp); } } small.add(Long.MAX_VALUE); big.add(Long.MAX_VALUE); Collections.sort(small, Collections.reverseOrder()); Collections.sort(big, Collections.reverseOrder()); small.set(0, 0L); big.set(0, 0L); for (int i = 1; i < small.size(); i++) { small.set(i, small.get(i - 1) + small.get(i)); } for (int i = 1; i < big.size(); i++) { big.set(i, big.get(i - 1) + big.get(i)); } if (big.size() == 1) { out.println(small.get(small.size() - 1)); return; } long ans = Long.MIN_VALUE; for (int i = 1; i < big.size(); i++) { long rem = n - ((i - 1) * (d + 1) + 1); if (rem >= 0) ans = Math.max(ans, big.get(i) + small.get((int) Math.min(rem, small.size() - 1))); // out.println(ans + " " + rem); } out.println(ans); } } 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
["5 2 11\n8 10 15 23 5", "20 2 16\n20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7"]
1 second
["48", "195"]
NoteIn the first example, you can set $$$a'=[15, 5, 8, 10, 23]$$$. Then Du's chatting record will be: Make fun of Boboniu with fun factor $$$15$$$. Be muzzled. Be muzzled. Make fun of Boboniu with fun factor $$$10$$$. Make fun of Boboniu with fun factor $$$23$$$. Thus the total fun factor is $$$48$$$.
Java 11
standard input
[ "dp", "sortings", "greedy", "brute force" ]
3dc8d6d89a29b0aa0b7527652c5ddae4
The first line contains three integers $$$n$$$, $$$d$$$ and $$$m$$$ ($$$1\le d\le n\le 10^5,0\le m\le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots,a_n$$$ ($$$0\le a_i\le 10^9$$$).
1,800
Print one integer: the maximum total fun factor among all permutations of $$$a$$$.
standard output
PASSED
d693957960f0b1cd42d594a403fd0b5b
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.*; public class Rain{ public static int solve(int[] day){ if( day.length == 1 ){ return day[0]; } int[] check = new int[day.length]; int i = 0; outer: while(true){ for(int j = 0; j < day.length; j++){ if( day[j] <= i ){ check[j] = 1; } } if( check[0] == 1 || check[day.length-1] == 1 ){ break; } for(int j = 0; j < check.length-1; j++){ if( check[j] == 1 && check[j+1] == 1 ){ break outer; } } i++; } return i; } public static void main(String[] args){ Scanner stdIn = new Scanner(System.in); int n = stdIn.nextInt(); int[] day = new int[n]; for(int i = 0; i < n; i++){ day[i] = stdIn.nextInt(); } System.out.println(solve(day)); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
bda6036cc282cb4be37f026450c3f1f4
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.*; public class Rain{ public static int solve(int[] day){ if( day.length == 1 ){ return day[0]; } int[] check = new int[day.length]; int i = 0; outer: while(true){ for(int j = 0; j < day.length; j++){ if( day[j] <= i ){ check[j] = 1; } } if( check[0] == 1 || check[day.length-1] == 1 ){ break; } for(int j = 0; j < check.length-1; j++){ if( check[j] == 1 && check[j+1] == 1 ){ break outer; } } i++; } return i; } public static void main(String[] args){ Scanner stdIn = new Scanner(System.in); int n = stdIn.nextInt(); int[] day = new int[n]; for(int i = 0; i < n; i++){ day[i] = stdIn.nextInt(); } System.out.println(solve(day)); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
228d2b5fe2b31d175ce78cac04cdecbd
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.*; public class Walk { public static void main(String[] args){ Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int[] t = new int[n]; for(int i = 0; i < n; i++) t[i] = reader.nextInt(); int min = Math.min(t[0], t[n-1]); boolean[] out = new boolean[n]; for(int i = 2; i < 1001; i++){ for(int j = 0; j < n; j++) out[j] |= t[j] < i; for(int j = 0; j < n-1; j++) if(out[j] && out[j+1]) min = Math.min(min,i-1); } System.out.println(min); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
2c3194ad8b7fea8db8cedbc00201578e
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.Scanner; public class CF192B { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int numTiles = scan.nextInt(); scan.nextLine(); int[] a_i = new int[numTiles+1]; int[] aiOriginal = new int[numTiles+1]; for(int i = 1; i < numTiles+1; i++) { a_i[i] = scan.nextInt(); aiOriginal[i] = a_i[i]; } int num = a_i[1]; int flag = 0; for(int i = 1; i < numTiles+1; i++) { if(num != a_i[i]) { flag = 1; } } if(0 == flag) { System.out.println(num); System.exit(0); } int day = 0; while(true) { day++; for(int i = 1; i < numTiles+1; i++) { a_i[i] -= 1; } for(int i = 1; i < numTiles; i++) { if((a_i[1] <= 0) || (a_i[numTiles] <= 0) || (a_i[i] <= 0 && a_i[i+1] <= 0)) { if(day > aiOriginal[numTiles] || day > aiOriginal[1]) { //System.out.println("option1 " + day + " " + a_i[numTiles]); System.out.println(0); } else { System.out.println(day); } System.exit(0); } } } } } /* public class CF192B { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int numTiles = scan.nextInt(); scan.nextLine(); int[] a_i = new int[numTiles+1]; for(int i = 1; i < numTiles+1; i++) { a_i[i] = scan.nextInt(); } int numDays = 1001; // Max number of tiles plus one. int maxDays; int prev1; int prev2; for(int i = numTiles+1; i > 3; i--) { maxDays = a_i[i]; prev1 = a_i[i-1]; prev2 = a_i[i-2]; if(maxDays >= prev1 && maxDays >= prev2) { if(prev1 > prev2) { maxDays = prev1; i -= 0; } else if (prev2 > prev1) { maxDays = prev2; i -= 1; } else if (prev2 == prev1) { maxDays = prev1; i -= 0; } } else if(maxDays <= prev1 || maxDays <= prev2) { } } } public static void checkInput(int numTiles, int[] a_i) { System.out.println(numTiles); for(int i = 1; i < numTiles+1; i++) { System.out.print(a_i[i] + " "); } } }*/
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
c224cd9aba9ee931d1e8dae5c5dde5da
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class B { 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()){ tok = new StringTokenizer(in.readLine()); } 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()); } int[] readArr(int n) throws IOException{ int[] res = new int[n]; for(int i = 0; i < n; i++){ res[i] = readInt(); } return res; } long[] readArrL(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++){ res[i] = readLong(); } return res; } public static void main(String[] args){ new B().run(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } void solve() throws IOException{ int n = readInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = readInt(); int min = a[0]; for(int i = 1; i < n-1; i++){ int cur = max(a[i], a[i+1]); min = min(min, cur); } min = min(min, a[n-1]); out.println(min); } void maxHepify(int[] a, int i, int length){ int l = (i<<1) + 1; int r = (i<<1) + 2; int largest = i; if(l < length && a[l] > a[largest]) largest = l; if(r < length && a[r] > a[largest]) largest = r; if(largest != i){ a[largest] += a[i]; a[i] = a[largest] - a[i]; a[largest] -= a[i]; maxHepify(a, largest, length); } } void buildMaxHeap(int[] a){ for(int i = a.length/2 - 1; i >= 0; i--){ maxHepify(a, i, a.length); } } void heapSort(int[] a){ buildMaxHeap(a); for(int i = a.length - 1; i > 0; i--){ a[i] += a[0]; a[0] = a[i] - a[0]; a[i] -= a[0]; maxHepify(a, 0, i); } } int[] zFunction(char[] s){ int[] z = new int[s.length]; z[0] = 0; for (int i=1, l=0, r=0; i<s.length; ++i) { if (i <= r) z[i] = min (r-i+1, z[i-l]); while (i+z[i] < s.length && s[z[i]] == s[i+z[i]]) ++z[i]; if (i+z[i]-1 > r){ l = i; r = i+z[i]-1; } } return z; } int[] prefixFunction(char[] s){ int[] pr = new int[s.length]; for (int i = 1; i< s.length; ++i) { int j = pr[i-1]; while (j > 0 && s[i] != s[j]) j = pr[j-1]; if (s[i] == s[j]) ++j; pr[i] = j; } return pr; } int ModExp(int a, int n, int mod){ int res = 1; while (n!=0) if ((n & 1) != 0) { res = (res*a)%mod; --n; } else { a = (a*a)%mod; n >>= 1; } return res; } public static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } boolean isPrime(int a){ for(int i = 2; i <= sqrt(a); i++) if(a % i == 0) return false; return true; } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ if(min(a,b) == 0) return max(a,b); return gcd(max(a, b) % min(a,b), min(a,b)); } static long lcm(long a, long b){ return a * b /gcd(a, b); } } /*class Treap<K extends Comparable<K>>{ public K x; public double y; public Treap<K> left; public Treap<K> right; public Treap(K x, double y, Treap<K> left, Treap<K> right) { this.x = x; this.y = y; this.left = left; this.right = right; } public static <K extends Comparable<K>> Treap<K> merge(Treap<K> l, Treap<K> r){ if(l == null) return r; if(r == null) return l; if(l.y > r.y){ return new Treap<K>(l.x, l.y, l.left, merge(l.right, r)); } else{ return new Treap<K>(r.x, r.y, merge(l, r.left), r.right); } } public void split(K x, Treap<K> left, Treap<K> right){ Treap<K> newTreap = null; if(this.x.compareTo(x) <= 0){ if(this.right == null){ right = null; } else{ right.split(x, newTreap, right); } left = new Treap<K>(this.x, this.y, left, newTreap); } else{ if(this.left == null){ left = null; } else{ left.split(x, left, newTreap); } right = new Treap<K>(x, y, newTreap, right); } } public Treap<K> add(K x){ Treap<K> left = null, right = null; this.split(x, left, right); Treap<K> temp = new Treap<K>(x, random(), null, null); return merge(merge(left, temp), right); } @SuppressWarnings("null") public Treap<K> remove(K x){ Treap<K> left = null, temp = null, right = null; this.split(x, left, right); right.split(x, temp, right); return merge(left, right); } public static <K extends Comparable<K>> Treap<K> build(K[] a){ Treap<K> temp = new Treap<K>(a[0], random(), null, null); for(int i = 1; i < a.length; i++){ temp = temp.add(a[i]); } return temp; } }*/
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
fe0d935311e2dfd6fef10dd937f0e2d9
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.*; public class cf120 { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=ini(s),a[]=new int[n],res=0,max=0,min=100000; for(int i=n-1;i>=0;i--) a[i]=ini(s); /*for(int i=0;i<n-1;i++) if(max<a[i])max=a[i]; for(int i=0;i<=max;i++) { a[0]--; for(int j=1;j<n;j++) { a[j]--; if(a[j] res++; } }*/ if(a[0]<=a[n-1])res=a[0]; else res=a[n-1]; for(int i=1;i<n-2;i++) { res = Math.min(res,Math.max(a[i],a[i+1])); } outl(res); } public static String ins(Scanner s) { String str=s.next(); return str; } public static int ini(Scanner s) { int n=s.nextInt(); return n; } public static long inl(Scanner s) { long n=s.nextLong(); return n; } public static double ind(Scanner s) { double n=s.nextDouble(); return n; } public static void out(Object a) { System.out.print(a+" "); } public static void outl(Object a) { System.out.println(a); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
a69837c06fd7f5bf8d5e0691bfc8453b
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int []a = new int [n+1]; for (int i = 1; i < a.length; i++) { a[i] = sc.nextInt(); } int []dp = new int [n+1]; dp[1] = a[1]; for (int i = 2; i < dp.length; i++) { dp[i] = Math.min(a[i], Math.max(dp[i-1], dp[i-2])); } System.out.println(dp[n]); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
a37f45abca36a790b64ad19e03d3c9b2
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.Scanner; public class A { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); int a[]= new int[n]; for(int i=0; i<n; i++) a[i]=sc.nextInt(); int min = Math.min(a[0],a[n-1]); int max; int k =0; while (k<n-2) { if (a[k+1]>a[k+2]) { max = a[k+1]; k++; } else { max = a[k+2]; k+=2; } if (max<min) min = max; } System.out.println(min); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
23fc5ab57f7492e58ce5868427095871
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.*; import java.util.*; import java.lang.Math.*; public class B121 { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st; int n=Integer.parseInt(br.readLine()); st=new StringTokenizer(br.readLine()); int i; int a[]=new int[n]; for(i=0;i<n;i++) { a[i]=Integer.parseInt(st.nextToken()); } int min=Math.min(a[0],a[n-1]); for(i=1;i<n-1;i++) { if(min>Math.max(a[i],a[i+1]))min=Math.max(a[i],a[i+1]); } pw.println(min); pw.flush(); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
7f1e2dbf2eccfa0491b9ebc7b41659d4
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) throws Exception { new B().solve(); } void solve() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int[] a = new int[n]; String[] sp = in.readLine().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sp[i]); } if (n == 1) { System.out.println(a[0]); return; } int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = a[i]; } dp[n - 2] = Math.min(dp[n - 2], dp[n - 1]); for (int i = n - 3; i >= 0; i--) { dp[i] = Math.min(dp[i], Math.max(dp[i + 1], dp[i + 2])); } System.out.println(dp[0]); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
d0f6e4034499cee5487587588411fca4
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.*; import java.io.*; public class B{ public static void main(String args[]) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine()); int tile[] = new int[num]; String s = br.readLine(); String st[] = s.split(" "); Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); ArrayList<Integer> key = new ArrayList<Integer>(); boolean floor[] = new boolean[num]; Arrays.fill(floor, true); for(int i = 0 ; i < st.length ; i++){ tile[i] = Integer.parseInt(st[i]); key.add(tile[i]); if(map.containsKey(tile[i])) { ArrayList<Integer> tempal = map.get(tile[i]); tempal.add(i); map.put(tile[i], tempal); } else { ArrayList<Integer>tempal = new ArrayList<Integer>(); tempal.add(i); map.put(tile[i], tempal); } } int sum = 0; Collections.sort(key); //~ for(int i = 0 ; i < key.size() ; i++){ //~ int tmp = key.get(i); //~ System.out.println(tmp + " size "+map.get(tmp).size()); //~ } boolean finished = false; for(int i = 0 ; i < key.size() ; i++){ int temp = key.get(i); sum = temp; ArrayList<Integer> tempal = map.get(temp); //~ System.out.println(tempal.size()); for(int j = 0 ; j < tempal.size() ; j++){ floor[tempal.get(j)] = false; } //~ System.out.println(Arrays.toString(floor)); if(!floor[0] || !floor[num-1]) { break; } for(int j = 0 ; j < num-2 ; j++){ if(!floor[j]) {continue;} if(!floor[j+1] && !floor[j+2]) { finished = true; break; } } if(finished) { break; } } System.out.println(sum); //~ for(int } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
e726b4aaa0bbaa1c1e4ee66227291c09
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); int num=0,max,min; max=min=num=in.nextInt(); int k=0; while(--n>0) { num= in.nextInt(); if(num<max) { if(k==0) { k=num; } else { if(k>num) { max=k; min=num; k=num; } else { max=num; min=k; k=0; } } } else { k=0; } } if(max>num) { System.out.println(num); } else { System.out.println(max); } } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
7c85df1f0a1b5048df8851651dbceab0
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.*; public class WalkingInRain192B { public static class Rain implements Comparable<Rain> { public int index; public int time; public Rain(int inputindex, int inputtime) { index = inputindex; time = inputtime; } public int compareTo(Rain other) { return this.time - other.time; } } public static void main(String[] args) { // Set up scanner Scanner sc = new Scanner(System.in); // System.out.println("Enter n"); int n = sc.nextInt(); Rain[] a = new Rain[n]; for (int i=0; i<n; i++) { // System.out.println("Enter time for cell " + (i+1)); int x = sc.nextInt(); a[i] = new Rain(i+1, x); } Arrays.sort(a); Set<Integer> badones = new HashSet<Integer>(); for (int i=0; i<n; i++) { Rain r = a[i]; int time = r.time; int index = r.index; if (index == 1 || index == n) // End square destroyed { System.out.println(time); return; } if (badones.contains(index)) { System.out.println(time); // Two in a row destroyed return; } // Add the bad squares to the set badones.add(index+1); badones.add(index-1); } } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
be2e1b69f49675214c63fc2eef0f941c
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class b121 { public static void debug(Object... obs) { System.out.println(Arrays.deepToString(obs)); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int[]a=new int[n]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextInt(); } int ma=Math.min(a[0],a[n-1]); for(int i=0;i<n-1;i++) { if(i+2 < n) { if(a[i+2] > a[i+1]) { ma = Math.min(ma, a[i+2]); i++; } else { ma = Math.min(ma, a[i+1]); } continue; } ma = Math.min(ma, a[i+1]); } System.out.println(ma); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
437967b38926923de709951417a001bc
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.*; import java.util.*; public class TaskB { BufferedReader br; PrintWriter out; StringTokenizer stok; void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } if (n == 1) { out.println(a[0]); return; } int[] min = new int[n]; min[0] = a[0]; min[1] = Math.min(a[0], a[1]); for (int i = 2; i < n; i++) { min[i] = Math.min(a[i], Math.max(min[i - 1], min[i - 2])); } out.println(min[n - 1]); } String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } void run() throws IOException { // br = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); br.close(); out.close(); } public static void main(String[] args) throws IOException { // Locale.setDefault(Locale.US); new TaskB().run(); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
ede1d55c8e265de8c41372381314d34a
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.*; public class B { public static void main(String[] args) throws IOException { BufferedReader r; r = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(r.readLine()); int[] a = str2int(r.readLine().toCharArray(), n); boolean[] b = new boolean[n]; if (n == 1) System.out.print(a[0]); else { int m = Math.max(a[0], a[1]); for (int i = 1; i < n; i++) { if ((m > a[i]) && (m > a[i - 1])) { m = Math.max(a[i], a[i - 1]); } } System.out.print(Math.min(Math.min(a[0], a[n - 1]), m)); } } static int[] str2int(char[] str, int n) { int[] q = new int[n]; int p = 0; for (int i = 0; i < n; i++) { int j = 0; while (str[p] != ' ') { j *= 10; j += (int) str[p] - 48; if (++p == str.length) break; } p++; q[i] = j; } return q; } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
64e74dccd6aa8f527c4c4752450ea8c8
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; public class B { public static void main(String[] args) throws Exception { int n = nextInt(); int[] mas = new int[n]; for(int i = 0; i<n; i++) { mas[i] = nextInt(); } int cur = 0; int ans = mas[0]; if(n < 3) { exit(Math.min(mas[0], mas[n-1])); } while(true) { if(cur == n-2 || cur == n - 3) { exit(Math.min(ans, mas[n-1])); } if(mas[cur+1] > mas[cur+2]) { ans = Math.min(ans, mas[cur+1]); cur += 1; } else { ans = Math.min(ans, mas[cur+2]); cur += 2; } } } // /////////////////////////////////////////////////////////////// // IO // /////////////////////////////////////////////////////////////// private static StreamTokenizer in; private static PrintWriter out; private static BufferedReader inB; private static boolean FILE = false; private static int nextInt() throws Exception { in.nextToken(); return (int) in.nval; } private static String nextString() throws Exception { in.nextToken(); return in.sval; } static { try { out = new PrintWriter(false ? (new FileOutputStream("out.txt")) : System.out); inB = new BufferedReader(new InputStreamReader( FILE ? new FileInputStream("lvl3.txt") : System.in)); } catch (Exception e) { e.printStackTrace(); } in = new StreamTokenizer(inB); } // /////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////// // pre - written // /////////////////////////////////////////////////////////////// private static void println(Object o) throws Exception { out.println(o); out.flush(); } private static void exit(Object o) throws Exception { println(o); exit(); } private static void exit() { System.exit(0); } private static final int INF = Integer.MAX_VALUE; private static final int MINF = Integer.MIN_VALUE; // //////////////////////////////////////////////////////////////// }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
b78db1249c06b47d7f64b54c180ef9f5
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Main { public static void main(String[] args) throws IOException { (new Main()).solve(); } public void Main() { } void solve() throws IOException { // BufferedReader in = new BufferedReader(new // InputStreamReader(System.in)); // Scanner in = new Scanner(System.in); MyReader in = new MyReader(); PrintWriter out = new PrintWriter(System.out); // Scanner in = new Scanner(new FileReader("input.txt")); // PrintWriter out = new PrintWriter("output.txt"); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int[] res = new int[n]; res[0] = a[0]; if (n > 1) { res[1] = Math.min(a[0], a[1]); } for (int i = 2; i < n; i++) { res[i] = Math.min(a[i], Math.max(res[i - 1], res[i - 2])); } out.print(res[n - 1]); out.close(); } }; class MyReader { private BufferedReader in; String[] parsed; int index = 0; public MyReader() { in = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Integer.parseInt(parsed[index++]); } public long nextLong() throws NumberFormatException, IOException { if (parsed == null || parsed.length == index) { read(); } return Long.parseLong(parsed[index++]); } public String nextString() throws IOException { if (parsed == null || parsed.length == index) { read(); } return parsed[index++]; } private void read() throws IOException { parsed = in.readLine().split(" "); index = 0; } public String readLine() throws IOException { return in.readLine(); } };
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
5537ed50e44d495060d63609d0c842d5
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class Walking { private static boolean possible(int[] a, int x) { int dp[] = new int[a.length]; Arrays.fill(dp, 0); dp[0] = 1; for (int i=1; i<a.length; i++) { dp[i] = dp[i-1]; if (i-2 >= 0 && dp[i] == 0) dp[i] = dp[i-2]; if (a[i] < x) dp[i] = 0; } if (dp[a.length-1] > 0) return true; return false; } public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); st.nextToken(); int n = (int) st.nval; int a[] = new int[n]; for (int i=0; i<n; i++) { st.nextToken(); a[i] = (int) st.nval; } int last = Math.min(a[0], a[n-1]); int res = 0; for (int x=last; x>=0; x--) if (possible(a, x)) { res = x; break; } pw.write(res + "\n"); pw.flush(); pw.close(); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
0adbc3d82bedac7be96bea44ab223786
train_003.jsonl
1338132600
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i &lt; n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class Walking { private static boolean possible(int[] a, int x) { int dp[] = new int[a.length]; Arrays.fill(dp, 0); dp[0] = 1; for (int i=1; i<a.length; i++) { dp[i] = dp[i-1]; if (i-2 >= 0 && dp[i] == 0) dp[i] = dp[i-2]; if (a[i] < x) dp[i] = 0; } if (dp[a.length-1] > 0) return true; return false; } public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); st.nextToken(); int n = (int) st.nval; int a[] = new int[n]; for (int i=0; i<n; i++) { st.nextToken(); a[i] = (int) st.nval; } int last = Math.min(a[0], a[n-1]); int res = 0; int left = 1, right = last + 1; while (right - left > 1) { int x = (left + right) / 2; if (possible(a, x)) { left = x; } else { right = x; } } res = left; pw.write(res + "\n"); pw.flush(); pw.close(); } }
Java
["4\n10 3 5 10", "5\n10 2 8 3 5"]
2 seconds
["5", "5"]
NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
Java 6
standard input
[ "implementation", "brute force" ]
d526af933b5afe9abfdf9815e9664144
The first line contains integer n (1 ≀ n ≀ 103) β€” the boulevard's length in tiles. The second line contains n space-separated integers ai β€” the number of days after which the i-th tile gets destroyed (1 ≀ ai ≀ 103).
1,100
Print a single number β€” the sought number of days.
standard output
PASSED
0da84d98c70b8b4ee54480008e808d70
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class GoodMatrixElements { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int total = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int num = input.nextInt(); if(i == j || i == n/2 || j == n/2 || i == n - 1 - j) { total += num; } } } System.out.println(total); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
75aad91acbaea98b29126f36872b6c2a
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { PrintWriter pw = new PrintWriter(System.out); Input(new FastReader(), pw); pw.flush(); pw.close(); } public static void Input(FastReader input, PrintWriter pw) { int n = input.nextInt(); int c, sum = 0; int m = (n - 1) / 2, l = 0, r = n - 1; int i = 0; while (i < n) { int j = 0; while (j < n) { c = input.nextInt(); if (i == m || j == m || j == l || j == r) { sum += c; } ++j; } ++i; r--; l++; } pw.println(sum); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String string = ""; try { string = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return string; } char nextChar() { return next().charAt(0); } String[] nextStringArray() { String[] str = null; try { str = this.br.readLine().trim().split(" "); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray() { String[] data = nextStringArray(); int[] a = new int[data.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(data[i]); } return a; } Integer[] nextIntegerArray() { String[] data = nextStringArray(); Integer[] a = new Integer[data.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(data[i]); } return a; } long[] nextLongArray() { String[] data = nextStringArray(); long[] a = new long[data.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(data[i]); } return a; } public boolean hasNext() throws IOException { if (st != null && st.hasMoreTokens()) { return true; } String s = br.readLine(); if (s == null || s.isEmpty()) { return false; } st = new StringTokenizer(s); return true; } } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
b0151b66fce35222b9b4a7f8563f6f9c
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class GoodMatrixA { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), i, sum = 0; ArrayList<Integer> good = new ArrayList<Integer>(); for (i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a = in.nextInt(); if (i == n / 2 || j == n / 2 || i == j || i + j == n - 1) { good.add(a); } } } for (int x : good) { sum += x; } System.out.println(sum); in.close(); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
427adf277c0b5f791f8f7f3465de1edd
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] matrix = new int[n][n]; int sum = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { matrix[i][j] = in.nextInt(); } if (i == n / 2) { for (int z = 0; z < n; z++) { sum += matrix[i][z]; } } else { sum += matrix[i][i]; sum += matrix[i][n/2]; sum += matrix[i][n-1-i]; } } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
c8024ba335710dc6eb82cd1853bedc1e
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class A2_177 { static boolean[][] noolean; static int size; public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(System.in); size = scan.nextInt(); int[][] arr = new int[size][size]; noolean = new boolean[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { arr[i][j] = scan.nextInt(); } } int sum=0; for(int i=0;i<size;i++) { for(int j=0;j<size;j++) { if(noolean[i][j]!= true) { if(i==j) { sum+=arr[i][j]; noolean[i][j]=true; } else if(i+j==size-1) { sum+=arr[i][j]; noolean[i][j]= true; } else if(i==(size-1)/2) { sum+=arr[i][j]; noolean[i][j]=true; } else if(j==(size-1)/2) { sum+=arr[i][j]; noolean[i][j]=false; } } } } System.out.println(sum); } static void printBool() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print(noolean[i][j] + " "); } System.out.println(); } } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
85707bfa50ba2a9776a786679388465d
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public class cf177a2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; n=sc.nextInt(); int a; int s=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a=sc.nextInt(); if(i==j) { s=s+a; } else if(i+j==n-1) { s=s+a; } else if(2*i==n-1) { s=s+a; } else if(2*j==n-1) { s=s+a; } } } System.out.print(s+"\n"); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
949ff7a6989778a676d198c4032939ca
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
public class GoodMatrixElements{ public static void main(String []args){ java.util.Scanner scan=new java.util.Scanner(System.in); int n=scan.nextInt(); int [][]arr=new int[n][n]; for(int i=1;i<=arr.length;i++){ for(int j=1;j<=arr.length;j++){ arr[i-1][j-1]=scan.nextInt(); } } long sum=0; for(int i=1;i<=arr.length;i++){ sum+=arr[i-1][i-1]; sum+=arr[i-1][arr.length-i]; } //System.out.println("sum: "+sum); for(int i=1;i<=arr.length;i++){ sum+=arr[arr.length/2][i-1]; sum+=arr[i-1][arr.length/2]; } //System.out.println("sum: "+sum); sum-=3*arr[arr.length/2][arr.length/2]; System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
eaaa0fcd64e96c6e0fc0e68aa12f66e5
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int arr[][] = new int[n][n]; int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { arr[i][j] = input.nextInt(); if (i == j) { sum+= arr[i][j] ; } else if ((i+j) == (n-1)) { sum+= arr[i][j] ; }else if ( (i == ((n-1)/2) ) || (j == ((n-1)/2) ) ) { sum+= arr[i][j] ; } } } System.out.print(""+sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
82eb19a49973db5bb95a409eb9b13c45
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x = input.nextInt(); int[][] data = new int[x][x]; x = 0; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { data[i][j] = input.nextInt(); if (i == j || i == (data.length - 1) / 2 || j == (data.length - 1) / 2 || i + j == data.length - 1 || j + i == data.length - 1) { x = x + data[i][j]; } } } System.out.println(x); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
227d9d26a9796289568349cf3b2f8a7f
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.Scanner; public class CF2 { Scanner inputScan = new Scanner(System.in); int[][] mat; public static void main(String[] args) { CF2 program = new CF2(); program.execute(); } public void execute() { int N = inputScan.nextInt(); mat = new int[N][N]; int result = 0, input; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { input = inputScan.nextInt(); if(i==j) result += input; else if(i == (N-j-1)) result += input; else if(i == (N/2)) result += input; else if(j == (N/2)) result += input; } } System.out.println(result); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
d3547ad586f1fe2bdbb9478346f413a7
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws IOException { InputStreamReader input = new InputStreamReader(System.in); // FileReader input = new FileReader("input.txt"); In in = new In(input); // int t = in.ni(); int t = 1; while (t-- > 0) { int n = in.ni(); int[][] mat = new int[n][n]; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { mat[i][j] = in.ni(); } } int sum = 0; for (int i=0; i<n; i++) { sum += mat[n/2][i]; sum += mat[i][n/2]; sum += mat[i][i]; sum += mat[i][n-1-i]; } System.out.println(sum-(3*mat[n/2][n/2])); } } } class In { BufferedReader br; StringTokenizer st; public In (InputStreamReader input) { this.br = new BufferedReader(input); this.st = new StringTokenizer(""); } public In (FileReader input) { this.br = new BufferedReader(input); this.st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } /* read next-string */ String ns() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } /* read next-int */ int ni() { return Integer.parseInt(next()); } /* read next-double */ long nl() { return Long.parseLong(next()); } /* read next-long */ double nd() { return Double.parseDouble(next()); } /* read int array[n] */ int[] ia(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) { a[i] = ni(); } return a; } /* read long array[n] */ long[] la(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) { a[i] = nl(); } return a; } /* read double array[n] */ double[] da(int n) { double[] a = new double[n]; for (int i=0; i<n; i++) { a[i] = nd(); } return a; } } class Out { StringBuffer s = new StringBuffer(""); void lp(StringBuffer s) { System.out.println(s); } /* print int[] array */ void pa(int[] a) { StringBuffer s = new StringBuffer(""); for (int x: a) { s.append(x+" "); } lp(s); } /* print String[] array */ void pa(String[] a) { StringBuffer s = new StringBuffer(""); for (String x: a) { s.append(x+" "); } lp(s); } /* print char[] array */ void pa(char[] a) { StringBuffer s = new StringBuffer(""); for (char x: a) { s.append(x+" "); } lp(s); } /* print long[] array */ void pa(long[] a) { StringBuffer s = new StringBuffer(""); for (long x: a) { s.append(x+" "); } lp(s); } /* print boolean[] array */ void pa(boolean[] a) { StringBuffer s = new StringBuffer(""); for (boolean x: a) { s.append(x+" "); } lp(s); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
3d95e8f15f6cf8170efdfd1b230976ab
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class mohammad{ public static void main(String args[]){ Scanner U = new Scanner(System.in); int t = U.nextInt(); int[][] a= new int[t][t]; for(int i=0 ; i<t ; i++) for(int j=0 ; j<t ; j++) a[i][j]= U.nextInt(); int sum = 0; for(int i=0 ; i<t ; i++) for(int j=0 ; j<t ; j++){ if(i==j || (i+j)==(t-1) || i==(t-1)/2 ||j==(t-1)/2) sum+=a[i][j]; } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
d33a67bd4be68473fea95e7e70a4fe0a
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.Scanner; public class Good_Matrix_Elements { public static void main(String[] args) { Scanner t = new Scanner(System.in); int n = t.nextInt(); int sum = 0; int centre = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a = t.nextInt(); if (i == j) sum += a; if (i + j == n - 1) sum += a; if (i == n / 2) sum += a; if (j == n / 2) sum += a; if (i == j && i == n / 2) centre = a; } } System.out.println(sum - 3 * centre); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
0e860ce3b82cfe603c1fef8972a59642
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class Solution{ static Scanner scan = new Scanner(System.in); public static void main(String args[]){ int n = scan.nextInt(); int[][] matrix = new int[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ matrix[i][j] = scan.nextInt(); } } int mid = n / 2; long sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0; for(int i = 0; i < n; i++){ sum1 += matrix[i][i]; sum2 += matrix[i][n-i-1]; sum3 += matrix[i][mid]; sum4 += matrix[mid][i]; } System.out.println((sum1+sum2+sum3+sum4) - 3*matrix[mid][mid]); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
c8b766b3c602f48ee0c75ae24ea1cfa4
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.Scanner; public class CodeAvr { public static void main(String[] args) { Scanner jin = new Scanner(System.in); int n = jin.nextInt(); int a[][] = new int[101][101]; int sum = 0; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ a[i][j] = jin.nextInt(); if(i == j){ sum += a[i][j]; }else if( i == (n-1)/2){ sum += a[i][j]; }else if (j == (n-1)/2){ sum += a[i][j]; }else if (i + j == n - 1){ sum += a[i][j]; } } } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
e83da33fb6d00bb178fee80dbbf7deb9
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class A_177 { public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[][] matrix = new int[n][n]; for (int i=0; i<n; i++) { String[] row = br.readLine().split(" "); for (int j=0; j<n; j++) { matrix[i][j] = Integer.parseInt(row[j]); } } int sum=0; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if ((i==j) || (i == (n-1-j) || ( i== (n-1)/2) || (j == (n-1)/2))) { sum += matrix[i][j]; } } } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
fbb2c8039fc3343ce9528927ae9231be
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class A1177 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int sum = 0; int middle = (int) Math.floor(n/2); int[][] ary = new int[n][n]; for( int i = 0; i < n; i++ ) { for( int j = 0; j < n; j++ ) { ary[i][j] = in.nextInt(); } } for( int i = 0; i < n; i++ ) { for( int j = 0; j < n; j++ ) { if ( i == middle ) { sum += ary[i][j]; } else { if( j == i || j == middle || j == n-i-1 ) { sum += ary[i][j]; } } } } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
a35cdbac5b5eea54b65fd659160daa4d
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.FileReader; import java.io.FileWriter; //import java.lang.StringBuilder; import java.util.StringTokenizer; //import java.lang.Comparable; //import java.util.Arrays; //import java.util.HashMap; //import java.util.ArrayList; //import java.util.LinkedList; public class GoodMatrixElements { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws IOException { //StringTokenizer st = new StringTokenizer(in.readLine()); //BufferedReader in = new BufferedReader(new FileReader("input.txt")); //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); int n = Integer.parseInt(in.readLine()); int m = n / 2; int sum = 0; int g = 0; int d = n - 1; for(int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(in.readLine()); if(i == m) { for(int j = 0; j < n; j++) { sum += Integer.parseInt(st.nextToken()); } } else { for(int j = 0; j < n; j++) { int k = Integer.parseInt(st.nextToken()); if(j == g || j == m || j == d) { sum += k; } } } g++; d--; } out.print(sum); out.close(); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
ed1022103a4c3fa2a72924f92de66dc3
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int res=0; for(int i = 0;i<n;i++) { for(int j = 0 ;j<n;j++){ int x = sc.nextInt(); if(i==n/2 || j==n/2 || i==j || (i+j) == n-1 ){ res+=x; } } } System.out.println(res); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
d0885c54a94b9d78bac0faf5da83d94b
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.io.*; import java.util.*; public class GoodMatrixElements { public static void main(String []args)throws IOException { Scanner scan = new Scanner(System.in); int n; long sum = 0; n = scan.nextInt(); boolean sumDone[][] = new boolean[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) sumDone[i][j] = false; } int matrix[][] = new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) matrix[i][j] = scan.nextInt(); } for(int i=0;i<n;i++) { sum += matrix[i][i]; sumDone[i][i] = true; } for(int i=0;i<n;i++) { if(sumDone[i][n-1-i] == false) { sum += matrix[i][n-1-i]; sumDone[i][n-1-i] = true; } } for(int i=0;i<n;i++) { if(sumDone[(n-1)/2][i] == false) { sum += matrix[(n-1)/2][i]; sumDone[(n-1)/2][i] = true; } } for(int i=0;i<n;i++) { if(sumDone[i][(n-1)/2] == false) { sum += matrix[i][(n-1)/2]; sumDone[i][(n-1)/2] = true; } } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
c0f34a43b53d871190c5e2342732982d
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class ABA { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[][] array = new int[N][N]; boolean[][] B = new boolean[N][N]; long ans = 0; for(int a=0;a<N;a++){ for(int b=0;b<N;b++){ array[a][b]=sc.nextInt(); } } for(int a=0;a<N;a++){ for(int b=0;b<N;b++){ if(a==b){ if(!B[a][b]){ B[a][b]=true; ans+=array[a][b]; } } } } for(int a=0;a<N;a++){ for(int b=0;b<N;b++){ if(a==(N-1-b)){ if(!B[a][b]){ B[a][b]=true; ans+=array[a][b]; } } } } for(int a=0;a<N;a++){ //for(int b=0;b<N;b++){ int b = N/2; if(!B[a][b]){ B[a][b]=true; ans+=array[a][b]; } } for(int a=0;a<N;a++){ //for(int b=0;b<N;b++){ int b = N/2; if(!B[b][a]){ B[b][a]=true; ans+=array[b][a]; } } System.out.println(ans); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
e5ded8ab7d77dc68d272872ccccded32
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
import java.util.*; public class ABA { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[][] array = new int[N][N]; boolean[][] B = new boolean[N][N]; long ans = 0; for(int a=0;a<N;a++){ for(int b=0;b<N;b++){ array[a][b]=sc.nextInt(); } } for(int a=0;a<N;a++){ for(int b=0;b<N;b++){ if(a==b){ if(!B[a][b]){ B[a][b]=true; ans+=array[a][b]; } } } } for(int a=0;a<N;a++){ for(int b=0;b<N;b++){ if(a==(N-1-b)){ if(!B[a][b]){ B[a][b]=true; ans+=array[a][b]; } } } } for(int a=0;a<N;a++){ //for(int b=0;b<N;b++){ int b = N/2; if(!B[a][b]){ B[a][b]=true; ans+=array[a][b]; } } for(int a=0;a<N;a++){ //for(int b=0;b<N;b++){ int b = N/2; if(!B[b][a]){ B[b][a]=true; ans+=array[b][a]; } } System.out.println(ans); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
98ba8cf453dd1ca32f7411a1521c6029
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
//package javaapplication262; import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[][] = new int[n][n]; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { arr[i][j] = sc.nextInt(); } } int sum = 0; for(int i=0; i<n; i++) { sum += arr[i][i]; } for(int i=0; i<n; i++) { sum += arr[n/2][i]; } for(int i=0; i<n; i++) { sum += arr[i][n/2]; } for(int i=0; i<n; i++) { sum += arr[i][n-i-1]; } int middle = arr[n/2][n/2]; System.out.println(sum - 3*middle); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output
PASSED
ad062c0cad5edc419c22531e3702227d
train_003.jsonl
1335016800
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n × n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the "middle" row β€” the row which has exactly rows above it and the same number of rows below it. Elements of the "middle" column β€” the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 × 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.math.*; /* Name of the class has to be "Main" only if the class is public. */ public final class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[][]=new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { arr[i][j]=sc.nextInt(); } } long sum=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j || j==(n-1-i) || i==n/2 || j==n/2) sum+=arr[i][j]; } } System.out.println(sum); } }
Java
["3\n1 2 3\n4 5 6\n7 8 9", "5\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1"]
2 seconds
["45", "17"]
NoteIn the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
Java 8
standard input
[ "implementation" ]
5ebfad36e56d30c58945c5800139b880
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 ≀ aij ≀ 100) separated by single spaces β€” the elements of the given matrix. The input limitations for getting 30 points are: 1 ≀ n ≀ 5 The input limitations for getting 100 points are: 1 ≀ n ≀ 101
800
Print a single integer β€” the sum of good matrix elements.
standard output