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
74810bdcdffa31166a4c6ec2c3f75a11
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here' Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){ List<Integer> a = new ArrayList<>(); List<Integer> b = new ArrayList<>(); for(int j=0;j<3;j++) a.add(sc.nextInt()); for(int j=0;j<3;j++) b.add(sc.nextInt()); int sum =0;int m=0; m = Math.min(a.get(0),b.get(2)); a.set(0,a.get(0)-m); b.set(2,b.get(2)-m); m = Math.min(a.get(1),b.get(0)); a.set(1,a.get(1)-m); b.set(0,b.get(0)-m); m = Math.min(a.get(2),b.get(1)); a.set(2,a.get(2)-m); b.set(1,b.get(1)-m); sum+=m*2; m = Math.min(a.get(1),b.get(2)); sum-= m*2; System.out.println(sum); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
721b58ceaf178157b62a4da1bec520b6
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { FastScanner sc = new FastScanner(); int yo = sc.nextInt(); while(yo-->0) { int n = 3; int[] a = new int[3]; for(int i = 0; i < 3; i++) { a[i] = sc.nextInt(); } int[] b = new int[3]; for(int i = 0; i < 3; i++) { b[i] = sc.nextInt(); } int maxTwoInA = a[2]; int maxOneInB = b[1]; int maxTwoInB = b[2]<=a[0] ? 0 : b[2]-a[0]; int maxOneInA = a[1]<=b[0] ? 0 : a[1]-b[0]; int min1 = Math.min(maxTwoInB, maxOneInA); int min2 = Math.min(maxTwoInA, maxOneInB); // System.out.println(min1 + " " + min2); System.out.println(min2*2-(min1*2)); } } 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()); } } static int gcd(int a, int b) { return a%b == 0 ? b : gcd(b,a%b); } static boolean[] sieve(int n) { boolean isPrime[] = new boolean[n+1]; for(int i = 2; i <= n; i++) { if(isPrime[i]) continue; for(int j = 2*i; j <= n; j+=i) { isPrime[j] = true; } } return isPrime; } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
4c4cfadaa0705bcbc778041cfbec84e2
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
//package prob.daniels; import javax.swing.*; import java.math.BigInteger; import java.util.*; import java.awt.Desktop; public class Problem { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { boats1(); } public static void boats1() { int test = scanner.nextInt(); while(test>0) { long[] a = new long[3]; long[] b = new long[3]; for(int i = 0;i<3;i++) { a[i] = scanner.nextLong(); } for(int i = 0;i<3;i++) { b[i] = scanner.nextLong(); } // System.out.println(a); // System.out.println(b); if(a[0] < b[2]) { b[2] = b[2] - a[0]; a[0] = 0; if(a[2] == b[2]) System.out.println(0); else if(a[2] < b[2]) { b[2] = b[2] - a[2]; a[2] = 0; a[1] = a[1] - b[0]; // System.out.println("Im here"); System.out.println(b[2] - a[1] >0 ? a[1] * (-2) : b[2] * (-2)); } else { a[2] = a[2] - b[2]; b[2] = 0; //System.out.println("HEy there"); System.out.println(a[2] - b[1] >0 ? b[1] *2:a[2] * 2); } } else { b[2] = 0; System.out.println(a[2] - b[1] >0 ? b[1] *2:a[2] * 2); } test--; } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
f7d75d6d7a9c0845cbfe9140cf92832c
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.util.*; import java.io.*; public class B_ { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } private static long readLong() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public static void main(String[] args) throws IOException { int T = readInt(); while (T-- > 0) pw.println(solve()); pw.close(); } private static int solve() throws IOException { int a0 = readInt(); int a1 = readInt(); int a2 = readInt(); int b0 = readInt(); int b1 = readInt(); int b2 = readInt(); int minA2B1 = Math.min(a2, b1); int res = 2 * minA2B1; int total = a0 + a1 + a2; int extra = (a1 + b2) - (total - minA2B1); return res - Math.max(extra * 2, 0); } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
d9f20233dfeac4934264c5d26883ee31
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.util.*; import java.io.*; public class B_TernarySequence { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StringTokenizer st; private static int readInt() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } private static long readLong() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } public static void main(String[] args) throws IOException { int T = readInt(); while (T-- > 0) pw.println(solve()); pw.close(); } private static int solve() throws IOException { int a0 = readInt(); int a1 = readInt(); int a2 = readInt(); int b0 = readInt(); int b1 = readInt(); int b2 = readInt(); int minA2B1 = Math.min(a2, b1); int res = 2 * minA2B1; int total = a0 + a1 + a2 - minA2B1; int extra = (a1 + b2) - total; return res - Math.max(extra * 2, 0); } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
429463376327e7caa6dcb73e274bafc2
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
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(); sc.nextLine(); while(t-- > 0){ int x1, y1, z1; x1 = sc.nextInt(); y1 = sc.nextInt(); z1 = sc.nextInt(); int x2, y2, z2; x2 = sc.nextInt(); y2 = sc.nextInt(); z2 = sc.nextInt(); int sum = Math.min(z1, y2)*2; z1 = z1 > y2 ? z1-y2 : 0; if (x1 + z1 < z2){ sum -= (z2 - x1 - z1)*2; } System.out.println(sum); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
92ceda163b1c5594115186b1f4219ac3
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args) { FastReader reader = new FastReader(); int tests = reader.nextInt(); for(int test = 1; test <= tests; test++) { long zerosA = reader.nextInt(); long onesA = reader.nextInt(); long twosA = reader.nextInt(); long zerosB = reader.nextInt(); long onesB = reader.nextInt(); long twosB = reader.nextInt(); //2 1 long t = Math.min(twosA, onesB); long res = t * 2; twosA -= t; onesB -= t; //2 2 t = Math.min(twosA, twosB); twosA -= t; twosB -= t; //1 1 t = Math.min(onesA, onesB); onesA -= t; onesB -= t; t = Math.min(onesA, zerosB); onesA -= t; zerosB -= t; //0 2 t = Math.min(twosB, zerosA); twosB -= t; zerosA -= t; t = Math.min(twosB, onesA); res -= t * 2; twosB -= t; onesA -= t; System.out.println(res); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
57a8f77d661412eec35ae2671130646a
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1401b { public static void main(String[] args) throws IOException { int t = ri(); while (t --> 0) { int x1 = rni(), y1 = ni(), z1 = ni(), x2 = rni(), y2 = ni(), z2 = ni(); z2 -= min(z2, x1); int d = min(z1, z2); z1 -= d; z2 -= d; prln(2 * min(z1, y2) - 2 * min(y1, z2)); } close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // references // IBIG = 1e9 + 7 // IMAX ~= 2e9 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // math util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int fli(double d) {return (int)d;} static int cei(double d) {return (int)ceil(d);} static long fll(double d) {return (long)d;} static long cel(double d) {return (long)ceil(d);} static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);} static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);} static int lcm(int a, int b) {return a * b / gcf(a, b);} static long lcm(long a, long b) {return a * b / gcf(a, b);} static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;} static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);} // array util static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(double[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static double[] copy(double[] a) {double[] ans = new double[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;} // graph util static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;} static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;} static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);} static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);} static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);} static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);} // input static void r() throws IOException {input = new StringTokenizer(rline());} static int ri() throws IOException {return Integer.parseInt(rline());} static long rl() throws IOException {return Long.parseLong(rline());} static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni(); return a;} static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni() - 1; return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for(int i = 0; i < n; ++i) a[i] = nl(); return a;} static char[] rcha() throws IOException {return rline().toCharArray();} static String rline() throws IOException {return __in.readLine();} static String n() {return input.nextToken();} static int rni() throws IOException {r(); return ni();} static int ni() {return Integer.parseInt(n());} static long rnl() throws IOException {r(); return nl();} static long nl() {return Long.parseLong(n());} static double rnd() throws IOException {r(); return nd();} static double nd() {return Double.parseDouble(n());} static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;} static void rg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);} static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;} static void rdg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);} static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;} static void rsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);} static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;} static void rdsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(double... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();} static void h() {__out.println("hlfd"); flush();} static void flush() {__out.flush();} static void close() {__out.close();} }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
92e7a1a690cce86e7d9450d39a475040
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.util.Scanner; public class Ternarysequence { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-- > 0) { int [] a, b; a = new int[3]; b = new int[3]; for (int i=0; i<3; i++) a[i] = sc.nextInt(); for (int i=0; i<3; i++) b[i] = sc.nextInt(); int sum = 2*Math.min(a[2], b[1]), t = b[1]; b[1] -= Math.min(a[2], b[1]); a[2] -= Math.min(a[2], t); t = b[2]; b[2] -= Math.min(a[2], b[2]); a[2] -= Math.min(a[2], t); t = b[2]; b[2] -= Math.min(a[0], b[2]); a[0] -= Math.min(a[0], t); t = b[1]; b[1] -= Math.min(a[0], b[1]); a[0] -= Math.min(a[0], t); t = b[1]; b[1] -= Math.min(a[1], b[1]); a[1] -= Math.min(a[1], t); sum -= 2*Math.min(b[2], a[1]); System.out.println(sum); } sc.close(); } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
f2beffa9372d517d4edec1dc2e7c812d
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.time.chrono.MinguoChronology; import java.util.*; public class test{ public static void main(String args[]) { Scanner take=new Scanner(System.in); int t=take.nextInt(); int z1,o1,t1,z2,t2,o2,i,j,n,ans,tmp; int a[],b[],c[]; while(t--!=0) { ans=0; z1=take.nextInt(); o1=take.nextInt(); t1=take.nextInt(); z2=take.nextInt(); o2=take.nextInt(); t2=take.nextInt(); tmp=Math.min(t1, o2); ans+=(2*tmp); t1-=tmp; o2-=tmp; tmp=Math.min(o1,o2); o1-=tmp; o2-=tmp; tmp=Math.min(o1,z2); o1-=tmp; z2-=tmp; ans-=(o1*2); System.out.println(ans); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
c1573dc0f938ac8b43f6ec13d0cb0503
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class TernarySequence { static int[] a, b; public static void main(String[] args) throws IOException { InputReader reader = new InputReader(); PrintWriter writer = new PrintWriter(System.out); int t = reader.nextInt(); for (int cases=0; cases<t; cases++) { a = new int[3]; b = new int[3]; for (int i=0; i<3; i++) a[i] = reader.nextInt(); for (int i=0; i<3; i++) b[i] = reader.nextInt(); int ans = 0; ans += exhaust(2,1) * 2; exhaust(0, 2); exhaust(2,2); if (b[2] > 0) ans -= b[2] * 2; writer.println(ans); } writer.close(); } static int exhaust(int aNum, int bNum) { int taken = Math.min(a[aNum], b[bNum]); a[aNum] -= taken; b[bNum] -= taken; return taken; } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader() throws FileNotFoundException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
25ad865ebda0ead736ca2b79a36a10af
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args)throws IOException { Scanner scan=new Scanner(System.in); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //String[] temp=br.readLine().split(" "); // int t=Integer.parseInt(temp[0]); int t=scan.nextInt(); while(t-->0) { long x1=scan.nextLong(); long y1=scan.nextLong(); long z1=scan.nextLong(); long x2=scan.nextLong(); long y2=scan.nextLong(); long z2=scan.nextLong(); if(z2>=x1) { z2=z2-x1; x1=0; }else { x1=x1-z2; z2=0; } long ans=0; if(z1>=z2) { z1=z1-z2; z2=0; }else { z2=z2-z1; z1=0; } if(z1==0) { ans=z2*(-2); }else { ans=Math.min(z1,y2)*2; } System.out.println(ans); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
3fbad9c0051d451b241eb9e6e3712949
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; import java.lang.Math; import java.util.ArrayList; import java.util.List; /* 1 3 20 977 0 5 995 */ public class A { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int TT = 0; TT < t; TT++) { int[] a = fs.readArray(3); int[] b = fs.readArray(3); long plusScore = 2*Math.min(a[2],b[1]); a[2]-=plusScore/2; b[1]-=plusScore/2; long minScore = 2*(b[2]-a[0]-a[2]); if (minScore>0) { System.out.println(plusScore - minScore); } else { System.out.println(plusScore); } } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
fceb88250dc8727d968dbb40b7861ca9
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
/* IMPORTANT: Multiple classes and nested static classes are supported */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class cf { public static void main(String args[] ) throws Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int a0 = sc.nextInt(); int a1 = sc.nextInt(); int a2 = sc.nextInt(); int b0 = sc.nextInt(); int b1 = sc.nextInt(); int b2 = sc.nextInt(); int sum = 0, a = 0, b = 0; sum = sum+(Math.min(a2,b1)*2); a2 = a2 - Math.min(a2,b1); b2 = b2 - (a2 + a0); if(b2>0) System.out.println(sum - (b2*2)); else System.out.println(sum); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
1d9e4f40b72c91b3e246e0676f004a7a
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.util.Scanner; import java.awt.image.BandedSampleModel; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class B { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int cases = sc.nextInt(); for(int i=0;i<cases;i++) { //int n = sc.nextInt(); long arr[][] = new long[2][3]; for(int j=0;j<2;j++) { for(int k=0;k<3;k++) { arr[j][k]=sc.nextLong();} } long ans=0; if(arr[0][2]>arr[1][1]) { ans=ans+2*arr[1][1]; arr[0][2]=arr[0][2]-arr[1][1]; arr[1][1]=0; } else { ans=ans+2*arr[0][2]; arr[1][1]=arr[1][1]-arr[0][2]; arr[0][2]=0; //ans=ans+2*arr[0][2]; } long adder=arr[0][2]+arr[0][0]; long s=0; if(arr[1][2]>adder) { s=2*(arr[1][2]-adder); } ans=ans-s; System.out.println(ans); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
667ff0a18734569c5c0af0be7e80ecdd
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
import java.util.*; import java.io.*; public class Pb{ public static void main(String[] args)throws IOException{ FastReader sc = new FastReader(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out),"ASCII"),512); int t = sc.nextInt(); while(t-- > 0){ int[] a = new int[3]; int[] b = new int[3]; for(int i=0;i<3;i++) a[i] = sc.nextInt(); for(int i=0;i<3;i++) b[i] = sc.nextInt(); long sum = 0l; int ai = 2; int bi = 1; int temp = Math.min(a[2], b[1]); a[2] -= temp; b[1] -= temp; sum += (2*temp); temp = a[0] + a[2]; b[2] -= temp; if(b[2] <= 0) out.write(sum + ""); else{ sum -= (2*b[2]); out.write(sum + ""); } /* while(true){ if(ai == 2){ if(bi == 1){ int min = Math.min(a[ai], b[bi]); sum += (ai*bi*min); a[ai] -= min; b[bi] -= min; if(a[ai] == 0) ai = 1; else bi = 0; }else if(bi == 0){ int min = Math.min(a[ai], b[bi]); a[ai] -= min; b[bi] -= min; if(a[ai] == 0) ai = 1; else bi = 2; }else if(bi == 2){ int min = Math.min(a[ai], b[bi]); a[ai] -= min; b[bi] -= min; if(a[ai] == 0) ai = 1; else break; } }else if(ai == 1){ if(bi == 1){ int min = Math.min(a[ai], b[bi]); a[ai] -= min; b[bi] -= min; if(a[ai] == 0) ai = 0; else bi = 0; }else if(bi == 0){ int min = Math.min(a[ai], b[bi]); a[ai] -= min; b[bi] -= min; if(a[ai] == 0) ai = 0; else bi = 2; }else if(bi == 2){ b[bi] -= a[0]; if(bi <= 0) break; else{ sum -= (b[bi]*2); break; } } }else if(ai == 0) break; } out.write(sum + ""); */ out.write('\n'); } out.flush(); } } 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
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
07bde7eb73a30125125d88da6599d519
train_002.jsonl
1598020500
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i &amp; \mbox{if }a_i &gt; b_i \\ 0 &amp; \mbox{if }a_i = b_i \\ -a_i b_i &amp; \mbox{if }a_i &lt; b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?
256 megabytes
/* Modassir_Ali algo.java */ import java.util.*; import java.io.*; import java.util.Stack ; import java.util.HashSet; import java.util.HashMap; public class B_TernarySequence { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static int[][] nai2(int n,int m)throws IOException{int a[][] = new int[n][m];for(int i=0;i<n;i++)for(int j=0;j<m;j++)a[i][j] = ni();return a;} static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;} static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();} static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits. static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;} static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;} static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a = n*a;}return a;} static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a = n*a;}return a;} static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}} static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}} static void swap(int a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];} static void swap(long a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];} static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;} static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;} static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;} static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;} static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;} static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);} static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;} static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;} static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;} static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;} static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;} static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;} static void close(){out.flush();} /*-------------------------Main Code Starts(algo.java)----------------------------------*/ public static void main(String[] args) throws IOException { long t = nl() ; while(t--!=0){ long sum = 0L ; long x1 = nl() ; long y1 = nl() ; long z1 = nl() ; long x2 = nl() ; long y2 = nl() ; long z2 = nl() ; if(z1<=y2){ sum = sum + z1*2 ; y2 = y2-z1 ; z1 = 0 ; } else if(z1>y2){ sum = sum + y2*2 ; z1 = z1-y2 ; y2 = 0 ; } // if(x1>=z2){ // x1 = x1-z2 ; // z2 = 0 ; // } // if(x1<z2){ // z2 = z2-x1 ; // x1 = 0 ; // } // sum = sum -z2*2 ; // System.out.println(sum); long total1 = x2 + y2 ; long total2 = x1 + z1 ; if(z2>=total2){ z2 = z2 - total2 ; } else if(z2<total2){ z2 = 0 ; } if(y1>=total1){ y1 = y1 - total1 ; } else if(y1<total1){ y1 = 0 ; } sum = sum - Math.min(y1,z2)*2 ; System.out.println(sum); } } }
Java
["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"]
2 seconds
["4\n2\n0"]
NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$
Java 11
standard input
[ "constructive algorithms", "greedy", "math" ]
e1de1e92fac8a6db3222c0d3c26843d8
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 &gt; 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.
1,100
For each test case, print the maximum possible sum of the sequence $$$c$$$.
standard output
PASSED
677994e5e2755b237dc2d7e4f32357cc
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Kraken7 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC2 solver = new TaskC2(); solver.solve(1, in, out); out.close(); } static class TaskC2 { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[][] p = new int[n][4]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { p[i][j] = in.nextInt(); } p[i][3] = i + 1; } Arrays.sort(p, (o1, o2) -> { for (int i = 0; i < 3; i++) { if (o1[i] != o2[i]) return o1[i] - o2[i]; } return 0; }); // for (int[] i : p) out.println(Arrays.toString(i)); boolean[] fine = new boolean[n]; Arrays.fill(fine, true); for (int i = 0; i < n; i++) { if (fine[i]) { int j = i + 1; while (!fine[j]) j++; int xis = 0; while (p[i][xis] == p[j][xis]) xis++; int k = j; while (k < n && p[k][xis] == p[j][xis]) k++; int bs = Integer.MAX_VALUE; int bi = -1; for (int x = j; x < n; x++) { if (fine[x]) { int s = 0; for (int u = 0; u < 3; u++) s += Math.abs(p[i][u] - p[x][u]); if (s < bs) { bs = s; bi = x; } } } fine[i] = false; fine[bi] = false; out.printf("%d %d\n", p[i][3], p[bi][3]); } } } } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
379450ea5478fa49ff680752a8badce1
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Objects; import java.util.Random; import java.util.StringTokenizer; public class Solution{ static long inf = (long) 1e18; static boolean[] completed; public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { int n = fs.nextInt(); Point[] arr = new Point[n]; for(int i=0;i<n;i++) { long x = fs.nextLong(), y = fs.nextLong(), z = fs.nextLong(); arr[i] = new Point(x,y,z); } completed = new boolean[n]; for(int i=0;i<n;i++) { if(completed[i]) continue; long min = inf; int ind=-1; for(int j=i+1;j<n;j++) { if(!completed[j]) { long dist = (arr[j].x-arr[i].x)*(arr[j].x-arr[i].x) + (arr[j].y-arr[i].y)*(arr[j].y-arr[i].y) + (arr[j].z-arr[i].z)*(arr[j].z-arr[i].z); if(dist<min) { min = dist; ind = j; } } } out.println((i+1)+" "+(ind+1)); completed[i] = completed[ind] = true; } } out.close(); } static class Point{ long x,y,z; Point(long x,long y , long z){ this.x = x; this.y = y; this.z = z; } } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
750c9489ac295d63a193c5a523544a53
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int N=s.nextInt(); point[] rem=new point[N]; Map<point,Integer> index=new HashMap(); for(int i=0;i<N;i++){ rem[i]=new point(s.nextInt(),s.nextInt(),s.nextInt()); index.put(rem[i],i+1); } boolean[] used=new boolean[N]; for (int i=0;i<N;i++){ if(used[i])continue; int help=1000000000; int ind=-1; for(int j=i+1;j<N;j++){ if(used[j])continue; if(help>rem[i].help(rem[j])) { ind=j; help = rem[i].help(rem[j]); } } used[i]=true; used[ind]=true; System.out.println((i+1)+" "+(ind+1)); } } static class point { int x,y,z; public point(int x,int y,int z){ this.x=x; this.y=y; this.z=z; } public int help(point o){ return Math.abs(x-o.x)+Math.abs(y-o.y)+Math.abs(z-o.z); } } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
c7503c5ea74e0ccd64f2644933b39126
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Objects; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); C2BalancedRemovalsHarder solver = new C2BalancedRemovalsHarder(); solver.solve(1, in, out); out.close(); } static class C2BalancedRemovalsHarder { private int n; private int[][] arr; private boolean[] flag; private static final int D = 3; private ArrayList<Util.Pair<Integer>> ans = new ArrayList<>(); private void function(int from, int to, int dim) { if (dim == 1) { for (int i = from; i + 1 <= to; i += 2) { ans.add(new Util.Pair<>(arr[i][0], arr[i + 1][0])); flag[arr[i][0]] = flag[arr[i + 1][0]] = true; } return; } int num = arr[from][dim]; int start = from, end = from; for (int i = from; i <= to; i++) { if (num == arr[i][dim]) end = i; else { function(start, end, dim - 1); num = arr[i][dim]; start = i; end = i; } } function(start, end, dim - 1); ArrayList<Integer> temp = new ArrayList<>(); for (int i = from; i <= to; i++) { if (!flag[arr[i][0]]) temp.add(arr[i][0]); } for (int i = 0; i + 1 < temp.size(); i += 2) { ans.add(new Util.Pair<>(temp.get(i), temp.get(i + 1))); flag[temp.get(i)] = flag[temp.get(i + 1)] = true; } } public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); flag = new boolean[n + 1]; arr = new int[n][D + 1]; for (int i = 0; i < n; i++) { arr[i][0] = i + 1; for (int j = 1; j <= D; j++) arr[i][j] = in.nextInt(); } Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { for (int j = D; j >= 1; j--) if (o1[j] != o2[j]) return o1[j] - o2[j]; return 0; } }); // for (int i=0; i<arr.length; i++) out.println(Arrays.toString(arr[i])); function(0, n - 1, D); for (int i = 0; i < ans.size(); i++) { out.printsc(ans.get(i).x, ans.get(i).y); out.println(); } out.flush(); } } static class Util { public static class Pair<T> { public T x; public T y; public Pair(T x, T y) { this.x = x; this.y = y; } public boolean equals(Object obj) { if (!(obj instanceof Util.Pair)) return false; Util.Pair<T> pair = (Util.Pair<T>) obj; return this.x.equals(pair.x) && this.y.equals(pair.y); } public String toString() { return ("(" + this.x + "," + this.y + ")"); } public int hashCode() { return Objects.hash(x, y); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public InputReader(FileInputStream file) { this.stream = file; } 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 = (res << 3) + (res << 1) + c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } static class OutputWriter { private final PrintWriter writer; private ArrayList<String> res = new ArrayList<>(); private StringBuilder sb = new StringBuilder(""); public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void println(Object... objects) { for (int i = 0; i < objects.length; i++) { sb.append(objects[i]); } res.add(sb.toString()); sb = new StringBuilder(""); } public void printsc(Object... objects) { for (int i = 0; i < objects.length; i++) { sb.append(objects[i]).append(' '); } } public void close() { writer.close(); } public void flush() { for (String str : res) writer.printf("%s\n", str); res.clear(); sb = new StringBuilder(""); } } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
f84d6936243cc1a4913377e07584940b
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.util.*; import java.io.*; import java.math.*; public class C1 { static class Coord{ int x, y, z; public Coord(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } } public static void process(int test_number)throws IOException { int n = ni(), idx = 0; boolean taken[] = new boolean[n]; Coord arr[] = new Coord[n]; for(int i = 0; i < n; i++){ int x = ni(), y = ni(), z = ni(); arr[i] = new Coord(x, y, z); } long d, dist; for(int i = 0; i < n; i++){ if(taken[i]) continue; d = oo; for(int j = i + 1; j < n; j++){ if(taken[j]) continue; dist = Math.abs(arr[i].x - arr[j].x) + Math.abs(arr[i].y - arr[j].y) + Math.abs(arr[i].z - arr[j].z); if(dist < d){ idx = j; d = dist; } } taken[idx] = true; taken[i] = true; pn((i+1)+" "+(idx+1)); } } static final long oo = (long) 1e12; static final long mod = (long)1e9+7l; static FastReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc = new FastReader(); long s = System.currentTimeMillis(); int t = 1; //t = ni(); for(int i = 1; i <= t; i++) process(i); out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); } static void trace(Object... o){ System.err.println(Arrays.deepToString(o)); }; static void pn(Object o){ out.println(o); } static void p(Object o){ out.print(o); } static int ni()throws IOException{ return Integer.parseInt(sc.next()); } static long nl()throws IOException{ return Long.parseLong(sc.next()); } static double nd()throws IOException{ return Double.parseDouble(sc.next()); } static String nln()throws IOException{ return sc.nextLine(); } static long gcd(long a, long b)throws IOException{ return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{ return (b==0)?a:gcd(b,a%b); } static int bit(long n)throws IOException{ return (n==0)?0:(1+bit(n&(n-1))); } static class 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
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
382aeb44f0a1e49fa234a6cb216cd6d5
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Comparator; import java.util.TreeMap; public class d { static BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); // static Scanner s=new Scanner(System.in); public static void main(String[] args) throws IOException { StringBuilder sb = new StringBuilder(); String[] s1=s(); int n=i(s1[0]); TreeMap<Integer,Integer> t1=new TreeMap<>(); TreeMap<Integer,TreeMap<Integer,Integer>> t2=new TreeMap<>(); TreeMap<Integer,TreeMap<Integer,TreeMap<Integer,Integer>>> t3=new TreeMap<>(); int[] X=new int[n];int[] Y=new int[n];int[] Z=new int[n]; for(int i=0;i<n;i++){ String[] s2=s(); int x=i(s2[0]);int y=i(s2[1]);int z=i(s2[2]); X[i]=x;Y[i]=y;Z[i]=z; t1.put(x,i); TreeMap<Integer,Integer> tt=t2.getOrDefault(x,new TreeMap<>()); tt.put(y,i); t2.put(x,tt); TreeMap<Integer,TreeMap<Integer,Integer>> ttt=t3.getOrDefault(x,new TreeMap<>()); TreeMap<Integer,Integer> t4=ttt.getOrDefault(y,new TreeMap<>()); t4.put(z,i); ttt.put(y,t4); t3.put(x,ttt); }int[] vis=new int[n]; for(int i=0;i<n;i++){ if(vis[i]==0){ vis[i]=1; // System.out.println(i); // System.out.println(t1.containsKey(2)); t3.get(X[i]).get(Y[i]).remove(Z[i]); if(t3.get(X[i]).get(Y[i]).size()==0){ // System.out.println(i); t2.get(X[i]).remove(Y[i]);} if(t2.get(X[i]).size()==0) t1.remove(X[i]); // System.out.println(t1.containsKey(2)); if(t1.ceilingKey((int)X[i])!=null){ int val=t1.ceilingKey((int)X[i]); // System.out.println("HI"+val+" "+i); if(t2.get(val).ceilingKey((int)Y[i])!=null){ int val1=t2.get(val).ceilingKey((int)Y[i]); if(t3.get(val).get(val1).ceilingKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).ceilingKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } else if(t3.get(val).get(val1).floorKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).floorKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); // t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } } else{ int val1=t2.get(val).floorKey((int)Y[i]); if(t3.get(val).get(val1).ceilingKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).ceilingKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } else if(t3.get(val).get(val1).floorKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).floorKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } } } else{ // System.out.println(t1.floorKey(X[i])); int val=t1.floorKey((int)X[i]); // System.out.println("HI"+i+" "+val); if(t2.get(val).ceilingKey((int)Y[i])!=null){ int val1=t2.get(val).ceilingKey((int)Y[i]); /* if(i==5){ System.out.println(val1); } */ if(t3.get(val).get(val1).ceilingKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).ceilingKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } else if(t3.get(val).get(val1).floorKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).floorKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } } else{ int val1=t2.get(val).floorKey((int)Y[i]); if(t3.get(val).get(val1).ceilingKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).ceilingKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } else if(t3.get(val).get(val1).floorKey((int)Z[i])!=null){ int val2=t3.get(val).get(val1).floorKey((int)Z[i]); vis[t3.get(val).get(val1).get(val2)]=1; sb.append((i+1)+" "+(t3.get(val).get(val1).get(val2)+1)+"\n"); t3.get(val).get(val1).remove(val2); if(t3.get(val).get(val1).size()==0){ t2.get(val).remove(val1); } if(t2.get(val).size()==0){ t1.remove(val); } } } } } } System.out.println(sb.toString()); } static int MAXN; static int[] spf; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); } static int i(String ss) { return Integer.parseInt(ss); } static long l(String ss) { return Long.parseLong(ss); } } class Student12 { int l;int r; public Student12(int l, int r) { this.l = l; this.r = r; } public String toString() { return this.l+" "; } } class Sortbyroll12 implements Comparator<Student12> { public int compare(Student12 a, Student12 b){ /* if(b.r<a.r) return -1; else if(b.r==a.r) return 0; return 1;*/ if(b.r-b.l>a.r-a.l) return 1; else if(b.r-b.l==a.r-a.l) { return a.l-b.l; }else return -1;/* if(b.l<a.l) return -1; else if(b.l==a.l) { return b.r-a.r; } return 1;*/ // return b.r- a.r; // return (int) a.l-(int) b.l; /* if(a.r<b.r) return -1; else if(a.r==b.r){ if(a.r==b.r){ return 0; } if(a.r<b.r) return -1; return 1;} return 1; */} }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
8ee89b552ade03885115cd1a65c48d5c
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); int i = 0; int t = 1; // t = in.nextInt(); for (; i < t; i++) solver.solve(i, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[][] ar = new int[n][4]; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { ar[i][j] = in.nextInt(); } ar[i][3] = i; } //out.println(Arrays.deepToString(ar)); MergeSort(ar , 0 , ar.length - 1); //out.println(Arrays.deepToString(ar)); int[][] ans = new int[n/2][2]; int pa = 0; boolean[] check = new boolean[n]; int ptr1 = 0; int ptr2 = 1; while(ptr2 < n) { //System.out.println(pa); //System.out.println(Arrays.deepToString(ans)); if(!check[ar[ptr2][3]] && !check[ar[ptr1][3]] && ar[ptr1][0] == ar[ptr2][0] && ar[ptr1][1] == ar[ptr2][1]) { ans[pa][0] = ar[ptr1][3]; ans[pa][1] = ar[ptr2][3]; check[ar[ptr1][3]] = true; check[ar[ptr2][3]] = true; pa++; } ptr1 = ptr2; ptr2++; while(ptr2 < n && check[ar[ptr2][3]]) ptr2++; } ptr1 = 0; while(ptr1 < n && check[ar[ptr1][3]]) ptr1++; ptr2 = ptr1 + 1; while(ptr2 < n && check[ar[ptr2][3]]) ptr2++; while(ptr2 < n) { if(!check[ar[ptr2][3]] && !check[ar[ptr1][3]] && ar[ptr1][0] == ar[ptr2][0]) { ans[pa][0] = ar[ptr1][3]; ans[pa][1] = ar[ptr2][3]; check[ar[ptr1][3]] = true; check[ar[ptr2][3]] = true; pa++; } ptr1 = ptr2; ptr2++; while(ptr2 < n && check[ar[ptr2][3]]) ptr2++; } ptr1 = 0; while(ptr1 < n && check[ar[ptr1][3]]) ptr1++; ptr2 = ptr1 + 1; while(ptr2 < n && check[ar[ptr2][3]]) ptr2++; while(ptr2 < n) { if(!check[ar[ptr2][3]] && !check[ar[ptr1][3]]) { ans[pa][0] = ar[ptr1][3]; ans[pa][1] = ar[ptr2][3]; check[ar[ptr1][3]] = true; check[ar[ptr2][3]] = true; pa++; } ptr1 = ptr2; ptr2++; while(ptr2 < n && check[ar[ptr2][3]]) ptr2++; } //out.println(Arrays.deepToString(ans)); for(int i = 0; i < n/2; i++) { out.println((ans[i][0]+1) + " " + (ans[i][1]+1)); } } static void MergeSort(int[][] ar, int l, int r) { //System.out.println("entering " + "l: " + l + " r: " + r); //System.out.println(Arrays.deepToString(ar)); if (r > l) { int middle = (l + r) / 2; MergeSort(ar , l , middle); MergeSort(ar , middle + 1 , r); Merge(ar , l , middle , r); } // /* // * System.out.println("exiting " + "l: " + l + " r: " + r); // * System.out.println((Arrays.deepToString(ar))); // */ } static void Merge(int[][] ar, int l, int m, int r) { int n1 = m-l+1; int n2 = r-m; int[][] L = new int[n1][4]; int[][] R = new int[n2][4]; for(int i = 0; i < n1; i++) { for(int j = 0; j < 4; j++) { L[i][j] = ar[i+l][j]; } } for(int i = 0; i < n2; i++) { for(int j = 0; j < 4; j++) { R[i][j] = ar[i+m+1][j]; } } int k = l; int ptr1 = 0, ptr2 = 0; while(ptr1 < n1 && ptr2 < n2) { if(compare(L[ptr1] , R[ptr2])) { for(int i = 0; i < 4; i++) { ar[k][i] = R[ptr2][i]; } ptr2++; k++; }else { for(int i = 0; i < 4; i++) { ar[k][i] = L[ptr1][i]; } ptr1++; k++; } } while(ptr1 < n1) { for(int i = 0; i < 4; i++) { ar[k][i] = L[ptr1][i]; } ptr1++; k++; } while(ptr2 < n2) { for(int i = 0; i < 4; i++) { ar[k][i] = R[ptr2][i]; } ptr2++; k++; } } static boolean compare(int[] ar1 , int[] ar2) { if(ar1[0] > ar2[0]) { return true; }else if(ar1[0] == ar2[0] && ar1[1] > ar2[1]) { return true; }else if(ar1[0] == ar2[0] && ar1[1] == ar2[1] && ar1[2] > ar2[2]) { return true; }else { return false; } } } // template code static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static long modexp(long a, long b, long p) { // returns a to the power b mod p by modular exponentiation long res = 1; long mult = a % p; while (b > 0) { if (b % 2 == 1) { res = (res * mult) % p; } b /= 2; mult = (mult * mult) % p; } return res; } static double log(double arg, double base) { // returns log of a base b, contains floating point errors, dont use for exact // calculations. if (base < 0 || base == 1) { throw new ArithmeticException("base cant be 1 or negative"); } if (arg < 0) { throw new ArithmeticException("log of negative number undefined"); } return Math.log10(arg) / Math.log10(base); } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
4686994ba5ddab20887bed717a1c4c0e
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.io.*; import java.util.*; public class C { static class Point { long x, y, z; Point(int xx, int yy, int zz) { x = xx; y = yy; z = zz; } } static long dist(Point a, Point b) { return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z); } static void solve(FastIO io) { int n = io.nextInt(); // int[] a = new int[n]; Point[] arr = new Point[n]; for (int i = 0; i < n; i++) { arr[i] = new Point(io.nextInt(), io.nextInt(), io.nextInt()); } boolean[] deleted = new boolean[n]; for (int i = 0; i < n; i++) { if (deleted[i]) continue; long dist = Long.MAX_VALUE; int p = -1; for (int j = 0; j < n; j++) { if (i == j || deleted[j]) continue; long temp = dist(arr[i], arr[j]); if (temp < dist) { dist = temp; p = j; } } io.println((i + 1) + " " + (p + 1)); deleted[i] = deleted[p] = true; } } public static void main(String[] args) { FastIO io = new FastIO(); solve(io); io.close(); } } class FastIO extends PrintWriter { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); FastIO() { super(System.out); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(r.readLine()); } catch (Exception e) { //TODO: handle exception } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
0b8cf353caf3495c6dee79584cfbcc4c
train_002.jsonl
1571236500
This is an easier version of the problem. In this version, $$$n \le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
512 megabytes
import java.io.*; import java.util.*; public class Main { static FastReader in; static PrintWriter out; static class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = j; } } static class Point { int i, x, y, z; Point(int i, int x, int y, int z) { this.i = i; this.x = x; this.y = y; this.z = z; } } static void solve() { int n = in.nextInt(); ArrayList<Point> points = new ArrayList<>(); for (int i = 0; i < n; i++) points.add(new Point(i, in.nextInt(), in.nextInt(), in.nextInt())); points.sort(new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { if (o1.x == o2.x) { if (o1.y == o2.y) { return o1.z - o2.z; } return o1.y - o2.y; } return o1.x - o2.x; } }); ArrayList<Pair> ans = new ArrayList<>(); ArrayList<Integer> lx = new ArrayList<>(); for (int i = 0; i < n; i++) { int ind = i; while (ind < n && points.get(ind).x == points.get(i).x) ind++; ArrayList<Integer> ly = new ArrayList<>(); for (int j = i; j < ind; j++) if (j + 1 < ind && points.get(j).y == points.get(j + 1).y) { ans.add(new Pair(points.get(j).i, points.get(j + 1).i)); j++; } else ly.add(points.get(j).i); for (int j = 0; j < ly.size() - 1; j += 2) ans.add(new Pair(ly.get(j), ly.get(j + 1))); if ((ind - i) % 2 == 1) lx.add(ly.get(ly.size() - 1)); i = ind - 1; } for (int i = 0; i < lx.size(); i += 2) { ans.add(new Pair(lx.get(i), lx.get(i + 1))); } for (Pair p : ans) { out.println((p.i + 1) + " " + (p.j + 1)); } } public static void main(String[] args) { in = new FastReader(System.in); out = new PrintWriter(System.out); int q = 1; while (q-- > 0) solve(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } Integer nextInt() { return Integer.parseInt(next()); } Long nextLong() { return Long.parseLong(next()); } Double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } }
Java
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
1 second
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Java 11
standard input
[ "constructive algorithms", "geometry", "greedy" ]
47a9d72651f1407de89e28fb4b142367
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
1,700
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
standard output
PASSED
f80e544ff309d57b32694dd5563d8e2c
train_002.jsonl
1359732600
Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.
256 megabytes
import java.util.Scanner; public class D { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int m = s.nextInt(); int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = s.nextInt(); s.nextDouble(); } int[] f = new int[n + 1]; for (int mi = 1; mi <= m; mi++) { int[] nf = new int[n + 1]; int cnt = 0; for (int ni = 1; ni <= n; ni++) { if (sa[ni - 1] == mi) { cnt++; } if (sa[ni - 1] < mi) { nf[ni] = Math.min(nf[ni - 1] + 1, f[ni] + cnt); } else { nf[ni] = nf[ni - 1]; } } f = nf; } System.out.println(f[n]); } }
Java
["3 2\n2 1\n1 2.0\n1 3.100", "3 3\n1 5.0\n2 5.5\n3 6.0", "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125"]
2 seconds
["1", "0", "2"]
NoteIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.In the second test case, the species are already in the correct order, so no replanting is needed.
Java 7
standard input
[ "dp" ]
32f245fa1a2d99bfabd30b558687ca5f
The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi &lt; xi + 1, 1 ≤ i &lt; n).
1,700
Output a single integer — the minimum number of plants to be replanted.
standard output
PASSED
21bbc82dff34ef86d8477517c429d943
train_002.jsonl
1359732600
Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author George Marcus */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); in.nextInt(); int[] s = new int[N]; for(int i = 0; i < N; i++) { s[i] = in.nextInt() - 1; in.nextDouble(); } int[] dp = new int[N]; int max = 0; for(int i = 0; i < N; i++) { dp[i] = 1; for(int j = 0; j < i; j++) if(s[j] <= s[i]) dp[i] = Math.max(dp[i], dp[j] + 1); max = Math.max(max, dp[i]); } out.println(N - max); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(nextString()); } public double nextDouble() { return Double.parseDouble(nextString()); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
Java
["3 2\n2 1\n1 2.0\n1 3.100", "3 3\n1 5.0\n2 5.5\n3 6.0", "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125"]
2 seconds
["1", "0", "2"]
NoteIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.In the second test case, the species are already in the correct order, so no replanting is needed.
Java 7
standard input
[ "dp" ]
32f245fa1a2d99bfabd30b558687ca5f
The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi &lt; xi + 1, 1 ≤ i &lt; n).
1,700
Output a single integer — the minimum number of plants to be replanted.
standard output
PASSED
70e8a0553773da280fafa67375dc3386
train_002.jsonl
1359732600
Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ new Main().run(); } public void run(){ Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int n = Integer.parseInt(line.split(" ")[0]); int m = Integer.parseInt(line.split(" ")[1]); int[] types = new int[n]; for(int i = 0; i < n; i++) { line = scanner.nextLine(); types[i] = Integer.parseInt(line.split(" ")[0]) - 1; //System.out.println(types[i]); } //System.out.println(line); int[] dp = new int[m]; Arrays.fill(dp, 0); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(types[i] == j) dp[j]++; if(j < m - 1) dp[j + 1] = Math.max(dp[j], dp[j + 1]); } } System.out.println(n - dp[m - 1]); } }
Java
["3 2\n2 1\n1 2.0\n1 3.100", "3 3\n1 5.0\n2 5.5\n3 6.0", "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125"]
2 seconds
["1", "0", "2"]
NoteIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.In the second test case, the species are already in the correct order, so no replanting is needed.
Java 7
standard input
[ "dp" ]
32f245fa1a2d99bfabd30b558687ca5f
The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi &lt; xi + 1, 1 ≤ i &lt; n).
1,700
Output a single integer — the minimum number of plants to be replanted.
standard output
PASSED
fde1a2ac36a033da566a1b9740ff372b
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.io.*; public class Main { static 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; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { int a=input.scanInt(); int b=input.scanInt(); if(a>b) { int tmp=a; a=b; b=tmp; } int c=input.scanInt(); int r=input.scanInt(); int lft=Math.max(a, c-r); int rgt=Math.min(b,c+r); if(lft<=rgt) { ans.append((b-a)-(rgt-lft)); } else { ans.append((b-a)); } ans.append("\n"); } System.out.println(ans); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
d3b4b27708a76937c87dc3f4bf91b8a6
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class Solution { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), r = sc.nextInt(); int left = Math.max(Math.min(a, b), c - r); int right = Math.min(Math.max(a, b), c + r); System.out.println(Math.max(a, b) - Math.min(a, b) - Math.max(0, right - left)); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
1bae3ebf0671838d8a123f47ef2a7510
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static int mod = (int)(Math.pow(10, 9) + 7); public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t= sc.nextInt(); while (t-->0) { int a = sc.nextInt(); int b = sc.nextInt(); if (b < a){ int temp = a; a = b; b = temp; } int c = sc.nextInt(); int r = sc.nextInt(); int sol = Integer.max(c-r-a, 0) + Integer.max(b-c-r, 0); if (a < c-r && b < c-r) { out.println(b-a); } else if (a > c+r && b > c+r) out.println(b-a); else out.println(sol); } out.close(); } static long pow(long a, long N) { if (N == 0) return 1; else if (N == 1) return a; else { long R = pow(a,N/2); if (N % 2 == 0) { return R*R; } else { return R*R*a; } } } static long powMod(long a, long N) { if (N == 0) return 1; else if (N == 1) return a % mod; else { long R = powMod(a,N/2) % mod; R *= R % mod; if (N % 2 == 1) { R *= a % mod; } return R % mod; } } static void mergeSort(int[] A){ // low to hi sort, single array only int n = A.length; if (n < 2) return; int[] l = new int[n/2]; int[] r = new int[n - n/2]; for (int i = 0; i < n/2; i++){ l[i] = A[i]; } for (int j = n/2; j < n; j++){ r[j-n/2] = A[j]; } mergeSort(l); mergeSort(r); merge(l, r, A); } static void merge(int[] l, int[] r, int[] a){ int i = 0, j = 0, k = 0; while (i < l.length && j < r.length && k < a.length){ if (l[i] < r[j]){ a[k] = l[i]; i++; } else{ a[k] = r[j]; j++; } k++; } while (i < l.length){ a[k] = l[i]; i++; k++; } while (j < r.length){ a[k] = r[j]; j++; k++; } } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int 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
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
e35aac18fa348407e555b222828424ca
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; import java.util.Map.Entry; import javax.swing.text.InternationalFormatter; import java.math.*; public class temp1 { public static PrintWriter out; static int[] a; static int[] b; public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t-- != 0) { int a = scn.nextInt(), b = scn.nextInt(), c = scn.nextInt(), r = scn.nextInt(); int min = Math.min(a, b); int max = Math.max(a, b); a = min; b = max; long ans = b - a ; if (c < a) { if (c + r >= a) ans -= c + r - a ; } else if (c > b) { if (c - r <= b) ans -= b - (c - r) ; } else { int l = Math.max(a, c - r); int u = Math.min(b, c + r); ans -= u - l ; } System.out.println(ans<0?0:ans); } } // _________________________TEMPLATE_____________________________________________________________ // private static int gcd(int a, int b) { // if(a== 0) // return b; // // return gcd(b%a,a); // } // static class comp implements Comparator<pair> { // // @Override // public int compare(pair o1, pair o2) { // return o2.b - o1.b; // } // // } static class pair implements Comparable<pair> { int min; int max; int ans; pair(int a, int b) { min = a; max = b; } @Override public int compareTo(pair o) { return (int) (this.min - o.max); } } public 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[100000 + 1]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[][] nextInt2DArray(int m, int n) throws IOException { int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
9e170b518f2344f3ef2dffd995a9656c
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int t =scanner.nextInt(); while (t-->0){ int x =scanner.nextInt(); int y=scanner.nextInt(); int l =scanner.nextInt(); int r =scanner.nextInt(); if(y<x){ int tt =y; y=x; x=tt; } int a = l-r; int b = l+r; if(a>y) { System.out.println(y-x); continue; } if(x>b){ System.out.println(y-x); continue; } System.out.println(max(0,a-x)+max(0,y-max(b,x))); } } static int max(int a, int b){ if(a>b)return a; return b; } static int min(int a , int b){ if(a>b)return b; return a; } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
cef6a818fd2e424a2493f06d11466d1d
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.Scanner; public class TemporarilyUnavailable { public static void main(String [] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int i = 0; i < t; i++) { long a = input.nextLong(), b = input.nextLong(); long c = input.nextLong(), r = input.nextLong(); long leftSpan = c - r, rightSpan = c + r; long max = Math.max(a, b), min = Math.min(a, b); long ans = 0; if(max < leftSpan || min > rightSpan) ans = max - min; else { ans = Math.max(0, max - rightSpan); ans += Math.max(0, leftSpan - min); } System.out.println(ans); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
af7d91130183fb27ab0d9251096efffc
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.io.*; public class a { public static void main(String[] Args) throws Exception { FS sc = new FS(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = sc.nextInt(); while (t-->0) { int a = sc.nextInt(); int b = sc.nextInt(); if (a > b) { a^=b; b^=a; a^=b; } int c = sc.nextInt(); int r = sc.nextInt(); int c1 = c - r; int c2 = c + r; int ans = 0; if (a < c1) ans += c1 - a; if (b > c2) ans += b - c2; if (b < c1) ans += b - c1; if (a > c2) ans += c2 - a; out.println(ans); } out.close(); } public static class FS { BufferedReader br; StringTokenizer st; FS(InputStream in) throws Exception { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(br.readLine()); } String next() throws Exception { if (st.hasMoreTokens()) return st.nextToken(); st = new StringTokenizer(br.readLine()); return next(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
6e7be795fa629e678704be5f9202a78a
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class A { public static void main(String[] args){ FastInput in = new FastInput(); PrintWriter out = new PrintWriter(System.out); int test = in.nextInt(); while (test-- > 0) { int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(), r = in.nextInt(); if (a > b) { a ^= b; b ^= a; a ^= b; } int towerMin = c - r, towerMax = c + r; int travelTime = b - a; if (towerMin <= a && towerMax >=b) out.println(0); else if (towerMax < a || towerMin > b) out.println(travelTime); else if (towerMin >= a && towerMax <= b) out.println(travelTime - 2 * r); else if (towerMin >= a) out.println(travelTime - b + towerMin); else out.println(travelTime + a - towerMax); } out.flush(); out.close(); in.close(); } static class FastInput { private BufferedReader br; private StringTokenizer tk; FastInput() { br = new BufferedReader(new InputStreamReader(System.in)); tk = new StringTokenizer(""); } String nextLine() { try { return br.readLine(); } catch (IOException ignored) { } return nextLine(); } String next() { if (hasNext()) return tk.nextToken(); else tk = new StringTokenizer(nextLine()); return next(); } boolean hasNext() { return tk.hasMoreTokens(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException ignored) { } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
398c8de30e37911e5e33ab019a225593
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class solution { 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(); int b=sc.nextInt(); int c=sc.nextInt(); int r=sc.nextInt(); int ans=0; if(r==0) { ans=Math.abs(a-b); } else if(a<=b) { if(c-r>=b) { ans=0; } else if(c-r<=a) { if(c+r<=a) { ans=0; } else if(c+r<=b) { ans=c+r-a; } else { ans=b-a; } } else { if(c+r<=b) { ans=2*r; } else { ans=b-c+r; } } ans=b-a-ans; } else { if(c-r>=a) { ans=0; } else if(c-r<=b) { if(c+r<=b) { ans=0; } else if(c+r<=a) { ans=c+r-b; } else { ans=a-b; } } else { if(c+r<=a) { ans=c+r-c+r; } else { ans=a-c+r; } } ans=a-b-ans; } System.out.println(ans); } sc.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
7a1064e6be227772b735ec21f09ac9dc
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public final class Main { public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ int a = s.nextInt(); int b = s.nextInt(); int c = s.nextInt(); int r = s.nextInt(); int left = Math.min(a,b); int right= Math.max(a,b); if(c+r>=right){ if(c-r<=left){ System.out.println(0); } else if(c-r>right){ System.out.println(right-left); } else{ System.out.println(c-r-left); } } else if(c-r<=left){ if(c+r>=left && c+r<right){ System.out.println(right-(c+r)); } else if(c+r<left){ System.out.println(right-left); } else{ System.out.println((c-r-left)+(right-(c+r))); } } else if(c+r< right && c-r>left){ System.out.println((c-r-left)+(right-(c+r))); } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
6b79e7a3f4b92ada0e44b5895ffed50d
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; public class A_1282 { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { int testCases = Integer.parseInt(br.readLine()); for (int i = 0; i < testCases; i++) { String inp = br.readLine(); String[] values = inp.split(" "); int a = Integer.parseInt(values[0]); int b = Integer.parseInt(values[1]); int c = Integer.parseInt(values[2]); int r = Integer.parseInt(values[3]); int lowerCoverage = c - r; int upperCoverage = c + r; int startPoint = Math.min(a, b); int endPoint = Math.max(a, b); int outOfCoverageMinutes = 0; if (upperCoverage <= startPoint || lowerCoverage >= endPoint){ outOfCoverageMinutes = Math.abs(startPoint - endPoint); } else if (lowerCoverage >= startPoint && upperCoverage <= endPoint){ outOfCoverageMinutes = Math.abs(lowerCoverage - startPoint) + Math.abs(endPoint - upperCoverage); } else if (lowerCoverage <= startPoint && upperCoverage < endPoint){ outOfCoverageMinutes = Math.abs(endPoint - upperCoverage); } else if (lowerCoverage > startPoint && upperCoverage >= endPoint){ outOfCoverageMinutes = Math.abs(lowerCoverage - startPoint); } System.out.println(outOfCoverageMinutes); } } catch (IOException e) { e.printStackTrace(); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
3fded93f59d4259a7433f1b4d14ecbfa
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class temporary { 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(); int b = sc.nextInt(); int c = sc.nextInt(); int r = sc.nextInt(); int mi = Math.min(a,b); int ma = Math.max(a,b); if(c>=ma) { if(c-r>ma) { System.out.println(Math.abs(ma-mi)); continue; } else { int min = Math.max(mi,c-r); System.out.println(Math.abs(min - mi)); continue; } } else if(c<=mi) { if(c+r<mi) { System.out.println(Math.abs(ma-mi)); continue; } else { int max = Math.min(ma,c+r); System.out.println(Math.abs(ma-max)); continue; } } else { int x = Math.max(mi,c-r); int y = Math.min(ma,c+r); System.out.println(Math.abs(x-mi) + Math.abs(ma-y)); continue; } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
fb32578d1f61de9d369557a4e8514f3e
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.PrintWriter; import java.util.Scanner; public class TemporarilyUnavailableA extends PrintWriter { //this trick improves performances TemporarilyUnavailableA() { super(System.out); } public static void main(String[] $) { TemporarilyUnavailableA o = new TemporarilyUnavailableA(); o.main(); o.flush(); } void main() { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); //use this if just a single test //int count = 1; while (count-- > 0) { final int a = sc.nextInt(); final int b = sc.nextInt(); final int c = sc.nextInt(); final int r = sc.nextInt(); int min = Math.min(a,b); int max = Math.max(a,b); if(c+r<min||c-r>max){ println(max-min); continue; } int answer=0; if(c-r>min){ answer = c-r-min; } if(c+r<max){ answer +=max-c-r; } println(answer); } sc.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
559a1a32b292f36ab252208644d932c0
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Stack; import java.util.StringTokenizer; public class Solution { static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { Reader r = new Reader(); int t = r.nextInt(); for (int i = 0; i < t; i++) { int a = r.nextInt(); int b = r.nextInt(); int c = r.nextInt(); int rr = r.nextInt(); int s = a; if (b < a) { a = b; b = s; } if (c - rr <= a && c + rr >= b) { System.out.println(0); } else if (c + rr < a || c - rr > b) { System.out.println(b - a); } else if (c - rr < a) { System.out.println(b - a - (c + rr - a)); } else if (c + rr > b) { System.out.println(b - a - (b - c + rr)); } else { System.out.println(b - a - rr * 2); } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
18c7accfb2264206a20bda539c3c5d98
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.io.*; public class TaskA { public static void main(String [] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int temp = a+b; int c = sc.nextInt(); int r = sc.nextInt(); a = Math.min(a, b); b = temp - a; if (a > c + r) { out.println(b-a); } else if (b < c-r) { out.println(b-a); } else if (a > c - r) { if (b < c + r) { out.println(0); } else { out.println(b - (c+r)); } } else { if (b < c+r) { out.println(c-r-a); } else { out.println(c-r-a + b - (c+r)); } } } out.close(); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int 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
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
a726f9245f55f4f399e83d73d788a2f5
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t>0) { t--; int a,b,c,r; a=sc.nextInt(); b=sc.nextInt(); c=sc.nextInt(); r=sc.nextInt(); if(a>b) { int q=a;a=b;b=q; } if(c+r<a||c-r>b) System.out.println(b-a); else { int mi=(int)2e8; if(c-r>=a) { mi=Math.min(2*r, b-c+r); System.out.println(b-a-mi<=0? 0:b-a-mi); } else { mi=Math.min(2*r,c+r-a); System.out.println(b-a-mi<=0? 0:b-a-mi); } } } sc.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
8ae0098e5d90357b057959909534ae25
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class CodeForceContestQuestionTemporarilyUnavailable { public static void main(String[] args) { TemporarilyUnavailable(); } public static void TemporarilyUnavailable(){ Scanner scanner = new Scanner(System.in); int testCase = scanner.nextInt(); int[][] positions = new int[testCase][]; for(int i = 0; i < testCase; i++){ int[] tempPositions = new int[4]; for (int j = 0; j < 4; j++) { tempPositions[j] = scanner.nextInt(); } positions[i] = tempPositions; } for (int i = 0; i < testCase; i++){ System.out.println(getNumberOfUnavailableMinutes(positions[i])); } scanner.close(); } public static int getNumberOfUnavailableMinutes(int[] positions){ if(positions[0] > positions[1]){ int temp = positions[1]; positions[1] = positions[0]; positions[0] = temp; } if(positions[2] - positions[3] <= positions[0] && positions[1] <= positions[2] + positions[3]){ return 0; } int left = 0; int right = 0; if(positions[0] < positions[2] - positions[3] && positions[1] > positions[2] - positions[3]){ left = positions[2] - positions[3] - positions[0]; } if(positions[0] < positions[2] + positions[3] && positions[1] > positions[2] + positions[3]){ right = positions[1] - (positions[2] + positions[3]); } if(left + right == 0) return positions[1] - positions[0]; return left + right; } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
0d396032675255c490a5f23afa5ff3aa
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Q1 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub MScanner s = new MScanner(System.in); int t = s.nextInt(); while (t-- > 0) { int a = s.nextInt(); int b = s.nextInt(); int c = s.nextInt(); int r = s.nextInt(); int left = Math.max(Math.min(a, b), c - r); int right = Math.min(Math.max(a, b), c + r); System.out.println(Math.max(a, b) - Math.min(a, b) - Math.max(0, right - left)); } } static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(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 int[] intArr(int n) throws IOException { int[] in = new int[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); return in; } public long[] longArr(int n) throws IOException { long[] in = new long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[] in = new int[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[] in = new long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[] in = new Integer[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[] in = new Long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); int tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static void shuffle(long[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); long tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
1a22aad00f7cee04a4a73c22165d8acc
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class Solution { public static void main(String []args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int aa = sc.nextInt(); int bb = sc.nextInt(); int c = sc.nextInt(); int r = sc.nextInt(); int a = Math.min(aa , bb); int b = Math.max(aa , bb); int p1 = Math.max(a , c - r); int p2 = Math.min(b , c + r); if(p1 <= p2) System.out.println(b - a - (p2 - p1)); else System.out.println(b - a); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
95254762252e79ab1f5809bb111b4dec
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
// package com.company; import java.math.BigInteger; import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static void main(String args[]){ PrintWriter out=new PrintWriter(System.out); InputReader in=new InputReader(System.in); TASK solver = new TASK(); int t = in.nextInt(); // int t=1; /* ** @author Jigar_Nainuji ** SVNIT-SURAT */ for(int i=0;i<t;i++) { solver.solve(in,out,i); } out.close(); } static class TASK { static int mod = 998244353; void solve(InputReader in, PrintWriter out, int testNumber) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int r = in.nextInt(); if(a>b) { int x = b; b=a; a=x; } if(c+r<a || c-r>b) { System.out.println(b-a); return; } int x1=c-r; int x2=c+r; if(x1<=a && x2>=b) { System.out.println(0); } else if(x1>=a && x2<=b) { System.out.println(b-a-2*r); } else if(x2>=a && x1<=a) { System.out.println(b-x2); } else if(x1<=b && x2>=b) { System.out.println(x1-a); } } } static class pair{ int x; int y; pair(int x,int y) { this.x=x; this.y=y; } } static class Maths{ static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(int n){ long fact = 1; for(int i=1;i<=n;i++){ fact *= i; } return fact; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
ee5f4f7973273fb662c675aa40db37e9
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
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 a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), r = sc.nextInt(); if (a > b) { a = a ^ b; b = a ^ b; a = a ^ b; } int left = c - r, right = c + r; if (a <= left && right <= b) { System.out.println(left - a + b - right); continue; } if (b < left || right < a) { System.out.println(b - a); continue; } if (a <= left) { System.out.println(left - a); continue; } if (right <= b) { System.out.println(b - right); continue; } System.out.println(0); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
f03e6b4969830379b6da1462bb55edfd
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
//package CodeforcesProject; import java.io.*; import java.lang.*; import java.util.*; public class Main extends IO { public static void main(String[] args) throws Exception { int quantity = readInt(); for (int i = 0; i < quantity; i++) { int[] number = readArrayInt(" "); if (number[1] < number[0]) { int n = number[1]; number[1] = number[0]; number[0] = n; } Pair<Integer, String>[] base = new Pair[]{Pair.createPair(number[0], "start"), Pair.createPair(number[1], "stop"), Pair.createPair(number[2] - number[3], "startCommunication"), Pair.createPair(number[2] + number[3], "stopCommunication")}; Arrays.sort(base, Comparator.comparingInt(Pair::getFirstElement)); boolean communication = false; boolean way = false; Integer[] truePosition = new Integer[2]; for (Pair<Integer, String> value : base) { if (value.getSecondElement().equals("startCommunication")) { communication = true; if (way) { truePosition[0] = value.getFirstElement(); } } else if (value.getSecondElement().equals("start")) { way = true; if (communication) { truePosition[0] = value.getFirstElement(); } } else if (value.getSecondElement().equals("stopCommunication")) { communication = false; if (way) { truePosition[1] = value.getFirstElement(); break; } } else if (value.getSecondElement().equals("stop")) { way = false; if (communication) { truePosition[1] = value.getFirstElement(); break; } } } if (truePosition[0] == null) { writeInt(number[1] - number[0], "\n"); } else { writeInt((number[1] - number[0] + 1) - (truePosition[1] - truePosition[0] + 1), "\n"); } } print(); } } class math { protected static int remains = 0x3B800001; protected static int gcd(int a, int b) { // NOD if (b == 0) { return a; } return gcd(b, a % b); } protected static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } protected static float gcd(float a, float b) { if (b == 0) { return a; } return gcd(b, a % b); } protected static double gcd(double a, double b) { if (b == 0) { return a; } return gcd(b, a % b); } protected static double lcm(double a, double b) { // NOK return a / gcd(a, b) * b; } protected static float lcm(float a, float b) { // NOK return a / gcd(a, b) * b; } protected static int lcm(int a, int b) { // NOK return a / gcd(a, b) * b; } protected static long lcm(long a, long b) { return a / gcd(a, b) * b; } protected static double log(double value, int base) { return Math.log(value) / Math.log(base); } protected static long factorial(int number) { if (number < 0) { return 0; } return solveFactorial(number); } private static long solveFactorial(int number) { if (number > 0) { return solveFactorial(number - 1) * number; } return 1; } } class Fraction<T extends Number> extends Pair { private Fraction(T dividend, T divider) { super(dividend, divider); reduce(); } protected static <T extends Number> Fraction<T> createFraction(T dividend, T divider) { return new Fraction<>(dividend, divider); } protected void reduce() { if (getFirstElement() instanceof Integer) { Integer Dividend = (Integer) getFirstElement(); Integer Divider = (Integer) getSecondElement(); int gcd = math.gcd(Dividend, Divider); setFirst(Dividend / gcd); setSecond(Divider / gcd); } else if (getFirstElement() instanceof Long) { Long Dividend = (Long) getFirstElement(); Long Divider = (Long) getSecondElement(); long gcd = math.gcd(Dividend, Divider); setFirst(Dividend / gcd); setSecond(Divider / gcd); } else if (getFirstElement() instanceof Float) { Float Dividend = (Float) getFirstElement(); Float Divider = (Float) getSecondElement(); float gcd = math.gcd(Dividend, Divider); setFirst(Dividend / gcd); setSecond(Divider / gcd); } else if (getFirstElement() instanceof Double) { Double Dividend = (Double) getFirstElement(); Double Divider = (Double) getSecondElement(); double gcd = math.gcd(Dividend, Divider); setFirst(Dividend / gcd); setSecond(Divider / gcd); } } protected void addWithoutReturn(Fraction number) throws UnsupportedOperationException { add(number, 0); } private Fraction add(Fraction number, int function) throws UnsupportedOperationException { if (getFirstElement() instanceof Integer && number.getFirstElement() instanceof Integer) { Integer Dividend = (Integer) getFirstElement(); Integer Divider = (Integer) getSecondElement(); Integer Dividend1 = (Integer) number.getFirstElement(); Integer Divider1 = (Integer) number.getSecondElement(); Integer lcm = math.lcm(Divider, Divider1); if (function == 0) { setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1); setSecond(lcm); reduce(); return null; } Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm); result.reduce(); return result; } else if (getFirstElement() instanceof Long && number.getFirstElement() instanceof Long) { Long Dividend = (Long) getFirstElement(); Long Divider = (Long) getSecondElement(); Long Dividend1 = (Long) number.getFirstElement(); Long Divider1 = (Long) number.getSecondElement(); Long lcm = math.lcm(Divider, Divider1); if (function == 0) { setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1); setSecond(lcm); reduce(); return null; } Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm); result.reduce(); return result; } else if (getFirstElement() instanceof Float && number.getFirstElement() instanceof Float) { Float Dividend = (Float) getFirstElement(); Float Divider = (Float) getSecondElement(); Float Dividend1 = (Float) number.getFirstElement(); Float Divider1 = (Float) number.getSecondElement(); Float lcm = math.lcm(Divider, Divider1); if (function == 0) { setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1); setSecond(lcm); reduce(); return null; } Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm); result.reduce(); return result; } else if (getFirstElement() instanceof Double && number.getFirstElement() instanceof Double) { Double Dividend = (Double) getFirstElement(); Double Divider = (Double) getSecondElement(); Double Dividend1 = (Double) number.getFirstElement(); Double Divider1 = (Double) number.getSecondElement(); Double lcm = math.lcm(Divider, Divider1); if (function == 0) { setFirst((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1); setSecond(lcm); reduce(); return null; } Fraction result = Fraction.createFraction((lcm / Divider) * Dividend + (lcm / Divider1) * Dividend1, lcm); result.reduce(); return result; } else { throw new UnsupportedOperationException(); } } protected Fraction addWithReturn(Fraction number) { return add(number, 1); } protected void multiplyWithoutReturn(Fraction number) throws UnsupportedOperationException { multiply(number, 0); } protected Fraction multiplyWithReturn(Fraction number) throws UnsupportedOperationException { return multiply(number, 1); } private Fraction multiply(Fraction number, int function) throws UnsupportedOperationException { if (getFirstElement() instanceof Integer && number.getFirstElement() instanceof Integer) { Integer first = (Integer) getFirstElement() * (Integer) number.getFirstElement(); Integer second = (Integer) getSecondElement() * (Integer) number.getSecondElement(); if (function == 0) { setFirst(first); setSecond(second); reduce(); return null; } Fraction answer = Fraction.createFraction(first, second); answer.reduce(); return answer; } else if (getFirstElement() instanceof Long && number.getFirstElement() instanceof Long) { Long first = (Long) getFirstElement() * (Long) number.getFirstElement(); Long second = (Long) getSecondElement() * (Long) number.getSecondElement(); if (function == 0) { setFirst(first); setSecond(second); reduce(); return null; } Fraction answer = Fraction.createFraction(first, second); answer.reduce(); return answer; } else if (getFirstElement() instanceof Float && number.getFirstElement() instanceof Float) { Float first = (Float) getFirstElement() * (Float) number.getFirstElement(); Float second = (Float) getSecondElement() * (Float) number.getSecondElement(); if (function == 0) { setFirst(first); setSecond(second); reduce(); return null; } Fraction answer = Fraction.createFraction(first, second); answer.reduce(); return answer; } else if (getFirstElement() instanceof Double && number.getFirstElement() instanceof Double) { Double first = (Double) getFirstElement() * (Double) number.getFirstElement(); Double second = (Double) getSecondElement() * (Double) number.getSecondElement(); if (function == 0) { setFirst(first); setSecond(second); reduce(); return null; } Fraction answer = Fraction.createFraction(first, second); answer.reduce(); return answer; } else { throw new UnsupportedOperationException(); } } } class Pair<T, T1> { private T first; private T1 second; Pair(T obj, T1 obj1) { first = obj; second = obj1; } protected static <T, T1> Pair<T, T1> createPair(T element, T1 element1) { return new Pair<>(element, element1); } protected T getFirstElement() { return first; } protected T1 getSecondElement() { return second; } protected void setFirst(T element) { first = element; } protected void setSecond(T1 element) { second = element; } @Override public boolean equals(Object obj) { if (!(obj instanceof Pair)) { return false; } Pair pair = (Pair) obj; return first.equals(pair.first) && second.equals(pair.second); } @Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + (first == null ? 0 : first.hashCode()); return 31 * hashCode + (second == null ? 0 : second.hashCode()); } } class Graph { private int[][] base; private boolean[] used; private int quantity; private Integer[] ancestor; public int[][] getBase() { return base.clone(); } public boolean[] getUsed() { return used.clone(); } public int getQuantity() { return quantity; } public Integer[] getAncestor() { return ancestor.clone(); } public void setBase(int[][] base) { this.base = base; } public void start(int length) { used = new boolean[length]; ancestor = new Integer[length]; Arrays.fill(ancestor, -1); quantity = 0; } public void ribMatrixToDefault(int length, int quantity, boolean readConsole, int[][] value) throws Exception { base = new int[length][]; List<ArrayList<Integer>> inputBase = new ArrayList<>(); for (int i = 0; i < length; i++) { inputBase.add(new ArrayList<>()); } for (int i = 0; i < quantity; i++) { int[] input = readConsole ? IO.readArrayInt(" ") : value[i]; inputBase.get(input[0] - 1).add(input[1] - 1); //inputBase.get(input[0] - 1).add(input[2]); // price inputBase.get(input[1] - 1).add(input[0] - 1); //inputBase.get(input[1] - 1).add(input[2]); // price } for (int i = 0; i < length; i++) { base[i] = inputBase.get(i).stream().mapToInt(Integer::intValue).toArray(); } start(length); } public void adjacencyMatrixToDefault(int length, int not, boolean readConsole, int[][] value) throws Exception { base = new int[length][]; List<Integer> buffer = new ArrayList<>(); int[] InputArray; for (int i = 0; i < length; i++) { InputArray = readConsole ? IO.readArrayInt(" ") : value[i]; for (int index = 0; index < length; index++) { if (i != index && InputArray[index] != not) { buffer.add(index); // buffer.add(InputArray[index]); // price } } base[i] = buffer.stream().mapToInt(Integer::intValue).toArray(); buffer.clear(); } start(length); } public void dfs(int position) throws Exception { used[position] = true; quantity++; int next; for (int index = 0; index < base[position].length; index++) { next = base[position][index]; if (!used[next]) { ancestor[next] = position; dfs(next); } /*else { if (next != ancestor[position]) { // if cycle throw new Exception(); } }*/ } } public int dijkstra(int start, int stop, int size) { start--; stop--; int[] dist = new int[size]; for (int i = 0; i < size; i++) { if (i != start) { dist[i] = Integer.MAX_VALUE; } ancestor[i] = start; } Queue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt((int[] ints) -> ints[1])); queue.add(new int[]{start, 0}); int position; int[] getQueue; while (queue.size() != 0) { getQueue = queue.poll(); position = getQueue[0]; if (getQueue[1] > dist[position]) { continue; } for (int index = 0; index < base[position].length; index += 2) { if (dist[position] + base[position][index + 1] < dist[base[position][index]] && !used[base[position][index]]) { dist[base[position][index]] = dist[position] + base[position][index + 1]; ancestor[base[position][index]] = position; queue.add(new int[]{base[position][index], dist[base[position][index]]}); } } used[position] = true; } return dist[stop] == Integer.MAX_VALUE ? -1 : dist[stop]; } public static boolean floydWarshall(int[][] base, int length, int not) { for (int k = 0; k < length; k++) { for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (base[i][k] == not || base[k][j] == not) { continue; } int total = base[i][k] + base[k][j]; if (base[i][j] != not) { base[i][j] = Math.min(base[i][j], total); } else { base[i][j] = total; } } } } for (int index = 0; index < length; index++) { if (base[index][index] != 0) { // if cycle return false; } } return true; } } interface Array { void useArray(int[] a); } interface Method { void use(); } class FastSort { protected static void sortWithoutReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) { sort(array, ShellHeapMergeMyInsertionSort, length); } protected static int[] sortWithReturn(int[] array, int length, int ShellHeapMergeMyInsertionSort) { int[] base = array.clone(); sort(base, ShellHeapMergeMyInsertionSort, length); return base; } private static void sort(int[] array, int ShellHeapMergeMyInsertionSort, int length) { if (ShellHeapMergeMyInsertionSort < 0 || ShellHeapMergeMyInsertionSort > 4) { Random random = new Random(); ShellHeapMergeMyInsertionSort = random.nextInt(4); } if (ShellHeapMergeMyInsertionSort == 0) { ShellSort(array); } else if (ShellHeapMergeMyInsertionSort == 1) { HeapSort(array); } else if (ShellHeapMergeMyInsertionSort == 2) { MergeSort(array, 0, length - 1); } else if (ShellHeapMergeMyInsertionSort == 3) { straightMergeSort(array, length); } else if (ShellHeapMergeMyInsertionSort == 4) { insertionSort(array); } } private static void straightMergeSort(int[] array, int size) { if (size == 0) { return; } int length = (size / 2) + ((size % 2) == 0 ? 0 : 1); Integer[][] ZeroBuffer = new Integer[length + length % 2][2]; Integer[][] FirstBuffer = new Integer[0][0]; for (int index = 0; index < length; index++) { int ArrayIndex = index * 2; int NextArrayIndex = index * 2 + 1; if (NextArrayIndex < size) { if (array[ArrayIndex] > array[NextArrayIndex]) { ZeroBuffer[index][0] = array[NextArrayIndex]; ZeroBuffer[index][1] = array[ArrayIndex]; } else { ZeroBuffer[index][0] = array[ArrayIndex]; ZeroBuffer[index][1] = array[NextArrayIndex]; } } else { ZeroBuffer[index][0] = array[ArrayIndex]; } } boolean position = false; int pointer0, pointer, pointer1, number = 4, NewPointer, count; Integer[][] NewBuffer; Integer[][] OldBuffer; length = (size / 4) + ((size % 4) == 0 ? 0 : 1); while (true) { pointer0 = 0; count = (number / 2) - 1; if (!position) { FirstBuffer = new Integer[length + length % 2][number]; NewBuffer = FirstBuffer; OldBuffer = ZeroBuffer; } else { ZeroBuffer = new Integer[length + length % 2][number]; NewBuffer = ZeroBuffer; OldBuffer = FirstBuffer; } for (int i = 0; i < length; i++) { pointer = 0; pointer1 = 0; NewPointer = pointer0 + 1; if (length == 1) { for (int g = 0; g < size; g++) { if (pointer > count || OldBuffer[pointer0][pointer] == null) { array[g] = OldBuffer[NewPointer][pointer1]; pointer1++; } else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) { if (OldBuffer[pointer0][pointer] == null) { continue; } array[g] = OldBuffer[pointer0][pointer]; pointer++; } else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) { array[g] = OldBuffer[NewPointer][pointer1]; pointer1++; } else { array[g] = OldBuffer[pointer0][pointer]; pointer++; } } return; } for (int g = 0; g < number; g++) { if (pointer > count || OldBuffer[pointer0][pointer] == null) { if (OldBuffer[NewPointer][pointer1] == null) { continue; } NewBuffer[i][g] = OldBuffer[NewPointer][pointer1]; pointer1++; } else if (pointer1 > count || OldBuffer[NewPointer][pointer1] == null) { if (OldBuffer[pointer0][pointer] == null) { continue; } NewBuffer[i][g] = OldBuffer[pointer0][pointer]; pointer++; } else if (OldBuffer[pointer0][pointer] >= OldBuffer[NewPointer][pointer1]) { NewBuffer[i][g] = OldBuffer[NewPointer][pointer1]; pointer1++; } else { NewBuffer[i][g] = OldBuffer[pointer0][pointer]; pointer++; } } pointer0 += 2; } position = !position; length = length / 2 + length % 2; number *= 2; } } private static void ShellSort(int[] array) { int j; for (int gap = array.length / 2; gap > 0; gap /= 2) { for (int i = gap; i < array.length; i++) { int temp = array[i]; for (j = i; j >= gap && array[j - gap] > temp; j -= gap) { array[j] = array[j - gap]; } array[j] = temp; } } } private static void HeapSort(int[] array) { for (int i = array.length / 2 - 1; i >= 0; i--) shiftDown(array, i, array.length); for (int i = array.length - 1; i > 0; i--) { swap(array, 0, i); shiftDown(array, 0, i); } } private static void shiftDown(int[] array, int i, int n) { int child; int tmp; for (tmp = array[i]; leftChild(i) < n; i = child) { child = leftChild(i); if (child != n - 1 && (array[child] < array[child + 1])) child++; if (tmp < array[child]) array[i] = array[child]; else break; } array[i] = tmp; } private static int leftChild(int i) { return 2 * i + 1; } private static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } private static void MergeSort(int[] array, int low, int high) { if (low < high) { int mid = (low + high) / 2; MergeSort(array, low, mid); MergeSort(array, mid + 1, high); merge(array, low, mid, high); } } private static void merge(int[] array, int low, int mid, int high) { int n = high - low + 1; int[] Temp = new int[n]; int i = low, j = mid + 1; int k = 0; while (i <= mid || j <= high) { if (i > mid) Temp[k++] = array[j++]; else if (j > high) Temp[k++] = array[i++]; else if (array[i] < array[j]) Temp[k++] = array[i++]; else Temp[k++] = array[j++]; } for (j = 0; j < n; j++) array[low + j] = Temp[j]; } private static void insertionSort(int[] elements) { for (int i = 1; i < elements.length; i++) { int key = elements[i]; int j = i - 1; while (j >= 0 && key < elements[j]) { elements[j + 1] = elements[j]; j--; } elements[j + 1] = key; } } } class IO { private static BufferedReader read; private static boolean fileInput = false; private static BufferedWriter write; private static boolean fileOutput = false; public static void setFileInput(boolean fileInput) { IO.fileInput = fileInput; } public static void setFileOutput(boolean fileOutput) { IO.fileOutput = fileOutput; } private static void startInput() { try { read = new BufferedReader(fileInput ? new FileReader("input.txt") : new InputStreamReader(System.in)); } catch (Exception error) { } } private static void startOutput() { try { write = new BufferedWriter(fileOutput ? new FileWriter("output.txt") : new OutputStreamWriter(System.out)); } catch (Exception error) { } } protected static int readInt() throws Exception { if (read == null) { startInput(); } return Integer.parseInt(read.readLine()); } protected static long readLong() throws Exception { if (read == null) { startInput(); } return Long.parseLong(read.readLine()); } protected static String readString() throws Exception { if (read == null) { startInput(); } return read.readLine(); } protected static int[] readArrayInt(String split) throws Exception { if (read == null) { startInput(); } return Arrays.stream(read.readLine().split(split)).mapToInt(Integer::parseInt).toArray(); } protected static long[] readArrayLong(String split) throws Exception { if (read == null) { startInput(); } return Arrays.stream(read.readLine().split(split)).mapToLong(Long::parseLong).toArray(); } protected static String[] readArrayString(String split) throws Exception { if (read == null) { startInput(); } return read.readLine().split(split); } protected static void writeArray(int[] array, String split, boolean enter) { if (write == null) { startOutput(); } try { int length = array.length; for (int index = 0; index < length; index++) { write.write(Integer.toString(array[index])); if (index + 1 != length) { write.write(split); } } if (enter) { writeEnter(); } } catch (Exception error) { } } protected static void writeArray(Integer[] array, String split, boolean enter) { if (write == null) { startOutput(); } try { int length = array.length; for (int index = 0; index < length; index++) { write.write(Integer.toString(array[index])); if (index + 1 != length) { write.write(split); } } if (enter) { writeEnter(); } } catch (Exception error) { } } protected static void writeArray(long[] array, String split, boolean enter) { if (write == null) { startOutput(); } try { int length = array.length; for (int index = 0; index < length; index++) { write.write(Long.toString(array[index])); if (index + 1 != length) { write.write(split); } } if (enter) { writeEnter(); } } catch (Exception error) { } } protected static void writeArray(Long[] array, String split, boolean enter) { if (write == null) { startOutput(); } try { int length = array.length; for (int index = 0; index < length; index++) { write.write(Long.toString(array[index])); if (index + 1 != length) { write.write(split); } } if (enter) { writeEnter(); } } catch (Exception error) { } } public static void writeArray(String[] array, String split, boolean enter) { if (write == null) { startOutput(); } try { int length = array.length; for (int index = 0; index < length; index++) { write.write(array[index]); if (index + 1 != length) { write.write(split); } } if (enter) { writeEnter(); } } catch (Exception ignored) { } } protected static void writeArray(boolean[] array, String split, boolean enter) { if (write == null) { startOutput(); } try { int length = array.length; for (int index = 0; index < length; index++) { write.write(Boolean.toString(array[index])); if (index + 1 != length) { write.write(split); } } if (enter) { writeEnter(); } } catch (Exception ignored) { } } protected static void writeInt(int number, String end) { if (write == null) { startOutput(); } try { write.write(Integer.toString(number)); write.write(end); } catch (Exception ignored) { } } protected static void writeInt(Integer number, String end) { if (write == null) { startOutput(); } try { write.write(Integer.toString(number)); write.write(end); } catch (Exception ignored) { } } protected static void writeLong(long number, String end) { if (write == null) { startOutput(); } try { write.write(Long.toString(number)); write.write(end); } catch (Exception ignored) { } } protected static void writeLong(Long number, String end) { if (write == null) { startOutput(); } try { write.write(Long.toString(number)); write.write(end); } catch (Exception ignored) { } } protected static void writeString(String word, String end) { if (write == null) { startOutput(); } try { write.write(word); write.write(end); } catch (Exception ignored) { } } protected static void writeBoolean(boolean value, String end) { if (write == null) { startOutput(); } try { write.write(value ? "true" : "false"); write.write(end); } catch (Exception ignored) { } } protected static void writeBoolean(Boolean value, String end) { if (write == null) { startOutput(); } try { write.write(value ? "true" : "false"); write.write(end); } catch (Exception ignored) { } } protected static void writeChar(char word, String end) { if (write == null) { startOutput(); } try { write.write(word); write.write(end); } catch (Exception ignored) { } } protected static void writeChar(Character word, String end) { if (write == null) { startOutput(); } try { write.write(word); write.write(end); } catch (Exception ignored) { } } protected static void writeEnter() { if (write == null) { startOutput(); } try { write.newLine(); } catch (Exception ignored) { } } protected static void print(boolean exit) throws Exception { if (exit) { print(); } else { write.flush(); } } protected static void print() throws Exception { if (write == null) { return; } write.flush(); read.close(); write.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
8823ab6140f2b3bec5094edaadcff06a
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main(String[] args) { // code Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t > 0) { t--; int a = scn.nextInt(); int b = scn.nextInt(); int c = scn.nextInt(); int n = scn.nextInt(); int x = c - n; int y = c + n; if (a > b) { int u = b; b = a; a = u; } int ans = 0; if (x >= a && x <= b) { ans += x - a; } if (y >= a && y <= b) { ans += b - y; } if (x >= b || y <= a) { ans = b - a; } System.out.println(ans); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
f4b11fe9374ecfa2d47cc3d1b215bdd2
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { int a = in.nextInt(); int b = in.nextInt(); if (a > b) { int temp = a; a = b; b = temp; } int c = in.nextInt(); int r = in.nextInt(); int left = c - r; int right = c + r; if (right < a || left > b) { out.println(b - a); } else { int x = b - Math.min(b, right); int y = left - Math.min(left, a); out.println(x + y); } } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
9883f6e051964eb5b36abda0854d30d5
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.Scanner; public class TemporarilyUnavailable { public static void main(String[] args) { Scanner s = new Scanner(System.in); boolean start = true; int count = s.nextInt(); for(int i = 0; i < count; i++) { if(start) { System.out.println(); start = false; } int a1 = s.nextInt(); int b1 = s.nextInt(); int c = s.nextInt(); int r = s.nextInt(); int a = 0; int b = 0; if(a1 > b1) { b = a1; a = b1; } else { a = a1; b = b1; } int d = Math.abs(a - b); int c1 = c - r; int c2 = c + r; if(c1 >= a && c2 <= b) { System.out.println(d - Math.abs(c1 - c2)); } else if(c1 == c2) { System.out.println(d); } else if(c1 >= a && c1 <= b) { System.out.println(d - Math.abs(b - c1)); } else if(c2 >= a && c2 <= b) { System.out.println(d - Math.abs(c2 - a)); } else if(c1 < a && c2 > b) { System.out.println(0); } else { System.out.println(d); } } s.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
3c4a4c35994cced1fe15051cf737600b
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.Scanner; public class TemporarilyUnavailable { public static void main(String[] args) { Scanner s = new Scanner(System.in); boolean start = true; int count = s.nextInt(); for(int i = 0; i < count; i++) { if(start) { System.out.println(); start = false; } int a1 = s.nextInt(); int b1 = s.nextInt(); int c = s.nextInt(); int r = s.nextInt(); int a = 0; int b = 0; if(a1 > b1) { b = a1; a = b1; } else { a = a1; b = b1; } int d = Math.abs(a - b); int c1 = c - r; int c2 = c + r; if(c1 >= a && c2 <= b) { System.out.println(d - Math.abs(c1 - c2)); } else if(c1 == c2) { System.out.println(d); } else if(c1 >= a && c1 <= b) { System.out.println(d - Math.abs(b - c1)); } else if(c2 >= a && c2 <= b) { System.out.println(d - Math.abs(c2 - a)); } else if(c1 < a && c2 > b && 2*r > d) { System.out.println(0); } else { System.out.println(d); } } s.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
c388628ba06a0654d7e3eaf153e0c62b
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while(n-->0) { int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),d=sc.nextInt(); int min=Math.min(a, b); int max=Math.max(a, b); int i=c-d; int j=c+d; int sum=0; if(i>=min && j<=max) { sum=max-min-d*2; }else if(i<=min && j>=max) { sum=0; } else if(j<=min || i>=max) { sum=max-min; }else if(i<min && j>min) { sum=max-j; }else if(j>max && i<max) { sum=i-min; } System.out.println(sum); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
84775e508449f802e5d6ccd7d56b524a
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Solve5 { public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); new Solve5().solve(pw); pw.flush(); pw.close(); } public void solve(PrintWriter pw) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), r = sc.nextInt(); if (a > b) { a = b - a + (b = a); } int left = c - r, right = c + r; int s = b - a; if (left >= a) { if (right <= b) { pw.println(s - (right - left)); } else if (left <= b) { pw.println(s - (b - left)); } else { pw.println(s); } } else { if (right <= b && right >= a) { pw.println(s - (right - a)); } else if (right >= b) { pw.println(0); } else { pw.println(s); } } } } static class FastReader { StringTokenizer st; BufferedReader br; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (Exception e) { } } public String next() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() throws IOException { return br.readLine(); } public boolean hasNext() throws IOException { if (st != null && st.hasMoreTokens()) { return true; } String s = br.readLine(); if (s == null || s.isEmpty()) { return false; } st = new StringTokenizer(s); return true; } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
27440474332bc98a82f969cc85c0d86b
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; public final class A1282 { static void solve() throws IOException { int aa = nextInt(), bb = nextInt(), c = nextInt(), r = nextInt(); int a = Math.min(aa, bb), b = Math.max(aa, bb), ss = Math.max(a, c - r), se = Math.min(b, c + r); int dist = se - ss; if (dist < 0) { dist = 0; } out.println(b - a - dist); } public static void main(String args[]) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); int tt = nextInt(); while (tt-- > 0) { solve(); } out.close(); } static final long mod = (long) (1e9 + 7); static final int inf = (int) (1e9 + 1); static class Pair implements Comparable<Pair> { int first, second; Pair(int a, int b) { first = a; second = b; } public int compareTo(Pair p) { return this.first - p.first; } public boolean equals(Object p) { Pair p1 = (Pair) p; return (first == p1.first && second == p1.second); } public String toString() { return this.first + " " + this.second; } public int hashCode() { return (int) ((1l * (inf + 1) * this.first + this.second) % mod); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static BufferedReader br; static StringTokenizer st; static PrintWriter out; static String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } static int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } static int[] memset(int n, int val) { int ar[] = new int[n]; Arrays.fill(ar, val); return ar; } static void debug(Object... a) { System.out.print("> "); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } static void debug(int a[]) { debugnsp(Arrays.stream(a).boxed().toArray()); } static void debug(long a[]) { debugnsp(Arrays.stream(a).boxed().toArray()); } static void debugnsp(Object a[]) { System.out.print("> "); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } } /* * Jai Sita Ram Ji * * @author: Nishchal Siddharth Pandey */
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
45e36f83e2fb0deb36645daced3a6188
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void process()throws IOException { int a=ni(),b=ni(),c=ni(),rad=ni(); int l=Math.min(a, b),r=Math.max(a, b); if(l>c+rad || c-rad>r){ pn(r-l); } else{ if(l<=c-rad && r>=c+rad) pn(Math.abs(c-rad-l+r-(c+rad))); else if(c-rad<=l && c+rad>=r) pn(0); else if(r>=c-rad && r<=c+rad) pn(Math.abs(c-rad-l)); else//(l>=c-rad && l<=c+rad) pn(Math.abs(r-(c+rad))); } } static FastReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { out = new PrintWriter(System.out); sc=new FastReader(); long s = System.currentTimeMillis(); int t=1; t=ni(); while(t-->0) process(); out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static int ni()throws IOException{return Integer.parseInt(sc.next());} static long nl()throws IOException{return Long.parseLong(sc.next());} static double nd()throws IOException{return Double.parseDouble(sc.next());} static String nln()throws IOException{return sc.nextLine();} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} static long mod=(long)1e9+7l; static<T> void r_sort(T arr[],int n){ Random r = new Random(); for (int i = n-1; i > 0; i--){ int j = r.nextInt(i+1); T temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } Arrays.sort(arr); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// 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
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
ff0039cd7c85ba47a16cc803464bf131
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.Scanner; public class gogo { public static void main(String args[]) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); for(int q=0;q<t;q++) { int a=scan.nextInt(); int b=scan.nextInt(); if(a>b) { int temp=a; a=b; b=temp; } int c=scan.nextInt(); int r=scan.nextInt(); int left=c-r;int right=c+r; if(right<a || left>b) { System.out.println(b-a); }else { int ok=b-Math.min(b,right); int ww=left-Math.min(left,a); System.out.println(ok+ww); } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
b10bc567659410afa59b7790d73d13d4
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.util.Scanner; public class Cf1282A { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int r=sc.nextInt(); int L=c-r; int R=c+r; int start=Math.max(L, Math.min(a, b)); int end=Math.min(R, Math.max(a,b)); int p=Math.abs(a-b)-Math.max(0, end-start); System.out.println(p); t--; } sc.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
a3a2aa14eb7f09da4d05565971280d99
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Cf131 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Cf131(),"Main",1<<27).start(); } static class Pair{ int nod; int ucn; Pair(int nod,int ucn){ this.nod=nod; this.ucn=ucn; } public static Comparator<Pair> wc = new Comparator<Pair>(){ public int compare(Pair e1,Pair e2){ //reverse order if (e1.ucn < e2.ucn) return 1; // 1 for swaping. else if (e1.ucn > e2.ucn) return -1; else{ // if(e1.siz>e2.siz) // return 1; // else // return -1; return 0; } } }; } public static long gcd(long a,long b){ if(b==0)return a; else return gcd(b,a%b); } ////recursive BFS public static int bfsr(int s,ArrayList<Integer>[] a,int[] dist,boolean[] b,int[] pre){ b[s]=true; int p = 0; int t = a[s].size(); for(int i=0;i<t;i++){ int x = a[s].get(i); if(!b[x]){ dist[x] = dist[s] + 1; p+= (bfsr(x,a,dist,b,pre)+1); } } pre[s] = p; return p; } //// iterative BFS public static void bfs(int s,ArrayList<Integer>[] a,int[] dist,boolean[] b,PrintWriter w){ b[s]=true; Queue<Integer> q = new LinkedList<>(); q.add(s); while(q.size()!=0){ int i=q.poll(); // w.println(" #"+i+"# "); Iterator<Integer> it =a[i].listIterator(); int z=0; while(it.hasNext()){ z=it.next(); if(!b[z]){ // w.println("* "+z+" *"); b[z]=true; dist[z] = dist[i]+1; // x.add(z); // w.println("@ "+x.get(x.size()-1)+" @"); q.add(z); } } } } ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t= sc.nextInt(); while(t-->0){ long A = sc.nextLong(); long B = sc.nextLong(); long c = sc.nextLong(); long r = sc.nextLong(); long a = Math.min(A,B); long b = Math.max(A,B); long s = c-r; long e = c+r; long ans = 0; if(s<b && e>=a){ ans = Math.min(b,e)-Math.max(s,a); } ans = b - a -ans ; w.println(ans); } w.flush(); w.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
ece5494f787e6b1b200803f30ffc73de
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; public class CF1282A extends PrintWriter { CF1282A() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1282A o = new CF1282A(); o.main(); o.flush(); } int intersect(int a, int b, int c, int d) { int l = Math.max(a, c); int r = Math.min(b, d); return l < r ? r - l : 0; } void main() { int t = sc.nextInt(); while (t-- > 0) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int r = sc.nextInt(); if (a > b) { int tmp = a; a = b; b = tmp; } println(b - a - intersect(a, b, c - r, c + r)); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
4a64f443b1d1ab69ebf8d98dffbb8cc2
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.awt.*; import java.awt.dnd.Autoscroll; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.*; import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException, NullPointerException { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { long min = 0; int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), r = sc.nextInt(); if (b <= a) { int temp = b; b = a; a = temp; } int x = c - r; int y = c + r; if(x<=a && y>=b) System.out.println(0); else if (x >= b) System.out.println(b - a); else if (y <= a) System.out.println(b - a); else { if (x <= a && y <= b) System.out.println(b - y); else if (x > a && y < b) System.out.println((x - a) + (b - y)); else System.out.println(x - a); } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
4ee309220d43569a14b0001ab7e0ac27
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Candidate { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t-->0) { int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),r=sc.nextInt(),l=c-r; r=c+r; if(b<a){ int temp=b; b=a; a=temp; } if(l<=a){ int z=Integer.max(a,r); System.out.println((b-z)>=0?(b-z):0); } else{ int z=Integer.min(b,l); System.out.println(z-a+(b-r>=0?b-r:0)); } } } } 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
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
3c764d67198a95cc77e3ad7cd6bcc516
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
/*import java.util.*; public class test { public static void main(String args[]) { int size =1000; int array[]=new int[size]; String ch; int i=size; Scanner sc= new Scanner(System.in); while(i>0) { ch = sc.next(); for(int j=0;j<50;j++) { array[size-i+j]=ch.charAt(j)-'0'; System.out.print(array[size-i+j]+" "); } System.out.println(); i-=50; } long max=0,p=1; int c=0,length =13; i=0; while(i<=size-length) { if(c==0) { p=1; for(;c<length;c++) { p*=array[i+c]; if(p==0) { i=i+c+1; c=0;p=1;break;} } } else { p/=array[i-1]; p*=array[i+length-1]; if(p==0) {i= i+length;c=0;continue;} } System.out.println(p+" "+i+" "+c+" "+array[i]+" "+array[i+1]+" "+array[i+2]+" "+ array[i+3]+" "); if(p>max)max=p; i++; } System.out.println(max); sc.close(); } } */ import java.util.*; public class test{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc . nextInt(); while(t--!=0) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int r = sc.nextInt(); int c1 = c-r; int c2 = c+r; int ans; if(a>b) { int temp=a; a=b; b=temp; } if(c1<a && c2>a) ans = b-c2; else if(c1<b && c2>b) ans = c1-a; else if(c1>=a && c2<=b) ans= c1-a+b-c2; else ans= b-a; if(ans<0) ans=0; System.out.println(ans); } sc.close(); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
360949dd03a0342247e21b439b8a6ab1
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
/*input 5 5 2 3 */ import java.util.*; import java.lang.*; import java.io.*; public class Main { static Scanner in = new Scanner(System.in); public static void solve() { int a=in.nextInt(),b=in.nextInt(),c=in.nextInt(),r=in.nextInt(); int min=(int)Math.min(a,b); int max=(int)Math.max(a,b); int tot=0; if(c<=min) { tot+=c+r<min?max-min:c+r<=max?max-(c+r):0; } else if(c>=max) { tot+=c-r>max?max-min:c-r>=min?c-r-min:0; } else { tot+=c-r<=min?0:c-r-min; tot+=c+r>=max?0:max-(c+r); } System.out.println(tot); } public static void main (String[] args) { int t=in.nextInt(); while(t-->0) { solve(); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
1f7677585366cf32ecdd7c2f45e5b15e
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class test { public static void main(String[] args) { int test = fs.nextInt(); for (int cases = 0; cases < test; cases++) { int a = fs.nextInt(); int b = fs.nextInt(); int c = fs.nextInt(); int r = fs.nextInt(); int L = Integer.max(Integer.min(a, b), c - r); int R = Integer.min(Integer.max(a, b), c + r); System.out.println(Integer.max(a, b) - Integer.min(a, b) - Integer.max(0, R - L)); } } /* * array list * * ArrayList<Integer> al=new ArrayList<>(); creating BigIntegers * * BigInteger a=new BigInteger(); BigInteger b=new BigInteger(); * * hash map * * HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int * i=0;i<ar.length;i++) { Integer c=hm.get(ar[i]); if(hm.get(ar[i])==null) { * hm.put(ar[i],1); } else { hm.put(ar[i],++c); } } * * while loop * * int t=sc.nextInt(); while(t>0) { t--; } * * array input * * int n = sc.nextInt(); int ar[] = new int[n]; for (int i = 0; i < ar.length; * i++) { ar[i] = sc.nextInt(); } */ static class Node { Node left, right; int data; public Node(int data) { this.data = data; } public void insert(int val) { if (val <= data) { if (left == null) { left = new Node(val); } else { left.insert(val); } } else { if (right == null) { right = new Node(val); } else { right.insert(val); } } } public boolean contains(int val) { if (data == val) { return true; } else if (val < data) { if (left == null) { return false; } else { return left.contains(val); } } else { if (right == null) { return false; } else { return right.contains(val); } } } public void inorder() { if (left != null) { left.inorder(); } System.out.print(data + " "); if (right != null) { right.inorder(); } } public int maxDepth() { if (left == null) return 0; if (right == null) return 0; else { int ll = left.maxDepth(); int rr = right.maxDepth(); if (ll > rr) return (ll + 1); else return (rr + 1); } } public int countNodes() { if (left == null) return 1; if (right == null) return 1; else { return left.countNodes() + right.countNodes() + 1; } } public void preorder() { System.out.print(data + " "); if (left != null) { left.inorder(); } if (right != null) { right.inorder(); } } public void postorder() { if (left != null) { left.inorder(); } if (right != null) { right.inorder(); } System.out.print(data + " "); } public void levelorder(Node node) { LinkedList<Node> ll = new LinkedList<Node>(); ll.add(node); getorder(ll); } public void getorder(LinkedList<Node> ll) { while (!ll.isEmpty()) { Node node = ll.poll(); System.out.print(node.data + " "); if (node.left != null) ll.add(node.left); if (node.right != null) ll.add(node.right); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } } 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++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static final Scanner sc = new Scanner(System.in); private static final FastReader fs = new FastReader(); private static final OutputWriter op = new OutputWriter(System.out); static int[] getintarray(int n) { int ar[] = new int[n]; for (int i = 0; i < n; i++) { ar[i] = fs.nextInt(); } return ar; } static long[] getlongarray(int n) { long ar[] = new long[n]; for (int i = 0; i < n; i++) { ar[i] = fs.nextLong(); } return ar; } static int[][] get2darray(int n, int m) { int ar[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ar[i][j] = fs.nextInt(); } } return ar; } static Pair[] getpairarray(int n) { Pair ar[] = new Pair[n]; for (int i = 0; i < n; i++) { ar[i] = new Pair(fs.nextInt(), fs.nextInt()); } return ar; } static void printarray(int ar[]) { for (int i : ar) { op.print(i + " "); } op.flush(); } static int fact(int n) { int fact = 1; for (int i = 2; i <= n; i++) { fact *= i; } return fact; } // // function to find largest prime factor }/* * 1 5 -2 -3 -1 -4 -6till here */ // while (t > 0) { // int a[] = getintarray(3); // int b[] = getintarray(3); // int ans = getans(a, b); // System.out.println(ans); // }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
6ad8777c830d949bfb03740d0e36868a
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class temporarilyUnavailable { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int a; int b; int c; int r; int convergenceMin; int convergenceMax; int temp; for (int i = 0; i < n; i++) { a = input.nextInt(); b = input.nextInt(); c = input.nextInt(); r = input.nextInt(); convergenceMin = c-r; convergenceMax = c+r; if (a > b) { temp = a; a = b; b = temp; } if ((convergenceMax <= a) || (convergenceMin >= b)) { System.out.println(b-a); } else if (convergenceMin <= a && convergenceMax >= b) { System.out.println(0); } else if (convergenceMax <= b && convergenceMin <= a) { System.out.println(b - convergenceMax); } else if (convergenceMin >= a && convergenceMax >= b) { System.out.println(convergenceMin - a); } else { System.out.println(convergenceMin - a + b - convergenceMax); } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
83af65f131f4c94103cf2484955d1954
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.io.*; /* */ public class Main{ public static OutputStream out=new BufferedOutputStream(System.out); //nl-->neew line; //l-->line; //arp-->array print; //arpnl-->array print new line public static void nl(Object o) throws IOException{out.write((o+"\n").getBytes());} public static void l(Object o) throws IOException{out.write((o+"").getBytes());} public static void arp(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+" ").getBytes());} public static void arpnl(int[] o) throws IOException{for(int i=0;i<o.length;i++) out.write((o[i]+"\n").getBytes());} // static int MOD=(int)1e9+7; static int[][] pascal_trgl=new int[4005][4005]; // public static void main(String[] args) throws IOException{ // long sttm=System.currentTimeMillis(); FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0){ int[] a=new int[]{sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt()}; int max=Math.max(a[0],a[1]),min=Math.min(a[0], a[1]); a[0]=min;a[1]=max; int r1=a[2]+a[3],r2=a[2]-a[3]; int ans=a[1]-a[0]; int dif=0; // nl(r1+" "+r2+" "+a[0]+" "+a[1]); if((r1>a[0] && r2<a[1])){ if(r2<a[0]){ r2=a[0]; } if(r1>a[1]){ r1=a[1]; } dif=r1-r2; } ans-=dif; nl(ans); } out.flush(); } public static int nCr(int n,int r){ return pascal_trgl[n][r]%MOD; } public static void intiTriangle(){ for (int i = 0; i <= 4004; i++) { Arrays.fill(pascal_trgl[i], 0); } pascal_trgl[0][0] = 1; for (int i = 1; i <= 4004; i++) { pascal_trgl[i][0] = 1; for (int j = 1; j <= i; j++) { pascal_trgl[i][j] = (pascal_trgl[i - 1][j - 1] + pascal_trgl[i - 1][j]) % MOD; } } } public static String decToBin(long val){ StringBuilder binstr=new StringBuilder(); while(val>0){ Long rem=val%2;val/=2; binstr.append(Long.toString(rem)); } binstr.reverse(); return new String(binstr); } public static long binToDec(String binstr){ long ans=0; long mul=1;int val=0; int n=binstr.length(); for(int i=0;i<n;i++){ if(binstr.charAt(n-1-i)=='1') val=1; ans+=(mul*val); val=0; mul*=2; } return ans; } public static StringBuilder OR(long val1,long val2){ StringBuilder s=new StringBuilder(); while(val1>0 || val2>0){ long rem1=0,rem2=0; if(val1>0) rem1=val1%2; if(val2>0) rem2=val2%2; val1/=2;val2/=2; s.append(Long.toString(Math.max(rem1,rem2))); } s.reverse(); return s; } public static boolean dfsCycle(ArrayList<Integer>[] arrl,int[] vis,int st,int pr){ vis[st]=1; int cupr=st; for(int elm:arrl[st]){ if(vis[elm]==1 && elm!=pr){ return true; } else if(vis[elm]==0 && dfsCycle(arrl, vis, elm, cupr)){ return true; } } vis[st]=2; return false; } public static long powMOD(long n,long k){ //n-pow(k) if(k==1){ return n%MOD; } if(k==0){ return 1; } long val=pow(n,k/2)%MOD; if(k%2==0)return ((val%MOD)*(val%MOD))%MOD; else return (((val*val)%MOD)*n)%MOD; } public static long pow(long n,long k){ //n-pow(k) if(k==1){ return n; } if(k==0){ return 1; } long val=pow(n,k/2); if(k%2==0)return ((val)*(val)); else return (((val*val))*n); } public static HashMap<Integer,Integer> lcm(int num){ ArrayList<Integer> arrl=new ArrayList<Integer>(); HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); while(num%2==0){ if(!map.containsKey(2)){ arrl.add(2); map.put(2, 1); } else{ map.put(2,map.get(2)+1); } num/=2; } for(int i=3;i*i<=num;i+=2){ while(num%i==0){ if(!map.containsKey(i)){ arrl.add(i); map.put(i, 1); } else{ map.put(i,map.get(i)+1); } num/=i; } } if(num>2){ if(!map.containsKey(num)){ arrl.add(num); map.put(num, 1); } else{ map.put(num,map.get(num)+1); } } return map; } static long m=998244353l; static long modInverse(long a,long m) { long m0 = m; long y = 0l, x = 1l; if (m == 1) return 0l; while (a > 1) { long q = a / m; long t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } public static int num_fact(int num){ ArrayList<Integer> arr=new ArrayList<Integer>(); int cnt=0; for(int i=1;i*i<=num;i++){ if(num%i==0){ if(i*i==num){ cnt+=1; } else{ cnt+=2; } } } return cnt; } public static void sort_inc(int[][] arr,int col){ //change dimention if req Arrays.sort(arr, new Comparator<int[]>() { public int compare(final int[] entry1,final int[] entry2) { if (entry1[col] < entry2[col]) //this is for dec return 1; else if(entry1[col]==entry2[col]) return 0; else return -1; } }); } public static boolean prime(int n){ for(int i=2;i*i<=n;i++){ if(n%i==0){ return false; } } return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long lcmOfTwo(int a,int b){ return a*b/gcd(a,b); } } 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; } } //Pair Task in process // class Pair<D1,D2> extends Comparable{ // D1 v1; // D2 v2; // Pair(D1 a,D2 b){ // v1=a;v2=b; // } // }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
7c7cdb0aa4dd71c4d10290a8e973187b
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.Scanner; public class Policarp { //task 1282A public static void main(String[] args) { Scanner sca = new Scanner(System.in); int t =sca.nextInt(); int[][] data = new int[t][4]; for (int i = 0; i < t; i++) { data[i][0] = sca.nextInt(); data[i][1] = sca.nextInt(); data[i][2] = sca.nextInt(); data[i][3] = sca.nextInt(); } int[] ans = new int[t]; for (int i = 0; i < t; i++) { ans[i] = poli(data[i][0], data[i][1], data[i][2], data[i][3]); System.out.println(ans[i]); } } private static int poli(int a, int b, int c, int r) { if (a == b) { return 0; } int c1 = c - r; int c2 = c + r; int minX = a < b ? a : b; int maxX = a > b ? a : b; if (minX >= c2 || maxX <= c1) { return maxX - minX; } int t1 = c1 > minX ? c1 - minX : 0; int t2 = c2 < maxX ? maxX - c2 : 0; return t1 + t2; } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
6610783a3da3f99608870d57705a6974
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.math.BigInteger; import java.util.*; import java.util.function.BiFunction; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t>0){ int x = sc.nextInt();int y = sc.nextInt(); int a = Math.min(x,y);int b = Math.max(x,y); int c = sc.nextInt();int r = sc.nextInt(); int rangeStart = c-r; int rangeEnd = c+r; int ans = (b-a); if((rangeEnd >= b && rangeStart <= a) || (rangeEnd <= b && rangeEnd>=a) || (rangeStart >= a && rangeStart <=b) || (rangeStart >= a && rangeEnd <= b)){ ans = ans - (Math.min(b,rangeEnd) - Math.max(rangeStart,a)); } System.out.println(ans); t--; } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
5abc6c5ebcfc8f4db5163e7dc2958bcd
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; public class TemporarilyUnavailable { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); int[] results = new int[t]; for(int i = 0; i < t; i++) { st = new StringTokenizer(f.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int left = Math.min(a, b); int right = Math.max(a, b); int dist = right - left; if(c + r <= left || c - r >= right) results[i] = dist; else if(c + r >= right && c - r <= left) results[i] = 0; else { if(c >= right) results[i] = dist - (r - (c - right)); else if(c <= left) results[i] = dist - (r - (left - c)); else if(c - r < left) results[i] = dist - (c + r - left); else if(c + r > right) results[i] = dist - (right - (c - r)); else results[i] = dist - 2*r; } } for(int i : results) System.out.println(i); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
c390ee5f13e103b88692aba9d31e9feb
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ky112233 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int a = in.nextInt(); int b = in.nextInt(); int temp = Math.min(a, b); int temp2 = Math.max(a, b); a = temp; b = temp2; int c = in.nextInt(); int r = in.nextInt(); int left = Math.max(a, c - r); int right = Math.min(b, c + r); if (right <= a) out.println(b - a); else if (left >= b) out.println(b - a); else out.println(b - a - (right - left)); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
0bee1f13f7c22fafcf2841990d522ffa
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main implements Runnable { boolean multiple = true; void solve() throws Exception { int s = 0, e = 0; int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int r = sc.nextInt(); int t = abs(a-b); int rs = c-r; int re = c+r; //System.out.println("t: " + t + " rs: " + rs + " re: " + re); if (rs <= max(a,b)) { s = max(min(a,b), rs); //if (re >= s && re <= max(a,b)) e = min(max(a,b), re); } //System.out.println("s: " + s + " e: " + e); if (e < s) { System.out.println(t); } else { System.out.println(t - (e - s)); } } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); if (multiple) { int q = sc.nextInt(); for (int i = 0; i < q; i++) solve(); } else solve(); } catch (Throwable uncaught) { Main.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Main(), "", (1 << 26)); thread.start(); thread.join(); if (Main.uncaught != null) { throw Main.uncaught; } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
1b75c6458e6695e6be0f480303616b14
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.util.*; public class b { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); int i; for(i=0;i<t;i++) { int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int r=sc.nextInt(); int d=0; int temp; if(a>b) { temp=a; a=b; b=temp; } if(r==0) d=b-a; //station between a and b else if(c>=a && c<=b) { if(b-c>=r && c-a>=r) d=b-a-2*r; else if(b-c<r && c-a>r) d=c-a-r; else if(b-c>r && c-a<r) d=b-c-r; else if(b-c<r && c-a<r) d=0; } //station on right of b else if(c>b) { if(c-b>=r) d=b-a; else d=b-a-(r-(c-b)); } //station on left of a else if(c<a) { if(a-c>=r) d=b-a; else d=b-a-(r-(a-c)); } if(d<0) System.out.println("0"); else System.out.println(d); } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
71d063649e7f7a295652af95ebc21e6f
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Almas Turganbayev */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int min = 0, max = 0; while ((n--) > 0) { int a, b, r, c; a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); r = in.nextInt(); min = Math.max(Math.min(a, b), c - r); max = Math.min(Math.max(a, b), c + r); out.printf("%d\n", (Math.abs(a - b) - Math.max(0, (max - min)))); } } } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
206489c6ab10315c12c79f2649a22ebe
train_002.jsonl
1577198100
Polycarp lives on the coordinate axis $$$Ox$$$ and travels from the point $$$x=a$$$ to $$$x=b$$$. It moves uniformly rectilinearly at a speed of one unit of distance per minute.On the axis $$$Ox$$$ at the point $$$x=c$$$ the base station of the mobile operator is placed. It is known that the radius of its coverage is $$$r$$$. Thus, if Polycarp is at a distance less than or equal to $$$r$$$ from the point $$$x=c$$$, then he is in the network coverage area, otherwise — no. The base station can be located both on the route of Polycarp and outside it.Print the time in minutes during which Polycarp will not be in the coverage area of the network, with a rectilinear uniform movement from $$$x=a$$$ to $$$x=b$$$. His speed — one unit of distance per minute.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int ПУТИН_ПОМОГИ = nextInt(); for (int navalniy = 0; navalniy < ПУТИН_ПОМОГИ; navalniy++) { int a = nextInt(); int b = nextInt(); int ot = Math.min(a, b); int v = Math.max(a, b); int put = v - ot; int c = nextInt(); int r = nextInt(); int ot_rab = Math.min(c - r, c + r); int do_rab = Math.max(c - r, c + r); if (do_rab < ot || ot_rab > v) { out.println(put); } else if (ot_rab <= ot) { if (do_rab < v) { out.println(v - do_rab); } else out.println(0); } else { if (do_rab < v) { out.println(ot_rab - ot + v - do_rab); } else { out.println(ot_rab - ot); } } } out.close(); } static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["9\n1 10 7 1\n3 3 3 0\n8 2 10 4\n8 2 10 100\n-10 20 -17 2\n-3 2 2 0\n-3 1 2 0\n2 3 2 3\n-1 3 -2 2"]
1 second
["7\n0\n4\n0\n30\n5\n4\n0\n3"]
NoteThe following picture illustrates the first test case. Polycarp goes from $$$1$$$ to $$$10$$$. The yellow area shows the coverage area of the station with a radius of coverage of $$$1$$$, which is located at the point of $$$7$$$. The green area shows a part of the path when Polycarp is out of coverage area.
Java 11
standard input
[ "implementation", "math" ]
783772cb7a54bf65f648d3f8b7648263
The first line contains a positive integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. In the following lines are written $$$t$$$ test cases. The description of each test case is one line, which contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$r$$$ ($$$-10^8 \le a,b,c \le 10^8$$$, $$$0 \le r \le 10^8$$$) — the coordinates of the starting and ending points of the path, the base station, and its coverage radius, respectively. Any of the numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (either any pair or all three numbers). The base station can be located both on the route of Polycarp and outside it.
900
Print $$$t$$$ numbers — answers to given test cases in the order they are written in the test. Each answer is an integer — the number of minutes during which Polycarp will be unavailable during his movement.
standard output
PASSED
b68f01bdc36a99c4f9a3579ea192f632
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.io.*; import java.util.*; public class Main { InputStream is; BufferedReader bufferedReader; PrintWriter out; String INPUT = ""; int row[] = { -1, 0, 1, 0 }; int column[] = { 0, 1, 0, -1 }; void solve() { int n = ni(); int m = ni(); for (int i = 0 ; i < m ; ++i) { int x = ni(); int y = ni(); } int a = n / 2; int b = n - a; for (int i = 1; i <= n; ++i) { if (i%2 == 0) { out.print(0); } else { out.print(1); } } //set.stream().forEach(i -> System.out.println(i)); } void run() throws Exception { is = System.in; //is = new BufferedInputStream(new FileInputStream("./input.txt")); out = new PrintWriter(System.out); //out = new PrintWriter(new BufferedWriter(new FileWriter("../output.txt"))); long s = System.currentTimeMillis(); solve(); //inputThird(); out.close(); //tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private boolean isNewLineChar(int c) { return c == 10;} private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private String nsTillNewLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isNewLineChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private String nsF() { int b = skip(); StringBuilder sb = new StringBuilder(); while((isSpaceChar(b)) && b!=' '){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; long b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
83217b60949899e20099676fb508db7d
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args)throws IOException { FastReader in=new FastReader(System.in); StringBuffer st=new StringBuffer(); int n=in.nextInt(); int m=in.nextInt(); while(m--!=0){ int l=in.nextInt();int r=in.nextInt(); } for(int i=0;i<n;i++) st.append(i%2); System.out.println(st); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } }
Java
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
5bd43bc9335c0956cd20720d5844d6f5
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Solution { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static class node implements Comparable<node> { int l; int r; node(int a1,int a2) { l=a1; r=a2; } public int compareTo(node n1) { if(this.l==n1.l) { return(this.r-n1.r); } return(this.l-n1.l); } } public static void main(String[] args) throws IOException { InputReader in=new InputReader(System.in); int n=in.nextInt(); StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++) { sb.append(i%2); } System.out.println(sb.toString()); } }
Java
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
023d4aecfcabfc90cb9a172d8c388cca
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.util.*; public class MainClass{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); for(int i= 0; i<m; i++) in.nextInt(); for(int i = 0; i<n; i++)System.out.print(i%2); } }
Java
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
7261fc3802c448a3042c99579de76e1c
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.util.*; public class SonyaAndExhibition{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); for(int i= 0; i<m; i++){ in.nextInt(); } for(int i = 0; i<n; i++){ System.out.print(i%2); } } }
Java
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
0a09559fadd6f5d9a3729534c08ba1e7
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws java.lang.Exception { Reader pm =new Reader(); //Scanner pm = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int t = 1; while(t-- > 0){ int n = pm.nextInt(); int m = pm.nextInt(); ArrayList<Integer> al = new ArrayList(); for(int i=0;i<m;i++){ int l = pm.nextInt(); int r = pm.nextInt(); } StringBuffer sb = new StringBuffer(); for(int i=0;i<n;i++){ if(i % 2 == 0) sb.append("0"); else sb.append("1"); } System.out.println(sb); } //end of tests } //end of main class static int countInRange(int arr[], int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } public static StringBuilder dec_to_bin(long n) { // decimal to binary upto 30 binaries if(n==0) { StringBuilder str=new StringBuilder(""); for(int i=0;i<30;i++) { str.append("0"); } return str; } StringBuilder str=new StringBuilder(""); while(n!=0) { str.append(n%2L); n/=2L; } str=str.reverse(); StringBuilder tmp_str=new StringBuilder(""); int len=str.length(); while(len!=30) { tmp_str.append("0"); len++; } tmp_str.append(str); return tmp_str; } private static int binarySearchPM(int[] arr, int key){ int n = arr.length; int mid = -1; int begin = 0,end=n; while(begin <= end){ mid = (begin+end) / 2; if(mid == n){ return n; } if(key < arr[mid]){ end = mid-1; } else if(key > arr[mid]){ begin = mid+1; } else { return mid; } } //System.out.println(begin+" "+end); return -begin; //expected Index } 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
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
0feb7c8779501c15776fbb22e2b65b6f
train_002.jsonl
1530808500
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily.She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies.Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class CF81 { final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); final static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int[]arr=readIntArr(); int n=arr[0]; int m=arr[1]; for(int i=0;i<n;i++){ if(i%2==0){ pw.print(1); }else{ pw.print(0); } } pw.println(); pw.close(); } static String readLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } static String readString() { return readLine(); } static public long readlong() { return Long.parseLong(readLine()); } static public int readInt() { return Integer.parseInt(readLine()); } static String[] stringArray() { StringTokenizer st = new StringTokenizer(readLine()); int n = st.countTokens(); String ret[] = new String[n]; for (int i = 0; i < n; i++) { ret[i] = st.nextToken(); } return ret; } static public int[] readIntArr() { String[] str = stringArray(); int arr[] = new int[str.length]; for (int i = 0; i < arr.length; i++) arr[i] = Integer.parseInt(str[i]); return arr; } static public double[] readDoubleArr() { String[] str = stringArray(); double arr[] = new double[str.length]; for (int i = 0; i < arr.length; i++) arr[i] = Double.parseDouble(str[i]); return arr; } static public long[] readLongArr() { String[] str = stringArray(); long arr[] = new long[str.length]; for (int i = 0; i < arr.length; i++) arr[i] = Long.parseLong(str[i]); return arr; } static public double readDouble() { return Double.parseDouble(readLine()); } }
Java
["5 3\n1 3\n2 4\n2 5", "6 3\n5 6\n1 4\n4 6"]
1 second
["01100", "110010"]
NoteIn the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1\ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1\cdot 2=2$$$; in the segment $$$[2\ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$.In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5\ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1\cdot 1=1$$$; in the segment $$$[1\ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2\cdot 2=4$$$; in the segment $$$[4\ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2\cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
Java 8
standard input
[ "constructive algorithms", "implementation", "greedy", "math" ]
cac8ca5565e06021a44bb4388b5913a5
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 10^3$$$) — the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive.
1,300
Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any.
standard output
PASSED
3a0200cc38397ea91ea5cac6a560e347
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author EigenFunk */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); AAnuHasAFunction solver = new AAnuHasAFunction(); solver.solve(1, in, out); out.close(); } static class AAnuHasAFunction { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); long[] arr = new long[n]; long[] pref = new long[n]; long[] suffix = new long[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextLong(); } pref[0] = arr[0]; for (int i = 1; i < n; i++) { pref[i] = pref[i - 1] | arr[i]; } suffix[n - 1] = arr[n - 1]; for (int i = n - 2; i >= 0; i--) { suffix[i] = suffix[i + 1] | arr[i]; } long max = 0; int index = 0; for (int i = 1; i < n - 1; i++) { long cand = (arr[i] & ~(pref[i - 1] | suffix[i + 1])); if (cand > max) { max = cand; index = i; } } if (n > 1) { if ((arr[0] & ~suffix[1]) > max) { max = (arr[0] & ~suffix[1]); index = 0; } if ((arr[n - 1] & ~pref[n - 2]) > max) { // max = (arr[n-1] & ~pref[n-1]); index = n - 1; } } out.write(arr[index] + " "); for (int i = arr.length - 1; i >= 0; i--) { if (i != index) { out.write(arr[i] + " "); } } } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
288fa923c411024c261ef72cab597f86
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.*; import java.util.*; public class cf1299_Div1_A { public static void count(int val, int[] bits) { for (int i = 0; i < 32; i++) { if ((val & (1 << i)) > 0) bits[i]++; } } public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); int n = in.nextInt(); int[] arr = new int[n]; int[] bits = new int[32]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } for (int i = 0; i < arr.length; i++) { count(arr[i], bits); } int b = -1; for (int i = 0; i < 32; i++) { if (bits[i] == 1) b = i; } // System.out.println(b); int[] make = new int[n]; int index = -1; if (b > -1) { for (int i = 0; i < arr.length; i++) { if ((arr[i] & (1 << b)) > 0) { index = i; break; } } } int pos = 0; if (index > -1) { make[0] = arr[index]; pos++; } for (int i = 0; i < arr.length; i++) { if (i == index) continue; make[pos] = arr[i]; pos++; } for (int i = 0; i < make.length; i++) { System.out.print(make[i] + " "); } } public static void shuffle(int[] arr) { Random rgen = new Random(); for (int i = 0; i < arr.length; i++) { int rPos = rgen.nextInt(arr.length); int temp = arr[i]; arr[i] = arr[rPos]; arr[rPos]=temp; } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { st = null; try { return br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
f52a17b5cb07adb77ff6d0c3b44bef76
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyScanner in = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, MyScanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); } int c = 0; int id = -1; for (int i = 30; i >= 0; i--) { int d = 1 << i; for (int j = 0; j < n; j++) { if ((a[j] & d) != 0) { c++; id = j; } if (c > 1) { continue; } } if (c == 1) { int tmp = a[id]; a[id] = a[0]; a[0] = tmp; break; } c = 0; } for (int i = 0; i < n; i++) { out.print(a[i] + " "); } } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
4d3192e6e355198e7af04e4bd0e787ee
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
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 ilyakor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); } int[] pref = new int[n + 1]; for (int i = 0; i < n; ++i) { pref[i + 1] = pref[i] | a[i]; } int[] suf = new int[n + 1]; for (int i = n - 1; i >= 0; --i) { suf[i] = suf[i + 1] | a[i]; } int ind = 0, val = 0; for (int i = 0; i < n; ++i) { int cur = a[i]; cur &= (~pref[i]); cur &= (~suf[i + 1]); if (cur > val) { val = cur; ind = i; } } out.print(a[ind]); for (int i = 0; i < ind; ++i) { out.print(" " + a[i]); } for (int i = ind + 1; i < n; ++i) { out.print(" " + a[i]); } out.printLine(); } } 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++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) { return -1; } } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
d7b2ac4b4a2d2893d26e7f0abe0a06b9
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.util.*; import java.io.*; public class Anu_Has_A_Function { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int n = t.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; ++i) a[i] = t.nextLong(); if (n == 1) { o.println(a[0]); o.close(); return; } long[] l = new long[n]; long[] r = new long[n]; l[0] = ~a[0]; r[n - 1] = ~a[n - 1]; for (int i = 1; i < n; i++) l[i] = l[i - 1] & (~a[i]); for (int i = n - 2; i >= 0; i--) r[i] = r[i + 1] & (~a[i]); long max = -1; int idx = -1; for (int i = 0; i < n; i++) { long temp = a[i]; if (i == 0) temp &= r[1]; else if (i == n - 1) temp &= l[n - 2]; else temp &= r[i + 1] & l[i - 1]; if (max < temp) { idx = i; max = temp; } } o.print(a[idx] + " "); for (int i = 0; i < n; i++) { if (i != idx) o.print(a[i] + " "); } o.flush(); o.close(); } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
979e1fba9da1129cf5e56072e5d0091b
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); AAnuHasAFunction solver = new AAnuHasAFunction(); solver.solve(1, in, out); out.close(); } static class AAnuHasAFunction { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); int[] prefix = new int[n]; int[] suffix = new int[n]; for (int i = 0; i < n; i++) { prefix[i] = arr[i]; if (i > 0) prefix[i] |= prefix[i - 1]; } for (int i = n - 1; i >= 0; i--) { suffix[i] = arr[i]; if (i < n - 1) suffix[i] |= suffix[i + 1]; } int max = Integer.MIN_VALUE; int idx = -1; for (int i = 0; i < n; i++) { int min = 0; if (i > 0) min |= prefix[i - 1]; if (i < n - 1) min |= suffix[i + 1]; if ((arr[i] & (~min)) > max) { max = arr[i] & (~min); idx = i; } } pw.print(arr[idx] + " "); for (int i = 0; i < n; i++) if (i != idx) pw.print(arr[i] + " "); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
9baa1963cd5d2640aaf4ed1d3252e98e
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.*; import java.util.*; public class A { static int f(int x, int y) { return (x | y) - y; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int[] cnt = new int[30]; for (int x : a) { for (int bit = 0; bit < 30; bit++) if ((x & 1 << bit) > 0) cnt[bit]++; } int ans = 0, max = 0; for (int i = 0; i < n; i++) { int curr = 0; for (int bit = 0; bit < 30; bit++) if ((a[i] & 1 << bit) > 0 && cnt[bit] == 1) curr += 1 << bit; if (curr > max) { max = curr; ans = i; } } out.print(a[ans]); for (int i = 0; i < n; i++) if (i != ans) out.print(" " + a[i]); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } boolean ready() throws IOException { return br.ready(); } } static void sort(int[] a) { shuffle(a); Arrays.sort(a); } static void 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; } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
ff12904e264cbb2310accf415aa07e8d
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1299a { public static void main(String[] args) throws IOException { int n = ri(), a[] = ria(n), bits[] = new int[31], cur[] = new int[31], score[] = new int[n], mask[] = new int[31]; for(int i = 0; i < 31; ++i) { mask[i] = (1 << i); } for(int i = 0; i < n; ++i) { for(int j = 0; j < 31; ++j) { if((a[i] & mask[j]) > 0) { if(++bits[j] == 1) { cur[j] = i; score[i] |= mask[j]; } else if (bits[j] == 2) { score[cur[j]] &= ~mask[j]; } } } } int max = score[0], maxind = 0; for(int i = 1; i < n; ++i) { if(score[i] > max) { max = score[i]; maxind = i; } } int __swap = a[0]; a[0] = a[maxind]; a[maxind] = __swap; prln(a); close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random rand = new Random(); // references // IBIG = 1e9 + 7 // IRAND ~= 3e8 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IRAND = 327859546; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int floori(double d) {return (int)d;} static int ceili(double d) {return (int)ceil(d);} static long floorl(double d) {return (long)d;} static long ceill(double d) {return (long)ceil(d);} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__out.close();} }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
e85554090f6948c49454fa256b061004
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.function.LongBinaryOperator; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedOutputStream; import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author mikit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; LightScanner in = new LightScanner(inputStream); LightWriter out = new LightWriter(outputStream); AAnuHasAFunction solver = new AAnuHasAFunction(); solver.solve(1, in, out); out.close(); } static class AAnuHasAFunction { public void solve(int testNumber, LightScanner in, LightWriter out) { // out.setBoolLabel(LightWriter.BoolLabel.YES_NO_FIRST_UP); int n = in.ints(); long[] a = in.longs(n); long max = 0; int maxIndex = 0; IntSparseTable st = new IntSparseTable(a, (x, y) -> x | y); for (int i = 0; i < n; i++) { long f = 0; if (0 < i) f |= st.query(0, i); if (i < n - 1) f |= st.query(i + 1, n); long x = (a[i] | f) - f; if (x > max) { max = x; maxIndex = i; } } out.ans(a[maxIndex]); for (int i = 0; i < n; i++) { if (i == maxIndex) continue; out.ans(a[i]); } out.ln(); } } static class IntSparseTable { private final int n; private final LongBinaryOperator f; private final long[][] table; public IntSparseTable(long[] a, LongBinaryOperator f) { this.n = a.length; this.f = f; this.table = new long[30][]; table[0] = a.clone(); for (int i = 1; (1 << i) < n; i++) { table[i] = new long[n]; int r = 1 << i, d = r + r; for (int j = r - 1; j < n; j += d) { table[i][j] = a[j]; for (int k = 1; k < r; k++) table[i][j - k] = f.applyAsLong(a[j - k], table[i][j - k + 1]); } for (int j = r; j < n; j += d) { table[i][j] = a[j]; for (int k = j + 1; k < j + r && k < n; k++) table[i][k] = f.applyAsLong(a[k], table[i][k - 1]); } } } public long query(int l, int r) { if (r <= l || l < 0 || n < r) throw new RuntimeException(); if (l == --r) return table[0][l]; int k = BitMath.msb(l ^ r); return f.applyAsLong(table[k][l], table[k][r]); } } static class LightScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public LightScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String string() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new UncheckedIOException(e); } } return tokenizer.nextToken(); } public int ints() { return Integer.parseInt(string()); } public long longs() { return Long.parseLong(string()); } public long[] longs(int length) { return IntStream.range(0, length).mapToLong(x -> longs()).toArray(); } } static final class BitMath { private BitMath() { } public static int count(int v) { return Integer.bitCount(v); } public static int msb(int v) { if (v == 0) { throw new IllegalArgumentException("Bit not found"); } v |= (v >> 1); v |= (v >> 2); v |= (v >> 4); v |= (v >> 8); v |= (v >> 16); return count(v) - 1; } } static class LightWriter implements AutoCloseable { private final Writer out; private boolean autoflush = false; private boolean breaked = true; public LightWriter(Writer out) { this.out = out; } public LightWriter(OutputStream out) { this(new OutputStreamWriter(new BufferedOutputStream(out), Charset.defaultCharset())); } public LightWriter print(char c) { try { out.write(c); breaked = false; } catch (IOException ex) { throw new UncheckedIOException(ex); } return this; } public LightWriter print(String s) { try { out.write(s, 0, s.length()); breaked = false; } catch (IOException ex) { throw new UncheckedIOException(ex); } return this; } public LightWriter ans(String s) { if (!breaked) { print(' '); } return print(s); } public LightWriter ans(long l) { return ans(Long.toString(l)); } public LightWriter ln() { print(System.lineSeparator()); breaked = true; if (autoflush) { try { out.flush(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } return this; } public void close() { try { out.close(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
6ecc78c1bf07bef800732a763cac2b88
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.util.*; public class AnuHasAFunction { public static void main(String[] args) { Scanner s = new Scanner(System.in); int l = s.nextInt(); int[] arr = new int[l]; int[] cnt = new int[32]; int[] idx = new int[32]; for (int i = 0; i < l; i++) { arr[i] = s.nextInt(); for (int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0 ) { cnt[j]++; idx[j] = i; } } } for (int i = 31; i >= 0; i--) { if (cnt[i] == 1) { int swap = arr[0]; arr[0] = arr[idx[i]]; arr[idx[i]] = swap; break; } } StringBuilder sb = new StringBuilder(); for (int i: arr) sb.append(Integer.toString(i) + " "); System.out.println(sb.toString()); } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
d09f3515e20f3c08d2e640003c60815b
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.util.*; import java.io.*; public class C618 { public static void main(String [] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); ArrayList <Integer> arr = new ArrayList <Integer> (); for (int i = 0; i < n; i++) { arr.add(sc.nextInt()); } Collections.sort(arr, new Comparator <Integer> (){ public int compare(Integer v1, Integer v2) { return v2 - v1; } }); String [] bits = new String [n]; for (int i = 0; i < n; i++) { bits[i] = Integer.toBinaryString(arr.get(i)); } int max_length = bits[0].length(); for (int i = 0; i < n; i++) { if (bits[i].length() < max_length){ while (bits[i].length() < max_length) { bits[i] = "0" + bits[i]; } } } int first_index = -1; int first = -1; for (int i = 0; i < bits[0].length(); i++) { int one_count = 0; first = -1; for (int j = 0; j < n; j++) { if (bits[j].substring(i, i+1).contentEquals("1")){ one_count++; first = j; } if (one_count > 1) break; } if (one_count == 1) { first_index = first; break; } } if (first == -1) first = 0; out.print(arr.get(first) + " "); for (int i = 0; i < n; i++) { if (i != first) out.print(arr.get(i) + " "); } out.close(); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
ac1129b0f803a737840d75c7665b8f19
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.util.*; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { int n = scanner.nextInt(); int[] a = new int[n]; int[] c = new int[32]; for (int i=0; i<n; i++) { a[i] = scanner.nextInt(); int val = a[i], k = 0; while (val > 0) { if ((val & 1) != 0 && c[k] < 2) { c[k]++; } val = val >> 1; k += 1; } } int mask = 0xFFFFFFFF; for (int i=0; i<32; i++) if (c[i] == 2) mask ^= 1 << i; int best = -1, k = -1; for (int i=0; i<n; i++) { int val = a[i] & mask; if (val > best) { best = val; k = i; } } System.out.print(a[k]); for (int i=0; i<n; i++) if (i != k) { System.out.print(" " + a[i]); } System.out.println(); } finally { scanner.close(); } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
4a17035c02a789d6b615ee809a6ed949
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import javax.swing.*; import java.awt.desktop.SystemSleepEvent; import java.util.*; import java.io.*; public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } } public class pair { int a; int b; pair(int p, int q) { a = p; b = q; } public int hashCode() { return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } } public static int gcd(int a, int b) { if (a == 1 || b == 1) return 1; if (a == 0) return b; if (b == 0) return a; return gcd(b % a, a); } public static void main(String[] args) { FastReader s = new FastReader(); int n=s.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); String[] arr=new String[n]; String str; for(int i=0;i<n;i++) { arr[i] = Integer.toBinaryString(a[i]); arr[i] = r(arr[i]); //System.out.println(arr[i]); } int ans=0; for(int i=0;i<n;i++) { ans = Integer.parseInt(String.valueOf(ans)) | a[i]; //System.out.println(ans); } //System.out.println(Integer.toBinaryString(ans)); int l = String.valueOf(Integer.toBinaryString(ans)).length(); //System.out.println(l); int temp=0; for(int i=0;i<50;i++) { int ct=0; for(int j=0;j<n;j++) { //System.out.println(arr[j].charAt(i)); if(arr[j].charAt(i)=='1') { ct++; temp = j; } } if(ct==1) { break; } } //System.out.println(temp); int[] b=new int[n]; b[0] = a[temp]; int c=0; int ct=1; for(int i=0;i<n;i++) { if(c!=temp) { b[ct] = a[c]; ct++; } c++; } for(int i=0;i<n;i++) System.out.print(b[i] + " "); } public static String r(String x) { int l=x.length(); int temp = 50-l; String str = ""; for(int i=0;i<temp;i++) str = str + '0'; str = str + x; return str; } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
814f1e0110e7f964cf5775ef3364b803
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
import java.io.*; import java.util.*; public class Main { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { int n = scanner.nextInt(); int[] v = new int[n]; int[] bin = new int[32]; for (int i = 0; i < n; ++i) { v[i] = scanner.nextInt(); for (int j = 0; j < 32; ++j) if (((v[i] >> j) & 1) == 1) ++bin[j]; } int mx = 0; int idx = 0; for (int i = 0; i < n; ++i) { int createdNum = 0; for (int j = 0; j < 32; ++j) { if (((v[i] >> j) & 1) == 1 && bin[j] == 1) createdNum |= (1 << j); } if (createdNum > mx) { mx = createdNum; idx = i; } } System.out.print(v[idx] + " "); for (int i = 0; i < n; ++i) { if (i == idx) continue; System.out.print(v[i] + " "); } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output
PASSED
352c21e94f88b9a810f91220f548a421
train_002.jsonl
1581257100
Anu has created her own function $$$f$$$: $$$f(x, y) = (x | y) - y$$$ where $$$|$$$ denotes the bitwise OR operation. For example, $$$f(11, 6) = (11|6) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array $$$[a_1, a_2, \dots, a_n]$$$ is defined as $$$f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
256 megabytes
//package com.company; import java.io.*; import java.util.*; public class Main { public static class Task { public void solve(Scanner sc, PrintWriter pw) throws IOException { int n = sc.nextInt(); int[] numbers = new int[n]; for (int i = 0; i < n; i++) { numbers[i] = sc.nextInt(); } int[] onlyNum = new int[32]; Arrays.fill(onlyNum, -1); for (int i = 31; i >= 0; i--) { for (int j = 0; j < n; j++) { if (((1 << i) & numbers[j]) != 0) { if (onlyNum[i] == -1) onlyNum[i] = j; else if (onlyNum[i] >= 0) onlyNum[i] = -2; } } if (onlyNum[i] >= 0) { pw.print(numbers[onlyNum[i]]); for (int j = 0; j < n; j++) if (j != onlyNum[i]) { pw.print(" " + numbers[j]); } return; } } for (int j = 0; j < n; j++) { pw.print(" " + numbers[j]); } } } static long TIME_START, TIME_END; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("input")); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); // PrintWriter pw = new PrintWriter(new FileOutputStream("input")); Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); pw.close(); System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000); System.err.println("Time used: " + (TIME_END - TIME_START) + "."); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader s) throws FileNotFoundException { br = new BufferedReader(s); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n4 0 11 6", "1\n13"]
1 second
["11 6 4 0", "13"]
NoteIn the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$.$$$[11, 4, 0, 6]$$$ is also a valid answer.
Java 11
standard input
[ "greedy", "math", "brute force" ]
14fd47f6f0fcbdb16dbd73dcca8a782f
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$). Elements of the array are not guaranteed to be different.
1,500
Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.
standard output