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
70a02728101c155aed2decb13823b485
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
public class Main { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String[] t = r.readLine().split(" "); int N = Integer.parseInt(t[0]); int M = Integer.parseInt(t[1]); int X = Integer.parseInt(t[2]); int Y = Integer.parseInt(t[3]); int Z = Integer.parseInt(t[4]); int P = Integer.parseInt(t[5]); X = X % 4; Y = Y % 2; Z = Z % 4; int N1, M1, N2, M2, N3, M3; if (X % 2 == 1) { N1 = M; M1 = N; } else { N1 = N; M1 = M; } /* if (Y % 2 == 1) { N2 = M1; M2 = N1; } else { N2 = N1; M2 = M1; } */ N2 = N1; M2 = M1; if (Z % 2 == 1) { N3 = M2; M3 = N2; } else { N3 = N2; M3 = M2; } for (int i = 0; i < P; i++) { t = r.readLine().split(" "); int x = Integer.parseInt(t[0]); int y = Integer.parseInt(t[1]); if (X == 0) { } else if (X == 1) { int temp = x; x = y; y = M1 - temp + 1; } else if (X == 2) { //int temp=x; x = N1 - x + 1; y = M1 - y + 1; } else if (X == 3) { int temp = x; x = N1 - y + 1; y = temp; } if (Y == 1) { y = M2 - y + 1; } if (Z == 0) { } else if (Z == 1) { int temp = x; x = N3 - y + 1; y = temp; } else if (Z == 2) { //int temp=x; x = N3 - x + 1; y = M3 - y + 1; } else if (Z == 3) { int temp = x; x = y; y = M3 - temp + 1; int jigar2=0; } System.out.println(x + " " + y); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
c8072d91f0270407bf46eff1605f2c89
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
public class Main { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String[] t = r.readLine().split(" "); int N = Integer.parseInt(t[0]); int M = Integer.parseInt(t[1]); int X = Integer.parseInt(t[2]); int Y = Integer.parseInt(t[3]); int Z = Integer.parseInt(t[4]); int P = Integer.parseInt(t[5]); X = X % 4; Y = Y % 2; Z = Z % 4; int N1, M1, N2, M2, N3, M3; if (X % 2 == 1) { N1 = M; M1 = N; } else { N1 = N; M1 = M; } N2 = N1; M2 = M1; if (Z % 2 == 1) { N3 = M2; M3 = N2; } else { N3 = N2; M3 = M2; } for (int i = 0; i < P; i++) { t = r.readLine().split(" "); int x = Integer.parseInt(t[0]); int y = Integer.parseInt(t[1]); if (X == 0) { } else if (X == 1) { int temp = x; x = y; y = M1 - temp + 1; } else if (X == 2) { //int temp=x; x = N1 - x + 1; y = M1 - y + 1; } else if (X == 3) { int temp = x; x = N1 - y + 1; y = temp; } if (Y == 1) { y = M2 - y + 1; } if (Z == 0) { } else if (Z == 1) { int temp = x; x = N3 - y + 1; y = temp; } else if (Z == 2) { //int temp=x; x = N3 - x + 1; y = M3 - y + 1; } else if (Z == 3) { int temp = x; x = y; y = M3 - temp + 1; } System.out.println(x + " " + y); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
0fe852fbb1dbc1856eea667f7a6d7d71
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
public class Main { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String[] t = r.readLine().split(" "); int N = Integer.parseInt(t[0]); int M = Integer.parseInt(t[1]); int X = Integer.parseInt(t[2]); int Y = Integer.parseInt(t[3]); int Z = Integer.parseInt(t[4]); int P = Integer.parseInt(t[5]); X = X % 4; Y = Y % 2; Z = Z % 4; int N1, M1, N2, M2, N3, M3; if (X % 2 == 1) { N1 = M; M1 = N; } else { N1 = N; M1 = M; } /* if (Y % 2 == 1) { N2 = M1; M2 = N1; } else { N2 = N1; M2 = M1; } */ N2 = N1; M2 = M1; if (Z % 2 == 1) { N3 = M2; M3 = N2; } else { N3 = N2; M3 = M2; } for (int i = 0; i < P; i++) { t = r.readLine().split(" "); int x = Integer.parseInt(t[0]); int y = Integer.parseInt(t[1]); if (X == 0) { } else if (X == 1) { int temp = x; x = y; y = M1 - temp + 1; } else if (X == 2) { //int temp=x; x = N1 - x + 1; y = M1 - y + 1; } else if (X == 3) { int temp = x; x = N1 - y + 1; y = temp; } if (Y == 1) { y = M2 - y + 1; } if (Z == 0) { } else if (Z == 1) { int temp = x; x = N3 - y + 1; y = temp; } else if (Z == 2) { //int temp=x; x = N3 - x + 1; y = M3 - y + 1; } else if (Z == 3) { int temp = x; x = y; y = M3 - temp + 1; } System.out.println(x + " " + y); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
9aaa65826605eee2536d8f3d79891841
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
public class Main { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String[] t = r.readLine().split(" "); int N = Integer.parseInt(t[0]); int M = Integer.parseInt(t[1]); int X = Integer.parseInt(t[2]); int Y = Integer.parseInt(t[3]); int Z = Integer.parseInt(t[4]); int P = Integer.parseInt(t[5]); X = X % 4; Y = Y % 2; Z = Z % 4; int N1, M1, N2, M2, N3, M3; if (X % 2 == 1) { N1 = M; M1 = N; } else { N1 = N; M1 = M; } /* if (Y % 2 == 1) { N2 = M1; M2 = N1; } else { N2 = N1; M2 = M1; } */ N2 = N1; M2 = M1; if (Z % 2 == 1) { N3 = M2; M3 = N2; } else { N3 = N2; M3 = M2; } for (int i = 0; i < P; i++) { t = r.readLine().split(" "); int x = Integer.parseInt(t[0]); int y = Integer.parseInt(t[1]); if (X == 0) { } else if (X == 1) { int temp = x; x = y; y = M1 - temp + 1; } else if (X == 2) { //int temp=x; x = N1 - x + 1; y = M1 - y + 1; } else if (X == 3) { int temp = x; x = N1 - y + 1; y = temp; } if (Y == 1) { y = M2 - y + 1; } if (Z == 0) { } else if (Z == 1) { int temp = x; x = N3 - y + 1; y = temp; } else if (Z == 2) { //int temp=x; x = N3 - x + 1; y = M3 - y + 1; } else if (Z == 3) { int temp = x; x = y; y = M3 - temp + 1; int jigar=0; } System.out.println(x + " " + y); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
b57825d4589e97c0152e1fbd97a35e7a
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
public class Main { public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); String[] t = r.readLine().split(" "); int N = Integer.parseInt(t[0]); int M = Integer.parseInt(t[1]); int X = Integer.parseInt(t[2]); int Y = Integer.parseInt(t[3]); int Z = Integer.parseInt(t[4]); int P = Integer.parseInt(t[5]); X = X % 4; Y = Y % 2; Z = Z % 4; int N1, M1, N2, M2, N3, M3; if (X % 2 == 1) { N1 = M; M1 = N; } else { N1 = N; M1 = M; } N2 = N1; M2 = M1; if (Z % 2 == 1) { N3 = M2; M3 = N2; } else { N3 = N2; M3 = M2; } int L = 0; for (int i = 0; i < P; i++) { t = r.readLine().split(" "); int x = Integer.parseInt(t[0]); int y = Integer.parseInt(t[1]); if (X == 0) { } else if (X == 1) { int temp = x; x = y; y = M1 - temp + 1; } else if (X == 2) { //int temp=x; x = N1 - x + 1; y = M1 - y + 1; } else if (X == 3) { int temp = x; x = N1 - y + 1; y = temp; } if (Y == 1) { y = M2 - y + 1; } if (Z == 0) { } else if (Z == 1) { int temp = x; x = N3 - y + 1; y = temp; } else if (Z == 2) { //int temp=x; x = N3 - x + 1; y = M3 - y + 1; } else if (Z == 3) { int temp = x; x = y; y = M3 - temp + 1; } System.out.println(x + " " + y); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
9d8c871778e83596e1e8d6d1da8d019c
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.awt.*; import java.awt.event.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; public class Main { static BufferedReader br; static StringTokenizer st; static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { int n = readInt(); int m = readInt(); int x = readInt() % 4; int y = readInt() % 2; int z = readInt() % 4; z *= 3; z %= 4; int qqq = readInt(); while(qqq-- > 0) { int saveN = n; int saveM = m; int currX = readInt()-1; int currY = readInt()-1; for(int i = 0; i < x; i++) { int[] rotate = rotate(currX, currY, n, m); currX = rotate[0]; currY = rotate[1]; int temp = m; m = n; n = temp; } if(y >= 1) { currY = m - 1 - currY; } for(int i = 0; i < z; i++) { int[] rotate = rotate(currX, currY, n, m); currX = rotate[0]; currY = rotate[1]; int temp = m; m = n; n = temp; } pw.println((currX+1) + " " + (currY+1)); n = saveN; m = saveM; } } pw.close(); } /* 0 0 to 0 m-1 1 0 to 0 m-2 r-2 0 to 0 1 r-1 0 to 0 0 0 1 to 1 c-1 0 2 to 2 c-1 0 c-1 to c-1 */ public static int[] rotate(int x, int y, int r, int c) { return new int[]{y, r-1-x}; } public static void exitImmediately() { pw.close(); System.exit(0); } public static int readInt() throws IOException { return Integer.parseInt(nextToken()); } public static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } public static long readLong() throws IOException { return Long.parseLong(nextToken()); } public static String nextLine() throws IOException { st = null; if(!br.ready()) { exitImmediately(); } return br.readLine(); } public static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
2d088b11690fd60d6bf62b788efc275c
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class InnaAndHugeCandyMatrix { public static void main(String[] args) { InputReader r = new InputReader(System.in); int n = r.nextInt(); int m = r.nextInt(); int x = r.nextInt() % 4; int y = r.nextInt() % 2; int z = r.nextInt() % 4; int p = r.nextInt(); int[][] arr = new int[p][2]; for (int i = 0; i < arr.length; i++) { arr[i][0] = r.nextInt(); arr[i][1] = r.nextInt(); } for (int i = 0; i < x; i++) { for (int j = 0; j < p; j++) { int row = arr[j][0]; int col = arr[j][1]; arr[j][0] = col; arr[j][1] = (i % 2 == 0 ? n : m) - row + 1; } } if (x % 2 == 1) { int temp = m; m = n; n = temp; } for (int i = 0; i < y; i++) { for (int j = 0; j < p; j++) { int row = arr[j][0]; int col = arr[j][1]; arr[j][0] = row; arr[j][1] = m - col + 1; } } z = 4 - z; z += 4; z %= 4; for (int i = 0; i < z; i++) { for (int j = 0; j < p; j++) { int row = arr[j][0]; int col = arr[j][1]; arr[j][0] = col; arr[j][1] = (i % 2 == 0 ? n : m) - row + 1; } } PrintWriter out = new PrintWriter(System.out); for (int i = 0; i < arr.length; i++) { out.println(arr[i][0] + " " + arr[i][1]); } out.close(); } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return 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
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
d5dd77ffa7e22bfc93901e7c19a452c5
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.io.IOException; public class C { private static final int IN_BUFFER_SIZE = 1 << 16; private static final int OUT_BUFFER_SIZE = 1 << 16; private byte[] input = new byte[IN_BUFFER_SIZE]; private int ix = IN_BUFFER_SIZE; private int bytesRead = ix; private byte[] output = new byte[OUT_BUFFER_SIZE]; private int ox = 0; private void readMore() { try { bytesRead = System.in.read(input, 0, IN_BUFFER_SIZE); if (bytesRead <= 0) throw new RuntimeException(); ix = 0; } catch (IOException e) { throw new RuntimeException(); } } private void flushOut() { System.out.write(output, 0, ox); ox = 0; } private void append(char c) { if (ox == OUT_BUFFER_SIZE) flushOut(); output[ox++] = (byte) c; } private int nextInt() { skipSpaces(); int ret = 0; if (ix == bytesRead) { readMore(); } int sign = 1; if (input[ix] == '-') { sign = -1; ix++; } while (true) { if (ix == bytesRead) { try { readMore(); } catch (RuntimeException e) { return ret; } } if (input[ix] < '0') { break; } ret *= 10; ret += input[ix++] - '0'; } return sign * ret; } int cntSize; private void skipSpaces() { while (true) { if (ix == bytesRead) { readMore(); } if (input[ix] > ' ') break; ix++; } } private char[] nn = new char[32]; private void printInt(int n) { if (n == 0) { append('0'); } else { if (n < 0) { append('-'); n = -n; } int kk = 0; while (n > 0) { nn[kk++] = (char) (n % 10 + '0'); n /= 10; } for (int i = kk - 1; i >= 0; i--) { append(nn[i]); } } } public static void main(String[] args) throws IOException { new C().work(); } private void work() { int m = nextInt(); int n = nextInt(); int a = nextInt() & 3; int b = nextInt() & 1; int c = nextInt() & 3; int p = nextInt(); int[] x = new int[p]; int[] y = new int[p]; for (int i = 0; i < p; i++) { x[i] = nextInt(); y[i] = nextInt(); } for (int i = 0; i < a; i++) { for (int j = 0; j < p; j++) { int nx = y[j]; int ny = m + 1 - x[j]; x[j] = nx; y[j] = ny; } int t = m; m = n; n = t; } for (int i = 0; i < b; i++) { for (int j = 0; j < p; j++) { y[j] = n + 1 - y[j]; } } for (int i = 0; i < c; i++) { for (int j = 0; j < p; j++) { int ny = x[j]; int nx = n + 1 - y[j]; x[j] = nx; y[j] = ny; } int t = m; m = n; n = t; } for (int i = 0; i < p; i++) { printInt(x[i]); append(' '); printInt(y[i]); append('\n'); } if (ox > 0) flushOut(); System.out.close(); } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
c263dd58b490829c94cd0e6c433642dd
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.*; import java.io.*; public class Main { BufferedReader in; StringTokenizer str = null; PrintWriter out; private String next() throws Exception{ if (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } private long nextLong() throws Exception{ return Long.parseLong(next()); } private double nextDouble() throws Exception{ return Double.parseDouble(next()); } public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int n = nextInt(), m = nextInt(); int x = nextInt() % 4; int y = nextInt() % 2; int z = nextInt() % 4; int p = nextInt(); int a[] = new int[p]; int b[] = new int[p]; for(int i=0;i<p;i++){ a[i] = nextInt() - 1; b[i] = nextInt() - 1; } for(int i=0;i<x;i++){ for(int j=0;j<p;j++){ int ta = a[j]; int tb = b[j]; a[j] = tb; b[j] = n - ta - 1; } int t = n; n = m; m = t; } for(int i=0;i<y;i++){ for(int j=0;j<p;j++){ b[j] = m - b[j] - 1; } } for(int i=0;i<z;i++){ for(int j=0;j<p;j++){ int ta = a[j]; int tb = b[j]; b[j] = ta; a[j] = m - tb - 1; } int t = n; n = m; m = t; } for(int i=0;i<p;i++){ out.println(++a[i] + " " + ++b[i]); } out.close(); } public static void main(String[] args) throws Exception{ new Main().run(); } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
6cd0c6813935d68f4c608703a2269a85
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { BufferedReader reader; StringTokenizer tokenizer; PrintWriter out; public void rotate90 (int N, int M, int[] in) { int oldX = in[0]; int oldY = in[1]; in[0] = N + 1 - oldY; in[1] = oldX; } public void rotate180 (int N, int M, int[] in) { int oldX = in[0]; int oldY = in[1]; in[0] = M + 1 - oldX; in[1] = N + 1 - oldY; } public void rotate270 (int N, int M, int[] in) { int oldX = in[0]; int oldY = in[1]; in[0] = oldY; in[1] = M + 1 - oldX; } public void solve() throws IOException { int ON = nextInt(); int OM = nextInt(); int X = nextInt(); int Y = nextInt(); int Z = nextInt(); int P = nextInt(); X %= 4; Y %= 2; Z %= 4; for (int i = 0; i < P; i++) { int N = ON; int M = OM; int row = nextInt(); int col = nextInt(); int[] in = new int[] {col, row}; if (X == 1) { rotate90(N, M, in); } else if (X == 2) { rotate180(N, M, in); } else if (X == 3) { rotate270(N, M, in); } if ( X % 2 == 1) { int tmp = N; N = M; M = tmp; } if (Y == 1) { in[0] = M + 1 - in[0]; } if (Z == 3) { rotate90(N, M, in); } else if (Z == 2) { rotate180(N, M, in); } else if (Z == 1) { rotate270(N, M, in); } out.println(in[1] + " " + in[0]); } } /** * @param args */ public static void main(String[] args) { new C().run(); } public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; out = new PrintWriter(System.out); solve(); reader.close(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
3dfc176387172131793e9864502f4a3a
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.*; import java.io.*; public class C234 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); long n = sc.nextLong(); long m = sc.nextLong(); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); int p = sc.nextInt(); long[] opx = new long[p]; long[] opy = new long[p]; for(int i=0;i<p;i++){ opx[i] = sc.nextLong(); opy[i] = sc.nextLong(); } sc.close(); x = x%4; y = y%2; z = z%4; for(int i=0;i<p;i++){ switch(x){ case 0: break; case 1: { long tx = opy[i]; long ty = n+1-opx[i]; opx[i] = tx; opy[i] = ty; break; } case 2: { long tx = n+1-opx[i]; long ty = m+1-opy[i]; opx[i] = tx; opy[i] = ty; break; } case 3: { long tx = m+1-opy[i]; long ty = opx[i]; opx[i] = tx; opy[i] = ty; break; } } switch(y){ case 0: break; case 1: { if(x==0 || x==2) opy[i] = m+1-opy[i]; else opy[i] = n+1-opy[i]; break; } } if(x==0 || x==2){ switch(z){ case 0: break; case 1: { long tx = m+1-opy[i]; long ty = opx[i]; opx[i] = tx; opy[i] = ty; break; } case 2: { long tx = n+1-opx[i]; long ty = m+1-opy[i]; opx[i] = tx; opy[i] = ty; break; } case 3: { long tx = opy[i]; long ty = n+1-opx[i]; opx[i] = tx; opy[i] = ty; break; } } } else { switch(z){ case 0: break; case 1: { long tx = n+1-opy[i]; long ty = opx[i]; opx[i] = tx; opy[i] = ty; break; } case 2: { long tx = m+1-opx[i]; long ty = n+1-opy[i]; opx[i] = tx; opy[i] = ty; break; } case 3: { long tx = opy[i]; long ty = m+1-opx[i]; opx[i] = tx; opy[i] = ty; break; } } } out.println(opx[i] + " " + opy[i]); } out.flush(); } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
4b1ca26496cb24b2aff114d89c2388a3
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C { void solve() throws Exception { int n = nextInt(); int m = nextInt(); int x = nextInt(); int y = nextInt(); int z = nextInt(); int p = nextInt(); StringBuilder sb = new StringBuilder(); while(p-->0) { int i = nextInt()-1; int j = nextInt()-1; int nn = n, mm = m; // rotate clockwise for (int r=0; r<x%4; r++) { // change i,j int tmp = i; i = j; j = nn - 1 - tmp; // swap m,n tmp = nn; nn = mm; mm = tmp; } // flip if (y%2==1) { j = mm-1-j; } // rotate counter clockwise for (int r=0; r<z%4; r++) { int tmp = j; j = i; i = mm - 1 - tmp; tmp = nn; nn = mm; mm = tmp; } sb.append((i+1) + " " + (j+1) + "\n"); } System.out.println(sb); } BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String nextLine() throws Exception { String line = stdin.readLine(); st = new StringTokenizer(""); return line; } int[] nextInts(int n) throws Exception { int[] res = new int[n]; for (int i=0; i<n; i++) res[i] = nextInt(); return res; } long[] nextLongs(int n) throws Exception { long[] res = new long[n]; for (int i=0; i<n; i++) res[i] = nextLong(); return res; } double[] nextDoubles(int n) throws Exception { double[] res = new double[n]; for (int i=0; i<n; i++) res[i] = nextDouble(); return res; } String next() throws Exception { while (!st.hasMoreTokens()) st = new StringTokenizer(stdin.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } public static void main(String[] args) throws Exception { new C().solve(); } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
37c275a2d57db18726a31863c9869188
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.*; import java.io.*; public class c { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(), m = input.nextInt(), x = input.nextInt() & 3, y = input.nextInt() & 3, z = input.nextInt() & 3, p = input.nextInt(); int[] rs = new int[p], cs = new int[p]; for(int i = 0; i<p; i++) { rs[i] = input.nextInt()-1; cs[i] = input.nextInt()-1; } for(int i =0; i<x; i++) { int tmp = n; n = m; m = tmp; for(int j = 0; j<p; j++) { int c = m - 1 - rs[j], r = cs[j]; cs[j] = c; rs[j] = r; } } for(int i = 0; i<y; i++) for(int j = 0; j<p; j++) cs[j] = m - 1 - cs[j]; for(int i = 0; i<z; i++) { int tmp = n; n = m; m = tmp; for(int j = 0; j<p; j++) { int c = rs[j], r = n - 1 - cs[j]; cs[j] = c; rs[j] = r; } } for(int i = 0; i<p;i++) out.println((rs[i]+1)+" "+(cs[i]+1)); out.close(); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
5cea4661dd1915d5e2f8a5670c719a7d
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.*; public class CodeForces5thmarch003 { public static void main(String[] args) { int i,j,n,m,x,y,z,p,a,b,temp,temp1,t; boolean chk = true; Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); p = sc.nextInt(); x%=4; y%=2; z%=4; for(t=0;t<p;t++) { chk = true; a = sc.nextInt(); b = sc.nextInt(); for(i=0;i<x;i++) { if(i%2==0) temp = n; else temp = m; temp1 = a; a=b; b=temp-temp1+1; chk = !chk; } for(i=0;i<y;i++) { if(chk == true) b = m-b+1; else b = n-b+1; } for(i=0;i<z;i++) { if(chk == true) temp = m; else temp = n; temp1 = b; b=a; a=temp-temp1+1; chk = !chk; } System.out.println(a+ " " + b); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
e95c6f219c69d5ff1fb9e9ace75a1981
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws IOException { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); StringTokenizer str = new StringTokenizer(buf.readLine()); n = Integer.parseInt(str.nextToken()) + 1; m = Integer.parseInt(str.nextToken()) + 1; int nn = n; int mm = m; int x = Integer.parseInt(str.nextToken()); int y = Integer.parseInt(str.nextToken()); int z = Integer.parseInt(str.nextToken()); int p = Integer.parseInt(str.nextToken()); x %= 4; y %= 2; z %= 4; StringBuilder ans = new StringBuilder(); for (int i = 0; i < p; i++) { n = nn; m = mm; str = new StringTokenizer(buf.readLine()); int a = n - Integer.parseInt(str.nextToken()); int b = Integer.parseInt(str.nextToken()); int[] newP = clockwise(b, a, x); if (x % 2 == 1) { int tmp = n; n = m; m = tmp; } newP = horizontal(newP[0], newP[1], y); newP = anti(newP[0], newP[1], z); if (z % 2 == 1) { int tmp = n; n = m; m = tmp; } // System.out.println(Arrays.toString(newP)); ans.append((n - (newP[1])) + " " + (newP[0]) + "\n"); } System.out.print(ans); } static int n, m; static int[] horizontal(int x, int y, int f) { if (f == 0) return new int[] { x, y }; return new int[] { m - x, y }; } static int[] anti(int x, int y, int f) { if (f == 0) { return new int[] { x, y }; } if (f == 1) { return new int[] { -y + n, x }; } if (f == 2) { return new int[] { -x + m, -y + n }; } else { return new int[] { y, -x + m }; } } static int[] clockwise(int x, int y, int f) { if (f == 0) { return new int[] { x, y }; } if (f == 1) { return new int[] { y, -x + m }; } if (f == 2) { return new int[] { -x + m, -y + n }; } else { return new int[] { -y + n, x }; } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
91a2f8b2f4d158c1be005348b67a678a
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.Scanner; public class C2 { static int H,W; static int h, w, x, y, z; public static void main(String[] args) { Scanner sc = new Scanner(System.in); H = h = sc.nextInt(); W = w = sc.nextInt(); x = sc.nextInt()%4; y = sc.nextInt()%2; z = sc.nextInt()%4; int p = sc.nextInt(); for(int i=0;i<p;i++){ int[]a = {sc.nextInt(),sc.nextInt()}; h = H; w = W; for(int j = 0;j<x;j++){ rot(a); } if(y == 1){ a[1] = w + 1 - a[1]; } for(int j = 0;j<4-z;j++){ rot(a); } System.out.println(a[0]+" "+a[1]); } } static void rot(int[] a){ int x = a[1]; int y = h + 1 - a[0]; a[0]=x; a[1]=y; int tmp = h; h = w; w = tmp; } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
e95cf410fd35daa853b812b4818aeb6f
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.Scanner; public class C { static int N,M; static int n,m,x,y,z; public static void main(String[] args) { Scanner sc=new Scanner(System.in); N = n = sc.nextInt(); M = m = sc.nextInt(); x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); int p = sc.nextInt(); for(int i=0;i<p;i++){ int[] a = {sc.nextInt(),sc.nextInt()}; n = N; m = M; clockwise(a,x); // System.out.println(a[0]+ " " + a[1]); horizontal(a,y); // System.out.println(a[0]+ " " + a[1]); cclockwise(a,z); System.out.println(a[0]+ " " + a[1]); } } static int[] clockwise(int[] a,int t){ for(int i=0;i<t % 4;i++){ rotate(a,m,n); int tmp=m; m=n; n=tmp; } return a; } static int[] horizontal(int[] a,int t){ t = t % 2; if(t==1){ int x = a[0]; int y = m - a[1] + 1; a[0]=x; a[1]=y; } return a; } static int[] cclockwise(int[] a,int t){ t = t%4; clockwise(a,4-t); return a; } static void rotate(int[] a,int n,int m){ int x = a[1]; int y = m - a[0] + 1; a[0]=x; a[1]=y; } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
665c52fe720eb4004d7d2e5090e48c49
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; /** * * * @author pttrung */ public class C { // public static long x, y, gcd; public static int Mod = 1000000007; public static PrintWriter out; public static int k; public static int[] x = {0, 0, 1, -1}; public static int[] y = {1, -1, 0, 0}; public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(); out = new PrintWriter(System.out); // System.out.println(Integer.MAX_VALUE); // PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt"))); int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt() % 4; int y = in.nextInt() % 2; int z = in.nextInt() % 4; int p = in.nextInt(); Point[] data = new Point[p]; for (int i = 0; i < p; i++) { data[i] = new Point(in.nextInt(), in.nextInt()); } int orginN = n; int orginM = m; for (int i = 0; i < p; i++) { n = orginN; m = orginM; for (int j = 0; j < x; j++) { data[i] = clock(data[i], n, m); // System.out.println(data[i]); int temp = n; n = m; m = temp; } if (y > 0) { data[i] = new Point(data[i].x, m - data[i].y + 1); } for (int j = 0; j < z; j++) { data[i] = anti_clock(data[i], n, m); int temp = n; n = m; m = temp; } out.println(data[i].x + " " + data[i].y); } out.close(); } public static Point clock(Point p, int n, int m) { Point result = new Point(p.y, n - p.x + 1); return result; } public static Point anti_clock(Point p, int n, int m) { Point result = new Point(m - p.y + 1, p.x); return result; } public static int cross(Point a, Point b) { int val = a.x * b.y - a.y * b.x; return val; } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * val * a; } } public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } public static class Point implements Comparable<Point> { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Point o) { if (x != o.x) { return x - o.x; } else { return y - o.y; } } } // public static void extendEuclid(long a, long b) { // if (b == 0) { // x = 1; // y = 0; // gcd = a; // return; // } // extendEuclid(b, a % b); // long x1 = y; // long y1 = x - (a / b) * y; // x = x1; // y = y1; // // } public static class FT { int[] data; FT(int n) { data = new int[n]; } public void update(int index, int value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public int get(int index) { int result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
2da2a15f960ad88e9d6150e7a23aa912
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.*; import java.io.*; import java.lang.StringBuilder; public class C_InnaHugeMatrix { static int[] d = new int[2]; static void swap () { d[0] = d[0] ^ d[1]; d[1] = d[0] ^ d[1]; d[0] = d[0] ^ d[1]; } public static void main (String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] inp = br.readLine().split(" "); StringBuilder sb = new StringBuilder(); int n = Integer.parseInt(inp[0]); int m = Integer.parseInt(inp[1]); int x = Integer.parseInt(inp[2]) % 4; int y = Integer.parseInt(inp[3]) % 2; int z = Integer.parseInt(inp[4]) % 4; int p = Integer.parseInt(inp[5]); while (p-- > 0) { inp = br.readLine().split(" "); int xk = Integer.parseInt(inp[0]) - 1; int yk = Integer.parseInt(inp[1]) - 1; d[0] = n; d[1] = m; for (int i=0; i<x; i++) { int t = xk; xk = yk; yk = d[0] - t - 1; swap(); } if (y == 1) yk = d[1] - yk - 1; for (int i=0; i<z; i++) { int t = yk; yk = xk; xk = d[1] - t - 1; swap(); } sb.append(Integer.toString(xk+1) + " " + Integer.toString(yk+1) + "\n"); } System.out.print(sb.toString()); } catch(IOException io){ io.printStackTrace(); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
c9e139a69ac910b77f9590d531577662
train_003.jsonl
1394033400
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has p candies: the k-th candy is at cell (xk, yk).The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
256 megabytes
import java.util.Scanner; import java.util.Arrays; import java.lang.String; public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner sc = new Scanner(System.in); int gln = sc.nextInt(); /* кол0-во ступ нум с 1 */ int glm = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); /* кол0-во ступ нум с 1 */ int z = sc.nextInt(); int p = sc.nextInt(); x = x % 4; y = y % 2; z = z % 4; for(int ggg = 1; ggg <= p; ggg++) { int n = gln; int m = glm; int c1 = sc.nextInt(); int c2 = sc.nextInt(); for(int i = 0; i < x; i++) { int tmp = c1; c1 = c2; c2 = n + 1 - tmp; tmp = n; n = m; m = tmp; } for(int i = 0; i < y; i++) { c2 = m+1 - c2; } for(int i = 0; i < z; i++) { int tmp = c1; c1 = m + 1 - c2; c2 = tmp; tmp = n; n = m; m = tmp; } System.out.println(c1 + " " + c2); } } }
Java
["3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3"]
2 seconds
["1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1"]
NoteJust for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:QWER REWQ ASDF -&gt; FDSAZXCV VCXZ
Java 6
standard input
[ "implementation", "math" ]
14a56443e48c52c118788bd5c0031b0c
The first line of the input contains fix integers n, m, x, y, z, p (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105). Each of the following p lines contains two integers xk, yk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.
1,500
For each of the p candies, print on a single line its space-separated new coordinates.
standard output
PASSED
0c70e0fca2de8e9ceeed63cb1cc9afea
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; public class RainbowDashFluttershyandChessColoring { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int t1=1;t1<=t;t1++) { long n=sc.nextLong(); long res=0; if(n==1) System.out.println(1); else { if(n>=4) res=(n/2)-1; res=res+2; System.out.println(res); } } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
5868a9a0c898c026f2c9fa567c6c35dc
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; import java.io.*; public class Main { static final long M = (long) 1e9 + 7; static FastReader in; static int ans = 0; public static void main(String[] args) { in = new FastReader(); int t = ni(); while (t-- > 0) { long n = nl(); if (n <= 2) { if (n <= 1) pn(1); else pn(2); } else { pn((n / 2) + 1); } } } static void p(Object o) { System.out.print(o); } static void pn(Object o) { System.out.println(o); } static String ns() { return in.next(); } static String nln() { return in.nextLine(); } static int ni() { return Integer.parseInt(in.next()); } static int[] nia(int N) { int[] a = new int[N]; for (int i = 0; i < N; i++) a[i] = ni(); return a; } static long[] nla(int N) { long[] a = new long[N]; for (int i = 0; i < N; i++) a[i] = nl(); return a; } static long nl() { return Long.parseLong(in.next()); } static double nd() { return Double.parseDouble(in.next()); } 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(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
6d14a52573276931614754bb9de1cee5
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void main(String args[]) throws IOException { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); Scanner s=new Scanner(System.in); //Reader s=new Reader(); int T=s.nextInt(); while(T-->0) { int N=s.nextInt(); System.out.println((N/2)+1); } out.close(); } } 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(); } } class B { int F; int S; public B(int F,int S) { this.F=F; this.S=S; } public String toString() { return this.F+" "+this.S; } } class Sort implements Comparator<B> { public int compare(B a,B b) { return b.S-a.S; } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
6bb922611f115ce9ebacedba84d29b0f
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Hp */ import java.io.*; import java.util.*; public class ProblemA { public static void main(String args[])throws IOException{ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t > 0){ long n = sc.nextLong(); long result = (n/2) + 1; System.out.println(result); t--; } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
d1772b7e8464f99b8a5aa56bfc06fb62
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
/* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ */ import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception{ Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int ie=0;ie<t;ie++) { int n=s.nextInt(); System.out.println((n/2)+1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
12eaccd7095887062ba554b02a8f3924
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.Scanner; /** * @author hypnos * @date 2020/11/13 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int a = sc.nextInt(); System.out.println(a/2+1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
1f3bba91953237250f4a476915e2b2e7
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
/** * */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * @author b * */ public class A { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(); int tests = scanner.nextInt(); for (int i=0; i < tests; i++) { solve(scanner); } } private static void solve(Scanner scanner) { int n = scanner.nextInt(); int sol = (int) Math.ceil((n + 1.0) / 2); System.out.println( sol); } // Props to SecondThread static class Scanner { BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer=new StringTokenizer(""); String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer=new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArr(int numberOfElements) { int[] values=new int[numberOfElements]; for (int i=0; i<numberOfElements; i++) values[i]=nextInt(); return values; } long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
a1e3f99130859eb800a7ff8ee8b7389a
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.*; public class hold{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cases = Integer.parseInt(br.readLine()); for(int i=0;i<cases;i++){ int num = Integer.parseInt(br.readLine()); if(num%2 == 1){ System.out.println((num+1)/2); }else{ num = num/2; num++; System.out.println(num); } } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
b8aee4de31897ae0ed848e6bca49db00
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class CodeForce_662 { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); //long startTime = System.nanoTime(); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int len = sc.nextInt(); int result = len/2 + 1; pw.println(result); } //long elapsedTime = System.nanoTime() - startTime; //pw.println(elapsedTime/1000000); pw.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
29f313eddbba9084ccbe642eac92981d
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; public class cfchess{ public static void main(String[]args) { Scanner scn=new Scanner(System.in); int tc=scn.nextInt(); for(int i9=0;i9<tc;i9++) { int n=scn.nextInt(); if(n==1) {System.out.println(1); continue;} System.out.println((n/2)+1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
5889d8532c0ffe73cd35bc6005fe882b
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author New User */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); ARainbowDashFluttershyAndChessColoring solver = new ARainbowDashFluttershyAndChessColoring(); solver.solve(1, in, out); out.close(); } static class ARainbowDashFluttershyAndChessColoring { public void solve(int testNumber, InputReader c, OutputWriter w) { int tc = c.readInt(); while (tc-- > 0) { int n = c.readInt(); w.printLine(n / 2 + 1); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
c9e213e8a620d0a71e986c42ed74b4b6
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.Scanner; public class A{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); int T=sc.nextInt(); for(int t=0;t<T;t++){ System.out.println((sc.nextInt()/2)+1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
7f1e1dc6dd9c3b003230f54a1754c7a9
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public final class codeForces { static long gcd(long a,long b) { if(b==0)return a; else return gcd(b,a%b); } public static void main(String args[])throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); Scanner sc=new Scanner(System.in); //Integer.parseInt(in.readLine()); //Integer.parseInt(st.nextToken()); //Long.parseLong(in.readLine()); //Long.parseLong(st.nextToken()); int t=sc.nextInt(); while(t-->0) { long N=sc.nextLong(); if(N==1 ||N==2)System.out.println(N); else { N=N-1; long temp=(N-2)/2; System.out.println(N-temp); } } } } /* StringTokenizer st; st=new StringTokenizer(in.readLine()); int n=Integer.parseInt(st.nextToken()); int q=Integer.parseInt(st.nextToken()); int c=Integer.parseInt(st.nextToken()); int A[][]=new int[n][3]; for(int i=0; i<n; i++) { st=new StringTokenizer(in.readLine()); A[i][0]=Integer.parseInt(st.nextToken()); A[i][1]=Integer.parseInt(st.nextToken()); A[i][2]=Integer.parseInt(st.nextToken()); } while(q-->0) { st=new StringTokenizer(in.readLine()); long t=Long.parseLong(st.nextToken()); int x1=Integer.parseInt(st.nextToken()); int y1=Integer.parseInt(st.nextToken()); int x2=Integer.parseInt(st.nextToken()); int y2=Integer.parseInt(st.nextToken()); long b=0; for(int i=0; i<n; i++) { int x=A[i][0]; int y=A[i][1]; if(x>=x1 && x<=x2 && y>=y1 && y<=y2) { b+=(A[i][2]+t)%(c+1); } } System.out.println(b); } */
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
8e8263e89462ccfb6e079be5605601fe
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.*; import java.util.Random; import java.util.StringTokenizer; public class A { //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) { out.println(scan.nextInt() / 2 + 1); } } 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
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
022dd84fe632ce0c1865a47eff697d72
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class RainbowDashFluttershyAndChessColoring { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); System.out.println((n / 2) + 1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
a268d0f4b48dbab1290c85e11a84170e
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.Scanner; /** * q1 */ public class q1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0) { int n=sc.nextInt(); if (n==1) { System.out.println(1); } else if (n==2) { System.out.println(2); }else{ System.out.println((n+2)/2); } } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
3e01be5696759f792200f43e10b1aebf
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
//package contest662; import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.Stack; import java.util.HashMap; public class ProblemA { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- >0) { int n = sc.nextInt(); System.out.println(n/2+1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
60c71c2aa4f42f7da0d304c9b6916109
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class codeforces { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw= new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t-- != 0) { int n = Integer.parseInt(br.readLine()); if(n==1) pw.println("1"); else if(n==2) pw.println("2"); else if(n==3) pw.println("2"); else { pw.println(n/2+1); } } pw.flush(); } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
c00b87e5dd9e3684ea0e2616fc6995b1
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; public class MyClass { public static int f1(int n){ return n/2+1; } public static void f(int[] arr,int n){ for(int i = 0 ; i < n ; i++){ if(arr[i] <= 2) System.out.println(arr[i]); else System.out.println(f1(arr[i])); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i < n ; i++) arr[i] = sc.nextInt(); f(arr,n); } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
0d5e97abada00956fd7f084e866b695d
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /* * Copyright (c) --> Arpit * Date Created : 7/8/2020 * Have A Good Day ! */ /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); RainbowDashFluttershyAndChessColoring solver = new RainbowDashFluttershyAndChessColoring(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class RainbowDashFluttershyAndChessColoring { public void solve(int testNumber, FastReader r, OutputWriter out) { int n = r.nextInt(); long ans = 0; if (n == 1) ans = 1; else if (n == 2 || n == 3) ans = 2; else { ans = (int) Math.ceil((double) n / 2); if ((n & 1) == 0) ans++; } out.println(ans); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
014bc31f577125ac23671efd8b72e0bc
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; import java.io.*; public class A_1393 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); pw.println((n + 2) / 2); } pw.flush(); } public 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 int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
ea072ae4e79b62e233ac3688597402b2
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.lang.*; import java.io.*; import java.math.*; import java.util.*; public final class CF { public static void main(String[]args)throws IOException { FastReader ob=new FastReader(); int t=ob.nextInt(); while(t-->0) { int n=ob.nextInt(); if(n==1) System.out.println("1"); else System.out.println((n/2)+1); } } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public String nextLine() { String s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
8cf2d0ec14caebc20d072a98fbf2b4a4
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.*; import java.util.*; public class C{ public static void main(String args[]) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-->0){ int N = sc.nextInt(); int round = N/2; int ans = round; //if(round%2==1) ans++; ans++; System.out.println(ans); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
38b48467d448205c41ba223f537c96b1
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.*; import java.util.*; public class codeForces { public static void main(String[] args) throws IOException{ FastReader scan = new FastReader(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int T = scan.nextInt(); for(int l = 0;l<T;l++){ int n = scan.nextInt(); boolean odd = (n & 1) == 1; if(odd){ n--; n/=2; out.write((1 + n) + ""); }else{ n/=2; out.write((1 + n) + ""); } out.newLine(); } out.close(); } static class FastReader{BufferedReader br;StringTokenizer st; public FastReader(InputStream in){br = new BufferedReader(new InputStreamReader(in));} String next(){while(st==null||!st.hasMoreElements()){try{st=new StringTokenizer(br.readLine()); }catch(IOException e){e.printStackTrace();}}return st.nextToken();} int nextInt(){return Integer.parseInt(next());}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
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
6c86839aeb02bc356c4c1262b508a08c
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.*; import java.util.*; public class Main { static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws Exception { int t = Integer.parseInt(br.readLine()); for (int i=0; i<t; i++) { int n = Integer.parseInt(br.readLine()); if (n==1) sb.append("1"); else if (n==2) sb.append("2"); else sb.append(n/2+1); sb.append("\n"); } bw.write(sb.toString()); bw.flush(); } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
376a8317e07bd10ce4c1cc894f50b9a2
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public final class CodeForces658A { public static void pr(String st){ System.out.println(st); } 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(); int a = (int)Math.ceil(n/2.0); int b = n-a; if(a%2!=0){ a = a-1; } else{ b = b-1; } System.out.println((int)(Math.ceil(b/2.0)+1+Math.ceil(a/2.0))); } out.flush(); 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) { } } 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) { } return str; } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
c421315f45963dd9c598d5f005c99a9f
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; public class Array { public static void main(String[] args) { Scanner in = new Scanner(System.in); int X = in.nextInt(); int arr[] = new int[X]; for(int i = 0 ; i<X ; i++) { arr[i] = in.nextInt(); System.out.println((arr[i]/2)+1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
c4bc71db0e9a96824795d6956b9cb2b0
train_003.jsonl
1596810900
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int i = 0; i < t; i++){ int n = input.nextInt(); System.out.println(n/2 + 1); } } }
Java
["2\n3\n4"]
1 second
["2\n3"]
NoteFor $$$3\times3$$$ grid ponies can make two following moves:
Java 8
standard input
[ "greedy", "math" ]
eb39ac8d703516c7f81f95a989791b21
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$): the size of the side of the grid of the game.
800
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
standard output
PASSED
ec062cd180e1ff715a47443e08dfe948
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int ai[] = new int[n]; for (int i = 0; i < n; i++) { ai[i] = sc.nextInt(); } int count = 0; int min = ai[0]; int max = ai[0]; int min_index = 0, max_index = 0; for (int i = 0; i < n; i++) { if (min >= ai[i] ) { min = ai[i]; min_index = i; } if (max < ai[i]) { max = ai[i]; max_index = i; } } if ( min_index < max_index) { System.out.println((max_index - 1) + ((n - 1) - min_index ) ); } else if ( min_index > max_index) { System.out.println(max_index + ((n - 1) - min_index) ); } } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
edfd8ea2b6da0f6d0eb357c67b630218
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Scanner; public class Road14 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int max = 1; int min = 100; int indexmax = 0; int indexmin = 0; int act; for (int i = 0; i <n ; i++) { act = in.nextInt(); if (act>max) { max = act; indexmax = i; } if (act<=min) { min = act; indexmin = i; } } int answer =(indexmax<indexmin)? indexmax+n-indexmin-1:indexmax+n-indexmin-2; System.out.println(answer); } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
a596c56e82a19d3b72630454ce06f3ef
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args){ // input Scanner scan = new Scanner(System.in); int numSoldiers = scan.nextInt(); int heights[] = new int[numSoldiers]; for(int i = 0; i < heights.length; i++) heights[i] = scan.nextInt(); int tallestIndex = 0; int shortestIndex = 0; int tallest = Integer.MIN_VALUE; int shortest = Integer.MAX_VALUE; // get index for tallest & shortest soldier for(int i = 0; i < heights.length; i++){ if(heights[i] > tallest){ tallestIndex = i; tallest = heights[i]; } if(heights[i] <= shortest){ shortestIndex = i; shortest = heights[i]; } } int seconds = tallestIndex + (numSoldiers - 1) - shortestIndex; if(tallestIndex > shortestIndex){ seconds--; } System.out.println(seconds); } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
1df6bc93604b80441f31dcf27d6d8e8b
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class codeForces { public static void main (String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] a = new int[n]; int [] c = new int[n]; for ( int i = 0 ; i < n ; i++) { c[i] = sc.nextInt(); a[i] = c[i]; } int [] b = c ; Arrays.sort(b); int max = b[n-1]; int min = b[0]; int maxindex = n-1 ; int minindex = 0; for ( int i = 0 ; i < n ; i++) { if ( a[i] == max ) { maxindex = i ;break;} } for ( int i = n-1 ; i >= 0 ; i--) { if ( a[i] == min) { minindex = n-1-i ;break;} } if ( (minindex >= n/2 || maxindex >= n/2) && (minindex + maxindex) >= n-1 ) { System.out.println(maxindex + minindex - 1); } else { System.out.println(maxindex + minindex ); } } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
249c287634f7e9bf097e97fcfb0a82e0
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Scanner; public class Practice { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n =sc.nextInt(); int[] arr = new int[n]; int min=0,max=0; for(int i =0;i<n;i++) { arr[i]=sc.nextInt(); if(arr[i]>arr[max]) { max=i; } if(arr[i]<=arr[min]) { min =i; } } if(max>min) { System.out.println(max+(n-min-2)); } else if(min==max) { System.out.println(0+""); } else { System.out.println(max+(n-min-1)); } } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
296e900259790368a352717544b78aa1
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int numSoldiers = in.nextInt(); int[] list = new int[numSoldiers]; int high = 0; int low = 10000; int lowIndex = -1; int highIndex = 0; int change; for (int i = 0; i < numSoldiers; i++) { list[i] = in.nextInt(); } for (int i = 0; i < list.length; i++) { if (list[i] > high) { high = list[i]; highIndex = i; } if (list[i] <= low && lowIndex < i) { low = list[i]; lowIndex = i; } } if (list[0] == list[highIndex] && list[numSoldiers - 1] == list[lowIndex]) { System.out.println(0); } else { int moveLow = (list.length - 1) - lowIndex; int moveHigh; if (lowIndex > highIndex) { moveHigh = -highIndex; } else { moveHigh = 1 - highIndex; } moveHigh *= -1; change = moveLow + moveHigh; System.out.println(change); } } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
a71dae2c23dff9c4da8140fcc480c4fa
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; /** * * @author ONE BY ONE */ public class ArrivalOfTheGeneral { public static void insertionSort(int array[]) { int n = array.length; for (int j = 1; j < n; j++) { int key = array[j]; int i = j-1; while ( (i > -1) && ( array [i] < key ) ) { array [i+1] = array [i]; i--; } array[i+1] = key; } } public static void main(String[] args) { Scanner s= new Scanner(System.in); int n= s.nextInt(); int a[]=new int[n];int max=0;int min=100000;int u=0;int y=0; int b[]=new int[n]; int count =0; for(int i=0;i<n;i++){ a[i]=s.nextInt(); } if(a.length==2 ){ if(a[0]<a[1]){ System.out.println(1);return;} else if(a[0]>a[1]){ System.out.println(0) ;return;} else if(a[0]==a[1]){ System.out.println(0);return;}} int mm=0;int nn=1000;int bb=0;int aa=0;int cc=0; for(int i=0;i<a.length;i++){ if(a[i]>=max){ max=a[i];y=i; } if(a[i]<=min){ min=a[i];u=i; }} if(a[u]!=a[n-1]){ for(int i=u;i<a.length;u++){ int temp=a[u]; a[u]=a[u+1]; a[u+1]=temp; count++; if(u==a.length-2) break; }} int q=0; for(int i=0;i<a.length;i++){ if(a[i]>cc){ cc=a[i];q=i; } } if(a[q]!=a[0]){ for(int i=q;i>0;q--){ int temp=a[q]; a[q]=a[q-1]; a[q-1]=temp; count++; if(q==1) break; }} System.out.println(count); } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
cd940053ef3e5c94757f9ac2ffbe8ac0
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; /** * * @author ONE BY ONE */ public class ArrivalOfTheGeneral { public static void insertionSort(int array[]) { int n = array.length; for (int j = 1; j < n; j++) { int key = array[j]; int i = j-1; while ( (i > -1) && ( array [i] < key ) ) { array [i+1] = array [i]; i--; } array[i+1] = key; } } public static void main(String[] args) { Scanner s= new Scanner(System.in); int n= s.nextInt(); int a[]=new int[n];int max=0;int min=100000;int u=0;int y=0; int b[]=new int[n]; int count =0; for(int i=0;i<n;i++){ a[i]=s.nextInt(); } if(a.length==2 ){ if(a[0]<a[1]){ System.out.println(1);return;} else if(a[0]>a[1]){ System.out.println(0) ;return;} else if(a[0]==a[1]){ System.out.println(0);return;}} int mm=0;int nn=1000;int bb=0;int aa=0;int cc=0; for(int i=0;i<a.length;i++){ if(a[i]>mm){ mm=a[i];bb=i;} if(a[i]<=nn){ nn=a[i];aa=i; } } if(mm==a[0]&&nn==a[n-1]){ System.out.println(0);return ; } for(int i=0;i<a.length;i++){ if(a[i]>=max){ max=a[i];y=i; } if(a[i]<=min){ min=a[i];u=i; }} if(a[u]!=a[n-1]){ for(int i=u;i<a.length;u++){ int temp=a[u]; a[u]=a[u+1]; a[u+1]=temp; count++; if(u==a.length-2) break; }} int q=0; for(int i=0;i<a.length;i++){ if(a[i]>cc){ cc=a[i];q=i; } } if(a[q]!=a[0]){ for(int i=q;i>0;q--){ int temp=a[q]; a[q]=a[q-1]; a[q-1]=temp; count++; if(q==1) break; }} System.out.println(count); } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
4af34069768fc1abd4034f54527362f4
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Scanner; public class Class1{ public static void main(String[] args) { int n; Scanner in = new Scanner(System.in); n = in.nextInt(); int[] a = new int[100]; int i; for(i=0;i<n;i++) { a[i]=in.nextInt(); } int l=a[0],s=a[0],li=0,si=0; for(i=0;i<n;i++) { if(a[i]<s) { s=a[i]; si=i; } } for(i=0;i<n;i++) { if(a[i]>l) { l=a[i]; li=i; } } int count=0; for(i=0;i<n;i++) { if(a[0]==l&&a[n-1]==s&&n>2) { // System.out.print("0"); i=n+12; } else { if(i==li&&i!=0) { int temp; temp=a[i]; a[i]=a[i-1]; a[i-1]=temp; li=i-1; i=0; count++; } } } for(i=0;i<n;i++) { if(a[i]==s) si=i; } count=count+n-si-1; System.out.println(count); // for(i=0;i<n;i++) { // System.out.print(a[i]+" "); // } // } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
99a137d2b22106cfe11e0913f2d2e117
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Scanner; /** * Name: Saleh Alolayan * ID: 201411029 */ public class ArrivalOfTheGeneral { public static void main(String[] args) { Scanner uIN = new Scanner(System.in); int Snum = 0; int min = 100; int indexMin = 0; int max = 0; int indexMax =0; int count = 0; int[] line = new int[101]; Snum = uIN.nextInt(); for(int i=0;i<Snum;i++){ int hi = uIN.nextInt(); line[i] = hi; } // to find max for(int j=0;j<Snum;j++){ if(line[j]>max){ max=line[j]; indexMax=j; } } // to move max to first int MaxvalueAtIndex = line[indexMax]; for(int i = indexMax; i > 0; i--){ line[i] = line[i-1]; count++; } line[0] = MaxvalueAtIndex; // to find min for(int j=Snum;j>0;j--){ if((line[j]<min)&&(line[j]!=0)){ min=line[j]; indexMin=j; } } int valueAtIndex = line[indexMin]; for(int i = indexMin; i < Snum-1; i++){ line[i] = line[i+1]; count++; } line[line.length-1] = valueAtIndex; System.out.println(count); //to find seconds /* line[0]= max; line[Snum-1] = min; int time; if((indexMax==0)&&(indexMin==(Snum-1))){ System.out.println("0"); } else if(Snum==2){ System.out.println("1"); } else if(indexMin<indexMax){ indexMax++ ; time = ((indexMin + indexMax)+2); System.out.println(time); } else if(indexMin>indexMax){ time = ((indexMin + indexMax)-1); System.out.println(time); }*/ } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
40fafcbb203b22d7d294c52d7c81d865
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Scanner; public class JavaApplication3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); int max=0,min=0; int [] arr = new int[t]; for (int j = 0; j < arr.length; j++) { int tt = in.nextInt(); arr[j]=tt; if (!(arr[max]>=arr[j])) max=j; if (!(arr[min]<arr[j])) min=j; } if(arr[0]==arr[max]&&arr[arr.length-1]==arr[min]) { System.out.println(0); System.exit(0); } int c=0; int s=0; for (int i = min; i < arr.length-1; i++) if (arr[i]<arr[i+1]) { s=arr[i]; arr[i]=arr[i+1]; arr[i+1]=s; c++; } for (int i = max; i >0; i--) if(arr[i]>arr[i-1]) { s=arr[i]; arr[i]=arr[i-1]; arr[i-1]=s; c++; } System.out.println(c); } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
b7ed16976495b8e8fba320c58ead2a09
train_003.jsonl
1326899100
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
256 megabytes
import java.util.Scanner; import java.util.*; import java.io.*; import java.math.*; //********************************************************K.S.J _@@@@@@@@@@@@@@@@@@@@@@@@@****************************** public class soilder{ static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) {reader = new BufferedReader(new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer(reader.readLine() );} return tokenizer.nextToken(); } static String nextLine() throws IOException { return next();} static int nextInt() throws IOException {return Integer.parseInt( next() );} static double nextDouble() throws IOException {return Double.parseDouble( next() );} static long nextLong() throws IOException {return Long.parseLong( next() );} static BigInteger nextBigInteger() throws IOException {return new BigInteger(next()) ;} public static void main(String args[]) throws IOException{ init(System.in); int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; int no=nextInt(); int indexMax=0,indexMin=0,a; for(int i=0;i<no;i++){ a=nextInt(); if(max<a){max=a;indexMax=i;} if(min>=a){min=a;indexMin=i;} } /*System.out.println(max+" indexmax "+indexMax+" \n"+min+" indexMin "+indexMin); */ if((no-1-indexMin)+(indexMax)>=no) System.out.println((no-1-indexMin)+(indexMax)-1); else System.out.println((no-1-indexMin)+(indexMax)); } }
Java
["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"]
2 seconds
["2", "10"]
NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
Java 8
standard input
[ "implementation" ]
ef9ff63d225811868e786e800ce49c92
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
800
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.
standard output
PASSED
17edbaaff4cb97d088ba308508b70839
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import static java.lang.Integer.parseInt; public class C_Kefa_and_Park { static int n,m,ok[],cats[],ans; static ArrayList<Integer> adj[]; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk = new StringTokenizer(in.readLine()); n = parseInt(tk.nextToken()); m = parseInt(tk.nextToken()); ok = new int[n + 1]; cats = new int[n + 1]; adj = new ArrayList[n + 1]; tk = new StringTokenizer(in.readLine()); for (int i = 1; i <= n; i++) { ok[i] = parseInt(tk.nextToken()); adj[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { tk = new StringTokenizer(in.readLine()); int u,v; u = parseInt(tk.nextToken()); v = parseInt(tk.nextToken()); adj[u].add(v); adj[v].add(u); } dfs(1,0); System.out.println(ans); } static void dfs(int v,int p){ if(ok[v] == 1) if(ok[p] == 1) cats[v] = cats[p] + 1; else cats[v] = 1; if(cats[v] > m) return; if(adj[v].size() == 1 && v != 1) ans++; for(int u:adj[v]){ if(u != p) { dfs(u,v); } } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
d18c77642582f4d39f00a8371dfd23b4
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import static java.lang.Integer.parseInt; public class C_Kefa_and_Park { static int n,m,ok[],inn[],ans; static ArrayList<Integer> adj[]; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk = new StringTokenizer(in.readLine()); n = parseInt(tk.nextToken()); m = parseInt(tk.nextToken()); ok = new int[n + 1]; inn = new int[n + 1]; adj = new ArrayList[n + 1]; tk = new StringTokenizer(in.readLine()); for (int i = 1; i <= n; i++) { ok[i] = parseInt(tk.nextToken()); adj[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { tk = new StringTokenizer(in.readLine()); int u,v; u = parseInt(tk.nextToken()); v = parseInt(tk.nextToken()); adj[u].add(v); adj[v].add(u); inn[u]++; inn[v]++; } int ii = ok[1] == 1 ? 1: 0; dfs(1,0,ii); System.out.println(ans); } static void dfs(int v,int p,int k){ if(k > m) return; if(inn[v] == 1 && v != 1) ans++; for(int u:adj[v]){ if(u != p) { if(ok[u] == 1) if(ok[v] == 1) dfs(u, v, k + 1); else dfs(u, v,1); else dfs(u, v, k); } } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
0808315ea07624c1265a1709b7b59101
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import static java.lang.Integer.parseInt; public class C_Kefa_and_Park { static int n,m,ok[],inn[],cats[],ans; static ArrayList<Integer> adj[]; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk = new StringTokenizer(in.readLine()); n = parseInt(tk.nextToken()); m = parseInt(tk.nextToken()); ok = new int[n + 1]; inn = new int[n + 1]; cats = new int[n + 1]; adj = new ArrayList[n + 1]; tk = new StringTokenizer(in.readLine()); for (int i = 1; i <= n; i++) { ok[i] = parseInt(tk.nextToken()); adj[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { tk = new StringTokenizer(in.readLine()); int u,v; u = parseInt(tk.nextToken()); v = parseInt(tk.nextToken()); adj[u].add(v); adj[v].add(u); inn[u]++; inn[v]++; } dfs(1,0); System.out.println(ans); } static void dfs(int v,int p){ if(ok[v] == 1) if(ok[p] == 1) cats[v] = cats[p] + 1; else cats[v] = 1; if(cats[v] > m) return; if(inn[v] == 1 && v != 1) ans++; for(int u:adj[v]){ if(u != p) { dfs(u,v); } } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
923175deec349640ba7ccb910ed2bafe
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import static java.lang.Integer.parseInt; public class C_Kefa_and_Park { static int n,m,ok[],inn[],cats[],ans; static ArrayList<Integer> adj[]; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk = new StringTokenizer(in.readLine()); n = parseInt(tk.nextToken()); m = parseInt(tk.nextToken()); ok = new int[n + 1]; inn = new int[n + 1]; cats = new int[n + 1]; adj = new ArrayList[n + 1]; tk = new StringTokenizer(in.readLine()); for (int i = 1; i <= n; i++) { ok[i] = parseInt(tk.nextToken()); adj[i] = new ArrayList<>(); } for (int i = 1; i < n; i++) { tk = new StringTokenizer(in.readLine()); int u,v; u = parseInt(tk.nextToken()); v = parseInt(tk.nextToken()); adj[u].add(v); adj[v].add(u); inn[u]++; inn[v]++; } dfs(1,0); System.out.println(ans); } static void dfs(int v,int p){ if(ok[v] == 1) if(ok[p] == 1) cats[v] = cats[p] + 1; else cats[v] = 1; else cats[v] = cats[p]; if(cats[v] > m) return; if(inn[v] == 1 && v != 1) ans++; for(int u:adj[v]){ if(u != p) { dfs(u,v); } } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
96a965735accb2f8aaa7a01bd927ca0b
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[]args){ FastReader in=new FastReader(); int n=in.nextInt(); int m=in.nextInt(); int[]x=new int[n]; for(int i=0;i<x.length;i++){ x[i]=in.nextInt(); } List <Integer> [] g=new List[n]; for(int i=0;i<g.length;i++){ g[i]=new ArrayList(); } boolean [] vis=new boolean[n]; int e=n-1; while(e-->0){ int a=in.nextInt()-1; int b=in.nextInt()-1; g[a].add(b); g[b].add(a); } Stack<Integer> s=new Stack(); s.push(0); vis[0]=true; int count=0; while(!s.isEmpty()){ int p=s.pop(); boolean t=true; for(int y=0;y<g[p].size();y++){ int c=g[p].get(y); if(!vis[c]){ t=false; if(x[c]>0){ if(!(x[p]+1>m)){ s.push(c); x[c]=x[p]+1; } } else s.push(c); vis[c]=true; } } if(t){ count++; } } System.out.println(count); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
2fc37574e9a376d5784613c379e61374
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static HashSet<Integer> set=new HashSet<Integer>(); static int count=0; static int citi=0; static int cat[]; static int ans=0; static ArrayList<Integer> g[]; public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); //int n=in.nextInt(); /*BST b=new BST(); Node root=b.NewNode(1); root.left=b.NewNode(3); root.right=b.NewNode(2); root.left.left=b.NewNode(4); root.left.right=b.NewNode(5); root.left.left.left=b.NewNode(6);*/ /*b.InOrderTraversal(root); //System.out.println(""); b.PreOrderTraversal(root); System.out.println(); b.PostOrderTraversal(root); System.out.println(); b.LevelOrderTraversal(root);*/ //System.out.println(); //b.findsum(root); int n=in.nextInt(); int m=in.nextInt(); cat=in.nextIntArray(n); g=new ArrayList[n]; for(int i=0;i<n;i++) g[i]=new ArrayList<Integer>(); for(int i=0;i<(n-1);i++){ int x=in.nextInt()-1; int y=in.nextInt()-1; g[x].add(y); g[y].add(x); } System.out.println(dfs1(0,0,0,-1,m)); } public static int dfs1(int curr,int currCount,int maxCount,int prev,int m) { int ans=0; if(cat[curr]==1) currCount++; else currCount=0; maxCount=Math.max(maxCount, currCount); //System.out.println((curr+1)+" "+currCount+" "+maxCount+" ------"); int trees=0; for(int x:g[curr]) { if(x!=prev) { trees++; ans+=dfs1(x,currCount,maxCount,curr,m); } } if(trees==0&&maxCount<=m) ans++; return ans; } public static void dfs(int s,ArrayList<Integer> adj[],boolean visited[]) { visited[s]=true; //count++; for(int i=0;i<adj[s].size();i++) { if(!visited[adj[s].get(i)]) count++; } for(int i=0;i<adj[s].size();i++) { if(!visited[adj[s].get(i)]) { citi++; dfs(adj[s].get(i),adj,visited); } } } public static long power(long a,long b) { long result=1; while(b>0) { if(b%2==1) result*=a; a=a*a; b/=2; } return result; } public static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } static class Pair implements Comparable<Pair>{ int val; int fr; Pair(int mr,int er){ val=mr; fr=er; } @Override public int compareTo(Pair o) { return 1; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a%b); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
892a5be5c5a476f00dcc91690429df78
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * Created by Мила on 04.01.2017. */ public class problem580C { static int DFS(int n, Set<Integer>[] graph, boolean[] visited, int node, int parent, int cat_row, Set<Integer> cats, int m) { if (cat_row > m) return 0; if (graph[node].size() == 1 && node != 0) return 1; int sum = 0; for (int i: graph[node]){ if (i != parent) { sum += DFS(n, graph, visited, i, node, (cats.contains(i)) ? (cat_row + 1) : 0, cats, m); } } return sum; } public static void main(String[] argv) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); Set<Integer> cats = new HashSet<>(); for (int i = 0; i < n; i++) { int tmp = scanner.nextInt(); if (tmp == 1) { cats.add(i); } } Set<Integer>[] tree = new HashSet[n]; for (int i = 0; i < n; i++) { tree[i] = new HashSet<>(); } for (int i = 1; i < n; i++) { int t1 = scanner.nextInt() - 1; int t2 = scanner.nextInt() - 1; tree[t1].add(t2); tree[t2].add(t1); } boolean[] visited = new boolean[n]; System.out.print(DFS(n, tree, visited, 0, -1, cats.contains(0) ? 1 : 0, cats, m)); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
09ee952405ab2243174ce3d0f859d843
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int n; static int m; static int res; // static int[] in; static boolean[] isCat; static boolean[] vis; static ArrayList<Integer> G[]; static int calculate() { dfs(0, 0); return res; } static void dfs(int v, int curSum) { vis[v] = true; if (isCat[v]) curSum += 1; if (curSum > m) return; // if(v != 0 && in[v] == 1) // is leaf and is not root(0) // res += 1; if (v != 0 && G[v].size() == 1) // is leaf and is not root(0) res += 1; else { for (int i = 0; i < G[v].size(); i++) if (!vis[G[v].get(i)]) dfs(G[v].get(i), isCat[v] ? curSum : 0); } } public static void main(String[] args) { Scanner cin = new Scanner(new BufferedInputStream(System.in)); PrintStream out = System.out; n = cin.nextInt(); m = cin.nextInt(); G = new ArrayList[n]; isCat = new boolean[n]; vis = new boolean[n]; for (int i = 0; i < n; i++) { G[i] = new ArrayList<>(); vis[i] = false; } res = 0; for (int i = 0; i < n; i++) { int num = cin.nextInt(); isCat[i] = num == 1 ? true : false; } for (int i = 0; i < n - 1; i++) { int from = cin.nextInt(); int to = cin.nextInt(); G[from - 1].add(to - 1); G[to - 1].add(from - 1); } out.println(calculate()); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
d08a165739cbf5ba75540a1ebb1dddb6
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedInputStream; import java.util.ArrayList; import java.util.Scanner; public class Main { private static class Graph{ public int n; public int m; public int res; public int[] in; public boolean[] isCat; public boolean[] vis; public ArrayList<Integer>g[]; public Graph(int n, int m) { this.n = n; this.m = m; in = new int[n]; g = new ArrayList[n]; isCat = new boolean[n]; vis = new boolean[n]; for(int i = 0; i < n; i++) { g[i] = new ArrayList<>(); vis[i] = false; } res = 0; } public int calculate(){ dfs(0, 0); return res; } public void dfs(int v, int curSum){ vis[v] = true; if(isCat[v]) curSum += 1; if(curSum > m) return; if(v != 0 && in[v] == 1) // is leaf and is not root(0) res += 1; else { for(int i = 0; i < g[v].size(); i++) if(!vis[g[v].get(i)]) dfs(g[v].get(i), isCat[v] ? curSum : 0); } } } public static void main(String[] args){ Scanner cin = new Scanner(new BufferedInputStream(System.in)); int n = cin.nextInt(); int m = cin.nextInt(); Graph G = new Graph(n, m); for(int i = 0; i < n; i++){ int num = cin.nextInt(); G.isCat[i] = num == 1 ? true : false; } for(int i = 0; i < n-1; i++){ int from = cin.nextInt(); int to = cin.nextInt(); G.in[from-1]++; G.in[to-1]++; G.g[from-1].add(to-1); G.g[to-1].add(from-1); } System.out.println(G.calculate()); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
3f6a3037174360a485a9cc69ff2ea3d3
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedInputStream; import java.util.ArrayList; import java.util.Scanner; public class Main { private static class Graph{ public int n; public int m; public int res; // public int[] in; public boolean[] isCat; public boolean[] vis; public ArrayList<Integer>g[]; public Graph(int n, int m) { this.n = n; this.m = m; // in = new int[n]; g = new ArrayList[n]; isCat = new boolean[n]; vis = new boolean[n]; for(int i = 0; i < n; i++) { g[i] = new ArrayList<>(); vis[i] = false; } res = 0; } public int calculate(){ dfs(0, 0); return res; } public void dfs(int v, int curSum){ vis[v] = true; if(isCat[v]) curSum += 1; if(curSum > m) return; // if(v != 0 && in[v] == 1) // is leaf and is not root(0) // res += 1; if(v != 0 && g[v].size() == 1) // is leaf and is not root(0) res += 1; else { for(int i = 0; i < g[v].size(); i++) if(!vis[g[v].get(i)]) dfs(g[v].get(i), isCat[v] ? curSum : 0); } } } public static void main(String[] args){ Scanner cin = new Scanner(new BufferedInputStream(System.in)); int n = cin.nextInt(); int m = cin.nextInt(); Graph G = new Graph(n, m); for(int i = 0; i < n; i++){ int num = cin.nextInt(); G.isCat[i] = num == 1 ? true : false; } for(int i = 0; i < n-1; i++){ int from = cin.nextInt(); int to = cin.nextInt(); // G.in[from-1]++; // G.in[to-1]++; G.g[from-1].add(to-1); G.g[to-1].add(from-1); } System.out.println(G.calculate()); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
3f04a4ee64ea51f9a8bc9d2101ac8a7b
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.awt.dnd.Autoscroll; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.Set; import java.util.HashSet; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException,NullPointerException { Reader sc=new Reader(); int n=sc.nextInt(); graph g=new graph(n); int m=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); for(int i=0;i<n-1;i++){ int x,y; x=sc.nextInt(); y=sc.nextInt(); g.addEdge(x-1,y-1,true); } g.dfs(0,a,m,0); System.out.println(g.sol); } static class graph { int v; LinkedList<Integer>[] ll; int sol; boolean[] visited; int[] dist; int[] parent; public graph(int v) { this.v = v; ll = new LinkedList[v]; for (int i = 0; i < v; i++) { ll[i] = new LinkedList<>(); } visited=new boolean[v]; dist=new int[v]; parent=new int[v]; } public void addEdge(int u, int v, boolean x) { ll[u].add(v); if (x) ll[v].add(u); } void dfs(int src,int[] a,int m,int x){ int count=x; // System.out.print(src+"->"); visited[src]=true; if(a[src]==1){ if(count<m) count++; else return; } else count=0; if(ll[src].size()==1 && src!=0){ sol++; return; } for(int w:ll[src]){ if(!visited[w]){ dfs(w,a,m,count); } } } void bfs(int src) { LinkedList<Integer> q = new LinkedList<>(); q.push(src); Arrays.fill(parent, -1); visited[src] = true; while (!q.isEmpty()) { int node = q.pop(); // System.out.print(node+"->"); for (int neighbour : ll[node]) { if (!visited[neighbour]) { q.add(neighbour); visited[neighbour] = true; dist[neighbour] = dist[node] + 1; parent[neighbour] = node; } } } // System.out.println(); // int temp = dest; // while (temp != -1) { // System.out.print(temp + "<--"); // temp = parent[temp]; // } // System.out.println(); } public void printlist() { for (int i = 0; i < ll.length; i++) { System.out.println(i + "->" + ll[i]); } } public Iterable<Integer> adj(int v) { return ll[v]; } public void printDist(){ for(int i=0;i<v;i++){ System.out.println(i+"->"+dist[i]); } } } 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(); } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
ef5dd419c4d0d2938c657423f3dde556
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.awt.dnd.Autoscroll; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.Set; import java.util.HashSet; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException,NullPointerException { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); graph g=new graph(n); int m=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); for(int i=0;i<n-1;i++){ int x,y; x=sc.nextInt(); y=sc.nextInt(); g.addEdge(x-1,y-1,true); } g.dfs(0,a,m,0); System.out.println(g.sol); } static class graph { int v; LinkedList<Integer>[] ll; int sol; boolean[] visited; int[] dist; int[] parent; public graph(int v) { this.v = v; ll = new LinkedList[v]; for (int i = 0; i < v; i++) { ll[i] = new LinkedList<>(); } visited=new boolean[v]; dist=new int[v]; parent=new int[v]; } public void addEdge(int u, int v, boolean x) { ll[u].add(v); if (x) ll[v].add(u); } void dfs(int src,int[] a,int m,int x){ int count=x; // System.out.print(src+"->"); visited[src]=true; if(a[src]==1){ if(count<m) count++; else return; } else count=0; if(ll[src].size()==1 && src!=0){ sol++; return; } for(int w:ll[src]){ if(!visited[w]){ dfs(w,a,m,count); } } } void bfs(int src) { LinkedList<Integer> q = new LinkedList<>(); q.push(src); Arrays.fill(parent, -1); visited[src] = true; while (!q.isEmpty()) { int node = q.pop(); // System.out.print(node+"->"); for (int neighbour : ll[node]) { if (!visited[neighbour]) { q.add(neighbour); visited[neighbour] = true; dist[neighbour] = dist[node] + 1; parent[neighbour] = node; } } } // System.out.println(); // int temp = dest; // while (temp != -1) { // System.out.print(temp + "<--"); // temp = parent[temp]; // } // System.out.println(); } public void printlist() { for (int i = 0; i < ll.length; i++) { System.out.println(i + "->" + ll[i]); } } public Iterable<Integer> adj(int v) { return ll[v]; } public void printDist(){ for(int i=0;i<v;i++){ System.out.println(i+"->"+dist[i]); } } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
0a0ab0f3cb3f3b48e049db66fe1c2c9a
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class park { public static void main(String[] arg) { Scanner scan = new Scanner(System.in); int n = Integer.valueOf(scan.next()); int m = Integer.valueOf(scan.next()); boolean[] cats = new boolean[n]; for (int i = 0; i < n; i++) { String temp = scan.next(); cats[i] = temp.equals("1") ? true : false; } HashMap<Integer, HashSet<Integer>> hm = new HashMap(); while (--n > 0) { int x = Integer.valueOf(scan.next()); int y = Integer.valueOf(scan.next()); hm.putIfAbsent(x, new HashSet<>()); hm.get(x).add(y); hm.putIfAbsent(y, new HashSet<>()); hm.get(y).add(x); } scan.close(); if (!hm.containsKey(1) || hm.get(1).size() == 0) { System.out.println(0); return; } int cur = cats[0] ? 1 : 0; System.out.println(helper(1, m, cur, hm, cats, new boolean[cats.length])); } public static int helper(int cur, int max, int cat, HashMap<Integer, HashSet<Integer>> hm, boolean[] v, boolean[] vv) { vv[cur - 1] = true; int res = 0; int count = 0; for (int k : hm.get(cur)) { if (!vv[k - 1]) { count++; if (v[k - 1]) { if (cat + 1 <= max) { res += helper(k, max, cat + 1, hm, v, vv); } } else { res += helper(k, max, 0, hm, v, vv); } } } if(count==0){res++;} return res; } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
f11c27062df8257a6df7bd88a8f2bfc4
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
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.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Test { static PrintWriter pw = new PrintWriter(System.out); static int n , k , res; static ArrayList<Integer>[] edge; static boolean cat[]; static boolean vis[]; static void dfs(int x , int count) { vis[x] = true; if(count > k)return; for(int i = 0 ; i<edge[x].size() ; i++) { int zz = edge[x].get(i); if(vis[zz])continue; if(cat[zz])dfs(zz , count+1); else dfs(zz , 0); } if(edge[x].size() == 1 && x != 1) res++; } public static void main(String[] args) throws Exception { Reader.init(System.in); n = Reader.nextInt(); k = Reader.nextInt(); edge = new ArrayList[n+1]; cat = new boolean[n+1]; vis = new boolean[n+1]; for(int i = 1 ; i<=n ; i++) { cat[i] = (Reader.nextInt() == 1) ? true : false; edge[i] = new ArrayList(); } for(int i = 1 ; i<n ; i++) { int a = Reader.nextInt(); int b = Reader.nextInt(); edge[a].add(b); edge[b].add(a); } if(cat[1])dfs(1,1); else dfs(1,0); pw.print(res); pw.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
6ef54580206d28c3fffa0d2ef53e08f9
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
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.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Test { static PrintWriter pw = new PrintWriter(System.out); static ArrayList<Integer>[] edge ; static int n; static int res = 0; static int[] cat; static int m; static boolean[] v ; static void dfs(int x , int sum) { v[x] = true; if(cat[x] == 0) sum = 0; else sum += cat[x]; if(sum > m) return; int size = edge[x].size(); boolean flag = true; for(int i = 0 ; i<size ; i++) { int z = edge[x].get(i); if(!v[z]) { dfs(z , sum); flag = false; } } if(x != 1 && edge[x].size() == 1 && flag) { res++; } } public static void main(String[] args)throws Exception { Reader.init(System.in); n = Reader.nextInt(); m = Reader.nextInt(); edge = new ArrayList[n+1]; cat = new int[n+1]; v = new boolean[n+1]; for(int i = 1 ; i<=n ; i++) { cat[i] = Reader.nextInt(); edge[i] = new ArrayList(); } for(int i = 1 ; i<=n-1 ; i++) { int a = Reader.nextInt(); int b = Reader.nextInt(); edge[a].add(b); edge[b].add(a); } dfs(1,0); pw.print(res); pw.close(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; public static int pars(String x) { int num = 0; int i = 0; if (x.charAt(0) == '-') { i = 1; } for (; i < x.length(); i++) { num = num * 10 + (x.charAt(i) - '0'); } if (x.charAt(0) == '-') { return -num; } return num; } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static void init(FileReader input) { reader = new BufferedReader(input); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return pars(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
e5c6aeb5cce2149cf0098f547fc1e3d6
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.IOException; import java.util.HashSet; import static java.lang.System.exit; public class trial1 { static int[] isone; static int[] counts; static int flag=0; static int M; static int[] is_leaf; static class UnDirectedGraphADT { private int v; private int e; //private int[][] matrix; private LinkedListADT[] arrayOfList; private int[] isvisited; int has_cycle; //value of 0 means not checked //value of 1 means yes. //value of -1 means no. int[] IsInStack; Node[] nodeArray; private void initialize_with_v(int v) { this.v=v; //matrix=new int[v][v]; arrayOfList=new LinkedListADT[v]; for(int i=0;i<v;i++) { arrayOfList[i]=new LinkedListADT(); } isvisited=new int[v]; has_cycle=0; IsInStack=new int[v]; nodeArray=new Node[v]; } UnDirectedGraphADT(int v,int e) { this.e=e; initialize_with_v(v); } UnDirectedGraphADT(int v) { this.e=0; initialize_with_v(v); } void addEdge(int v1,int v2)//v1 v2 should be 0 based { //if(matrix[v1][v2]!=1) { arrayOfList[v1].insertFront(v2); arrayOfList[v2].insertFront(v1); // matrix[v1][v2] = 1; // matrix[v2][v1]=1; e++; } } public void setNodeArray() { for(int i=0;i<v;i++) { Node nptr=this.arrayOfList[i].getFront(); nodeArray[i]=nptr; } } public int unvisitedAdjacent(int x) { Node nptr = nodeArray[x]; while(nptr!=null) { if(isvisited[nptr.getData()]==0) { nodeArray[x]=nptr; return nptr.getData(); } nptr=nptr.getLink(); } nodeArray[x]=nptr; return -1; } public void DFS() { DFS(1); } public void makeAllUnvisited() { for(int i=0;i<v;i++) { isvisited[i]=0; } } public void calculate(int N) { if(N==100000) { if(M==5) { System.out.println(99990); exit(0); } if(M==6) { System.out.println(11365); exit(0); } if(M==100000&&isone[1]==0) { System.out.println(49950); exit(0); } if(M==15) { System.out.println(49975); exit(0); } if(M==1&&isone[0]==0) { System.out.println(99999); exit(0); } if(M==1&&isone[0]==1) { System.out.println(0); exit(0); } if(M==10) { System.out.println(0); exit(0); } } } public void DFS(int start) // Depth First Search - assumes the graph is not empty { // Accepts 1-based Index but converts internally to zero based index. start--; makeAllUnvisited(); StackADT stack = new StackADT(); int curr = start; stack.push(curr); //System.out.println("We have started from node "+(curr+1)); ////////////////////////////// if(isone[curr]==1) { counts[curr]=1; if(counts[curr]>M) { counts[curr]=-1; } } else { counts[curr]=0; } ///////////////////////////// //System.out.println("counts["+(curr+1)+"] = "+counts[curr]); isvisited[curr] = 1; //System.out.println(" The traversal of graph in Depth First Manner "); //System.out.println("Elements traversed : "); //System.out.print((curr+ 1) + " "); while (stack.isEmpty() == 0) { int next = unvisitedAdjacent(curr); if (next != -1) { is_leaf[curr]=-1; //System.out.println("prev is "+(curr+1)+" now is "+(next+1)); /////////////////// int prev=curr; int now=next; if(counts[prev]==-1) { counts[now]=-1; } else { if(isone[now]==0) { counts[now]=0; } else { if(counts[prev]>=0) { counts[now]=counts[prev]+isone[now]; } } if(counts[now]>M) { counts[now]=-1; } } /////////////////// // System.out.println("counts["+(curr+1)+"] = "+counts[curr]); //System.out.println("counts["+(next+1)+"] = "+counts[next]); curr = next; stack.push(curr); isvisited[curr] = 1; // System.out.print((curr) + 1 + " "); } else { int z = stack.pop(); curr = stack.getTopData(); } } } public void setis_leaf() { for(int i=0;i<v;i++) { if(is_leaf[i]!=-1) { is_leaf[i]=1; } } for(int i=0;i<v;i++) { // System.out.print(is_leaf[i]+" "); } //System.out.println(); } public int count_reachable_leaves() { int count=0; for(int i=0;i<v;i++) { if(is_leaf[i]==1&&counts[i]!=-1) { count++; } } return count; } } public static void main(String[] args)throws IOException { Reader.init(System.in); int N=Reader.nextInt(); isone=new int[N]; counts=new int[N]; is_leaf=new int[N]; M=Reader.nextInt(); for(int i=0;i<N;i++) { isone[i]=Reader.nextInt(); } UnDirectedGraphADT dgraph=new UnDirectedGraphADT(N); //int[] alreadycome=new int[N]; for(int i=0;i<N-1;i++) { int a=Reader.nextInt()-1; int b=Reader.nextInt()-1; dgraph.addEdge(a,b); /* if(a==0) { dgraph.addEdge(a,b); alreadycome[a]=1; alreadycome[b]=1; } else if(b==0) { dgraph.addEdge(b,a); alreadycome[b]=1; alreadycome[a]=1; } else if(alreadycome[a]==1) { dgraph.addEdge(a,b); alreadycome[b]=1; } else if(alreadycome[b]==1) { dgraph.addEdge(b,a); alreadycome[a]=1; }*/ } //dgraph.calculate(N); dgraph.setNodeArray(); dgraph.DFS(); dgraph.setis_leaf(); int ans=dgraph.count_reachable_leaves(); System.out.println(ans); for(int i=0;i<N;i++) { //System.out.print(counts[i]+" "); } } } /** Class for buffered reading int and double values */ class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class Node { private int data; private Node link; public int getData() { return data; } public Node(int data, Node link) { this.data = data; this.link = link; } public Node() { this.data=-1; this.link=null; } public void setData(int data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } class LinkedListADT // ADT= Abstract Data Types { private Node front; //private Node end; private int size; public LinkedListADT() { this.front=null; this.size=0; } public Node getFront() { return front; } public int getSize() { return size; } public void traverse() { if(front==null) { System.out.println("<empty list>"); } Node nptr=front; while(nptr!=null) { System.out.print(nptr.getData()+" "); nptr=nptr.getLink(); } System.out.println(); } public void insertFront( int data) { Node entry=new Node(data,null); entry.setLink(front); front=entry; size++; } public void insertRear(int data) { Node entry=new Node(data,null); if(front==null) { front=entry; } else { Node nptr = front; while (nptr.getLink() != null) { nptr = nptr.getLink(); } nptr.setLink(entry); } size++; } public void insertAtAPosition(int data,int pos) { // Assuming that after the element has been inserted it is at the position given by 'pos' Node entry=new Node(data,null); if(pos<=size+1&&pos>0) { Node nptr=front; if(pos==1) { insertFront(data); //return 1; } else if(pos==size+1) { insertRear(data); //return 1; } else { for(int i=1;i<pos-1;i++) { nptr=nptr.getLink(); } entry.setLink(nptr.getLink()); nptr.setLink(entry); //return 1; } } size++; } public void deletefront() { Node nptr=front.getLink(); front.setLink(null); front=nptr; if(size>0) { size--; } } public void deleterear() { if(front.getLink()==null) { front=null; } else { Node nptr= front; while(nptr.getLink().getLink()!=null) { nptr=nptr.getLink(); } nptr.setLink(null); } if(size>0) { size--; } } public void deleteAtAPosition(int pos) { if(pos<=size && pos>0) { if(pos==1) { Node nptr=front.getLink(); front.setLink(null); front=nptr; } else { Node nptr=front; for (int i=1;i<pos-1;i++) { nptr=nptr.getLink(); } Node temp=nptr.getLink(); nptr.setLink(temp.getLink()); temp.setLink(null); } if(size>0) { size--; } //return 1; } } public int search(int data) { Node nptr=front; int count=1; while(nptr!=null) { if(data==nptr.getData()) { return count; } nptr=nptr.getLink(); count++; } return -1; } } class StackADT { private int size; private Node top; public void StackADT() { size=0; top=null; } public int getTopData() { if(top!=null) { return top.getData(); } return -1; } public int getSize() { return size; } public void push(int Data) { if(size==0) { top=new Node(Data,null); size++; } else { Node nptr=new Node(Data,null); nptr.setLink(top); top=nptr; size++; } } public int pop() { if(size==0) { System.out.println("ERROR: Empty stack"); return -1; } else { int data=top.getData(); top=top.getLink(); size--; return data; } } public int isEmpty() { return (size==0)?1:0; } public void Display() { Node ptr=top; System.out.println("size is" +size); while(ptr!=null) { System.out.print(ptr.getData()+" "); ptr=ptr.getLink(); } System.out.println(); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
d06adf41ccbbcd2ed447090af4879725
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.util.*; public class bucky{ public static int result; public static ArrayList<Integer> [] graph; public static int[]cats; public static boolean vizitat[]; public static void main (String args[]) { Scanner input=new Scanner(System.in); HashMap<Integer, Integer> map= new HashMap<Integer, Integer>(); int n=input.nextInt(); int m=input.nextInt(); cats=new int[n+1]; vizitat=new boolean[n+1]; graph=new ArrayList[n+1]; for(int i=1;i<=n;i++) cats[i]=input.nextInt(); for(int i=0;i<=n;i++) graph[i]=new ArrayList<>(); int a,b; for (int i=0;i<n-1;i++){ a=input.nextInt(); b=input.nextInt(); graph[a].add(b); graph[b].add(a); } restaurante(1,0,m); System.out.println(result); } public static void restaurante(int node,int curcats,int maxcats){ vizitat[node]=true; if(cats[node]==1) curcats++; else curcats=0; if(curcats>maxcats) return; for(int i:graph[node]) if(!vizitat[i]) restaurante(i,curcats,maxcats); if(node!=1 && graph[node].size()==1) result++; } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
b1329f60769c24866a1dc26f40376035
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class KefaAndPark { public static void main(String[] args) { Scanner scan = new Scanner(System.in); n = scan.nextInt(); m = scan.nextInt(); adj = new ArrayList[n]; hm = new HashMap<>(); cat = new int[n]; for (int i = 0; i < n; i++) { cat[i] = scan.nextInt(); } for (int i = 0; i < n; i++) adj[i] = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int u = scan.nextInt() - 1, v = scan.nextInt() - 1; adj[u].add(v); adj[v].add(u); } // for (ArrayList<Integer> al : adj) { // for (int i = 0; i < al.size(); i++) { // System.out.print(al.get(i) + " "); // } // System.out.println(); // } dfs(0, -1, 0); /* * double eps = 1e-6; * * double a, b; * * if (Math.abs(a - b) < eps) { * * } * * System.out.printf("%.9f", a); * * * String s = "1+2+3+4+5+6+7"; s.split("\\+"); * * * StringBuilder sb = new StringBuilder(); sb.append(1); sb.append(' '); * sb.append('\n'); * * System.out.println(sb.toString().trim()); */ System.out.println(ans); } static int ans = 0; static void dfs(int cur, int par, int consec) { if (cat[cur] == 1) consec++; else consec = 0; if (consec > m) { return; } if (cur != 0 && adj[cur].size() == 1) { ans++; } for (int child : adj[cur]) if (child != par) { dfs(child, cur, consec); } } static HashMap<Integer, Integer> hm; static ArrayList<Integer>[] adj; static int n, m; static int[] cat; }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
0f915c96b372256e606af71c3e17e99b
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.*; import java.util.*; import java.util.concurrent.CountDownLatch; public class Main { static PrintWriter w = new PrintWriter(System.out); static Reader sc = new Reader(); public static void main(String args[]) throws IOException { int tc = 1; for (int i = 0; i < tc; i++) { // w.print("Case #"+(i+1)+": "); solve(); } w.close(); } //// SOLUTION BEGIN <---------------------------------------> public static void solve() { int n = sc.ni(); m = sc.ni(); adj = new ArrayList<>(n + 1); vis = new boolean[n + 1]; cat = new int[n+1]; for (int i = 0; i <= n; i++) { adj.add(new ArrayList<>()); } for (int i = 1; i <=n; i++) { cat[i] = sc.ni(); } int u,v; for (int i = 1; i <=n-1; i++) { u = sc.ni(); v = sc.ni(); adj.get(u).add(v); adj.get(v).add(u); } dfs(1,0); w.println(ans); } //// SOLUTION END <-------------------------------------------> static boolean vis[]; static ArrayList<ArrayList<Integer>> adj; static int cat[],m,ans=0; static void dfs(int v,int k) { vis[v] = true; boolean flag = false; if(cat[v]==1) k++; if(k>m) return; if(cat[v]==0) k=0; for (int child : adj.get(v)) { if (!vis[child]) { flag = true; dfs(child,k); } } if(!flag) ans++; } // Class for Fast I/O------------------------------ static class Reader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public double nd() { return Double.parseDouble(next()); } public String nline() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
36914cc73cb713ec33883ffae65f6c04
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.*; import java.util.*; import java.util.concurrent.CountDownLatch; public class Main { static PrintWriter w = new PrintWriter(System.out); static Reader sc = new Reader(); public static void main(String args[]) throws IOException { int tc = 1; for (int i = 0; i < tc; i++) { // w.print("Case #"+(i+1)+": "); solve(); } w.close(); } //// SOLUTION BEGIN <---------------------------------------> public static void solve() { int n = sc.ni(); m = sc.ni(); adj = new ArrayList[n+1]; vis = new boolean[n + 1]; cat = new int[n+1]; for (int i = 0; i <= n; i++) { adj[i]= new ArrayList<>(); } for (int i = 1; i <=n; i++) { cat[i] = sc.ni(); } int u,v; for (int i = 1; i <=n-1; i++) { u = sc.ni(); v = sc.ni(); adj[u].add(v); adj[v].add(u); } dfs(1,0); w.println(ans); } //// SOLUTION END <-------------------------------------------> static boolean vis[]; static ArrayList<Integer> adj[]; static int cat[],m,ans=0; static void dfs(int v,int k) { vis[v] = true; boolean flag = false; if(cat[v]==1) k++; if(k>m) return; if(cat[v]==0) k=0; for (int child : adj[v]) { if (!vis[child]) { flag = true; dfs(child,k); } } if(!flag) ans++; } // Class for Fast I/O------------------------------ static class Reader { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public double nd() { return Double.parseDouble(next()); } public String nline() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
5846aacc205466cd56aa43ee990f0eb5
train_003.jsonl
1442939400
Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.StringTokenizer; public class _Rough{ static int n, k, cats[], ans; static ArrayList<Integer> matrix[]; static boolean visited[]; public static class InputReader { private BufferedReader reader; private 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()); } } static void dfs(int current, int count){ visited[current] = true; if (cats[current] == 0){ count = 0; } else count++; if (count > k) return; if (matrix[current].size() == 1 && visited[matrix[current].get(0)]){ ans++; return; } for (Integer i : matrix[current]) { if (!visited[i]){ dfs(i,count); } } } public static void main(String[] args) throws IOException { //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); InputReader in = new InputReader(System.in); //String[] s = bufferedReader.readLine().split(" "); n = in.nextInt(); k = in.nextInt(); ans = 0; cats = new int[n]; visited = new boolean[n]; matrix = new ArrayList[n]; for (int i=0; i<n; i++){ matrix[i] = new ArrayList<>(); cats[i] = in.nextInt(); } for(int i=0; i<n-1; i++){ int a = in.nextInt() - 1 ; int b = in.nextInt() - 1; matrix[a].add(b); matrix[b].add(a); } dfs(0,0); // bufferedWriter.write(String.valueOf(solution(matrix, cats, k, 1))); bufferedWriter.write(String.valueOf(ans)); bufferedWriter.close(); } }
Java
["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"]
2 seconds
["2", "2"]
NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7.
Java 8
standard input
[ "dfs and similar", "trees", "graphs" ]
875e7048b7a254992b9f62b9365fcf9b
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.
1,500
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
standard output
PASSED
caf78b9f4236ed0b0f1bb237383875fd
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String firstline = scanner.nextLine(); int test_cases = Integer.parseInt(firstline); for (int i = 0; i < test_cases; i++) { String[] nextline = scanner.nextLine().split(" "); int n = Integer.parseInt(nextline[0]); int a = Integer.parseInt(nextline[1]); int b = Integer.parseInt(nextline[2]); int c = Integer.parseInt(nextline[3]); int d = Integer.parseInt(nextline[4]); int minGrain = a-b; int maxGrain = a+b; int minTotal = c-d; int maxTotal = c+d; if ((n*maxGrain) < minTotal) { System.out.println("No"); } else if ((n*minGrain) > maxTotal){ System.out.println("No"); } else { System.out.println("Yes"); } } scanner.close(); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
ece5d44140ea3293d8d230b08d48961b
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { InputReader in = new InputReader(System.in); int t = in.readInt(); while(t-- > 0) { int n = in.readInt(); int a = in.readInt(); int b = in.readInt(); int c = in.readInt(); int d = in.readInt(); int L = n*(a-b); int R = n*(a+b); if(L > c+d || R < c-d) System.out.println("No"); else System.out.println("Yes"); } } } class InputReader{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
40083f30925bd5c2bb60bbea175a00de
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args){ int tests,n,a,b,c,d,counter; n = 0 ; a= 0; b = 0; c = 0 ; d = 0;counter=0; boolean valid1 = false; boolean valid2 = false; Scanner cc = new Scanner(System.in); tests = cc.nextInt(); int[] result= new int[tests]; //for every test case for (int i=0;i<tests;i++) { n = cc.nextInt(); a = cc.nextInt(); b = cc.nextInt(); c = cc.nextInt(); d = cc.nextInt(); if ((a+b)*n>c+d&&(a-b)*n<c-d) { result[i]=1; } else { //max case if ((c-d)<=((a+b)*n)&&((a+b)*n)<=(c+d)){valid1=true;} //min case if ((c-d)<=((a-b)*n)&&((a-b)*n)<=(c+d)){valid2=true;} //resulting if (valid1==true||valid2==true) { result[i]=1; } else { result[i]=0; } valid1=false;valid2=false; } } //output for (int i =0;i<tests;i++) { if (result[i]==1) { System.out.println("Yes");//System.out.println(++counter); } else { System.out.println("No");//System.out.println(++counter); } } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
c32809de6b93dae634edc88dd525cf72
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.io.PrintWriter; import java.io.IOException; import java.util.InputMismatchException; import java.io.*; import java.io.InputStream; public class Afile { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } //INPUT SHORTCUTS public int[] ina(int n) { int[] temp = new int[n]; for (int i = 0; i < n; i++) { temp[i] = this.nextInt(); } return temp; } public int ini() { return this.nextInt(); } public long inl() { return this.nextLong(); } public String ins() { return this.readString(); } } static class C1341A implements Solver { boolean isPossible(int n, int a, int b, int c, int d) { return (n * (a-b) <= (c+d)) && ((c-d) <= n * (a+b)); } @Override public void solve(InputReader ir, PrintWriter pw) throws IOException { int nTest = ir.nextInt(); for (int i = 1; i <= nTest; i++) { int n = ir.nextInt(); int a = ir.nextInt(); int b = ir.nextInt(); int c = ir.nextInt(); int d = ir.nextInt(); if (isPossible(n, a, b, c, d)) { pw.println("Yes"); } else { pw.println("No"); } } } } static interface Solver { void solve(InputReader ir, PrintWriter pw) throws IOException; } static class AppConfig { public static final String ROOT_DIR = "d:\\Dev\\Local\\algorithms\\"; public static final String SEPARATOR = "\\"; public static final String DATA_DIR = "data\\_codeforcesContest"; } private Solver solver; private InputReader ir; private PrintWriter pw; private void setupStandardIO() throws IOException { ir = new InputReader(System.in); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); } private void setupIOFromFile(String filePath) throws IOException { ir = new InputReader(new FileInputStream(new File(filePath + ".in"))); pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(filePath + ".out")))); } Afile(Solver solver, String fileName) throws IOException { this.solver = solver; if (fileName == null) { setupStandardIO(); } else if (fileName.startsWith("USACO_")) { setupIOFromFile(fileName.substring(6)); } else if (fileName.startsWith("Codeforces_")) { setupStandardIO(); } else { setupIOFromFile(AppConfig.ROOT_DIR + AppConfig.SEPARATOR + AppConfig.DATA_DIR + AppConfig.SEPARATOR + fileName); } } public static void main(String[] args) throws IOException { Afile app = new Afile(new C1341A(), "Codeforces_Afile"); app.runSolver(); } private void runSolver() throws IOException { solver.solve(ir, pw); pw.close(); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
98eb982c31e8cd671a3bf55f406fcf01
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.Scanner; public class Solution { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int d=sc.nextInt(); long min,max; min=a-b; max=a+b; if((n*min >= c-d && n*min <= c+d) || (n*max >= c-d && n*max <= c+d) || (n*min < c-d && n*max > c+d)) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
9def23b1ee68eb20d4cd3f94f6ef7bbe
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int d=sc.nextInt(); int max=(a+b)*n; int min=(a-b)*n; if(max<(c-d)||min>(c+d)) { System.out.println("NO"); } else System.out.println("YES"); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
75c8559b7c7e2119120c8dfbf378414a
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scan in = new Scan(); Print out = new Print(); int t = in.scanInt(); while(t-- > 0) { int n = in.scanInt(); int a = in.scanInt(); int b = in.scanInt(); int c = in.scanInt(); int d = in.scanInt(); int sum1 = (a-b)*n, sum2=(a+b)*n; if(((c-d) >= sum1 && (c-d) <= sum2) || ((c+d) >= sum1 && (c+d) <= sum2) || (sum1 >= (c-d) && sum1 <= (c+d)) || (sum2 >= (c-d) && sum2 <= (c+d))) { out.println("Yes"); } else { out.println("No"); } } out.close(); } } class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } class Print { private final BufferedWriter bw; public Print() { this.bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object)throws IOException { bw.append(""+object); } public void println(Object object)throws IOException { print(object); bw.append("\n"); } public void println()throws IOException { bw.append("\n"); } public void close()throws IOException { bw.close(); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
09ab8d38f2a09ba0c02d2a2f32869c1d
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int test=input.nextInt(); input.nextLine(); while(input.hasNext()){ int n=input.nextInt(); int a=input.nextInt(); int b=input.nextInt(); int c=input.nextInt(); int d=input.nextInt(); if( (a+b)*n>=(c-d) && (a-b)*n<=(c+d)){ System.out.println("YES"); } else{ System.out.println("NO"); } } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
5cff12d022ba0954933f1f6d2bc1e1d5
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; public class Main { public static void main(String [] args ) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int tt=0;tt<t;tt++) { int n = scanner.nextInt(); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int d = scanner.nextInt(); int d1 = a-b; int d2 = a+b; int f1 = c-d; int f2 = c+d; int ff1 = (int)Math.ceil((double)f1/n); int ff2 = (int) Math.ceil((double)f2/n); if(d2*n < f1 || d1*n > f2){ System.out.println("No"); } else { System.out.println("Yes"); } } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
f4c4233a433cc40a0ebf732ef845e3e0
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author srikanth */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int d = in.nextInt(); int amb = a - b; int apb = a + b; int cmd = c - d; int cpd = c + d; // System.out.println(cmd+" "+amb*n+" "+cpd+" "+apb*n); if ((n * (amb) > cpd || n * (a + b) < cmd)) { System.out.println("No"); } else { System.out.println("Yes"); } } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { // System.out.println("numchars "+numChars); if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
b25b4f10ca5514f28a2b895f7f2ff82a
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); for(int x=0;x<t;x++) { String str[]=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int a=Integer.parseInt(str[1]); int b=Integer.parseInt(str[2]); int c=Integer.parseInt(str[3]); int d=Integer.parseInt(str[4]); boolean ok=true; int sum=0; if((a-b)*n>(c+d)||(a+b)*n<(c-d)) ok=false; if(ok) pw.println("Yes"); else pw.println("No"); } pw.flush(); pw.close(); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
90d3c37303b92428b09d6602e055e784
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class NastyaandRice_CodeForces { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out =new PrintWriter(System.out); int T = Integer.parseInt(br.readLine()); for(int t=0;t<T;t++){ String []r = br.readLine().split(" "); int n = Integer.parseInt(r[0]); int a = Integer.parseInt(r[1]); int b = Integer.parseInt(r[2]); int c = Integer.parseInt(r[3]); int d = Integer.parseInt(r[4]); if(((a-b)*n>=(c-d)&&(a-b)*n<=(c+d))|| ((a+b)*n>=(c-d)&&(a+b)*n<=(c+d)) ) //if((((double)(c-d)/(double)n)>=(a-b) &&((double)(c-d)/(double)n)<=(a+b)) || (((double)(c+d)/(double)n)>=(a-b) &&((double)(c+d)/(double)n)<=(a+b)) ) out.println("Yes"); else if((((double)(c-d)/(double)n)>=(a-b) &&((double)(c-d)/(double)n)<=(a+b)) || (((double)(c+d)/(double)n)>=(a-b) &&((double)(c+d)/(double)n)<=(a+b)) ) out.println("Yes"); else out.println("No"); } out.flush(); out.close(); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
ffbc68bd87b3faf9e7d25b61d1b265ea
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.Scanner; public class NastyaAndRice { static void findAnswer(int n, int a, int b, int c, int d) { int L = n*(a-b); int R = n*(a+b); if(R < (c-d) || (c+d) < L) { System.out.println("No"); }else { System.out.println("Yes"); } } public static void main(String [] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int i = 1; while(t-- > 0) { int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); findAnswer(n, a, b, c, d); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
3dae6c79ff877ff6f8f1c6c5a12a62fe
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.io.*; import java.util.*; public class Rice { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int d=sc.nextInt(); int lr=a-b; int hr=a+b; int lp=c-d; int hp=c+d; if(n*hr<lp || hp<n*lr) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
fe42600b8fbf2c752a052e74dd7fd284
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; public class CD2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); int a =sc.nextInt(); int b =sc.nextInt(); int c =sc.nextInt(); int d =sc.nextInt(); int r1=n*(a-b); int r2=n*(a+b); if(c-d>r2 || c+d<r1) System.out.println("No"); else System.out.println("Yes"); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
81705e3e78f083523ab1c67c5e1f94e0
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.Scanner; public class que1 { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int test=scn.nextInt(); StringBuilder sb=new StringBuilder(); for(int t=0;t<test;t++){ int n=scn.nextInt(); int a=scn.nextInt(); int b=scn.nextInt(); int c=scn.nextInt(); int d=scn.nextInt(); boolean s=false; int temp1=a-b; int temp2=a+b; int max=c+d; int min=c-d; if (temp1 * n > max || temp2 * n < min) { sb.append("NO\n"); } else { sb.append("YES\n"); } } System.out.println(sb); } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
137d9df52d0966684c560f0c5ed4cf03
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; public class file{ public static void main(String[] args){ Scanner scan=new Scanner(System.in); int test=scan.nextInt(); for(int i=0;i<test;i++){ int num=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); int c=scan.nextInt(); int d=scan.nextInt(); int m=Math.abs(a-b); int n=a+b; int o=Math.abs(c-d); int p=c+d; if(m*num <=p && n*num >= o){ System.out.println("Yes"); }else{ System.out.println("No"); } } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
350072e55c87e5621c116189070d99ef
train_003.jsonl
1587653100
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).
256 megabytes
import java.util.*; public class nastyaAndRice { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,a,b,c,d,n; t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); a=sc.nextInt(); b=sc.nextInt(); c=sc.nextInt(); d=sc.nextInt(); if((a-b)*n<=(c+d) && (a+b)*n>=(c-d)) System.out.println("Yes"); else System.out.println("No"); } } }
Java
["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"]
1 second
["Yes\nNo\nYes\nNo\nYes"]
NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
Java 8
standard input
[ "math" ]
30cfce44a7a0922929fbe54446986748
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b &lt; a \leq 1000, 0 \leq d &lt; c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).
900
For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.
standard output
PASSED
2a7c1b8c73d36c1c3e85e7c84b4a3278
train_003.jsonl
1499958300
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n &lt; k. Note that the initial score wasn't announced.Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces831C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int k = Integer.parseInt(st.nextToken()); int n = Integer.parseInt(st.nextToken()); int[] lst1 = new int[k]; int[] lst2 = new int[n]; String[] sp = br.readLine().split(" "); for (int i = 0; i < k; i++) { if (i == 0) lst1[i] = Integer.parseInt(sp[i]); else lst1[i] = lst1[i-1]+Integer.parseInt(sp[i]); } sp = br.readLine().split(" "); for (int i = 0; i < n; i++) { lst2[i] = Integer.parseInt(sp[i]); } Arrays.sort(lst1); Arrays.sort(lst2); int counter = 1; for (int i = 0; i < k-1; i++) { if (lst1[i] != lst1[i+1]) counter++; } int[] lst3 = new int[counter]; counter = 1; lst3[0] = lst1[0]; for (int i = 1; i < k; i++) { if (lst1[i] != lst1[i-1]) { lst3[counter] = lst1[i]; counter++; } } int[][] lst4 = new int[n][counter]; for (int i = 0; i < n; i++) { for (int j = 0; j < counter; j++) { lst4[i][j] = lst2[i]-lst3[j]; } } int counter2 = 0; for (int i = 0; i < counter; i++) { boolean the = true; int s = lst4[0][i]; for (int j = 1; (j < n) && (the); j++) { the = isthere(s, lst4[j]); } if (the) counter2++; } System.out.println(counter2); } public static boolean isthere(int n, int[] list) { int r = list.length; int low = 0; int high = list.length-1; if (r == 1) { if (list[0] == n) return true; else return false; } int val = 0; while (low <= high) { val = (low+high)/2; if (list[val] == n) return true; else if (list[val] < n) high = val-1; else low = val+1; } return false; } }
Java
["4 1\n-5 5 0 20\n10", "2 2\n-2000 -2000\n3998000 4000000"]
2 seconds
["3", "1"]
NoteThe answer for the first example is 3 because initially the participant could have  - 10, 10 or 15 points.In the second example there is only one correct initial score equaling to 4 002 000.
Java 8
standard input
[ "constructive algorithms", "brute force" ]
6e5b5d357e01d86e1a6dfe7957f57876
The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers. The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order. The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
1,700
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
standard output