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 | 331a3223f28a5c2ebe9f138b4e2eb788 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int t = 0;
Scanner sc = new Scanner(System.in);
t = Integer.parseInt(sc.nextLine());
for (int i = 0; i < t; i++) {
String input = sc.nextLine();
String[] arr = input.split(" ");
long x0 = Long.parseLong(arr[0]), n = Long.parseLong(arr[1]);
long k = (long) (n / 4) * 4;
if (x0 % 2 == 0) {
if (n % 4 == 0)
System.out.println(x0);
else if (n % 4 == 1)
System.out.println(x0 - (k + 1));
else if (n % 4 == 2)
System.out.println(x0 + 1);
else if (n % 4 == 3)
System.out.println(x0 + (k + 4));
} else {
if (n % 4 == 0)
System.out.println(x0);
else if (n % 4 == 1)
System.out.println(x0 + (k + 1));
else if (n % 4 == 2)
System.out.println(x0 - 1);
else if (n % 4 == 3)
System.out.println(x0 - (k + 4));
}
}
sc.close();
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 23fc59c9bbc2a5fe376f9e26eb0052d4 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Solution {
static PrintWriter out;
public static void main(String[] args) {
FastReader in = new FastReader();
out = new PrintWriter(System.out);
long t = in.nextLong();
long test = 1;
while (test <= t) {
out.println(solve(in));
//solve(in);
test++;
}
out.close();
}
private static long solve(FastReader in) {
long x = in.nextLong();
long n = in.nextLong();
long j = (n / 4) * 4;
j++;
while (j <= n) {
if (x % 2 == 0) {
x -= j;
} else {
x += j;
}
j++;
}
return x;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
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\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 31fa6b43008b471014d7f532946f5d36 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Solution {
static PrintWriter out;
public static void main(String[] args) {
FastReader in = new FastReader();
out = new PrintWriter(System.out);
long t = in.nextLong();
long test = 1;
while (test <= t) {
out.println(solve(in));
//solve(in);
test++;
}
out.close();
}
private static long solve(FastReader in) {
long x = in.nextLong();
long n = in.nextLong();
long mod = n % 4;
long ans;
if (mod == 0) ans = 0;
else if (mod == 1) ans = -n;
else if (mod == 2) ans = 1;
else ans = n + 1;
if (x % 2 == 0) return ans + x;
return x - ans;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
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\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 0d306f25def2d1a9ae8d7f75336877e7 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
sc.nextLine();
StringBuilder str = new StringBuilder();
while (testcase-- > 0) {
long x = sc.nextLong();
long jumps = sc.nextLong();
long R = jumps % 4;
long inc = (jumps / 4) * 4 + 1;
if (R == 0) {
} else if (R == 1) {
if (x % 2 == 0)
x -= inc;
else
x += inc;
} else {
for (int i = 0; i < R; i++, inc++) {
if (x % 2 == 0)
x -= inc;
else
x += inc;
}
}
str.append(x + "\n");
}
System.out.println(str);
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 42d62e62c8dcf96f4b1c0323671ebae8 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes |
import java.util.Scanner;
public class Solution{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t != 0){ t--;
long x = input.nextLong();
long n = input.nextLong();
long y = (n - 1) % 4;
if(n == 0){
System.out.println(x);
continue;
}
if(x % 2 == 0){
x -= 1;
x -= ((n - 1) / 4) * 4;
if(y == 1) x += n;
else if(y == 2){
x += n - 1 + n;
}else if(y == 3){
x += n -2 + n - 1 - n;
}
}else{
x += 1;
x += ((n - 1) / 4) * 4;
if(y == 1) x -= n;
else if(y == 2){
x -= (n - 1 + n);
}else if(y == 3){
x -= (n - 2 + n - 1 - n);
}
}
System.out.println(x);
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | d00771212db397a03b03f5ef5bf13f82 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
long pos=sc.nextLong();
long n=sc.nextLong();
if(n==0)
System.out.println(pos);
else if(n==1)
System.out.println(pos%2==0?pos-1:pos+1);
else if(pos%2==0){
if ((n - 2) % 4 == 1)
System.out.println(pos+1+n);
else if ((n - 2) % 4 == 2)
System.out.println(pos);
else if ((n - 2) % 4 == 3)
System.out.println(pos-n);
else
System.out.println(pos+1);
}
else{
pos+=((n-1)/4)*4;
if ((n - 1) % 4 == 1)
System.out.println(pos+1-n);
else if ((n - 1) % 4 == 2)
System.out.println(pos-2*n+2);
else if ((n - 1) % 4 == 3)
System.out.println(pos-n+4);
else
System.out.println(pos+1);
}
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | d22518d3551f61b8f67c8613d2ffe8c9 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes |
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.math.*;
public class b1607B {
public static void main(String[] args) throws Exception {
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(bu.readLine());
while (t-- > 0) {
String s[] = bu.readLine().split(" ");
long x = Long.parseLong(s[0]), n = Long.parseLong(s[1]);
if (n == 0) {
sb.append(x + "\n");
continue;
}
long ti = (n - 1)/4, ans = 1;
ans += ti*4;
if (n - 1 > 0) {
for (long i = n; i > ti * 4 + 1; i--) {
if (i % 4 == 0 || i % 4 == 1)
ans += i;
else
ans -= i;
}
}
if (x % 2 == 0)
x -= ans;
else
x += ans;
sb.append(x + "\n");
}
System.out.println(sb);
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 7be68cae6ac4aa47b319606c2ea25c7d | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.HashMap;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner go = new Scanner(System.in);
int t = go.nextInt();
while (t>0){
long x0 = go.nextLong();
long n = go.nextLong();
int g = 0;
long ans = 0;
if (x0%2 ==0){
if (n%4 == 0){
ans = x0;
}else if (n%4 == 1){
ans = x0 - n;
}else if (n%4 == 2){
ans = x0 +1;
}else if (n%4 == 3){
ans = x0 + n+1;
}
System.out.println(ans);
}else {
if (n%4 == 0){
ans = x0;
}else if (n%4 == 1){
ans = x0 + n;
}else if (n%4 == 2){
ans = x0 -1;
}else if (n%4 == 3){
ans = x0 - (n+1);
}
System.out.println(ans);
}
// System.out.println(x0);
t--;
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | e3e39ecfc316c8d8db57a45789c7a34e | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.HashMap;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner go = new Scanner(System.in);
int t = go.nextInt();
while (t>0){
long x0 = go.nextLong();
long n = go.nextLong();
long ans = 0;
if (x0%2 ==0){
if (n%4 == 0){
ans = x0;
}else if (n%4 == 1){
ans = x0 - n;
}else if (n%4 == 2){
ans = x0 +1;
}else if (n%4 == 3){
ans = x0 + n+1;
}
System.out.println(ans);
}else {
if (n%4 == 0){
ans = x0;
}else if (n%4 == 1){
ans = x0 + n;
}else if (n%4 == 2){
ans = x0 -1;
}else if (n%4 == 3){
ans = x0 - (n+1);
}
System.out.println(ans);
}
// System.out.println(x0);
t--;
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 9e38dddf1fcb01784893a34eebede91b | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
/*AUTHOR - ELDIIAR DZHUNUSOV */
public class a {
static int mod= 1000000007 ;
static final FastReader in = new FastReader();
static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
long t = in.nextLong();
for (long i = 1; i <= t; i++) {
new Solver();
}
out.flush();
out.close();
}
static class Solver{
Solver(){
//insert your code here
long x = in.nextLong();
long n = in.nextLong();
if(x%2==0){
int piv = (int) (n%4);
if(piv==0){
out.println(x);
}else if(piv==2){
out.println(x+1);
}else if(piv==1){
out.println(x-n);
}else if(piv==3){
out.println(x+n+1);
}
}else{
int piv = (int) (n%4);
if(piv==0){
out.println(x);
}else if(piv==2){
out.println(x-1);
}else if(piv==1){
out.println(x+n);
}else if(piv==3){
out.println(x-n-1);
}
}
}
}
// Collections.sort(arrayList);
// sort 1d
// sort(arr, 0, arr.length - 1);
// Sort 2d by the first index
// Arrays.sort(arr, Comparator.comparingDouble(o -> o[0]));
public static void swap(int[] arr, int i, int j){
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
static boolean isPrime(long n)
{
if(n==1)return false ;
for(long i = 2L; i*i <= n ;i++){ if(n% i ==0){return false; } }
return true ;
}
static boolean isPrime(int n)
{
if(n==1)return false ;
for(int i = 2; i*i <= n ;i++){ if(n% i ==0){return false ; } }
return true ;
}
public static int gcd(int a, int b )
{
if(b==0)return a ;else return gcd(b,a%b) ;
}
public static long gcd(long a, long b )
{
if(b==0)return a ;else return gcd(b,a%b) ;
}
static boolean isPower(int n, int p){
if(p==0) return n==1;
return (Double.compare(Math.pow(n,1.0/p),(int)Math.pow(n,1.0/p))==0);
}
static boolean isPower(long n, long p){
if(p==0L) return n==1L;
return (Double.compare(Math.pow(n,1.0/p),(long)Math.pow(n,1.0/p))==0);
}
public static long nCr(int n,int k) {
long ans=1L;
k=k>n-k?n-k:k;
int j=1;
for(;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
public static ArrayList<Integer> allFactors(int n)
{
ArrayList<Integer> list = new ArrayList<>() ;
for(int i = 1; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n)list.add(i) ;
else{ list.add(i) ;list.add(n/i) ; }
}
}
return list ;
}
public static ArrayList<Long> allFactors(long n)
{
ArrayList<Long> list = new ArrayList<>() ;
for(long i = 1L; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n) list.add(i) ;
else{ list.add(i) ; list.add(n/i) ; }
}
}
return list ;
}
public static void reverseArray(int[] arr){
int i = 0;
int j = arr.length-1;
while(i<j){
swap(arr,i,j);
i++;
j--;
}
}
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 | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 39019beaaa2e6ef32f1f55e3b6162262 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
/*AUTHOR - ELDIIAR DZHUNUSOV */
public class a {
static int mod= 1000000007 ;
static final FastReader in = new FastReader();
static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
long t = in.nextLong();
for (long i = 1; i <= t; i++) {
new Solver();
}
out.flush();
out.close();
}
static class Solver{
Solver(){
//insert your code here
long x = in.nextLong();
long n = in.nextLong();
if(x%2==0){
int piv = (int) (n%4);
if(piv==0){
out.println(x);
}else if(piv==2){
out.println(x+1);
}else if(piv==1){
if(n!=1){
out.println(x-n);
}else{
out.println(x-1);
}
}else if(piv==3){
out.println(x+n+1);
}
}else{
int piv = (int) (n%4);
if(piv==0){
out.println(x);
}else if(piv==2){
out.println(x-1);
}else if(piv==1){
if(n!=1){
out.println(x+n);
}else{
out.println(x+1);
}
}else if(piv==3){
out.println(x-n-1);
}
}
}
}
// Collections.sort(arrayList);
// sort 1d
// sort(arr, 0, arr.length - 1);
// Sort 2d by the first index
// Arrays.sort(arr, Comparator.comparingDouble(o -> o[0]));
public static void swap(int[] arr, int i, int j){
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
static boolean isPrime(long n)
{
if(n==1)return false ;
for(long i = 2L; i*i <= n ;i++){ if(n% i ==0){return false; } }
return true ;
}
static boolean isPrime(int n)
{
if(n==1)return false ;
for(int i = 2; i*i <= n ;i++){ if(n% i ==0){return false ; } }
return true ;
}
public static int gcd(int a, int b )
{
if(b==0)return a ;else return gcd(b,a%b) ;
}
public static long gcd(long a, long b )
{
if(b==0)return a ;else return gcd(b,a%b) ;
}
static boolean isPower(int n, int p){
if(p==0) return n==1;
return (Double.compare(Math.pow(n,1.0/p),(int)Math.pow(n,1.0/p))==0);
}
static boolean isPower(long n, long p){
if(p==0L) return n==1L;
return (Double.compare(Math.pow(n,1.0/p),(long)Math.pow(n,1.0/p))==0);
}
public static long nCr(int n,int k) {
long ans=1L;
k=k>n-k?n-k:k;
int j=1;
for(;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
public static ArrayList<Integer> allFactors(int n)
{
ArrayList<Integer> list = new ArrayList<>() ;
for(int i = 1; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n)list.add(i) ;
else{ list.add(i) ;list.add(n/i) ; }
}
}
return list ;
}
public static ArrayList<Long> allFactors(long n)
{
ArrayList<Long> list = new ArrayList<>() ;
for(long i = 1L; i*i <= n ;i++)
{
if( n % i == 0)
{
if(i*i == n) list.add(i) ;
else{ list.add(i) ; list.add(n/i) ; }
}
}
return list ;
}
public static void reverseArray(int[] arr){
int i = 0;
int j = arr.length-1;
while(i<j){
swap(arr,i,j);
i++;
j--;
}
}
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 | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 997725638c528e75a9770bd2dd10e1d7 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
for(long i=0;i<t;i++){
long x=sc.nextLong();
long n=sc.nextLong();
if(n==0 || n%4==0){
System.out.println(x);
continue;
}
long ans=0;
long news=n;
while(news%4!=0){
news-=1;
}
int run=0;
for(long u=news+1;u<=n;u++){
run++;
if(x%2==0){
ans=x-u;
}else{
ans=x+u;
}
x=ans;
}
System.out.println(ans);
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 2f737467cb64a7ef85b3de0889c5dd61 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Codeforces {
public static void main(String[] args) {
FastReader fastReader = new FastReader();
int t = fastReader.nextInt();
while (t-- > 0) {
long x= fastReader.nextLong();
long n = fastReader.nextLong();
long rem = Math.abs(n%4);
long st = n;
long index= 0;
if (x%2 == 0) {
if (rem == 2L) {
index = x + 1;
}
if (rem == 1L) {
index = x - st;
}
if (rem == 3L) {
index = (x + st) + 1;
}
if (rem == 0L) {
index = x;
}
}else{
if (rem == 2L) {
index = x -1;
}
if (rem == 1L) {
index = (x + st);
}
if (rem == 3L) {
index = (x - st) - 1;
}
if (rem == 0L) {
index = x;
}
}
System.out.println(index);
}
}
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 | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 7ff1c2c9f9b3a61648ecaadd5b4aba0f | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class problemB{
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
StringBuilder sb=new StringBuilder();
while(t-->0){
String[]s=br.readLine().split(" ");
long x=Long.parseLong(s[0]);
long n=Long.parseLong(s[1]);
long ans=0;
if(n%4==0)
ans=0;
else if(n%4==1)
ans=-n;
else if(n%4==2)
ans=1;
else
ans=1+n;
long temp=(x%2==0)?1:-1;
long res=x+temp*ans;
sb.append(res+"\n");
}
System.out.println(sb);
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 6d69ada2e8cca3f6fd17ae331ddb69a3 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class problemA{
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
StringBuilder sb=new StringBuilder();
while(t-->0){
String[]s=br.readLine().split(" ");
long x=Long.parseLong(s[0]);
long n=Long.parseLong(s[1]);
if(x%2==0){
if(n%4==0)
sb.append(x+"\n");
else if(n%4==1)
sb.append((x-n)+"\n");
else if(n%4==2)
sb.append((x+1)+"\n");
else
sb.append((x+n+1)+"\n");
}else{
if(n%4==0)
sb.append(x+"\n");
else if(n%4==1)
sb.append((x+n)+"\n");
else if(n%4==2)
sb.append((x-1)+"\n");
else
sb.append((x-n-1)+"\n");
}
}
System.out.println(sb);
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | ebd120f948b07ed5cbcb3ef99e34939d | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
long[] a = new long[n];
long[] b = new long[n];
for(int i = 0;i < n;i++){
a[i] = scanner.nextLong();
b[i] = scanner.nextLong();
scanner.nextLine();
}
for(int i = 0;i < n;i++){
long ans = 0;
long x = b[i] % 4;
if((a[i]&1) == 0){
if(x == 0) ans = a[i];
else if(x == 1) ans = a[i]-b[i];
else if(x == 2) ans = a[i]+1;
else ans = a[i]+1+b[i];
}else{
if(x == 0) ans = a[i];
else if(x == 1) ans = a[i]+b[i];
else if(x == 2) ans = a[i]-1;
else ans = a[i]-1-b[i];
}
System.out.println(ans);
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 514dfe77c813c44b877f353b9a9c0d6b | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes |
import java.math.BigInteger;
import java.util.*;
public class Solution {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int t = in.nextInt();
while(t-->0) {
BigInteger x = new BigInteger(in.next());
BigInteger n = new BigInteger(in.next());
BigInteger modN = n.mod(new BigInteger("4"));
if (modN.equals(new BigInteger("0"))) {
System.out.println(x);
} else {
boolean even = x.mod(new BigInteger("2")).equals(new BigInteger("0"));
if (even) {
if (modN.equals(new BigInteger("1"))) {
System.out.println(x.subtract(n));
} else if (modN.equals(new BigInteger("2"))) {
System.out.println(x.add(new BigInteger("1")));
} else if (modN.equals(new BigInteger("3"))) {
System.out.println(x.add(n.add(new BigInteger("1"))));
}
} else {
if (modN.equals(new BigInteger("1"))) {
System.out.println(x.add(n));
} else if (modN.equals(new BigInteger("2"))) {
System.out.println(x.subtract(new BigInteger("1")));
} else if (modN.equals(new BigInteger("3"))) {
System.out.println(x.subtract(n.add(new BigInteger("1"))));
}
}
}
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | e8a45230862baf4d512b7abdde697677 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class Codeforces {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
while(sc.hasNext()) {
int t = sc.nextInt();
while(t-->0){
long a = sc.nextLong();
long b = sc.nextLong();
long rem = b%4;
long temp = 0;
if(rem!=0) {
if(a%2 == 0) {
long start = (b/4)*4;
start++;
temp = temp - start;
start++;
int cnt = 2;
while(cnt<=rem) {
temp+=start;
start++;
cnt++;
}
}
else {
long start = (b/4)*4;
start++;
temp = temp+start;
start++;
int cnt = 2;
while(cnt<=rem) {
temp = temp - start;
start++;
cnt++;
}
}
//System.out.println(temp);
}
long ans = a + temp;
System.out.println(ans);
}
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 07758e81ffe3701003965c5becf90d6b | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static long mod = 1000000007;
static long max ;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
int t = sc.nextInt();
while( t-- > 0) {
long x = sc.nextLong();
long n = sc.nextLong();
if( x % 2 == 0) {
x--;
if( n == 0) {
out.println(x+1);
}
else if( n-1 == 0) {
out.println(x);
}
else {
x+=2;
long fst = (n-2)%4;
long scnd = 2 + ((n-2)/4)*4 + 1 ;
// out.println( x + " " + fst + " "+ scnd);
for( int i = 1 ; i<= fst ; i++) {
if( i == 1) {
x+=scnd++;
}
if( i == 2) {
x-=scnd++;
}
if( i == 3) {
x-=scnd++;
}
}
out.println(x);
}
}
else {
x++;
if( n == 0) {
out.println(x-1);
}
else if( n-1 == 0) {
out.println(x);
}
else {
x-=2;
long fst = (n-2)%4;
long scnd = 2 + ((n-2)/4)*4 + 1 ;
// out.println( x + " " + fst + " "+ scnd);
for( int i = 1 ; i<= fst ; i++) {
if( i == 1) {
x-=scnd++;
}
if( i == 2) {
x+=scnd++;
}
if( i == 3) {
x+=scnd++;
}
}
out.println(x);
}
}
}
out.flush();
}
public static boolean ifpowof2(long n ) {
return ((n&(n-1)) == 0);
}
public static int[] nextLargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[n-1] = -1;
stack.push( n-1);
for( int i = n-2 ;i >= 0 ; i--){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.push( i);
}
return rtrn;
}
@SuppressWarnings("unused")
private static void mySort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static long rightmostsetbit(long n) {
return n&-n;
}
static long leftmostsetbit(long n)
{
long k = (long)(Math.log(n) / Math.log(2));
return 1 << k;
}
static HashMap<Long,Long> primefactor( long n){
HashMap<Long ,Long> hm = new HashMap<>();
long temp = 0;
while( n%2 == 0) {
temp++;
n/=2;
}
if( temp!= 0) {
hm.put( 2L, temp);
}
long c = (long)Math.sqrt(n);
for( long i = 3 ; i <= c ; i+=2) {
temp = 0;
while( n% i == 0) {
temp++;
n/=i;
}
if( temp!= 0) {
hm.put( i, temp);
}
}
if( n!= 1) {
hm.put( n , 1L);
}
return hm;
}
@SuppressWarnings("unused")
private static ArrayList<Integer> allfactors(int abs) {
HashMap<Integer,Integer> hm = new HashMap<>();
ArrayList<Integer> rtrn = new ArrayList<>();
for( int i = 2 ;i*i <= abs; i++) {
if( abs% i == 0) {
hm.put( i , 0);
hm.put(abs/i, 0);
}
}
for( int x : hm.keySet()) {
rtrn.add(x);
}
if( abs != 0) {
rtrn.add(abs);
}
return rtrn;
}
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 | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 16a8ee9d8135634c2d4a88cc6f9044b1 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.Scanner;
public class OddGrasshopper {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
long x = sc.nextLong();
long n = sc.nextLong();
long i = n - n % 4 + 1;
n = n % 4;
while(n-- > 0){
if(x % 2 == 0){
x -= i;
}else{
x += i;
}
i++;
}
System.out.println(x);
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | d43395e2f08bbda16bd9ab907d08f9db | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.Scanner;
public class HelloWorld {
public static long result(long x,long n){
for(long i=(n-(n%4)+1);i<=n;i++){
if(x%2==0) x = x-i;
else x = x+i;
}
return x;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(int i=0;i<t;i++){
long x = scan.nextLong();
long n = scan.nextLong();
System.out.println(result(x,n));
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 8a9c27b25ef5d2c6606aa388017f69ee | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.*;
import java.util.*;
public class B
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine().trim());
StringBuilder sb=new StringBuilder();
while(T-->0)
{
String[] s=br.readLine().trim().split(" ");
long start=Long.parseLong(s[0]);
long N=Long.parseLong(s[1]);
for(long i=4*(N/4)+1;i<=N;i++)
{
if(start%2==0) start-=i;
else start+=i;
}
sb.append(start).append("\n");
}
System.out.println(sb);
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 89a2ee168242734e4b4f8859307ebd50 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
for (int tt = 0; tt < t; tt++) {
long x = fs.nextLong();
long n = fs.nextLong();
long ans = 0;
if (n % 4 == 0) ans = 0;
if (n % 4 == 1) ans = -n;
if (n % 4 == 2) ans = 1;
if (n % 4 == 3) ans = n + 1;
if (x % 2 != 0) ans *= -1;
System.out.println(ans + x);
}
}
static void sort(int[] a) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a)
al.add(i);
Collections.sort(al);
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 7ef712a6da75a3d6d46f6fa81fb21e75 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
for (int tt = 0; tt < t; tt++) {
long x = fs.nextLong();
long n = fs.nextLong();
long n2 = (n / 4) * 4;
for (long i = n2 + 1; i <= n; i++) {
if (x % 2 == 0) {
x -= i;
} else {
x += i;
}
}
System.out.println(x);
}
}
static void sort(int[] a) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a)
al.add(i);
Collections.sort(al);
for (int i = 0; i < a.length; i++)
a[i] = al.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 86a5d17bbde4008f2e81bafc1abc8e54 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.*;
public class MyClass {
public static void main(String args[])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long t=Long.parseLong(br.readLine());
while(t-->0){
String s[]=br.readLine().split(" ");
long x=Long.parseLong(s[0]);
long n=Long.parseLong(s[1]);
if(x%2==0){
if(n%4==0) System.out.println(x);
else if(n%4==1) System.out.println(-1*n+x);
else if(n%4==2) System.out.println(1+x);
else System.out.println(n+x+1);
}
else{
if(n%4==0) System.out.println(x);
else if(n%4==1) System.out.println(n+x);
else if(n%4==2) System.out.println(-1+x);
else System.out.println(-1*(n+1)+x);
}
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | ecda2b685d79757f86b9c2dad2814171 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | /**
* 11/02/21 morning
* https://codeforces.com/contest/1607/problem/B
*/
// package codeforce.cf.div3.r753;
import java.util.*;
import java.io.*;
public class B {
static PrintWriter pw;
/*
n is odd
-1 + 1 - 1 + 1
9 ( + 1 - 2 - 3 + 4 + 5 - 6 - 7 + 8) + 9 = 18 (9, 9)
+1 -1 + 1 - 1
10 (- 1 + 2 + 3 - 4 - 5 + 6 + 7 - 8) - 9 = 1 (10, 9)
n is even
-1+1-1+1-1
9 (+ 1 - 2 - 3 + 4 + 5 - 6 - 7 + 8 + 9 - 10) = 8 (9, 10)
+ 1 - 1 + 1 - 1 + 1
10 - 1 + 2 + 3 - 4 - 5 + 6 + 7 - 8 - 9 + 10 = 11 (10, 10)
1 - 0
-1 + 1 -1
9 ( + 1 - 2 - 3 + 4 + 5 - 6) - 7 = 1 (9 7)
+ 1 -1 + 1
10 (- 1 + 2 + 3 - 4 - 5 + 6) + 7 = 18 (10 7)
-1 + 1 - 1
9 ( +1 - 2 - 3 + 4 + 5 - 6) = 8 (9 6)
+ 1 - 1 + 1
10 ( -1 + 2 + 3 - 4 - 5 + 6) = 11 (10 6)
*/
// don't know
void solve(long x, long n) {
// tr(x, n);
long last = n;
long res = x;
if (x % 2 != 0 && n % 2 == 1) {
long pair = (n - 1) / 2;
if (pair % 2 != 0) res--;
// tr("pair", pair, "res", res);
res += res % 2 == 0 ? -last : last;
} else if (x % 2 == 0 && n % 2 == 1) {
long pair = (n - 1) / 2;
if (pair % 2 != 0) res++;
// tr("pair", pair, "res", res);
res += res % 2 == 0 ? -last : last;
} else if (x % 2 != 0 && n % 2 == 0) {
long pair = n / 2;
if (pair % 2 != 0) res--;
} else if (x % 2 == 0 && n % 2 == 0) {
long pair = n / 2;
if (pair % 2 != 0) res++;
}
pr(res);
}
void solve1(long x, long n) {
long res = x;
for (int i = 1; i <= n; i++) {
long add = res % 2 == 0 ? -i : i;
res += add;
}
pr(res);
}
private void run() {
// read_write_file(); // comment this before submission
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while (t-- > 0) {
long x = fs.nextLong(), n = fs.nextLong();
solve(x, n);
}
}
private final String INPUT = "input.txt";
private final String OUTPUT = "output.txt";
void read_write_file() {
FileInputStream instream = null;
PrintStream outstream = null;
try {
instream = new FileInputStream(INPUT);
outstream = new PrintStream(new FileOutputStream(OUTPUT));
System.setIn(instream);
System.setOut(outstream);
} catch (Exception e) {
}
}
public static void main(String[] args) {
pw = new PrintWriter(System.out);
new B().run();
pw.close();
}
void pr(int num) {
pw.println(num);
}
void pr(long num) {
pw.println(num);
}
void pr(double num) {
pw.println(num);
}
void pr(String s) {
pw.println(s);
}
void pr(char c) {
pw.println(c);
}
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;
}
Integer[] readIntegerArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
void tr(Object... o) {
pw.println(Arrays.deepToString(o));
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | fc5e327c4ad4e83acba13283d92a112e | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
long x=sc.nextLong();
long n=sc.nextLong();
long m=n/10;
if(n==0)
{
System.out.println(x);
continue;
}
if(m%2!=0)
{
if(x%2==0)
{
x++;
}
else
{
x--;
}
}
for(long i=m*10+1;i<=n;i++)
{
if(x%2==0)
{
x-=i;
}
else
{
x+=i;
}
}
System.out.println(x);
}
}
static int findfrequencies(int a[],int n)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
count++;
}
}
return count;
}
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());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int a[])
{
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
a[i]=list.get(i);
}
return a;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int DigitSum(int n)
{
int r=0,sum=0;
while(n>=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
return sum;
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | bdcbf951be95e5ac87b08dbaf779ccad | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 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 final class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
sc.nextLine();
while(t > 0){
long x = sc.nextLong();
long n = sc.nextLong();
if(x % 2 == 0){
if(n % 4 == 0){
System.out.println(x);
}else if (n % 4 == 1){
System.out.println(x - n);
}else if(n % 4 == 2){
System.out.println(x + 1);
}else{
System.out.println(x + 1 + n);
}
}else{
if(n % 4 == 0){
System.out.println(x);
}else if (n % 4 == 1){
System.out.println(x + n);
}else if(n % 4 == 2){
System.out.println(x - 1);
}else{
System.out.println(x - 1 - n);
}
}
t--;
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 9801e690554993487d6d660e6ccafe14 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
short t = Short.parseShort(br.readLine());
StringTokenizer st;
long x0, jumps;
for (short i=0; i<t; i++) {
st = new StringTokenizer(br.readLine());
x0 = Long.parseLong(st.nextToken());
jumps = Long.parseLong(st.nextToken());
if (x0 % 2 == 0) {
switch ((int)(jumps % 4)) {
case 1:
x0-= jumps;
break;
case 2:
x0+= 1;
break;
case 3:
x0+= jumps+1;
}
} else {
switch ((int)(jumps % 4)) {
case 1:
x0+= jumps;
break;
case 2:
x0-=1;
break;
case 3:
x0-= jumps+1;
}
}
pw.println(x0);
}
pw.close();
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | dfe3250803f49f370b76c924b46976c7 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
public class Basha{
public static void main(String []argv){
Scanner in = new Scanner(System.in);
int z;
z = in.nextInt();
while(z!=0){
long a = in.nextLong();
long b = in.nextLong();long c = a;
long temp = 0;
for(long i=1;i<=b;i++) {
if(c%2==0) c = c - i;
else c = c + i;
if(c == a) { temp = b / i;temp *= i; i = temp;}
}
System.out.println(c);
z--;
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 11 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 4cd288edbc040e778bc0d3c7615870b1 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
private static void run() throws IOException {
long x = in.nextLong();
long n = in.nextLong();
if (n == 0) {
out.println(x);
return;
}
long round = n / 4;
long remain = n % 4;
long base = 1 + 4 * round;
int[] d = new int[]{0, 1, -1, -1, 1};
int p = x % 2 == 0 ? -1 : 1;
for (int i = 1; i <= remain; i++) {
x += d[i] * p * (base + i - 1);
}
out.println(x);
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 17 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 176b70bb51c0bbb155491cc9d1916391 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* The grasshopper is located on the numeric axis at the point with coordinate x0.
*
* Having nothing else to do he starts jumping between integer points on the axis.
* Making a jump from a point with coordinate x with a distance d to the left moves the grasshopper to
* a point with a coordinate x−d, while jumping to the right moves him to a point with a coordinate x+d.
*
* The grasshopper is very fond of positive integers, so for each integer i starting with 1 the following holds:
* exactly i minutes after the start he makes a jump with a distance of exactly i. So, in the first minutes he jumps by 1,
* then by 2, and so on.
*
* The direction of a jump is determined as follows: if the point where the grasshopper was before the jump
* has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.
*
* For example, if after 18 consecutive jumps he arrives at the point with a coordinate 7,
* he will jump by a distance of 19 to the right, since 7 is an odd number, and will end up at a point 7+19=26.
* Since 26 is an even number, the next jump the grasshopper will make to the left by a distance of 20,
* and it will move him to the point 26−20=6.
*
* Find exactly which point the grasshopper will be at after exactly n jumps.
*/
public class OddGrasshopper {
private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int testCases = Integer.parseInt(reader.readLine());
for (int i = 0; i < testCases; i++) {
check();
}
}
private static void check() throws IOException {
Long[] data = Arrays.stream(reader.readLine().split(" ")).map(Long::parseLong).toArray(Long[]::new);
long startPos = data[0];
long jumps = data[1];
long realJumps = jumps%4;
if (realJumps == 0) {
System.out.println(startPos);
return;
}
for (long i = jumps - realJumps + 1; i <= jumps; i++) {
if (startPos % 2 == 0) {
startPos -= i;
} else {
startPos += i;
}
}
System.out.println(startPos);
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 17 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 5c5f7cbca3788821cc523892641fbf94 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.PriorityQueue;
import java.util.Deque;
public class scratch{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int test_cases = sc.nextInt();
outer : for(int h=0;h<test_cases;h++){
//scan the input
long x0 = sc.nextLong();
long n = sc.nextLong();
long d = 0;
if(x0%2==0){
if(n%4==0){
d=0;
}
else if(n%4==1){
d-=n;
}
else if(n%4==2){
d=1;
}
else{
d = n+1;
}
System.out.println(x0+d);
}
else{
if(n % 4 == 0)
{
d = -1;
}
else if(n % 4 == 1)
{
d = n - 1;
}
else if(n % 4 == 2)
{
d = -2;
}
else
{
d = -(n + 2);
}
System.out.println(x0+d+1);
}
}
sc.close();
}
public static void scan_string_array(String s[] , int n, Scanner sc){
for(int i=0;i<n;i++){
s[i] = sc.next();
}
}
public static void swap_elements(int a[], int s_index, int e_index){
int temp = a[s_index];
a[s_index] = a[e_index];
a[e_index] = temp;
}
public static void scan_array_long(long a[], int n, Scanner sc){
for(int i=0;i<n;i++){
a[i] = sc.nextLong();
}
}
public static void scan_array(int a[], int n, Scanner sc){
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
}
public static void print_array(int a[]){
for(int i=0;i<a.length;i++){
if(i==a.length-1){
System.out.println(a[i]);
return;
}
System.out.print(a[i]+" ");
}
}
public static long sum_array(long a[]){
long sum = 0;
for(int i=0;i<a.length;i++){
sum+=a[i];
}
return sum;
}
public static boolean perfect_square(double num){
boolean isSq = false;
double b = Math.sqrt(num);
double c = Math.sqrt(num) - Math.floor(b);
if(c==0){
isSq=true;
}
return isSq;
}
public static boolean ispalindrome(String s){
int i=0, j =s.length()-1;
while(i<=j){
if(s.charAt(i)!=s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
public static boolean ispowerOftwo(int n){
if(n>0 && (int)(n & (n-1))==0){
return true;
}
else{
return false;
}
}
public static int calculate_gcd(int x,int y){
int gcd = 1;
if(x%y==0){
return y;
}
else if(y%x==0){
return x;
}
else{
for(int i=2;i<=x && i<=y;i++){
if(x%i==0 && y%i==0){
gcd = i;
}
}
return gcd;
}
}
public static long calculate_factorial(long x){
long ans = 1;
for(int i=1;i<=x;i++){
ans*=i;
}
return ans;
}
public static void swap(int a[], int b[], int index_a, int index_b){
int temp = a[index_a];
a[index_a] = b[index_b];
b[index_b] = temp;
}
public static int find_maximum(int a[]){
int max = Integer.MIN_VALUE;
for(int i=0;i<a.length;i++){
max = Math.max(a[i], max);
}
return max;
}
public static int find_minimum(int a[]){
int min = Integer.MAX_VALUE;
for(int i=0;i<a.length;i++){
min = Math.min(a[i], min);
}
return min;
}
public static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
Random random = new Random();
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i];
a[i]=temp;
}
Arrays.sort(a);
}
public static int sum_of_digits(int n){
int s =0;
while(n!=0){
s+=n%10;
n/=10;
}
return s;
}
public static int[] mergeSort(int[] array) {
if (array.length <= 1) {
return array;
}
int midpoint = array.length / 2;
int[] left = new int[midpoint];
int[] right;
if (array.length % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
int[] result = new int[array.length];
left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);
return result;
}
public static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 17 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | aedc1b665cec7501fd157e10c335e272 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.io.*;
//import java.util.*;
public class OldGrasshopper {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0) {
String[] str = br.readLine().split("\\s+");
long x = Long.parseLong(str[0]), n = Long.parseLong(str[1]), z = 0;
if (n%4==1)
z = -n;
else if (n%4==2)
z = 1;
else if (n%4==3)
z = n+1;
if ((x&1)==1)
System.out.println(x-z);
else
System.out.println(x+z);
}
}
} | Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 17 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 4bff03df2fe6e599e676db54e56fecdb | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round753B {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
Round753B sol = new Round753B();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
long x0, n;
void getInput() {
x0 = in.nextLong();
n = in.nextLong();
}
void printOutput() {
out.printlnAns(ans);
}
long ans;
void solve(){
// x0 is even
// 1: -1 (odd)
// 2: +1 (odd)
// 3: +4 (even)
// 4: 0 (even)
// 5: -5 (odd)
// 6: +1 (odd)
// 7: +8 (even)
// 8: 0 (even)
ans = x0;
long add = switch((int)(n % 4)) {
case 0 -> 0;
case 1 -> -n;
case 2 -> 1;
case 3 -> n+1;
default -> 0;
};
if(x0 % 2 == 0)
ans += add;
else
ans -= add;
// x0 is odd
// 1: +1 (even)
// 2: -1 (even)
// 3: -4 (odd)
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean[] ans) {
for(boolean b: ans)
printlnAns(b);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 17 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 6d6cfaaa3c577bf6010f8221b4291db4 | train_108.jsonl | 1635863700 | The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps. | 256 megabytes | import java.util.*;
public class another {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-- > 0){
long x = sc.nextLong();
long n = sc.nextLong();
long ans = x;
if(x%2 == 0){
long r = n%4;
if(r == 0){
ans = x;
}else if(r == 1){
ans = x-n;
}else if(r == 2){
ans = x+1;
}else{
ans = x+1+n;
}
}else{
long r = n%4;
if(r == 0){
ans = x;
}else if(r == 1){
ans = x+n;
}else if(r == 2){
ans = x-1;
}else{
ans = x-1-n;
}
}
System.out.println(ans);
}
}
static class Pair{
int c,idx;
// public Pair(int u, int v){
// this.c = u;
// this.idx = v;
// }
}
static class Sorting implements Comparator<Pair>{
public int compare(Pair p1, Pair p2){
if(p2.c==p1.c){
return p2.c-p1.c;
}
return p2.c - p1.c;
}
}
}
| Java | ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"] | 1 second | ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"] | NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$. | Java 17 | standard input | [
"math"
] | dbe12a665c374ce3745e20b4a8262eac | The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps. | 900 | Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$. | standard output | |
PASSED | 66e5c1660a59f7f2d1dd26c5b293c50d | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round753H {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
Round753H sol = new Round753H();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
int n;
int[] a, b, m;
void getInput() {
n = in.nextInt();
a = new int[n];
b = new int[n];
m = new int[n];
for(int i=0; i<n; i++) {
a[i] = in.nextInt();
b[i] = in.nextInt();
m[i] = in.nextInt();
}
}
void printOutput() {
out.printlnAns(ans);
for(int i=0; i<n; i++) {
out.print(x[i]);
out.print(' ');
out.println(y[i]);
}
}
int ans;
int[] x, y;
void solve(){
x = new int[n];
y = new int[n];
// diag = a[i] + b[i] - m[i] is always same
// and then for each diag,
// we have ranges for a[i]-x[i]
// how many points do we need to pin all intervals?
// sort intervals by starting point
// all alive intervals can be pinned by one point
// when ending point is met, pin all alive intervals and discard them
final int MAX = 1_000_000;
ans = 0;
HashMap<Integer, ArrayList<Integer>> diagToIndices = new HashMap<>();
for(int i=0; i<n; i++) {
int diag = a[i] + b[i] - m[i];
if(!diagToIndices.containsKey(diag))
diagToIndices.put(diag, new ArrayList<>());
diagToIndices.get(diag).add(i);
}
for(var entry: diagToIndices.entrySet()) {
int diag = entry.getKey();
Integer[] indices = entry.getValue().toArray(new Integer[0]);
Arrays.sort(indices, (i, j) -> Integer.compare(Math.max(0, a[i]-m[i]), Math.max(0, a[j]-m[j])));
int k = indices.length;
int from = -1;
int cut = -1;
for(int i=0; i<k; i++) {
int index = indices[i];
int start = Math.max(0, a[index]-m[index]);
if(cut != -1 && start > cut) {
ans++;
for(int j=from; j<i; j++) {
int index2 = indices[j];
x[index2] = a[index2]-cut;
y[index2] = m[index2]-x[index2];
}
cut = -1;
}
int end = a[index] - (m[index] - (b[index] - Math.max(0, b[index]-m[index])));
if(cut == -1) {
from = i;
cut = end;
}
else {
cut = Math.min(cut, end);
}
}
if(cut != -1) {
ans++;
for(int j=from; j<k; j++) {
int index = indices[j];
x[index] = a[index]-cut;
y[index] = m[index]-x[index];
}
}
}
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean[] ans) {
for(boolean b: ans)
printlnAns(b);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 17 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 0d8d6c4c610f27fa44b1df391c76ff2b | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | // Problem: H. Banquet Preparations 2
// Contest: Codeforces - Codeforces Round #753 (Div. 3)
// URL: https://codeforces.com/contest/1607/problem/H
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.io.*;
import java.util.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
private void preparation() {
}
private void clear() {
}
private void solve() throws Exception {
// print("");
rf();rf();
int n = ri();
int [] a = new int[n];
int [] b = new int[n];
int [] m = new int[n];
HashMap<Integer, ArrayList<Integer>> mp = new HashMap<>();
for(int i = 0; i < n; i++){
rf();
a[i] = ri();
b[i] = ri();
m[i] = ri();
int t = a[i] + b[i] - m[i];
if(!mp.containsKey(t)){
mp.put(t, new ArrayList<Integer>());
}
mp.get(t).add(i);
}
int [] ans = new int[n];
int cnt = 0;
for(ArrayList<Integer> idxs : mp.values()){
int [][] spans = new int[idxs.size() + 1][3];
for(int i = 0; i < idxs.size(); i++){
int idx = idxs.get(i);
spans[i][0] = max(0,a[idx] - m[idx]);
spans[i][1] = a[idx] - max(m[idx] - b[idx], 0);
spans[i][2] = idx;
}
spans[idxs.size()][0] = Integer.MAX_VALUE;
spans[idxs.size()][1] = Integer.MAX_VALUE;
Arrays.sort(spans, (int [] sa, int [] sb)->{
if(sa[0] == sb[0]){
return sa[1] - sb[1];
}else{
return sa[0] - sb[0];
}
});
cnt += 1;
int l = spans[0][0], r = spans[0][1];
int pre = 0;
for(int i = 1; i < spans.length; i++){
boolean hasIn = false;
if(spans[i][0] >= l && spans[i][0] <= r){
hasIn = true;
l = spans[i][0];
}
if(spans[i][1] <= r){
hasIn = true;
r = spans[i][1];
}
if(!hasIn){
// print("l = %d, r = %d", l,r);
for(int j = pre; j < i; j++){
int idx = spans[j][2];
ans[idx] = a[idx] - l;
// if(m[idx] - ans[idx] == -1){
// print("l = %d, r = %d", l,r);
// print("idx = %d", idx);
// print("span = (%d, %d)", spans[j][0], spans[j][1]);
// }
}
if(i == spans.length - 1){
break;
}
pre = i;
cnt ++;
l = spans[i][0];
r = spans[i][1];
}
}
}
addAns(cnt);
for(int i = 0; i < n; i++){
sb.append(ans[i] + " " + (m[i] - ans[i]) + "\n");
}
}
private void run() throws Exception {
int T = 1;
rf();
T = ri();
preparation();
while (T-- > 0) {
solve();
if (T != 0) {
clear();
}
}
printAns();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
StringBuilder sb = new StringBuilder();
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer strT;
private void addAns(int a){
sb.append(a + "\n");
}
private void addAns(long a){
sb.append(a + "\n");
}
private void addAns(String s){
sb.append(s + "\n");
}
private void rf() throws IOException {
strT = new StringTokenizer(infile.readLine());
}
private int ri() {
return Integer.parseInt(strT.nextToken());
}
private long rl() {
return Long.parseLong(strT.nextToken());
}
private char [] rs2c(){
return strT.nextToken().toCharArray();
}
private String rs(){
return strT.nextToken();
}
private void printAns() {
System.out.println(sb);
}
private int[] readArr(int N) throws Exception {
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(strT.nextToken());
}
return arr;
}
private long[] readArr2(int N) throws Exception {
long[] arr = new long[N];
for (int i = 0; i < N; i++) {
arr[i] = Long.parseLong(strT.nextToken());
}
return arr;
}
private void print(String format, Object ... args){
System.out.println(String.format(format,args));
}
private void print(int[] arr, int x) {
//for debugging only
for (int i = 0;i < min(arr.length, x); i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
private void print(long[] arr, int x) {
//for debugging only
for (int i = 0;i < min(arr.length, x); i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | f8cff896098967afca5cb9678e2c4744 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | /*
I am dead inside
Do you like NCT, sKz, BTS?
5 4 3 2 1 Moonwalk
Imma knock it down like domino
Is this what you want? Is this what you want?
Let's ttalkbocky about that :()
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class x1607H
{
static final int INF = Integer.MAX_VALUE/2;
public static void main(String followthekkathyoninsta[]) throws Exception
{
FastScanner infile = new FastScanner();
int T = infile.nextInt();
StringBuilder sb = new StringBuilder();
while(T-->0)
{
int N = infile.nextInt();
int[] arr = new int[N];
int[] brr = new int[N];
int[] eat = new int[N];
int[] extraEaten = new int[N];
for(int i=0; i < N; i++)
{
arr[i] = infile.nextInt();
brr[i] = infile.nextInt();
eat[i] = infile.nextInt();
if(brr[i] < eat[i])
{
extraEaten[i] = eat[i]-brr[i];
eat[i] = brr[i];
arr[i] -= extraEaten[i];
}
}
int[] target = new int[N]; //target arr value
int distinct = 0;
HashMap<Integer, ArrayList<Integer>> buckets = new HashMap<Integer, ArrayList<Integer>>();
for(int i=0; i < N; i++)
{
int key = arr[i]+brr[i]-eat[i];
if(!buckets.containsKey(key))
buckets.put(key, new ArrayList<Integer>());
buckets.get(key).add(i);
}
for(int key: buckets.keySet())
{
ArrayList<Integer> ls = buckets.get(key);
Collections.sort(ls, (x, y) -> {
return arr[x]-arr[y];
});
int curr = -INF;
for(int plate: ls)
{
if(curr < arr[plate]-eat[plate])
{
curr = arr[plate];
distinct++;
}
target[plate] = curr;
}
}
sb.append(distinct+"\n");
for(int i=0; i < N; i++)
{
int fish = arr[i]-target[i];
int meat = eat[i]-fish;
fish += extraEaten[i];
sb.append(fish+" "+meat);
sb.append("\n");
}
}
System.out.print(sb);
}
}
/*
a_i+b_i-m_i = a_j+b_j-m_j
WLOG let a_i <= a_j
i can pair with j iff a_j-m_j <= a_i
range: [a_i-m_i : a_i]
*/
/*
1
2
3 4 1
5 1 2
*/
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 497c61599601a1018fb79b46d64a7b83 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution{
static void main() throws Exception{
int n=sc.nextInt();
int[][]in=new int[n][];
int[][]arr=new int[n<<1][];
int[]ans=new int[n];
for(int i=0;i<n;i++){
int a=sc.nextInt(),b=sc.nextInt(),m=sc.nextInt();
in[i]=new int[]{a,b,m};
arr[i<<1]=new int[]{a+b-m,Math.max(0,a-m),0,i};
arr[i<<1|1]=new int[]{a+b-m,Math.min(a,a-(m-b)),1,i};
ans[i]=-1;
}
int cnt=n;
Arrays.sort(arr,(x,y)->x[0]==y[0]?(x[1]==y[1]?(x[2]-y[2]):(x[1]-y[1])):(x[0]-y[0]));
LinkedList<Integer>q=new LinkedList<>();
for(int i=0;i<(n<<1);){
if(arr[i][2]==0){
q.add(arr[i][3]);
i++;
continue;
}
boolean found=false;
while(i+1<(n<<1) && arr[i][0]==arr[i+1][0] && arr[i][1]==arr[i+1][1]){
if(ans[arr[i][3]]!=-1){
i++;
continue;
}
i++;
found=true;
}
if(ans[arr[i][3]]==-1){
found=true;
}
if(!found){
i++;
continue;
}
cnt++;
while(!q.isEmpty()){
int idx=q.poll();
cnt--;
ans[idx]=arr[i][1];
}
i++;
}
pw.println(cnt);
for(int i=0;i<n;i++){
pw.println((in[i][0]-ans[i])+" "+((in[i][2]-(in[i][0]-ans[i]))));
}
}
public static void main(String[] args) throws Exception{
sc=new MScanner(System.in);
// sc=new MScanner(" .in");
pw = new PrintWriter(System.out);
int tc=1;
tc=sc.nextInt();
for(curt=1;curt<=tc;curt++) {
// pw.printf("Case #%d:", curt);
main();
}
pw.flush();
}
static int curt;
static PrintWriter pw;
static MScanner sc;
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 dbg(int[]in) {
System.out.println(Arrays.toString(in));
}
static void dbg(long[]in) {
System.out.println(Arrays.toString(in));
}
static void sort(int[]in) {
shuffle(in);
Arrays.sort(in);
}
static void sort(long[]in) {
shuffle(in);
Arrays.sort(in);
}
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 | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 83c46f773cbbcf7349a4ef10a5aba7aa | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
public class MainSolution extends MainSolutionT {
// global vars
public void init(int tests_count){}
private class Item implements Comparable
{
public int idx, v, m;
public Item(int idx, int v, int m)
{
this.idx = idx;
this.v = v;
this.m = m;
}
public int compareTo(Object o)
{
Item i = (Item)o;
return this.v - i.v;
}
}
public class TestCase extends TestCaseT
{
public Object solve()
{
int n = readInt();
int i;
int[] a = new int[n];
int[] b = new int[n];
int[] m = new int[n];
int[] o = new int[n];
int[] r = new int[n];
int C = 0;
int maxb=0, t;
for (i=0; i<n; i++)
{
a[i] = readInt();
b[i] = readInt();
m[i] = readInt();
if (b[i] < m[i])
{
t = m[i] - b[i];
o[i] = t;
m[i] -= t;
a[i] -= t;
//b[i] = 0;
}
else
{
o[i] = 0;
//b[i] -= m[i];
}
b[i] = a[i]+b[i]-m[i];
if (b[i]>maxb)
{
maxb = b[i];
}
}
int Q = maxb+1;
int[] bc = new int[Q];
Arrays.fill(bc, 0);
for (i=0; i<n; i++)
{
bc[b[i]] ++;
}
Item[][] bsq = new Item[Q][];
for (i=0; i<Q; i++)
{
if (bc[i]>0)
{
bsq[i] = new Item[bc[i]];
bc[i] = 0;
}
}
for (i=0; i<n; i++)
{
bsq[b[i]][bc[b[i]]++] = new Item(i, a[i], m[i]);
}
int j;
for (j=0; j<Q; j++)
{
if (bc[j]>0)
{
Arrays.sort(bsq[j]);
r[bsq[j][0].idx] = 0;
C ++;
for (i=1; i<bc[j]; i++)
{
t = bsq[j][i].v - bsq[j][i-1].v;
if (t<=bsq[j][i].m)
{
r[bsq[j][i].idx] = t;
bsq[j][i].v -= t;
}
else
{
r[bsq[j][i].idx] = 0;
C ++;
}
}
}
}
out.println(C);
for (i=0; i<n; i++)
{
out.printf("%d %d\n", r[i]+o[i], m[i]-r[i] );
}
return null;
}
}
public void run()
{
int t = multiply_test ? readInt() : 1;
this.init(t);
for (int i = 0; i < t; i++) {
TestCase T = new TestCase();
T.run(i + 1);
}
}
public void loc_params()
{
this.log_enabled = false;
}
public void params(){
this.multiply_test = true;
}
}
public class MainSolutionT extends MainSolutionBase
{
public class TestCaseT extends TestCaseBase
{
}
}
public class MainSolutionBase
{
public boolean is_local = false;
public MainSolutionBase()
{
}
public class TestCaseBase
{
public Object solve() {
return null;
}
public int caseNumber;
public TestCaseBase() {
}
public void run(int cn){
this.caseNumber = cn;
Object r = this.solve();
if ((r != null))
{
out.println(r);
}
}
}
public String impossible(){
return "IMPOSSIBLE";
}
public String strf(String format, Object... args)
{
return String.format(format, args);
}
public BufferedReader in;
public PrintStream out;
public boolean log_enabled = false;
public boolean multiply_test = true;
public void params()
{
}
public void loc_params()
{
}
private StringTokenizer tokenizer = null;
public int readInt() {
return Integer.parseInt(readToken());
}
public long readLong() {
return Long.parseLong(readToken());
}
public double readDouble() {
return Double.parseDouble(readToken());
}
public String readLn() {
try {
String s;
while ((s = in.readLine()).length() == 0);
return s;
} catch (Exception e) {
return "";
}
}
public String readToken() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
} catch (Exception e) {
return "";
}
}
public int[] readIntArray(int n) {
int[] x = new int[n];
readIntArray(x, n);
return x;
}
public void readIntArray(int[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readInt();
}
}
public long[] readLongArray(int n) {
long[] x = new long[n];
readLongArray(x, n);
return x;
}
public void readLongArray(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readLong();
}
}
public void readLongArrayRev(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[n-i-1] = readLong();
}
}
public void logWrite(String format, Object... args) {
if (!log_enabled) {
return;
}
out.printf(format, args);
}
public void readLongArrayBuf(long[] x, int n) {
char[]buf = new char[1000000];
long r = -1;
int k= 0, l = 0;
long d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void readIntArrayBuf(int[] x, int n) {
char[]buf = new char[1000000];
int r = -1;
int k= 0, l = 0;
int d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void printArray(long[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(int[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(long[] a, int n, char dl)
{
long x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(double[] a, int n, char dl)
{
long x;
double y;
int i, l = 0;
for (i=0; i<n; i++)
{
x = (long)a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1 + 10*n;
char[] s = new char[l];
l--;
boolean z;
int j;
for (i=n-1; i>=0; i--)
{
x = (long)a[i];
y = (long)(1000000000*(a[i]-x));
z = false;
if (x<0)
{
x = -x;
z = true;
}
for (j=0; j<9; j++)
{
s[l--] = (char)('0' + (y % 10));
y /= 10;
}
s[l--] = '.';
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(int[] a, int n, char dl)
{
int x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printMatrix(int[][] a, int n, int m)
{
int x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
public void printMatrix(long[][] a, int n, int m)
{
long x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
}
public void run()
{
MainSolution S;
try {
S = new MainSolution();
S.in = new BufferedReader(new InputStreamReader(System.in));
//S.out = System.out;
S.out = new PrintStream(new BufferedOutputStream( System.out ));
} catch (Exception e) {
return;
}
S.params();
S.run();
S.out.flush();
}
public static void main(String args[]) {
Locale.setDefault(Locale.US);
Main M = new Main();
M.run();
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 6cde2450823e573308b58c9bdccba0ac | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run() {
for(int q=ni();q>0;q--){
work();
}
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
long inf=Long.MAX_VALUE/3;
void work() {
int n=ni();
int[][] A=new int[n][];
int[][] ret=new int[n][2];
for(int i=0;i<n;i++){
A[i]=new int[]{ni(),ni(),ni(),i};
}
Arrays.sort(A,(a1,a2)->{
if(a1[2]-a1[1]-a1[0]==a2[2]-a2[1]-a2[0]){
return Math.max(a1[0]-a1[2],0)-Math.max(a2[0]-a2[2],0);
}
return (a1[0]+a1[1]-a1[2])-(a2[0]+a2[1]-a2[2]);
});
int r=0;
int[] rec2=new int[n];
for(int i=0,j=0;i<n;){
int v=A[i][0]+A[i][1]-A[i][2];
while(j<n&&A[j][0]+A[j][1]-A[j][2]==v){
j++;
}
for(int cur=-1,pre=i;i<j;i++){
int s=Math.max(0,A[i][0]-A[i][2]);
int e=A[i][0]-Math.max(0,A[i][2]-A[i][1]);
if(s<=cur){
cur=Math.min(cur,e);
}else{
for(;pre<i;pre++){
rec2[pre]=cur;
}
cur=e;
r++;
}
if(i==j-1){
for(;pre<=i;pre++){
rec2[pre]=cur;
}
}
}
}
for(int i=0;i<n;i++){
int idx=A[i][3];
int cur=rec2[i];
ret[idx][0]=A[i][0]-cur;
ret[idx][1]=A[i][2]-ret[idx][0];
}
out.println(r);
for(int[] re:ret){
out.println(re[0]+" "+re[1]);
}
}
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private double nd() {
return in.nextDouble();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
InputStreamReader input;//no buffer
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(boolean isBuffer)
{
if(!isBuffer){
input=new InputStreamReader(System.in);
}else{
br=new BufferedReader(new InputStreamReader(System.in));
}
}
public boolean hasNext(){
try{
String s=br.readLine();
if(s==null){
return false;
}
st=new StringTokenizer(s);
}catch(IOException e){
e.printStackTrace();
}
return true;
}
public String next()
{
if(input!=null){
try {
StringBuilder sb=new StringBuilder();
int ch=input.read();
while(ch=='\n'||ch=='\r'||ch==32){
ch=input.read();
}
while(ch!='\n'&&ch!='\r'&&ch!=32){
sb.append((char)ch);
ch=input.read();
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
}
}
while(st==null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return (int)nextLong();
}
public long nextLong() {
try {
if(input!=null){
long ret=0;
int b=input.read();
while(b<'0'||b>'9'){
b=input.read();
}
while(b>='0'&&b<='9'){
ret=ret*10+(b-'0');
b=input.read();
}
return ret;
}
}catch (Exception e){
e.printStackTrace();
}
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 53eef17f5b90e9fdcb079a05e1b98b49 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution{
static void main() throws Exception{
int n=sc.nextInt();
int[][]in=new int[n][];
int[][]arr=new int[n<<1][];
int[]ans=new int[n];
for(int i=0;i<n;i++){
int a=sc.nextInt(),b=sc.nextInt(),m=sc.nextInt();
in[i]=new int[]{a,b,m};
arr[i<<1]=new int[]{a+b-m,Math.max(0,a-m),0,i};
arr[i<<1|1]=new int[]{a+b-m,Math.min(a,a-(m-b)),1,i};
ans[i]=-1;
}
int cnt=n;
Arrays.sort(arr,(x,y)->x[0]==y[0]?(x[1]==y[1]?(x[2]-y[2]):(x[1]-y[1])):(x[0]-y[0]));
LinkedList<Integer>q=new LinkedList<>();
for(int i=0;i<(n<<1);){
if(arr[i][2]==0){
q.add(arr[i][3]);
i++;
continue;
}
boolean found=false;
while(i+1<(n<<1) && arr[i][0]==arr[i+1][0] && arr[i][1]==arr[i+1][1]){
if(ans[arr[i][3]]!=-1){
i++;
continue;
}
i++;
found=true;
}
if(ans[arr[i][3]]==-1){
found=true;
}
if(!found){
i++;
continue;
}
cnt++;
while(!q.isEmpty()){
int idx=q.poll();
cnt--;
ans[idx]=arr[i][1];
}
i++;
}
pw.println(cnt);
for(int i=0;i<n;i++){
pw.println((in[i][0]-ans[i])+" "+((in[i][2]-(in[i][0]-ans[i]))));
}
}
public static void main(String[] args) throws Exception{
sc=new MScanner(System.in);
// sc=new MScanner(" .in");
pw = new PrintWriter(System.out);
int tc=1;
tc=sc.nextInt();
for(curt=1;curt<=tc;curt++) {
// pw.printf("Case #%d:", curt);
main();
}
pw.flush();
}
static int curt;
static PrintWriter pw;
static MScanner sc;
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 dbg(int[]in) {
System.out.println(Arrays.toString(in));
}
static void dbg(long[]in) {
System.out.println(Arrays.toString(in));
}
static void sort(int[]in) {
shuffle(in);
Arrays.sort(in);
}
static void sort(long[]in) {
shuffle(in);
Arrays.sort(in);
}
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 | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | b34e88d69e17aacc05f089c6829ec0e9 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class TaskB {
static long mod = 1000000007;
static FastScanner scanner;
static final StringBuilder result = new StringBuilder();
public static void main(String[] args) {
scanner = new FastScanner();
int T = scanner.nextInt();
for (int t = 0; t < T; t++) {
solve(t + 1);
result.append("\n");
}
System.out.println(result);
}
static void solve(int t) {
int n = scanner.nextInt();
Data[] x = new Data[n];
Map<Integer, List<Data>> clusters = new HashMap<>();
for (int i = 0; i < n; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int m = scanner.nextInt();
int sum = a + b - m;
Data d= new Data(a, b, m, i);
clusters.computeIfAbsent(sum, kk -> new ArrayList<>()).add(d);
x[i] = d;
}
int res = 0;
for (List<Data> possible : clusters.values()) {
Collections.sort(possible);
Data first = possible.get(0);
first.x = Math.max(first.m - first.b, 0);
int lastTo = first.a - first.x;
res++;
for (int i = 1; i < possible.size(); i++) {
Data current = possible.get(i);
if (current.a - current.m <= lastTo) {
current.x = current.a - lastTo;
} else {
current.x = Math.max(current.m - current.b, 0);
lastTo = current.a - current.x;
res++;
}
}
}
result.append(res).append("\n");
for (int i = 0; i < n; i++) {
result.append(x[i].x).append(" ").append(x[i].m - x[i].x).append("\n");
}
// 1 4 1
// 3 1 0
// 4 5 4
}
static class Data implements Comparable<Data> {
int a, b, m, idx, x;
public Data(int a, int b, int m, int idx) {
this.a = a;
this.b = b;
this.m = m;
this.idx = idx;
}
@Override
public int compareTo(Data o) {
if (a == o.a) {
return Integer.compare(b, o.b);
}
return Integer.compare(a, o.a);
}
}
static class WithIdx implements Comparable<WithIdx> {
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(WithIdx o) {
if (val == o.val) {
return Integer.compare(idx, o.idx);
}
return Integer.compare(val, o.val);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
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 nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j];
j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
static long modInverse(long a, long m) {
long g = gcdF(a, m);
if (g != 1) {
throw new IllegalArgumentException("Inverse doesn't exist");
} else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
return modpow(a, m - 2, m);
}
}
static public long gcdF(long a, long b) {
while (b != 0) {
long na = b;
long nb = a % b;
a = na;
b = nb;
}
return a;
}
}
}
/*
5
3 2 3 8 8
2 8 5 10 1
*/ | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 8 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 6a822b5200ca6b164852dfbb15faa0f3 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | //package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
//H. Banquet Preparations 2
public class Solution15 {
final static int M = 1000000;
final static ArrayList<int[]>[] als = new ArrayList[M+M+1];
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
int Q = Integer.parseInt(input.readLine());
while (Q > 0){
input.readLine();
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken());
final int[] fish = new int[n], meat = new int[n], ms = new int[n];
long[] rf = new long[n], rm = new long[n];
for (int i=0;i<n;++i){
st = new StringTokenizer(input.readLine());
fish[i] = Integer.parseInt(st.nextToken());
meat[i] = Integer.parseInt(st.nextToken());
ms[i] = Integer.parseInt(st.nextToken());
}
long res = calc(n, fish, meat, ms, rf, rm);
out.println(res);
for (int i=0;i<n;++i){
out.println(rf[i] + " " + rm[i]);
}
Q--;
}
out.close(); // close the output file
}
private static int calc(final int n, final int[] fish, final int[] meat, int[] ms, long[] rf, long[] rm) {
for (int i=0;i<n;++i){
int min = Math.max(0, ms[i]-meat[i]);
int max = Math.min(ms[i], fish[i]);
if (als[fish[i]+meat[i]-ms[i]] == null)als[fish[i]+meat[i]-ms[i]] = new ArrayList<>();
als[fish[i]+meat[i]-ms[i]].add(new int[]{fish[i]-max, fish[i]-min, i});
}
int res = 0;
for (int i=0;i<n;++i) {
ArrayList<int[]> al = als[fish[i]+meat[i]-ms[i]];
if (al != null) {
Collections.sort(al, Comparator.comparingInt(a -> a[0]));
int l = -1, r = -1;
for (int j = 0, k = 0; k < al.size(); ++k) {
int[] kk = al.get(k);
if (kk[0] > r) {
res++;
while (j < k) {
int idx = al.get(j)[2];
rf[idx] = fish[idx] - l;
rm[idx] = ms[idx] - rf[idx];
j++;
}
l = kk[0];
r = kk[1];
} else {
l = Math.max(l, kk[0]);
r = Math.min(r, kk[1]);
}
if (k == al.size() - 1) {
while (j < al.size()) {
int idx = al.get(j)[2];
rf[idx] = fish[idx] - l;
rm[idx] = ms[idx] - rf[idx];
j++;
}
}
}
}
als[fish[i]+meat[i]-ms[i]] = null;
}
return res;
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 7c9d47a2ce08d330c9646293f42e0c51 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | //package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
//H. Banquet Preparations 2
public class Solution15 {
final static int M = 1000000;
final static ArrayList<int[]>[] als = new ArrayList[M+M+1];
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
int Q = Integer.parseInt(input.readLine());
while (Q > 0){
input.readLine();
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken());
final int[] fish = new int[n], meat = new int[n], ms = new int[n];
long[] rf = new long[n], rm = new long[n];
for (int i=0;i<n;++i){
st = new StringTokenizer(input.readLine());
fish[i] = Integer.parseInt(st.nextToken());
meat[i] = Integer.parseInt(st.nextToken());
ms[i] = Integer.parseInt(st.nextToken());
}
long res = calc(n, fish, meat, ms, rf, rm);
out.println(res);
for (int i=0;i<n;++i){
out.println(rf[i] + " " + rm[i]);
}
Q--;
}
out.close(); // close the output file
}
private static int calc(final int n, final int[] fish, final int[] meat, int[] ms, long[] rf, long[] rm) {
for (int i=0;i<n;++i){
int min = Math.max(0, ms[i]-meat[i]);
int max = Math.min(ms[i], fish[i]);
if (als[fish[i]+meat[i]-ms[i]] == null)als[fish[i]+meat[i]-ms[i]] = new ArrayList<>();
als[fish[i]+meat[i]-ms[i]].add(new int[]{fish[i]-max, fish[i]-min, i});
}
int res = 0;
for (int i=0;i<n;++i) {
ArrayList<int[]> al = als[fish[i]+meat[i]-ms[i]];
if (al != null) {
Collections.sort(al, Comparator.comparingInt(a -> a[0]));
int l = -1, r = -1;
for (int j = 0, k = 0; k < al.size(); ++k) {
int[] kk = al.get(k);
if (kk[0] > r) {
res++;
while (j < k) {
int idx = al.get(j)[2];
rf[idx] = fish[idx] - l;
rm[idx] = ms[idx] - rf[idx];
j++;
}
l = kk[0];
r = kk[1];
} else {
l = Math.max(l, kk[0]);
r = Math.min(r, kk[1]);
}
if (k == al.size() - 1) {
while (j < al.size()) {
int idx = al.get(j)[2];
rf[idx] = fish[idx] - l;
rm[idx] = ms[idx] - rf[idx];
j++;
}
}
}
als[fish[i]+meat[i]-ms[i]] = null;
}
}
return res;
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | f58afd26b2700141055d5aedc8832d50 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class ACMIND
{
static FastReader scan;
static PrintWriter pw;
static long MOD = 1_000_000_007;
static long INF = 2_000_000_000_000_000_000L;
static long inf = 2_000_000_000;
public static void main(String[] args) {
new Thread(null,null,"_",1<<27)
{
public void run()
{
try
{
solve();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void solve() throws IOException {
scan = new FastReader();
pw = new PrintWriter(System.out, true);
StringBuilder sb = new StringBuilder();
int t = ni();
while (t-->0) {
int n = ni();
Map<Integer, ArrayList<Integer>> rem_weight_to_idx = new HashMap<>();
int a[] = new int[n], b[] = new int[n], m[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = ni();
b[i] = ni();
m[i] = ni();
int rem = a[i] + b[i] - m[i];
ArrayList<Integer> list = rem_weight_to_idx.getOrDefault(rem, new ArrayList<>());
list.add(i);
rem_weight_to_idx.put(rem, list);
}
int ans = 0;
int ansx[] = new int[n];
for(int rem : rem_weight_to_idx.keySet()) {
ArrayList<Pair> sorted = new ArrayList<>();
ArrayList<Integer> list = rem_weight_to_idx.get(rem);
for(int index : list) {
int min = max(0, a[index] - m[index]);
int max = a[index] - max(0, (m[index] - b[index]));
sorted.add(new Pair(min, max,index));
}
Collections.sort(sorted);
int sz = sorted.size();
for(int i=0;i<sz;i++) {
int j = i;
Pair p = sorted.get(i);
int l = p.x, r = p.y;
while (j+1<sz && intersects(l,r,sorted.get(j+1))) {
j++;
l = max(l, sorted.get(j).x);
r = min(r, sorted.get(j).y);
}
for(int k=i;k<=j;k++) {
int index = sorted.get(k).idx;
ansx[index] = (a[index] - l);
}
i = j;
ans++;
}
}
sb.append(ans);
sb.append("\n");
for(int i=0;i<n;i++) {
sb.append(ansx[i]+" "+(m[i] - ansx[i]));
sb.append("\n");
}
}
psb(sb);
pw.flush();
pw.close();
}
static boolean intersects(int l, int r, Pair p) {
return max(l, p.x) <= min(r, p.y);
}
static class Pair implements Comparable<Pair>
{
int x,y,idx;
Pair(int x,int y, int idx)
{
this.x=x;
this.y=y;
this.idx = idx;
}
public int compareTo(Pair other)
{
if(this.x!=other.x)
return this.x-other.x;
return this.y-other.y;
}
public String toString()
{
return "("+x+","+y+","+idx+")";
}
}
static void assert_in_range(String varName, int n, int l, int r) {
if (n >=l && n<=r) return;
System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
System.exit(1);
}
static void assert_in_range(String varName, long n, long l, long r) {
if (n>=l && n<=r) return;
System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
System.exit(1);
}
static int ni() throws IOException
{
return scan.nextInt();
}
static long nl() throws IOException
{
return scan.nextLong();
}
static double nd() throws IOException
{
return scan.nextDouble();
}
static void pl()
{
pw.println();
}
static void p(Object o)
{
pw.print(o+" ");
}
static void pl(Object o)
{
pw.println(o);
}
static void psb(StringBuilder sb)
{
pw.print(sb);
}
static void pa(String arrayName, Object arr[])
{
pl(arrayName+" : ");
for(Object o : arr)
p(o);
pl();
}
static void pa(String arrayName, int arr[])
{
pl(arrayName+" : ");
for(int o : arr)
p(o);
pl();
}
static void pa(String arrayName, long arr[])
{
pl(arrayName+" : ");
for(long o : arr)
p(o);
pl();
}
static void pa(String arrayName, double arr[])
{
pl(arrayName+" : ");
for(double o : arr)
p(o);
pl();
}
static void pa(String arrayName, char arr[])
{
pl(arrayName+" : ");
for(char o : arr)
p(o);
pl();
}
static void pa(String listName, List list)
{
pl(listName+" : ");
for(Object o : list)
p(o);
pl();
}
static void pa(String arrayName, Object[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(Object o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, int[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(int o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, long[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(long o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, char[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(char o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, double[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(double o : arr[i])
p(o);
pl();
}
}
static class FastReader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(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[1000000];
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\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 114a500447edd9931a77cf63815ac3d5 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | // package c1607;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
//
// Codeforces Round #753 (Div. 3) 2021-11-02 06:35
// H. Banquet Preparations 2
// https://codeforces.com/contest/1607/problem/H
// time limit per test 3 seconds; memory limit per test 256 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// The chef has cooked n dishes yet again: the i-th dish consists of a_i grams of fish and b_i grams
// of meat.
//
// Banquet organizers consider two dishes i and j equal if a_i=a_j and b_i=b_j at the same time.
//
// The banquet organizers estimate the of n dishes as follows. The of a set of dishes is equal to
// the number of different dishes in it. The is, the .
//
// In order to reduce the , a taster was invited. He will eat m_i grams of food from each dish. For
// each dish, the taster determines separately how much fish and how much meat he will eat. The only
// condition is that he will eat exactly m_i grams of the i-th dish in total.
//
// Determine how much of what type of food the taster should eat from each dish so that the value of
// is the minimum possible. If there are several correct answers, you may output any of them.
//
// Input
//
// The first line of input data contains an integer t (1 <= t <= 10^4)-- the number of test cases.
//
// Each test case's description is preceded by a blank line. Next comes a line that contains an
// integer n (1 <= n <= 2 * 10^5)-- the number of dishes. Then follows n lines, i-th of which
// contains three integers a_i, b_i and m_i (0 <= a_i, b_i <= 10^6; 0 <= m_i <= a_i+b_i)-- the mass
// of fish in i-th dish, the mass of meat in i-th dish and how many grams in total the taster should
// eat in i-th dish.
//
// The sum of all n values for all input data sets in the test does not exceed 2 * 10^5.
//
// Output
//
// For each test case, print on the first line the minimum value of that can be achieved by eating
// exactly m_i grams of food (for all i from 1 to n) from a dish i.
//
// Then print n lines that describe a way to do this: the i-th line should contain two integers x_i
// and y_i (0 <= x_i <= a_i; 0 <= y_i <= b_i; x_i+y_i=m_i), where x_i is how many grams of fish the
// taster should eat from i-th dish, and y_i is how many grams of meat.
//
// If there are several ways to achieve a minimum balance, print any of them.
//
// Example
/*
input:
5
3
10 10 2
9 9 0
10 9 1
2
3 4 1
5 1 2
3
7 2 5
6 5 4
5 5 6
1
13 42 50
5
5 7 12
3 1 4
7 3 7
0 0 0
4 1 5
output:
1
1 1
0 0
1 0
2
0 1
1 1
2
3 2
0 4
1 5
1
8 42
2
5 7
3 1
4 3
0 0
4 1
*/
//
public class C1607H {
static final int MOD = (int)1e9+7;
static final Random RAND = new Random();
static int solve(int[][] abm, int[][] output) {
int n = abm.length;
// Consider each group of a + b - m
// find the minimal number of points to cover all the intervals
// ----------- ----------- ------- -----
// ------------------- --------- ------------
// ---
// ^ ^ ^ ^
// Map from remain k to range of qualified a
Map<Integer, List<int[]>> rim = new HashMap<>();
for (int i = 0; i < n; i++) {
int[] v = abm[i];
int a = v[0];
int b = v[1];
int m = v[2];
int r = a + b - m;
// Minimal and maximal of a to eat/reduce.
int mina = Math.max(0, m - b);
int maxa = Math.min(m, a);
rim.computeIfAbsent(r, x -> new ArrayList<>()).add(new int[] {a-maxa, a-mina, i, 0});
}
int ans = 0;
for (Map.Entry<Integer, List<int[]>> e : rim.entrySet()) {
int r = e.getKey();
List<int[]> intervals = e.getValue();
// System.out.format(" r:%d %s\n", r, Utils.traceListIntArray(intervals));
ans += countPointsToCover(intervals);
// apply chosen value to output
for (int[] v : intervals) {
int idx = v[2];
// Note that v[3] is count of remaining a
output[idx][0] = abm[idx][0] - v[3];
output[idx][1] = abm[idx][2] - output[idx][0];
}
}
return ans;
}
static int countPointsToCover(List<int[]> intervals) {
int n = intervals.size();
List<int[]> events = new ArrayList<>();
for (int i = 0; i < n; i++) {
int[] v = intervals.get(i);
events.add(new int[] {v[0], 0, i});
events.add(new int[] {v[1], 1, i});
}
// Sort by position, begin ahead of end
Collections.sort(events, (x,y)->x[0] != y[0] ? x[0] - y[0] : x[1] - y[1]);
// System.out.format(" events: %s\n", Utils.traceListIntArray(events));
boolean[] done = new boolean[n];
int ans = 0;
List<Integer> opened = new ArrayList<>();
for (int i = 0; i < events.size(); i++) {
int[] e = events.get(i);
int j = e[2];
if (done[j]) {
continue;
}
if (e[1] == 0) {
opened.add(j);
continue;
}
ans++;
// Complete all opened intervals
for (int idx : opened) {
done[idx] = true;
intervals.get(idx)[3] = e[0];
}
opened.clear();
}
return ans;
}
static String trace(int[][] a) {
StringBuilder sb = new StringBuilder();
for (int[] v : a) {
sb.append(v[0]);
sb.append(' ');
sb.append(v[1]);
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args) {
Scanner in = getInputScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int[][] abm = new int[n][3];
for (int i = 0; i < n; i++) {
abm[i][0] = in.nextInt();
abm[i][1] = in.nextInt();
abm[i][2] = in.nextInt();
}
int[][] output = new int[n][2];
long ans = solve(abm, output);
System.out.println(ans);
System.out.print(trace(output));
}
in.close();
}
static Scanner getInputScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
} catch (Exception e) {
return new Scanner(System.in);
}
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 0d86b4c33aaba07427c81d30f7324805 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
private static Node[] nodes;
private static void run() throws IOException {
int n;
n = in.nextInt();
nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
int m = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
nodes[i].cal_a_range();
}
Arrays.sort(nodes, Comparator.comparingLong(o -> o.remain));
int ans = 0;
int left = 0;
for (int right = 1; right < n; right++) {
if (nodes[left].remain != nodes[right].remain) {
ans += handle(left, right - 1);
left = right;
}
}
ans += handle(left, n - 1);
out.println(ans);
Arrays.sort(nodes, Comparator.comparingLong(o -> o.id));
for (Node now : nodes) {
now.show();
}
}
private static int handle(int left, int right) {
int size = right - left + 1;
RangeFlag[] rangeFlags = new RangeFlag[size * 2];
for (int i = 0; i < size; i++) {
rangeFlags[i * 2] = new RangeFlag();
rangeFlags[i * 2].id = left + i;
rangeFlags[i * 2].pos = nodes[left + i].min_a;
rangeFlags[i * 2].end = false;
rangeFlags[i * 2 + 1] = new RangeFlag();
rangeFlags[i * 2 + 1].id = left + i;
rangeFlags[i * 2 + 1].pos = nodes[left + i].max_a;
rangeFlags[i * 2 + 1].end = true;
}
Arrays.sort(rangeFlags, ((o1, o2) -> {
int pr = Long.compare(o1.pos, o2.pos);
if (pr != 0) return pr;
else return Boolean.compare(o1.end, o2.end);
}));
int ans = 0;
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < 2 * size; i++) {
if (!rangeFlags[i].end) {
set.add(rangeFlags[i].id);
} else if (set.contains(rangeFlags[i].id)) {
ans++;
for (int now : set) {
nodes[now].x = rangeFlags[i].pos;
}
set.clear();
}
}
return ans;
}
private static class RangeFlag {
int id;
long pos;
boolean end;
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 024faccf88dbaee0b57c359e5d468230 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class gotoJapan {
public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solution solver = new Solution();
boolean isTest = true;
int tC = isTest ? Integer.parseInt(in.next()) : 1;
for (int i = 1; i <= tC; i++)
solver.solve(in, out, i);
out.close();
}
/* ............................................................. */
static class Solution {
InputReader in;
PrintWriter out;
static class comP implements Comparator<Pair2>{
public int compare(Pair2 p1,Pair2 p2) {
return p1.x-p2.x;
}
}
public void solve(InputReader in, PrintWriter out, int test) {
this.in = in;
this.out = out;
int n=ni();
Pair a[]=new Pair[n];
int b[][]=new int[n][3];
int ans[][]=new int[n][2];
for(int i=0;i<n;i++) {
int x=ni();
int y=ni();
int m=ni();
b[i][0]=x;
b[i][1]=y;
b[i][2]=m;
x=x-m;
int dy=y-Math.max(y-m, 0);
int dx=x+dy;
x=Math.max(0, x);
a[i]=new Pair(x, dx, b[i][0]+b[i][1]-m,i);
//out.println(x+" "+dx);
}
Arrays.sort(a,new Comparator<Pair>() {
public int compare(Pair p1,Pair p2) {
if(p1.w!=p2.w)return p1.w-p2.w;
return p1.x-p2.x;
}
});
PriorityQueue<Pair2> pq =new PriorityQueue<>(new comP());
int cnt=0;
int p=-1;
for(int i=0;i<n;i++) {
if(p!=a[i].w||pq.size()==0||pq.size()==0||pq.peek().x<a[i].x) {
int tmp=-1;
if(pq.size()>0)tmp=pq.peek().x;
while(pq.size()>0) {
Pair2 p2=pq.poll();
ans[p2.y][0]=tmp;
ans[p2.y][1]=b[p2.y][1]-(b[p2.y][2]-(b[p2.y][0]-tmp));
}
cnt++;
}
p=a[i].w;
pq.add(new Pair2(a[i].y, a[i].i));
}
int tmp=-1;
if(pq.size()>0)tmp=pq.peek().x;
while(pq.size()>0) {
Pair2 p2=pq.poll();
ans[p2.y][0]=tmp;
ans[p2.y][1]=b[p2.y][1]-(b[p2.y][2]-(b[p2.y][0]-tmp));
}
pn(cnt);
for(int i=0;i<n;i++) {
out.println((b[i][0]-ans[i][0])+" "+(b[i][1]-ans[i][1]));
}
}
class Pair {
int x;
int y;
int w;
int i;
Pair(int x, int y,int w,int i) {
this.x = x;
this.y=y;
this.w=w;
this.i=i;
}
}
class Pair2 {
int x;
int y;
Pair2(int x, int y) {
this.x = x;
this.y=y;
}
}
char[] n() {
return in.next().toCharArray();
}
int ni() {
return in.nextInt();
}
long nl() {
return in.nextLong();
}
long[] nal(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
void pn(long zx) {
out.println(zx);
}
void pn(String sz) {
out.println(sz);
}
void pn(double dx) {
out.println(dx);
}
void pn(long ar[]) {
for (int i = 0; i < ar.length; i++)
out.print(ar[i] + " ");
out.println();
}
void pn(String ar[]) {
for (int i = 0; i < ar.length; i++)
out.println(ar[i]);
}
}
/* ......................Just Input............................. */
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());
}
}
/* ......................Just Input............................. */
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | cd43a7350c14e7b2d73b816ba4cf3ab1 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
private static Node[] nodes;
private static void run() throws IOException {
int n;
n = in.nextInt();
nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
int m = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
nodes[i].cal_a_range();
}
Arrays.sort(nodes, Comparator.comparingLong(o -> o.remain));
int ans = 0;
int left = 0;
for (int right = 1; right < n; right++) {
if (nodes[left].remain != nodes[right].remain) {
ans += handle(left, right - 1);
left = right;
}
}
ans += handle(left, n - 1);
out.println(ans);
Arrays.sort(nodes, Comparator.comparingLong(o -> o.id));
for (Node now : nodes) {
now.show();
}
}
private static int handle(int left, int right) {
int size = right - left + 1;
RangeFlag[] rangeFlags = new RangeFlag[size * 2];
for (int i = 0; i < size; i++) {
rangeFlags[i * 2] = new RangeFlag();
rangeFlags[i * 2].id = left + i;
rangeFlags[i * 2].pos = nodes[left + i].min_a;
rangeFlags[i * 2].end = false;
rangeFlags[i * 2 + 1] = new RangeFlag();
rangeFlags[i * 2 + 1].id = left + i;
rangeFlags[i * 2 + 1].pos = nodes[left + i].max_a;
rangeFlags[i * 2 + 1].end = true;
}
Arrays.sort(rangeFlags, ((o1, o2) -> {
int pr = Long.compare(o1.pos, o2.pos);
if (pr != 0) return pr;
else return Boolean.compare(o1.end, o2.end);
}));
int ans = 0;
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < 2 * size; i++) {
if (!rangeFlags[i].end) {
set.add(rangeFlags[i].id);
} else if (set.contains(rangeFlags[i].id)) {
ans++;
for (int now : set) {
nodes[now].x = rangeFlags[i].pos;
}
set.clear();
}
}
return ans;
}
private static class RangeFlag {
int id;
long pos;
boolean end;
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 07633fe85befac7b74b24b27e2335866 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | //package banquetpreparations2;
import java.util.*;
import java.io.*;
public class banquetpreparations2 {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(fin.readLine());
StringBuilder fout = new StringBuilder();
while(t-- > 0) {
fin.readLine();
int n = Integer.parseInt(fin.readLine());
int[][] dishes = new int[n][9]; //x, y, m, layer, start, end, eatenx, eateny, old index
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(fin.readLine());
for(int j = 0; j < 3; j++) {
dishes[i][j] = Integer.parseInt(st.nextToken());
}
dishes[i][8] = i;
}
for(int i = 0; i < n; i++) {
int x = dishes[i][0]; int y = dishes[i][1]; int m = dishes[i][2];
dishes[i][3] = x + y - m;
dishes[i][4] = Math.max(0, x - m);
dishes[i][5] = x;
}
Arrays.sort(dishes, (a, b) -> a[3] == b[3]? Integer.compare(a[4], b[4]) : Integer.compare(a[3], b[3]));
// for(int i = 0; i < n; i++) {
// for(int j : dishes[i]) {
// System.out.print(j + " ");
// }
// System.out.println();
// }
int prevLayer = dishes[0][3];
int high = dishes[0][5];
int low = dishes[0][4];
boolean[] answered = new boolean[n];
int numUnique = 0;
for(int i = 1; i < n; i++) {
int layer = dishes[i][3];
int start = dishes[i][4];
int end = dishes[i][5];
if(layer != prevLayer || start > high) {
numUnique ++;
//go back, and set the answers
int index = i - 1;
if(low < 0) {
low = 0;
}
int xLeft = low;
int yLeft = prevLayer - low;
//System.out.println(xLeft + " " + yLeft + " I: " + i);
//System.out.println("LO: " + low + " LAY: " + prevLayer);
while(true) {
int nextLow = dishes[index][4];
int nextHigh = dishes[index][5];
int nextLayer = dishes[index][3];
if(!(nextLow <= low && nextHigh >= high && nextLayer == prevLayer) || answered[index]) {
break;
}
answered[index] = true;
dishes[index][6] = dishes[index][0] - xLeft;
dishes[index][7] = dishes[index][1] - yLeft;
//System.out.println(dishes[index][6] + " " + dishes[index][7]);
index --;
if(index == -1) {
break;
}
}
prevLayer = layer;
low = start;
high = end;
}
else {
low = Math.max(low, start);
high = Math.min(high, end);
}
}
numUnique ++;
int index = n - 1;
int xLeft = low;
int yLeft = prevLayer - low;
while(true) {
int nextLow = dishes[index][4];
int nextHigh = dishes[index][5];
int nextLayer = dishes[index][3];
if(!(nextLow <= low && nextHigh >= high && nextLayer == prevLayer) || answered[index]) {
break;
}
answered[index] = true;
dishes[index][6] = dishes[index][0] - xLeft;
dishes[index][7] = dishes[index][1] - yLeft;
//System.out.println(dishes[index][6] + " " + dishes[index][7]);
index --;
if(index == -1) {
break;
}
}
Arrays.sort(dishes, (a, b) -> Integer.compare(a[8], b[8]));
fout.append(numUnique).append("\n");
for(int i = 0; i < n; i++) {
fout.append(dishes[i][6]).append(" ").append(dishes[i][7]).append("\n");
}
}
System.out.print(fout);
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 956743680ca3ff018b6fb6566663c8f9 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
private static Node[] nodes;
private static void run() throws IOException {
int n;
n = in.nextInt();
nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
int m = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
nodes[i].cal_a_range();
}
Arrays.sort(nodes, Comparator.comparingLong(o -> o.remain));
int ans = 0;
int left = 0;
for (int right = 1; right < n; right++) {
if (nodes[left].remain != nodes[right].remain) {
ans += handle(left, right - 1);
left = right;
}
}
ans += handle(left, n - 1);
out.println(ans);
Arrays.sort(nodes, Comparator.comparingLong(o -> o.id));
for (Node now : nodes) {
now.show();
}
}
private static int handle(int left, int right) {
int size = right - left + 1;
RangeFlag[] rangeFlags = new RangeFlag[size * 2];
for (int i = 0; i < size; i++) {
rangeFlags[i * 2] = new RangeFlag();
rangeFlags[i * 2].id = left + i;
rangeFlags[i * 2].pos = nodes[left + i].min_a;
rangeFlags[i * 2].end = false;
rangeFlags[i * 2 + 1] = new RangeFlag();
rangeFlags[i * 2 + 1].id = left + i;
rangeFlags[i * 2 + 1].pos = nodes[left + i].max_a;
rangeFlags[i * 2 + 1].end = true;
}
Arrays.sort(rangeFlags, ((o1, o2) -> {
int pr = Long.compare(o1.pos, o2.pos);
if (pr != 0) return pr;
else return Boolean.compare(o1.end, o2.end);
}));
int ans = 0;
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < 2 * size; i++) {
if (!rangeFlags[i].end) {
set.add(rangeFlags[i].id);
} else if (set.contains(rangeFlags[i].id)) {
ans++;
for (int now : set) {
nodes[now].x = rangeFlags[i].pos;
}
set.clear();
}
}
return ans;
}
private static class RangeFlag {
int id;
long pos;
boolean end;
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 2b82b96cead364cd830ea6a1a722333d | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
private static Node[] nodes;
private static void run() throws IOException {
int n;
n = in.nextInt();
nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
int m = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
nodes[i].cal_a_range();
}
Arrays.sort(nodes, Comparator.comparingLong(o -> o.remain));
int ans = 0;
int left = 0;
for (int right = 1; right < n; right++) {
if (nodes[left].remain != nodes[right].remain) {
ans += handle(left, right - 1);
left = right;
}
}
ans += handle(left, n - 1);
out.println(ans);
Arrays.sort(nodes, Comparator.comparingLong(o -> o.id));
for (Node now : nodes) {
now.show();
}
}
private static int handle(int left, int right) {
int size = right - left + 1;
RangeFlag[] rangeFlags = new RangeFlag[size * 2];
for (int i = 0; i < size; i++) {
rangeFlags[i * 2] = new RangeFlag();
rangeFlags[i * 2].id = left + i;
rangeFlags[i * 2].pos = nodes[left + i].min_a;
rangeFlags[i * 2].end = false;
rangeFlags[i * 2 + 1] = new RangeFlag();
rangeFlags[i * 2 + 1].id = left + i;
rangeFlags[i * 2 + 1].pos = nodes[left + i].max_a;
rangeFlags[i * 2 + 1].end = true;
}
Arrays.sort(rangeFlags, ((o1, o2) -> {
int pr = Long.compare(o1.pos, o2.pos);
if (pr != 0) return pr;
else return Boolean.compare(o1.end, o2.end);
}));
int ans = 0;
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < 2 * size; i++) {
if (!rangeFlags[i].end) {
set.add(rangeFlags[i].id);
} else if (set.contains(rangeFlags[i].id)) {
ans++;
for (int now : set) {
nodes[now].x = rangeFlags[i].pos;
}
set.clear();
}
}
return ans;
}
private static class RangeFlag {
int id;
long pos;
boolean end;
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | e8461e57663a00dc80057a2dd595eca6 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class BMain {
public static void main(String[] args) {
QuickReader in = new QuickReader(System.in);
try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
{
new BMain().solve(in, out);
}
}
public void solve(QuickReader in, PrintWriter out)
{
int T = in.nextInt();
while(T-->0)
{
int n;
n = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] m = new int[n];
int[] res = new int[n];
for(int i=0;i<n;i++)
{
a[i] = in.nextInt();
b[i] = in.nextInt();
m[i] = in.nextInt();
}
Map<Integer, ArrayList<Segment> > grp = new TreeMap<>();
for(int i=0;i<n;i++)
{
int M = a[i] + b[i] - m[i];
int lo = Math.max(0, m[i]-b[i]);
int hi = Math.min(m[i], a[i]);
if(!grp.containsKey(M))
grp.put(M, new ArrayList<>());
grp.get(M).add(new Segment(a[i]-b[i]+m[i]-2*hi, a[i]-b[i]+m[i]-2*lo, i));
}
for(ArrayList<Segment> segms: grp.values())
{
TreeSet<Segment> active = new TreeSet<Segment>();
TreeMap<Integer, ArrayList<Segment> > loMap = new TreeMap<>();
for(Segment s: segms)
{
if(!loMap.containsKey(s.l))
loMap.put(s.l, new ArrayList<>());
loMap.get(s.l).add(s);
}
for(Map.Entry<Integer, ArrayList<Segment> > i: loMap.entrySet() )
{
if(!active.isEmpty())
{
int R = active.first().r;
if( R < i.getKey() )
{
for( Segment s: active )
res[s.id] = R;
active.clear();
}
}
active.addAll(i.getValue());
}
if(!active.isEmpty())
{
int R = active.first().r;
for( Segment s: active )
res[s.id] = R;
active.clear();
}
}
TreeSet<Pair> resSet = new TreeSet<>();
int[] eatA = new int[n];
int[] eatB = new int[n];
for(int i=0;i<n;i++)
{
eatA[i] = (a[i]-b[i]+m[i]-res[i]) / 2;
eatB[i] = m[i] - eatA[i];
resSet.add(new Pair(a[i]-eatA[i], b[i]-eatB[i]));
}
out.println(resSet.size());
for(int i=0;i<n;i++)
out.println(eatA[i] + " " + eatB[i]);
}
}
}
class Segment implements Comparable<Segment>
{
@Override
public String toString() {
return "Segment [l=" + l + ", r=" + r + ", id=" + id + "]";
}
int l, r, id;
public Segment(int l, int r, int id)
{
this.l = l;
this.r = r;
this.id = id;
}
@Override
public int compareTo(Segment o) {
if(r != o.r)
return r - o.r;
return id - o.id;
}
}
class Pair implements Comparable<Pair>
{
@Override
public String toString() {
return "Pair [f=" + f + ", s=" + s + "]";
}
int f,s;
public Pair(int f, int s) {
super();
this.f = f;
this.s = s;
}
@Override
public int compareTo(Pair arg0) {
if(f!=arg0.f)
return f-arg0.f;
return s-arg0.s;
}
}
class QuickReader
{
BufferedReader in;
StringTokenizer token;
public QuickReader(InputStream ins)
{
in=new BufferedReader(new InputStreamReader(ins));
token=new StringTokenizer("");
}
public boolean hasNext()
{
while (!token.hasMoreTokens())
{
try
{
String s = in.readLine();
if (s == null) return false;
token = new StringTokenizer(s);
} catch (IOException e)
{
throw new InputMismatchException();
}
}
return true;
}
public String next()
{
hasNext();
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public int[] nextInts(int n)
{
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | f223041068d4c9492e32ad6cd41af105 | train_108.jsonl | 1635863700 | The chef has cooked $$$n$$$ dishes yet again: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. Banquet organizers consider two dishes $$$i$$$ and $$$j$$$ equal if $$$a_i=a_j$$$ and $$$b_i=b_j$$$ at the same time.The banquet organizers estimate the variety of $$$n$$$ dishes as follows. The variety of a set of dishes is equal to the number of different dishes in it. The less variety is, the better.In order to reduce the variety, a taster was invited. He will eat exactly $$$m_i$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he will eat exactly $$$m_i$$$ grams of the $$$i$$$-th dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of variety is the minimum possible. If there are several correct answers, you may output any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1607H extends PrintWriter {
CF1607H() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1607H o = new CF1607H(); o.main(); o.flush();
}
static final int INF = 0x3f3f3f3f;
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] aa = new int[n];
int[] bb = new int[n];
int[] mm = new int[n];
int[] ll = new int[n];
int[] rr = new int[n];
int[] cc = new int[n];
int[] ii = new int[n];
int[] aa_ = new int[n];
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int m = sc.nextInt();
aa[i] = a;
bb[i] = b;
mm[i] = m;
cc[i] = a + b - m;
ll[i] = Math.max(a - m, 0);
rr[i] = a - Math.max(m - b, 0);
ii[i] = i;
}
ii = Arrays.stream(ii).boxed().sorted((i, j) -> cc[i] != cc[j] ? cc[i] - cc[j] : rr[i] - rr[j]).mapToInt($->$).toArray();
int ans = 0;
for (int h1 = 0, h2; h1 < n; h1 = h2) {
int c = cc[ii[h1]];
for (h2 = h1 + 1; h2 < n && cc[ii[h2]] == c; h2++)
;
int r_ = -INF, a_ = -INF;
for (int h = h1; h < h2; h++) {
int i = ii[h];
int l = ll[i], r = rr[i];
if (r_ < l) {
ans++;
r_ = r;
}
aa_[i] = r_;
}
}
println(ans);
for (int i = 0; i < n; i++) {
int x = aa[i] - aa_[i];
int y = mm[i] - x;
println(x + " " + y);
}
}
}
}
| Java | ["5\n\n3\n10 10 2\n9 9 0\n10 9 1\n\n2\n3 4 1\n5 1 2\n\n3\n7 2 5\n6 5 4\n5 5 6\n\n1\n13 42 50\n\n5\n5 7 12\n3 1 4\n7 3 7\n0 0 0\n4 1 5"] | 3 seconds | ["1\n1 1\n0 0\n1 0\n2\n0 1\n1 1\n2\n3 2\n0 4\n1 5\n1\n8 42\n2\n5 7\n3 1\n4 3\n0 0\n4 1"] | null | Java 11 | standard input | [
"greedy",
"sortings",
"two pointers"
] | fdbfe3108e701123693c6e0a6bf9a539 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case's description is preceded by a blank line. Next comes a line that contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of dishes. Then follows $$$n$$$ lines, $$$i$$$-th of which contains three integers $$$a_i$$$, $$$b_i$$$ and $$$m_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$; $$$0 \le m_i \le a_i+b_i$$$) — the mass of fish in $$$i$$$-th dish, the mass of meat in $$$i$$$-th dish and how many grams in total the taster should eat in $$$i$$$-th dish. The sum of all $$$n$$$ values for all input data sets in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimum value of variety that can be achieved by eating exactly $$$m_i$$$ grams of food (for all $$$i$$$ from $$$1$$$ to $$$n$$$) from a dish $$$i$$$. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m_i$$$), where $$$x_i$$$ is how many grams of fish the taster should eat from $$$i$$$-th dish, and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimum balance, print any of them. | standard output | |
PASSED | 900dbbcbc15ec829abf4489c2f7f1def | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round753G {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
Round753G sol = new Round753G();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
int n;
long m;
long[] a, b;
void getInput() {
n = in.nextInt();
m = in.nextLong();
a = new long[n];
b = new long[n];
for(int i=0; i<n; i++) {
a[i] = in.nextLong();
b[i] = in.nextLong();
}
}
void printOutput() {
out.printlnAns(ans);
for(int i=0; i<n; i++) {
out.print(x[i]);
out.print(' ');
out.println(y[i]);
}
}
long ans;
long[] x, y;
void solve(){
// x[i] + y[i] = m
// abs of sum {a[i]-x[i]} - sum {b[i]-y[i]} must be minimized
// consider s = sum a[i] - sum b[i]
// if s > 0, then have to reduce a[i]
// if s < 0, then have to reduce b[i]
// reduce a[i]'s as much as possible,
// and for each i, compute max b[i] that could be instead used
long sum = 0;
x = new long[n];
y = new long[n];
for(int i=0; i<n; i++) {
sum += a[i];
sum -= b[i];
x[i] = Math.min(a[i], m);
y[i] = m - x[i];
sum -= x[i];
sum += y[i];
}
for(int i=0; i<n; i++) {
if(sum < -1) {
// have to reduce x[i] and increase y[i]
long k = -sum;
k /= 2;
k = Math.min(k, b[i] - y[i]);
k = Math.min(k, x[i]);
sum += k*2;
x[i] -= k;
y[i] += k;
}
else
break;
}
ans = Math.abs(sum);
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean[] ans) {
for(boolean b: ans)
printlnAns(b);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 17 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | ecd37863c384d81313b893295f619543 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
private static void run() throws IOException {
int n, m;
n = in.nextInt();
m = in.nextInt();
long count_remain = 0;
long count_a = 0;
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
count_remain += nodes[i].remain;
nodes[i].cal_a_range();
count_a += nodes[i].min_a;
}
long target_a = count_remain / 2;
for (int i = 0; i < n && count_a < target_a; i++) {
long more_a = Math.min(target_a - count_a, nodes[i].max_a - nodes[i].min_a);
count_a += more_a;
nodes[i].x += more_a;
}
out.println(Math.abs(count_remain - count_a * 2));
for (int i = 0; i < n; i++) {
nodes[i].show();
}
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 8e0e8cf442488b6381933ba7447b0e28 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | //Avoid division by decimal digits :). Always try to multiply with whole numbers or fractions instead.
//if getting wrong answer then use long/double instead of int/float
//e + e = o; o + o = e; e + o = o;
//see stuff in a jugaad way... if you are being complicated you are doing it wrong
//If a=b+1 and b is even, then a∧b=1
//If there is a statement in the question that it can be proved that ... then it means there is a very very simple logic behind it, and is not a simulation or dp question.
//Be confident in Maths you are not that bad at it.
// add break statement where you are stopping. Do not forget that, as people may use that weakness to hack your solution.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner reader = new Scanner(System.in);
int test = reader.nextInt();
for (int o = 0; o < test; o++) {
int n = reader.nextInt();
int m = reader.nextInt();
int[] aa = new int[n];
int[] bb = new int[n];
int[] xx = new int[n];
int[] yy = new int[n];
long a = 0, b = 0;
for (int i = 0; i < n; i++) {
aa[i] = reader.nextInt();
bb[i] = reader.nextInt();
xx[i] = Math.min(aa[i], m);
yy[i] = m - xx[i];
a += aa[i] - xx[i];
b += bb[i] - yy[i];
}
for (int i = 0; i < n && b - a >= 2; i++) {
int k = (int) Math.min((b - a) / 2, Math.min(xx[i], bb[i] - yy[i]));
xx[i] -= k;
yy[i] += k;
a += k;
b -= k;
}
System.out.println(Math.abs(b - a));
for (int i = 0; i < n; i++)System.out.println(xx[i] + " " + yy[i]);
}
}
}
//------------------------------------XX---Templatecode---XX--------------------------------------
class Template{
public static long gcd(long a, long b){
if (b == 0)
return a;
return gcd(b, a % b);
}
public static long lcm(long a,long 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;
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | d48cc7c9dd038d484a63859472c49b94 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class codeforces_753_G {
private static void solve(FastIOAdapter io) {
int n = io.nextInt();
int m = io.nextInt();
int[][] q = new int[n][2];
long canEatA = 0;
long canEatB = 0;
long aSum = 0;
long bSum = 0;
for (int i = 0; i < n; i++) {
int a = io.nextInt();
int b = io.nextInt();
q[i][0] = a;
q[i][1] = b;
aSum += a;
bSum += b;
canEatA += Math.min(a, m);
canEatB += Math.min(b, m);
}
long diff = aSum - bSum;
long diffIfEatAllA = diff + (long) n * m - 2 * canEatA;
long diffIfEatAllB = diff + 2 * canEatB - (long) n * m;
long minDiff;
long minAbs = Math.min(Math.abs(diffIfEatAllA), Math.abs(diffIfEatAllB));
if (minAbs == 0) {
minDiff = minAbs;
} else if (diffIfEatAllA / Math.abs(diffIfEatAllA) != diffIfEatAllB / Math.abs(diffIfEatAllB)) {
if (diffIfEatAllA % 2 == 0) {
minDiff = 0;
} else minDiff = 1;
} else {
minDiff = minAbs;
}
long countAToRemove = (Math.abs(diffIfEatAllA) - minDiff) / 2;
io.out.println(minDiff);
for (int i = 0; i < n; i++) {
int a = q[i][0];
int b = q[i][1];
int aToEat = Math.min(a, m);
int bNeededToEat = m - aToEat;
int toExchangePossible = Math.min(aToEat, b - bNeededToEat);
long toExchangeReal = Math.min(countAToRemove, toExchangePossible);
countAToRemove -= toExchangeReal;
io.out.println(aToEat - toExchangeReal + " " + (m - aToEat + toExchangeReal));
}
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.close();
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 628c187e37102488f4ce7e5fca58af03 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | //package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
//G. Banquet Preparations 1
public class Solution14 {
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
int Q = Integer.parseInt(input.readLine());
while (Q > 0){
input.readLine();
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
final int[] fish = new int[n], meat = new int[n];
long[] rf = new long[n], rm = new long[n];
for (int i=0;i<n;++i){
st = new StringTokenizer(input.readLine());
fish[i] = Integer.parseInt(st.nextToken());
meat[i] = Integer.parseInt(st.nextToken());
}
long res = calc(n, m, fish, meat, rf, rm);
out.println(res);
for (int i=0;i<n;++i){
out.println(rf[i] + " " + rm[i]);
}
Q--;
}
out.close(); // close the output file
}
private static long calc(final int n, final long m, final int[] fish, final int[] meat, long[] rf, long[] rm) {
long sum = 0, l = 0, r = 0;
long[] min = new long[n], max = new long[n];
for (int i=0;i<n;++i){
min[i] = Math.max(0, m-meat[i]);
max[i] = Math.min(m, fish[i]);
l += fish[i] - max[i];
r += fish[i] - min[i];
sum += fish[i] + meat[i];
}
long sum2 = sum - n*m;
sum = (sum - n*m) / 2;
long target = l>sum ? l : Math.min(r, sum), diff = target - l;
for (int i=0;i<n;++i){
rf[i] = max[i];
if (diff > 0){
long t = Math.min(diff, max[i]-min[i]);
rf[i] -= t;
diff -= t;
}
rm[i] = m - rf[i];
}
return Math.abs(target - (sum2-target));
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 15785461e195831311bc53fe9415df72 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | //package com.example.practice.codeforces.sc2200;
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.StringTokenizer;
//G. Banquet Preparations 1
public class Solution14 {
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
int Q = Integer.parseInt(input.readLine());
while (Q > 0){
input.readLine();
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
final int[] fish = new int[n], meat = new int[n];
long[] rf = new long[n], rm = new long[n];
for (int i=0;i<n;++i){
st = new StringTokenizer(input.readLine());
fish[i] = Integer.parseInt(st.nextToken());
meat[i] = Integer.parseInt(st.nextToken());
}
long res = calc(n, m, fish, meat, rf, rm);
out.println(res);
for (int i=0;i<n;++i){
out.println(rf[i] + " " + rm[i]);
}
Q--;
}
out.close(); // close the output file
}
private static long calc(final int n, final long m, final int[] fish, final int[] meat, long[] rf, long[] rm) {
long sum = 0, l = 0, r = 0;
long[] min = new long[n], max = new long[n];
for (int i=0;i<n;++i){
min[i] = Math.max(0, m-meat[i]);
max[i] = Math.min(m, fish[i]);
l += fish[i] - max[i];
r += fish[i] - min[i];
sum += fish[i] + meat[i];
}
long sum2 = sum - n*m;
sum = (sum - n*m) / 2;
long target = l>sum ? l : Math.min(r, sum), diff = target - l;
for (int i=0;i<n;++i){
rf[i] = max[i];
if (diff > 0){
long t = Math.min(diff, max[i]-min[i]);
rf[i] -= t;
diff -= t;
}
rm[i] = m - rf[i];
}
return Math.abs(target - (sum2-target));
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 9ccccfb38a2b653041d20494fe23b868 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class ACMIND
{
static FastReader scan;
static PrintWriter pw;
static long MOD = 1_000_000_007;
static long INF = 2_000_000_000_000_000_000L;
static long inf = 2_000_000_000;
public static void main(String[] args) {
new Thread(null,null,"_",1<<27)
{
public void run()
{
try
{
solve();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void solve() throws IOException {
scan = new FastReader();
pw = new PrintWriter(System.out, true);
StringBuilder sb = new StringBuilder();
int t = ni();
while (t-->0) {
int n = ni(), m = ni();
long a[] = new long[n], b[] = new long[n];
long sum_rem = 0;
for(int i=0;i<n;i++) {
a[i] = ni();
b[i] = ni();
sum_rem+=(a[i]+b[i]-m);
}
// pl("sum rem: "+sum_rem);
long min[] = new long[n+1], max[] = new long[n+1];
for(int i=n-1;i>=0;--i) {
min[i] = min[i+1] + max(0, a[i] - m);
max[i] = max[i+1] + a[i] + min(0, b[i] - m);
}
// pa("min", min);
// pa("max", max);
long req = sum_rem/2;
long S;
if(min[0]<=req && req<=max[0]) {
S = req;
}
else if(req>max[0]) {
S = max[0];
}
else {
S = min[0];
}
sb.append(abs(S - (sum_rem - S)));
sb.append("\n");
long curr = 0;
for(int i=0;i<n;i++) {
long x = min[i+1] - (S - curr - a[i]);
x = max(x, max(0, m-b[i]));
sb.append(x+" "+(m-x));
sb.append("\n");
curr += (a[i] - x);
}
assert_in_range("curr", curr, S, S);
}
psb(sb);
pw.flush();
pw.close();
}
static void assert_in_range(String varName, int n, int l, int r) {
if (n >=l && n<=r) return;
System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
System.exit(1);
}
static void assert_in_range(String varName, long n, long l, long r) {
if (n>=l && n<=r) return;
System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
System.exit(1);
}
static int ni() throws IOException
{
return scan.nextInt();
}
static long nl() throws IOException
{
return scan.nextLong();
}
static double nd() throws IOException
{
return scan.nextDouble();
}
static void pl()
{
pw.println();
}
static void p(Object o)
{
pw.print(o+" ");
}
static void pl(Object o)
{
pw.println(o);
}
static void psb(StringBuilder sb)
{
pw.print(sb);
}
static void pa(String arrayName, Object arr[])
{
pl(arrayName+" : ");
for(Object o : arr)
p(o);
pl();
}
static void pa(String arrayName, int arr[])
{
pl(arrayName+" : ");
for(int o : arr)
p(o);
pl();
}
static void pa(String arrayName, long arr[])
{
pl(arrayName+" : ");
for(long o : arr)
p(o);
pl();
}
static void pa(String arrayName, double arr[])
{
pl(arrayName+" : ");
for(double o : arr)
p(o);
pl();
}
static void pa(String arrayName, char arr[])
{
pl(arrayName+" : ");
for(char o : arr)
p(o);
pl();
}
static void pa(String listName, List list)
{
pl(listName+" : ");
for(Object o : list)
p(o);
pl();
}
static void pa(String arrayName, Object[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(Object o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, int[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(int o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, long[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(long o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, char[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(char o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, double[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(double o : arr[i])
p(o);
pl();
}
}
static class FastReader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(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[1000000];
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 | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 998354946d75f3ec7293eb46a334ba1e | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | // package c1607;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.Scanner;
//
// Codeforces Round #753 (Div. 3) 2021-11-02 06:35
// G. Banquet Preparations 1
// https://codeforces.com/contest/1607/problem/G
// time limit per test 2 seconds; memory limit per test 256 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// A known chef has prepared n dishes: the i-th dish consists of a_i grams of fish and b_i grams of
// meat.
//
// The banquet organizers estimate the of n dishes as follows. The is equal to the absolute value of
// the difference between the total mass of fish and the total mass of meat.
//
// Technically, the equals to <=ft|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|. The
// smaller the , the better.
//
// In order to improve the , a taster was invited. He will eat m grams of food from each dish. For
// each dish, the taster determines separately how much fish and how much meat he will eat. The only
// condition is that he should eat exactly m grams of each dish in total.
//
// Determine how much of what type of food the taster should eat from each dish so that the value of
// the is as minimal as possible. If there are several correct answers, you may choose any of them.
//
// Input
//
// The first line of input data contains an integer t (1 <= t <= 10^4)-- the number of the test
// cases.
//
// Each test case's description is preceded by a blank line. Next comes a line that contains
// integers n and m (1 <= n <= 2 * 10^5; 0 <= m <= 10^6). The next n lines describe dishes, the i-th
// of them contains a pair of integers a_i and b_i (0 <= a_i, b_i <= 10^6)-- the masses of fish and
// meat in the i-th dish.
//
// It is guaranteed that it is possible to eat m grams of food from each dish. In other words, m <=
// a_i+b_i for all i from 1 to n inclusive.
//
// The sum of all n values over all test cases in the test does not exceed 2 * 10^5.
//
// Output
//
// For each test case, print on the first line the minimal balance value that can be achieved by
// eating exactly m grams of food from each dish.
//
// Then print n lines that describe a way to do this: the i-th line should contain two integers x_i
// and y_i (0 <= x_i <= a_i; 0 <= y_i <= b_i; x_i+y_i=m), where x_i is how many grams of fish taster
// should eat from the i-th meal and y_i is how many grams of meat.
//
// If there are several ways to achieve a minimal balance, find any of them.
//
// Example
/*
input:
8
1 5
3 4
1 6
3 4
2 2
1 3
4 2
2 4
1 3
1 7
3 6
1 7
1 8
1 9
3 6
1 8
1 9
30 10
3 4
3 1
3 2
4 1
5 4
0 7
6 4
0 8
4 1
5 3
output:
0
2 3
1
3 3
0
1 1
1 1
2
1 3
0 4
3
0 6
0 6
0 6
7
1 5
1 5
6 0
0
3 1
3 1
3 1
0
0 4
2 2
0 4
3 1
1 3
*/
//
public class C1607G {
static final int MOD = (int)1e9+7;
static final Random RAND = new Random();
static long solve(int[][] ab, int m, int[][] output) {
int n = ab.length;
long sum = 0L;
for (int i = 0; i < n; i++) {
int[] v = ab[i];
int a = v[0];
int b = v[1];
// Minimal portion of a to eat
int mina = Math.max(0, m - b);
int maxa = Math.min(m, a);
// The minimal value of a - b after eat maxa of a, and (m-maxa) of b
int min = a - maxa - (b - (m - maxa));
sum += min;
}
for (int i = 0; i < n; i++) {
int[] v = ab[i];
int a = v[0];
int b = v[1];
int mina = Math.max(0, m - b);
int maxa = Math.min(m, a);
// Note that output contains counts of eaten, NOT left over
output[i][0] = maxa;
output[i][1] = (m - maxa);
if (sum < -1) {
int extra = (int) Math.min(maxa - mina, -sum / 2);
output[i][0] -= extra;
output[i][1] += extra;
sum += extra * 2;
}
}
return Math.abs(sum);
}
static String trace(int[][] a) {
StringBuilder sb = new StringBuilder();
for (int[] v : a) {
sb.append(v[0]);
sb.append(' ');
sb.append(v[1]);
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args) {
Scanner in = getInputScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int m = in.nextInt();
int[][] ab = new int[n][2];
for (int i = 0; i < n; i++) {
ab[i][0] = in.nextInt();
ab[i][1] = in.nextInt();
}
int[][] output = new int[n][2];
long ans = solve(ab, m, output);
System.out.println(ans);
System.out.print(trace(output));
}
in.close();
}
static Scanner getInputScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
} catch (Exception e) {
return new Scanner(System.in);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 2ee08643660f326ff6b9124f93bedee9 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.io.*;
public class G_1607 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt(), om = sc.nextInt();
int[] a = new int[n], b = new int[n];
int[] m = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
m[i] = a[i] + b[i] - om;
}
int[] maxA = new int[n], minA = new int[n];
long max = 0, min = 0;
for(int i = 0; i < n; i++) {
maxA[i] = Math.min(m[i], a[i]);
minA[i] = m[i] - Math.min(m[i], b[i]);
max += 2l * maxA[i] - m[i];
min += 2l * minA[i] - m[i];
}
long ans = 0;
if(min > 0)
ans = min;
else if(max < 0)
ans = max;
ans -= min;
if(ans % 2 == 0)
ans /= 2;
else
ans = (ans - 1) / 2;
pw.println(Math.abs(1l * min + 2 * ans));
for(int i = 0; i < n; i++) {
long add = Math.min(ans, maxA[i] - minA[i]);
ans -= add;
long na = minA[i] + add;
long nb = m[i] - na;
pw.println((a[i] - na) + " " + (b[i] - nb));
}
}
pw.flush();
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++)
array[i] = new Integer(nextInt());
return array;
}
public long[] nextLongArray(int n) throws IOException {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
public static int[] shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | ea157a1ff9aa03828b17a6afaed10034 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.io.*;
public class G_1607 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt(), om = sc.nextInt();
int[] a = new int[n], b = new int[n];
int[] m = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
b[i] = sc.nextInt();
m[i] = a[i] + b[i] - om;
}
int[] maxA = new int[n], minA = new int[n];
long max = 0, min = 0;
for(int i = 0; i < n; i++) {
maxA[i] = Math.min(m[i], a[i]);
minA[i] = m[i] - Math.min(m[i], b[i]);
max += 2l * maxA[i] - m[i];
min += 2l * minA[i] - m[i];
}
long ans = 0;
if(min > 0)
ans = min;
else if(max < 0)
ans = max;
ans -= min;
if(ans % 2 == 0)
ans /= 2;
else
ans = (ans - 1) / 2;
long trueAns = 0;
ArrayList<String> construct = new ArrayList<>();
for(int i = 0; i < n; i++) {
long add = Math.min(ans, maxA[i] - minA[i]);
ans -= add;
long na = minA[i] + add;
long nb = m[i] - na;
trueAns += na - nb;
construct.add((a[i] - na) + " " + (b[i] - nb));
}
pw.println(Math.abs(trueAns));
for(String s : construct)
pw.println(s);
}
pw.flush();
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++)
array[i] = new Integer(nextInt());
return array;
}
public long[] nextLongArray(int n) throws IOException {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
public static int[] shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 9e6bb21e52ad2f53d7475f09c046d0b9 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.StringTokenizer;
import java.lang.Math;
import java.lang.StringBuilder;
import java.io.*;
public class Coba{
public static void main (String args[]) throws IOException{
/*try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}*/
OutputStream out = new BufferedOutputStream(System.out);
FastScanner fs = new FastScanner();
int []max_interv = new int [200000];
int []min_interv = new int [200000];
int []old_meat = new int [200000];
int []old_fish = new int [200000];
int []meat = new int [200000];
int []fish = new int [200000];
int T = fs.nextInt();
for (int t = 0 ; t < T ; ++t){
int n = fs.nextInt();
int m = fs.nextInt();
long sum = 0;
for (int i = 0 ; i < n ; ++i){
fish[i] = fs.nextInt();
meat[i] = fs.nextInt();
old_meat[i] = meat[i];
old_fish[i] = fish[i];
max_interv[i] = meat[i] - Math.abs(m - fish[i]);
min_interv[i] = Math.abs(m - meat[i]) - fish[i];
sum += max_interv[i];
//System.out.println(min_interv[i] + " " + max_interv[i]);
meat[i] -= Math.max(0 , m - fish[i]);
fish[i] = Math.max(0 , fish[i] - m);
}
int kurang = (int)(sum % 2);
for (int i = 0 ; i < n && sum > 1 ; ++i){
int geser = (int)(Math.min(max_interv[i] - min_interv[i] , sum - kurang));
meat[i] -= geser / 2;
fish[i] += geser / 2;
sum -= geser;
}
out.write((Math.abs(sum) + "\n").getBytes());
for (int i = 0 ; i < n ; ++i)
out.write(((old_fish[i] - fish[i]) + " " + (old_meat[i] - meat[i]) + "\n").getBytes());
out.flush();
}
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
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());
}
char nextChar(){
return next().charAt(0);
}
String nextLine(){
String s = "";
try{
s = br.readLine();
}catch(IOException e){
e.printStackTrace();
}
return s;
}
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 647f62566cefe28b545058d208e6faca | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.StringTokenizer;
import java.lang.Math;
import java.lang.StringBuilder;
import java.io.*;
public class Coba{
public static void main (String args[]) throws IOException{
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
OutputStream out = new BufferedOutputStream(System.out);
FastScanner fs = new FastScanner();
int []max_interv = new int [200000];
int []min_interv = new int [200000];
int []old_meat = new int [200000];
int []old_fish = new int [200000];
int []meat = new int [200000];
int []fish = new int [200000];
int T = fs.nextInt();
for (int t = 0 ; t < T ; ++t){
int n = fs.nextInt();
int m = fs.nextInt();
long sum = 0;
for (int i = 0 ; i < n ; ++i){
fish[i] = fs.nextInt();
meat[i] = fs.nextInt();
old_meat[i] = meat[i];
old_fish[i] = fish[i];
max_interv[i] = meat[i] - Math.abs(m - fish[i]);
min_interv[i] = Math.abs(m - meat[i]) - fish[i];
sum += max_interv[i];
//System.out.println(min_interv[i] + " " + max_interv[i]);
meat[i] -= Math.max(0 , m - fish[i]);
fish[i] = Math.max(0 , fish[i] - m);
}
int kurang = (int)(sum % 2);
for (int i = 0 ; i < n && sum > 1 ; ++i){
int geser = (int)(Math.min(max_interv[i] - min_interv[i] , sum - kurang));
meat[i] -= geser / 2;
fish[i] += geser / 2;
sum -= geser;
}
out.write((Math.abs(sum) + "\n").getBytes());
for (int i = 0 ; i < n ; ++i)
out.write(((old_fish[i] - fish[i]) + " " + (old_meat[i] - meat[i]) + "\n").getBytes());
out.flush();
}
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
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());
}
char nextChar(){
return next().charAt(0);
}
String nextLine(){
String s = "";
try{
s = br.readLine();
}catch(IOException e){
e.printStackTrace();
}
return s;
}
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 17996ed650f758a11a5df7d9dc2e61f8 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
private static void run() throws IOException {
int n, m;
n = in.nextInt();
m = in.nextInt();
long count_remain = 0;
long count_a = 0;
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
count_remain += nodes[i].remain;
nodes[i].cal_a_range();
count_a += nodes[i].min_a;
}
long target_a = count_remain / 2;
for (int i = 0; i < n && count_a < target_a; i++) {
long more_a = Math.min(target_a - count_a, nodes[i].max_a - nodes[i].min_a);
count_a += more_a;
nodes[i].x += more_a;
}
out.println(Math.abs(count_remain - count_a * 2));
for (int i = 0; i < n; i++) {
nodes[i].show();
}
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | e057ea411736ba343057896b3655e09c | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class gotoJapan {
public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solution solver = new Solution();
boolean isTest = true;
int tC = isTest ? Integer.parseInt(in.next()) : 1;
for (int i = 1; i <= tC; i++)
solver.solve(in, out, i);
out.close();
}
/* ............................................................. */
static class Solution {
InputReader in;
PrintWriter out;
public void solve(InputReader in, PrintWriter out, int test) {
this.in = in;
this.out = out;
int n=ni();
int m=ni();
int a[][]=new int[n][2];
int b[][]=new int[n][2];
long sum=0;
for(int i=0;i<n;i++) {
int x=ni();
int y=ni();
int mx=m;
b[i][0]=x;
b[i][1]=y;
if(x<=y) {
int d=Math.min(x, mx);
x=x-d;
mx=mx-d;
y=y-mx;
}
else {
int d=Math.min(y, mx);
y=y-d;
mx=mx-d;
x=x-mx;
}
a[i][0]=x;
a[i][1]=y;
sum=sum+(x-y);
}
//out.println(a[0][0]+" "+a[0][1]);
//out.println(a[1][0]+" "+a[1][1]);
if(sum<0) {
sum=Math.abs(sum);
for(int i=0;i<n;i++) {
if(b[i][0]>b[i][1])continue;
long p=Math.min(a[i][0]+sum/2, b[i][0]);
long r=Math.min(a[i][1], p-a[i][0]);
a[i][1]=a[i][1]-(int)(r);
sum=sum-(r)*2;
a[i][0]=a[i][0]+(int)r;
}
}
else if(sum>0) {
for(int i=0;i<n;i++) {
if(b[i][1]>=b[i][0])continue;
long p=Math.min(a[i][1]+sum/2, b[i][1]);
long r=Math.min(a[i][0], p-a[i][1]);
a[i][0]=a[i][0]-(int)(r);
sum=sum-(r)*2;
a[i][1]=a[i][1]+(int)r;
}
}
pn(sum);
for(int i=0;i<n;i++) {
out.println((b[i][0]-a[i][0])+" "+(b[i][1]-a[i][1]));
}
}
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
char[] n() {
return in.next().toCharArray();
}
int ni() {
return in.nextInt();
}
long nl() {
return in.nextLong();
}
long[] nal(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
void pn(long zx) {
out.println(zx);
}
void pn(String sz) {
out.println(sz);
}
void pn(double dx) {
out.println(dx);
}
void pn(long ar[]) {
for (int i = 0; i < ar.length; i++)
out.print(ar[i] + " ");
out.println();
}
void pn(String ar[]) {
for (int i = 0; i < ar.length; i++)
out.println(ar[i]);
}
}
/* ......................Just Input............................. */
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());
}
}
/* ......................Just Input............................. */
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 2464b4225fcc88d1fc8d5289636b6b96 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
private static void run() throws IOException {
int n, m;
n = in.nextInt();
m = in.nextInt();
long count_remain = 0;
long count_a = 0;
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
nodes[i].id = i;
nodes[i].a = in.nextInt();
nodes[i].b = in.nextInt();
nodes[i].remain = nodes[i].a + nodes[i].b - m;
count_remain += nodes[i].remain;
nodes[i].cal_a_range();
count_a += nodes[i].min_a;
}
long target_a = count_remain / 2;
for (int i = 0; i < n && count_a < target_a; i++) {
long more_a = Math.min(target_a - count_a, nodes[i].max_a - nodes[i].min_a);
count_a += more_a;
nodes[i].x += more_a;
}
out.println(Math.abs(count_remain - count_a * 2));
for (int i = 0; i < n; i++) {
nodes[i].show();
}
}
private static class Node {
long a, b, remain, id, x;
long min_a, max_a;
private void cal_a_range() {
this.max_a = Math.min(remain, a);
this.x = this.min_a = remain - Math.min(remain, b);
}
private void show() {
out.print(a - x);
out.print(' ');
out.print(b - (remain - x));
out.println();
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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++];
}
//hqjf eyenh
public void close() throws IOException {
din.close();
}
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 51d4874619e218aa983d52ad182b448c | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | //package banquetpreparations1;
import java.util.*;
import java.io.*;
public class banquetpreparations {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(fin.readLine());
StringBuilder fout = new StringBuilder();
while(t-- > 0) {
fin.readLine();
StringTokenizer st = new StringTokenizer(fin.readLine());
int n = Integer.parseInt(st.nextToken());
long m = Integer.parseInt(st.nextToken());
long[][] meals = new long[n][2];
for(int i = 0; i < n; i++) {
st = new StringTokenizer(fin.readLine());
meals[i][0] = Integer.parseInt(st.nextToken());
meals[i][1] = Integer.parseInt(st.nextToken());
}
long[][] eaten = new long[n][2];
long a = 0;
long b = 0;
//first eat as much a as possible
for(int i = 0; i < n; i++) {
long aEaten = Math.min(meals[i][0], m);
eaten[i][0] = aEaten;
eaten[i][1] = m - aEaten;
a += meals[i][0] - eaten[i][0];
b += meals[i][1] - eaten[i][1];
//System.out.println(eaten[i][0] + " " + eaten[i][1]);
}
//now, switch to eating b, until you get a and b equal
long diff = b - a;
//System.out.println(diff);
for(int i = 0; i < n; i++) {
if(diff <= 1) {
break;
}
long maxChange = Math.min((long) Math.min(m, diff / 2), Math.min(meals[i][1] - eaten[i][1], eaten[i][0]));
diff -= maxChange * 2;
eaten[i][0] -= maxChange;
eaten[i][1] += maxChange;
}
fout.append(Math.abs(diff)).append("\n");
for(int i = 0; i < n; i++) {
fout.append(eaten[i][0]).append(" ").append(eaten[i][1]).append("\n");
}
}
System.out.print(fout);
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | dc96d8a599883561edbdfbcc51ccc347 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter out;
static Reader in;
public static void main(String[] args) throws Exception {
input_output();
Main solver = new Main();
solver.solve();
out.close();
out.flush();
}
static int INF = (int)1e9;
static int MAXN = (int)2e6 + 5;
static int q, t, n, m, k;
void solve() throws Exception {
t = in.nextInt();
while (t --> 0) {
n = in.nextInt();
m = in.nextInt();
long mid = ((long)m * n) / 2,
score = 0,
all = (long)m * n,
scoreA = 0,
scoreB = 0;
int[] a = new int[n],
b = new int[n],
maxA = new int[n],
eatA = new int[n],
eatB = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt(); scoreA += a[i];
b[i] = in.nextInt(); scoreB += b[i];
int minA = m - Math.min(m, b[i]);
maxA[i] = Math.min(a[i], m) - minA;
eatA[i] = minA;
eatB[i] = m-minA;
scoreA -= minA;
scoreB -= m-minA;
}
for (int i = 0; i < n; i++) {
int takeA = (int)Math.max(Math.min((scoreA-scoreB)/2, maxA[i]), 0);
eatA[i] += takeA;
eatB[i] -= takeA;
scoreA -= takeA;
scoreB += takeA;
}
out.println(Math.abs(scoreA - scoreB));
for (int i = 0; i < n; i++) out.println(eatA[i]+" "+eatB[i]);
}
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static void input_output() throws IOException {
File f = new File("in.txt");
if (f.exists() && !f.isDirectory()) {
in = new Reader(new FileInputStream("in.txt"));
} else in = new Reader();
f = new File("out.txt");
if (f.exists() && !f.isDirectory()) {
out = new PrintWriter(new File("out.txt"));
} else out = new PrintWriter(System.out);
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | d4687fa2fcf038d87a6717e934eaac8c | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1607G extends PrintWriter {
CF1607G() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1607G o = new CF1607G(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] aa = new int[n];
int[] bb = new int[n];
int[] xx = new int[n];
int[] yy = new int[n];
long a = 0, b = 0;
for (int i = 0; i < n; i++) {
aa[i] = sc.nextInt();
bb[i] = sc.nextInt();
xx[i] = Math.min(aa[i], m);
yy[i] = m - xx[i];
a += aa[i] - xx[i];
b += bb[i] - yy[i];
}
for (int i = 0; i < n && b - a >= 2; i++) {
int k = (int) Math.min((b - a) / 2, Math.min(xx[i], bb[i] - yy[i]));
xx[i] -= k;
yy[i] += k;
a += k;
b -= k;
}
println(Math.abs(b - a));
for (int i = 0; i < n; i++)
println(xx[i] + " " + yy[i]);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 70e7645f43bee3d55e883dc15cc595e5 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class BMain {
public static void main(String[] args) {
QuickReader in = new QuickReader(System.in);
try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
{
new BMain().solve(in, out);
}
}
public void solve(QuickReader in, PrintWriter out)
{
int T = in.nextInt();
while(T-->0)
{
int n,m;
n = in.nextInt();
m = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i=0;i<n;i++)
{
a[i] = in.nextInt();
b[i] = in.nextInt();
}
long s = 0L;
long eaLo = 0L;
long eaHi = 0L;
for(int i=0;i<n;i++)
s += (a[i]-b[i]+m);
for(int i=0;i<n;i++)
{
eaLo += Math.max(0, m-b[i]);
eaHi += Math.min(a[i], m);
}
long res = Math.abs(s-2*eaLo);
long resEA = eaLo;
if(res > Math.abs(s - 2*eaHi) )
{
res = Math.abs(s-2*eaHi);
resEA = eaHi;
}
long eaMid = s/2;
for(long ea = eaMid-2; ea<=eaMid+2; ea++)
if ( eaLo <= ea && ea <= eaHi && res > Math.abs(s - 2* ea))
{
res = Math.abs(s-2*ea);
resEA = ea;
}
out.println(res);
long[] resEArray = new long[n];
for(int i=0;i<n;i++)
{
resEArray[i] = Math.max(0, m-b[i]);
resEA -= resEArray[i];
}
for(int i=0;i<n;i++)
{
long incr = Math.min(Math.min(a[i],m) - Math.max(0, m-b[i]), resEA);
resEArray[i] += incr;
resEA -= incr;
}
for(int i=0;i<n;i++)
out.println(resEArray[i] + " " + (m-resEArray[i]));
}
}
}
class QuickReader
{
BufferedReader in;
StringTokenizer token;
public QuickReader(InputStream ins)
{
in=new BufferedReader(new InputStreamReader(ins));
token=new StringTokenizer("");
}
public boolean hasNext()
{
while (!token.hasMoreTokens())
{
try
{
String s = in.readLine();
if (s == null) return false;
token = new StringTokenizer(s);
} catch (IOException e)
{
throw new InputMismatchException();
}
}
return true;
}
public String next()
{
hasNext();
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public int[] nextInts(int n)
{
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | a817b80be77b1c163d6092e67f2309d5 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
bu.readLine();
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]);
int i,a[][]=new int[n][2],ans[][]=new int[n][2];
for(i=0;i<n;i++)
{
s=bu.readLine().split(" ");
a[i][0]=Integer.parseInt(s[0]); a[i][1]=Integer.parseInt(s[1]);
ans[i][1]=Math.min(a[i][1],m);
ans[i][0]=m-ans[i][1];
}
long fish=0,meat=0;
for(i=0;i<n;i++)
{
fish+=a[i][0]-ans[i][0];
meat+=a[i][1]-ans[i][1];
}
//we need tester to eat, not store
for(i=0;i<n;i++)
if(fish>meat)
{
long red=Math.min(a[i][0]-ans[i][0],ans[i][1]); //minimum change possible
red=Math.min(red,(fish-meat)/2); //decrease fish and increase meat, change happens 2 way
fish-=red;
meat+=red;
ans[i][0]+=red;
ans[i][1]-=red;
}
for(i=0;i<n;i++)
if(fish<meat) //do opposite
{
long red=Math.min(ans[i][0],a[i][1]-ans[i][1]);
red=Math.min(red,(meat-fish)/2);
fish+=red;
meat-=red;
ans[i][0]-=red;
ans[i][1]+=red;
}
fish=0; meat=0;
for(i=0;i<n;i++)
{
fish+=a[i][0]-ans[i][0];
meat+=a[i][1]-ans[i][1];
}
sb.append(Math.abs(fish-meat)+"\n");
for(i=0;i<n;i++) sb.append(ans[i][0]+" "+ans[i][1]+"\n");
}
System.out.print(sb);
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 36c8b72c7e7862d9a12328b5fe556824 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1607G extends PrintWriter {
CF1607G() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1607G o = new CF1607G(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] aa = new int[n];
int[] bb = new int[n];
int[] xx = new int[n];
int[] yy = new int[n];
long a = 0, b = 0;
for (int i = 0; i < n; i++) {
aa[i] = sc.nextInt();
bb[i] = sc.nextInt();
xx[i] = Math.min(aa[i], m);
yy[i] = m - xx[i];
a += aa[i] - xx[i];
b += bb[i] - yy[i];
}
for (int i = 0; i < n && b - a >= 2; i++) {
int k = (int) Math.min((b - a) / 2, Math.min(xx[i], bb[i] - yy[i]));
xx[i] -= k;
yy[i] += k;
a += k;
b -= k;
}
println(Math.abs(b - a));
for (int i = 0; i < n; i++)
println(xx[i] + " " + yy[i]);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 11 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 4130456884f8efae509af459045c6b7f | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1607G extends PrintWriter {
CF1607G() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1607G o = new CF1607G(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] aa = new int[n];
int[] bb = new int[n];
int[] xx = new int[n];
int[] yy = new int[n];
long a = 0, b = 0;
for (int i = 0; i < n; i++) {
aa[i] = sc.nextInt();
bb[i] = sc.nextInt();
xx[i] = Math.min(aa[i], m);
yy[i] = m - xx[i];
a += aa[i] - xx[i];
b += bb[i] - yy[i];
}
for (int i = 0; i < n && b - a >= 2; i++) {
int k = (int) Math.min((b - a) / 2, Math.min(xx[i], bb[i] - yy[i]));
xx[i] -= k;
yy[i] += k;
a += k;
b -= k;
}
println(Math.abs(b - a));
for (int i = 0; i < n; i++)
println(xx[i] + " " + yy[i]);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 151fd65a75eb8e6345cf0c1226d8967e | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
public class MainSolution extends MainSolutionT {
// global vars
public void init(int tests_count){}
public class TestCase extends TestCaseT
{
public Object solve()
{
long n = readLong(), m = readLong();
int N = (int)n;
int i;
long[] a = new long[N];
long[] b = new long[N];
long amax_sum = 0;
long amin_sum = 0;
long amins[] = new long[N];
long amin, amax;
long bal = 0;
for (i=0; i<n; i++)
{
a[i] = readLong();
b[i] = readLong();
bal += a[i] - b[i];
amax = Math.min(a[i],m);
amin = m-Math.min(b[i],m);
amins[i] = amin;
amax_sum += amax;
amin_sum += amin;
}
long x, agoal=0;
amax = amax_sum;
amin = amin_sum;
agoal = (n*m + bal) / 2;
if (agoal > amax)
{
agoal = amax;
}
if (agoal < amin)
{
agoal = amin;
}
// if (bal>=n*m)
// {
// agoal = amax;
// }
//
// if ((0<=bal)&&(bal<n*m))
// {
// agoal =
// }
out.println( (long)Math.abs( bal + n*m - 2*agoal ) );
for (i=0; i<n; i++)
{
x = amins[i];
if (agoal > amin_sum)
{
x += agoal - amin_sum;
}
x = Math.min(x, Math.min(m, a[i]));
agoal -= x;
amin_sum -= amins[i];
out.printf("%d %d\n", x, m-x);
}
return null;
}
}
public void run()
{
int t = multiply_test ? readInt() : 1;
this.init(t);
for (int i = 0; i < t; i++) {
TestCase T = new TestCase();
T.run(i + 1);
}
}
public void loc_params()
{
this.log_enabled = false;
}
public void params(){
this.multiply_test = true;
}
}
public class MainSolutionT extends MainSolutionBase
{
public class TestCaseT extends TestCaseBase
{
}
}
public class MainSolutionBase
{
public boolean is_local = false;
public MainSolutionBase()
{
}
public class TestCaseBase
{
public Object solve() {
return null;
}
public int caseNumber;
public TestCaseBase() {
}
public void run(int cn){
this.caseNumber = cn;
Object r = this.solve();
if ((r != null))
{
out.println(r);
}
}
}
public String impossible(){
return "IMPOSSIBLE";
}
public String strf(String format, Object... args)
{
return String.format(format, args);
}
public BufferedReader in;
public PrintStream out;
public boolean log_enabled = false;
public boolean multiply_test = true;
public void params()
{
}
public void loc_params()
{
}
private StringTokenizer tokenizer = null;
public int readInt() {
return Integer.parseInt(readToken());
}
public long readLong() {
return Long.parseLong(readToken());
}
public double readDouble() {
return Double.parseDouble(readToken());
}
public String readLn() {
try {
String s;
while ((s = in.readLine()).length() == 0);
return s;
} catch (Exception e) {
return "";
}
}
public String readToken() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
} catch (Exception e) {
return "";
}
}
public int[] readIntArray(int n) {
int[] x = new int[n];
readIntArray(x, n);
return x;
}
public void readIntArray(int[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readInt();
}
}
public long[] readLongArray(int n) {
long[] x = new long[n];
readLongArray(x, n);
return x;
}
public void readLongArray(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readLong();
}
}
public void readLongArrayRev(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[n-i-1] = readLong();
}
}
public void logWrite(String format, Object... args) {
if (!log_enabled) {
return;
}
out.printf(format, args);
}
public void readLongArrayBuf(long[] x, int n) {
char[]buf = new char[1000000];
long r = -1;
int k= 0, l = 0;
long d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void readIntArrayBuf(int[] x, int n) {
char[]buf = new char[1000000];
int r = -1;
int k= 0, l = 0;
int d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void printArray(long[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(int[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(long[] a, int n, char dl)
{
long x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(double[] a, int n, char dl)
{
long x;
double y;
int i, l = 0;
for (i=0; i<n; i++)
{
x = (long)a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1 + 10*n;
char[] s = new char[l];
l--;
boolean z;
int j;
for (i=n-1; i>=0; i--)
{
x = (long)a[i];
y = (long)(1000000000*(a[i]-x));
z = false;
if (x<0)
{
x = -x;
z = true;
}
for (j=0; j<9; j++)
{
s[l--] = (char)('0' + (y % 10));
y /= 10;
}
s[l--] = '.';
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(int[] a, int n, char dl)
{
int x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printMatrix(int[][] a, int n, int m)
{
int x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
public void printMatrix(long[][] a, int n, int m)
{
long x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
}
public void run()
{
MainSolution S;
try {
S = new MainSolution();
S.in = new BufferedReader(new InputStreamReader(System.in));
//S.out = System.out;
S.out = new PrintStream(new BufferedOutputStream( System.out ));
} catch (Exception e) {
return;
}
S.params();
S.run();
S.out.flush();
}
public static void main(String args[]) {
Locale.setDefault(Locale.US);
Main M = new Main();
M.run();
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | b5a6b0a27d1a9acd60288f4140481ec4 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run() {
for(int q=ni();q>0;q--){
work();
}
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
long inf=Long.MAX_VALUE/3;
void work() {
int n=ni(),m=ni();
int[][] A=new int[n][];
for(int i=0;i<n;i++)A[i]=new int[]{ni(),ni()};
int[][] rec=new int[n][2];
for(int i=0;i<n;i++){
int v1=Math.max(A[i][0]-m,0);
int v2=A[i][1]-Math.max(m-A[i][0],0);
rec[i][0]=v1-v2;
v2=Math.max(A[i][1]-m,0);
v1=A[i][0]-Math.max(m-A[i][1],0);
rec[i][1]=v1-v2;
}
long cur=0;
int[] rec2=new int[n];
for(int i=0;i<n;i++){
cur+=rec[i][0];
rec2[i]=rec[i][0];
}
for(int i=0;i<n;i++){
if(cur>=0)break;
if(cur-rec[i][0]+rec[i][1]>=0){
long v=-cur+rec[i][0];
if(Math.abs(v)%2!=Math.abs(rec[i][0])%2){
v++;
}
cur+=v-rec[i][0];
rec2[i]=(int)v;
}else{
cur+=rec[i][1]-rec[i][0];
rec2[i]=rec[i][1];
}
}
out.println(Math.abs(cur));
for(int i=0;i<n;i++){
//x+y==v1-m
//x-y==v2
int x=(rec2[i]+A[i][0]+A[i][1]-m)/2;
int y=(A[i][0]+A[i][1]-rec2[i]-m)/2;
out.println((A[i][0]-x)+" "+(A[i][1]-y));
}
}
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private double nd() {
return in.nextDouble();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
InputStreamReader input;//no buffer
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(boolean isBuffer)
{
if(!isBuffer){
input=new InputStreamReader(System.in);
}else{
br=new BufferedReader(new InputStreamReader(System.in));
}
}
public boolean hasNext(){
try{
String s=br.readLine();
if(s==null){
return false;
}
st=new StringTokenizer(s);
}catch(IOException e){
e.printStackTrace();
}
return true;
}
public String next()
{
if(input!=null){
try {
StringBuilder sb=new StringBuilder();
int ch=input.read();
while(ch=='\n'||ch=='\r'||ch==32){
ch=input.read();
}
while(ch!='\n'&&ch!='\r'&&ch!=32){
sb.append((char)ch);
ch=input.read();
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
}
}
while(st==null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return (int)nextLong();
}
public long nextLong() {
try {
if(input!=null){
long ret=0;
int b=input.read();
while(b<'0'||b>'9'){
b=input.read();
}
while(b>='0'&&b<='9'){
ret=ret*10+(b-'0');
b=input.read();
}
return ret;
}
}catch (Exception e){
e.printStackTrace();
}
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | c7365418a6645452f2c552a0d1dc140b | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class D {
public static void process() throws IOException {
int n = sc.nextInt(),m = sc.nextInt();
ArrayList<Pair> lis = new ArrayList<D.Pair>();
ArrayList<Pair> initial = new ArrayList<D.Pair>();
for(int i = 0; i<n; i++) {
int a = sc.nextInt(),b = sc.nextInt();
lis.add(new Pair(a, b));
initial.add(new Pair(a, b));
}
long backup[] = new long[n];
for(int i = 0; i<n; i++) {
int min = min(lis.get(i).x, m);
lis.get(i).x-=min;
lis.get(i).y-=(m-min);
int have = min(min,lis.get(i).y);
backup[i] = have;
}
long sum1 = 0,sum2 = 0;
for(int i = 0; i<n; i++) {
sum1+=lis.get(i).x;
sum2+=lis.get(i).y;
}
int i = 0;
while(sum2 > sum1 && i<n) {
long have = backup[i];
long req = (sum2-sum1)/2;
long curr = min(have,req);
lis.get(i).x+=curr;
lis.get(i).y-=curr;
sum2-=curr;
sum1+=curr;
i++;
}
System.out.println(abs(sum1-sum2));
for( i = 0; i<n; i++) {
System.out.println((initial.get(i).x-lis.get(i).x) + " "+(initial.get(i).y-lis.get(i).y));
}
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | e54c4e194b487016a24eb16fa4d74303 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1607G extends PrintWriter {
CF1607G() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1607G o = new CF1607G(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] aa = new int[n];
int[] bb = new int[n];
int[] xx = new int[n];
int[] yy = new int[n];
long a = 0, b = 0;
for (int i = 0; i < n; i++) {
aa[i] = sc.nextInt();
bb[i] = sc.nextInt();
xx[i] = Math.min(aa[i], m);
yy[i] = m - xx[i];
a += aa[i] - xx[i];
b += bb[i] - yy[i];
}
for (int i = 0; i < n && b - a >= 2; i++) {
int k = (int) Math.min((b - a) / 2, Math.min(xx[i], bb[i] - yy[i]));
xx[i] -= k;
yy[i] += k;
a += k;
b -= k;
}
println(Math.abs(b - a));
for (int i = 0; i < n; i++)
println(xx[i] + " " + yy[i]);
}
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | b11a92a607c1ef902d80fded1703193a | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | // Problem: G. Banquet Preparations 1
// Contest: Codeforces - Codeforces Round #753 (Div. 3)
// URL: https://codeforces.com/contest/1607/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
import java.io.*;
import java.util.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
private void preparation() {
}
private void clear() {
}
private void solve() throws Exception {
rf();rf();
int n = ri();
int m = ri();
int [] a = new int[n];
int [] b = new int[n];
long suma = 0, sumb = 0;
for(int i = 0; i < n; i ++){
rf();
suma += a[i] = ri();
sumb += b[i] = ri();
}
int [] ansa = new int[n];
for(int i = 0; i < n; i++){
suma -= ansa[i] = max(m - b[i],0);
sumb -= m - ansa[i];
}
if(suma < sumb){
//this case, have no optiminal solution.
addAns(abs(suma - sumb));
for(int i = 0; i < n; i++){
sb.append(ansa[i] + " " + (m - ansa[i]) + "\n");
}
return ;
}
for(int i = 0; i < n; i++){
if(suma - sumb > 1){
long t = min(a[i] - ansa[i],m - ansa[i]);
if(suma - (suma + sumb) / 2 >= t){
ansa[i] += t;
suma -= t;
sumb += t;
}else{
t = suma - (suma + sumb) / 2;
ansa[i] += t;
suma -= t;
sumb += t;
}
}else{
break;
}
}
addAns(abs(suma - sumb));
for(int i = 0; i < n; i++){
sb.append(ansa[i] + " " + (m - ansa[i]) + "\n");
}
}
private void run() throws Exception {
int T = 1;
rf();
T = ri();
preparation();
while (T-- > 0) {
solve();
if (T != 0) {
clear();
}
}
printAns();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
StringBuilder sb = new StringBuilder();
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer strT;
private void addAns(int a){
sb.append(a + "\n");
}
private void addAns(long a){
sb.append(a + "\n");
}
private void addAns(String s){
sb.append(s + "\n");
}
private void rf() throws IOException {
strT = new StringTokenizer(infile.readLine());
}
private int ri() {
return Integer.parseInt(strT.nextToken());
}
private long rl() {
return Long.parseLong(strT.nextToken());
}
private char [] rs2c(){
return strT.nextToken().toCharArray();
}
private String rs(){
return strT.nextToken();
}
private void printAns() {
System.out.println(sb);
}
private int[] readArr(int N) throws Exception {
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(strT.nextToken());
}
return arr;
}
private long[] readArr2(int N) throws Exception {
long[] arr = new long[N];
for (int i = 0; i < N; i++) {
arr[i] = Long.parseLong(strT.nextToken());
}
return arr;
}
private void print(String format, Object ... args){
System.out.println(String.format(format,args));
}
private void print(int[] arr, int x) {
//for debugging only
for (int i = 0;i < min(arr.length, x); i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
private void print(long[] arr, int x) {
//for debugging only
for (int i = 0;i < min(arr.length, x); i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
| Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 08f87aa727d2f1b5f5e47f672a1ed7a6 | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.util.*;
import java.io.*;
////***************************************************************************
/* public class E_Gardener_and_Tree implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start();
}
public void run(){
WRITE YOUR CODE HERE!!!!
JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!!
}
}
*/
/////**************************************************************************
public class G_Banquet_Preparations_1{
public static void main(String[] args) {
FastScanner s= new FastScanner();
//PrintWriter out=new PrintWriter(System.out);
//end of program
//out.println(answer);
//out.close();
StringBuilder res = new StringBuilder();
int t=s.nextInt();
int p=0;
while(p<t){
long n=s.nextLong();
long m=s.nextLong();
long a[]= new long[(int)n];// fish
long b[]= new long[(int)n];// meat
long tfish=0;
long tmeat=0;
for(int i=0;i<n;i++){
a[i]=s.nextLong();
b[i]=s.nextLong();
tfish+=a[i];
tmeat+=b[i];
}
long k=((tfish-tmeat)+(n*m));
int nn=(int)n;
long min[]= new long[nn];
long max[]= new long[nn];
long l=0;
long r=0;
for(int i=0;i<n;i++){
if(a[i]>=m){
max[i]=m;
}
else{
max[i]=a[i];
}
if(b[i]>=m){
min[i]=0;
}
else{
min[i]=m-b[i];
}
l+=min[i];
r+=max[i];
}
if(k>0){
if(k%2==0){
long h=k/2;
if(h>=l && h<=r){
long up=h-l;
res.append("0\n");
find(a,b,min,max,up,res,(int)n,m);
}
else{
long a1=Math.abs(k-(2*(l)));
long a2=Math.abs(k-(2*r));
if(a1<a2){
res.append(a1+" \n");
find(a,b,min,max,0,res,(int)n,m);
}
else{
res.append(a2+"\n");
find(a,b,min,max,(r-l),res,(int)n,m);
}
}
}
else{
long num1=k/2;
long num2=num1+1;
long a1=Math.abs(k-(2*(l)));
long a2=Math.abs(k-(2*r));
if(num1>=l && num1<=r){
long up=num1-l;
res.append("1\n");
find(a,b,min,max,up,res,(int)n,m);
}
else if(num2>=l && num2<=r){
long up=num2-l;
res.append("1\n");
find(a,b,min,max,up,res,(int)n,m);
}
else if(a1<a2){
res.append(a1+" \n");
find(a,b,min,max,0,res,(int)n,m);
}
else{
res.append(a2+"\n");
find(a,b,min,max,(r-l),res,(int)n,m);
}
}
}
else{
long a1=Math.abs(k-(2*(l)));
//System.out.println("hh");
res.append(a1+" \n");
find(a,b,min,max,0,res,(int)n,m);
}
p++;
}
System.out.println(res);
}
private static void find(long[] a, long[] b, long[] min, long[] max, long up, StringBuilder res,int n,long m) {
long fish[]= new long[n];
long meat[]= new long[n];
long yo=0;
for(int i=0;i<n;i++){
if(yo<up){
long diff=up-yo;
long hh=max[i]-min[i];
if(diff>hh){
fish[i]=max[i];
meat[i]=m-fish[i];
yo+=hh;
}
else{
fish[i]=min[i]+diff;
meat[i]=m-fish[i];
yo+=diff;
}
}
else{
fish[i]=min[i];
meat[i]=m-fish[i];
}
}
for(int i=0;i<n;i++){
res.append(fish[i]+" "+meat[i]+" \n");
}
res.append("\n");
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 24d63581524bae2dc8814fc79bcc4aca | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | // Main Code at the Bottom
import java.util.*;
import java.io.*;
import java.sql.Time;
public class Main implements Runnable{
//Fast IO class
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
boolean env=System.getProperty("ONLINE_JUDGE") != null;
//env=true;
if(!env) {
try {
br=new BufferedReader(new FileReader("src\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long MOD=(long)1e9+7;
//debug
static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
//Global variables and functions
class Pair {
int a, b;
Pair(int x, int y) {
a = x;
b = y;
}
}
//Main function(The main code starts from here)
public void run() {
int test=1;
test=sc.nextInt();
while(test-->0) {
sc.nextLine();
int n = sc.nextInt(), m = sc.nextInt();
Pair arr[] = new Pair[n];
long sumA = 0, sumB = 0;
for(int i = 0; i < n; i++) {
arr[i] = new Pair(sc.nextInt(), sc.nextInt());
sumA += arr[i].a;
sumB += arr[i].b;
}
long sum = sumA - sumB + (long)n*(long)m;
long l = 0, r = 0;
for(Pair p: arr) {
l += m - Math.min(p.b, m);
r += Math.min(m, p.a);
}
long ans = l;
while(l <= r) {
long mid = l + (r - l) / 2;
long val = sum - 2 * mid;
if(val < 0) {
r = mid - 1;
if(-val < Math.abs(sum - 2 * ans)) {
ans = mid;
}
}
else {
l = mid + 1;
if(val < Math.abs(sum - 2 * ans)) {
ans = mid;
}
}
}
out.println(Math.abs(sum - 2 * ans));
long x[] = new long[n];
for(int i = 0; i < n; i++) {
x[i] = Math.max(m - arr[i].b, 0);
ans -= x[i];
}
for(int i = 0; i < n; i++) {
arr[i].a -= x[i];
long rem = m - x[i];
long v = Math.min(rem, Math.min(ans, arr[i].a));
ans -= v;
x[i] += v;
out.println(x[i] + " "+ (m - x[i]));
}
}
out.flush();
out.close();
}
public static void main (String[] args) throws java.lang.Exception {
new Thread(null, new Main(), "anything", (1<<28)).start();
}
} | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | c33f147a03a068c0944ffb3fd9398bda | train_108.jsonl | 1635863700 | A known chef has prepared $$$n$$$ dishes: the $$$i$$$-th dish consists of $$$a_i$$$ grams of fish and $$$b_i$$$ grams of meat. The banquet organizers estimate the balance of $$$n$$$ dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to $$$\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|$$$. The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly $$$m$$$ grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly $$$m$$$ grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class TaskB {
static long mod = 1000000007;
static FastScanner scanner;
static final StringBuilder result = new StringBuilder();
public static void main(String[] args) {
scanner = new FastScanner();
int T = scanner.nextInt();
for (int t = 0; t < T; t++) {
solve(t + 1);
result.append("\n");
}
System.out.println(result);
}
static void solve(int t) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] x = new int[n];
int[] y = new int[n];
long sumA = 0;
long sumB = 0;
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
b[i] = scanner.nextInt();
if (a[i] >= m) {
x[i] = m;
} else {
x[i] = a[i];
y[i] = m - a[i];
}
sumA += a[i] - x[i];
sumB += b[i] - y[i];
}
if (sumA >= sumB) {
result.append(sumA - sumB).append("\n");
for (int i = 0; i < n; i++) {
result.append(x[i]).append(" ").append(y[i]).append("\n");
}
return;
}
for (int i = 0; i < n; i++) {
if (y[i] == m) continue;
long required = (sumB - sumA) / 2;
if (required == 0) break;
long available = Math.min(m - y[i], b[i] - y[i]);
long toChange = Math.min(required, available);
x[i] -= toChange;
y[i] += toChange;
sumA += toChange;
sumB -= toChange;
}
result.append(Math.abs(sumA - sumB)).append("\n");
for (int i = 0; i < n; i++) {
result.append(x[i]).append(" ").append(y[i]).append("\n");
}
}
static class WithIdx implements Comparable<WithIdx> {
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
@Override
public int compareTo(WithIdx o) {
if (val == o.val) {
return Integer.compare(idx, o.idx);
}
return Integer.compare(val, o.val);
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
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 nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j];
j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
static long modInverse(long a, long m) {
long g = gcdF(a, m);
if (g != 1) {
throw new IllegalArgumentException("Inverse doesn't exist");
} else {
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
return modpow(a, m - 2, m);
}
}
static public long gcdF(long a, long b) {
while (b != 0) {
long na = b;
long nb = a % b;
a = na;
b = nb;
}
return a;
}
}
}
/*
5
3 2 3 8 8
2 8 5 10 1
*/ | Java | ["8\n\n1 5\n3 4\n\n1 6\n3 4\n\n2 2\n1 3\n4 2\n\n2 4\n1 3\n1 7\n\n3 6\n1 7\n1 8\n1 9\n\n3 6\n1 8\n1 9\n30 10\n\n3 4\n3 1\n3 2\n4 1\n\n5 4\n0 7\n6 4\n0 8\n4 1\n5 3"] | 2 seconds | ["0\n2 3\n1\n3 3\n0\n1 1\n1 1\n2\n1 3\n0 4\n3\n0 6\n0 6\n0 6\n7\n1 5\n1 5\n6 0\n0\n3 1\n3 1\n3 1\n0\n0 4\n2 2\n0 4\n3 1\n1 3"] | null | Java 8 | standard input | [
"greedy"
] | c63f5994543b136305a7ae2b9c744e13 | The first line of input data contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of the test cases. Each test case's description is preceded by a blank line. Next comes a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$0 \leq m \leq 10^6$$$). The next $$$n$$$ lines describe dishes, the $$$i$$$-th of them contains a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \leq a_i, b_i \le 10^6$$$) — the masses of fish and meat in the $$$i$$$-th dish. It is guaranteed that it is possible to eat $$$m$$$ grams of food from each dish. In other words, $$$m \leq a_i+b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive. The sum of all $$$n$$$ values over all test cases in the test does not exceed $$$2 \cdot 10^5$$$. | 2,200 | For each test case, print on the first line the minimal balance value that can be achieved by eating exactly $$$m$$$ grams of food from each dish. Then print $$$n$$$ lines that describe a way to do this: the $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq a_i$$$; $$$0 \leq y_i \leq b_i$$$; $$$x_i+y_i=m$$$), where $$$x_i$$$ is how many grams of fish taster should eat from the $$$i$$$-th meal and $$$y_i$$$ is how many grams of meat. If there are several ways to achieve a minimal balance, find any of them. | standard output | |
PASSED | 9d83aadaa3fcdd4183b54bf1975ee764 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.*;
public class gotoJapan {
public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solution solver = new Solution();
boolean isTest = true;
int tC = isTest ? Integer.parseInt(in.next()) : 1;
for (int i = 1; i <= tC; i++)
solver.solve(in, out, i);
out.close();
}
/* ............................................................. */
static class Solution {
InputReader in;
PrintWriter out;
public void solve(InputReader in, PrintWriter out, int test) {
this.in = in;
this.out = out;
int n = ni();
int m = ni();
char a[][] = new char[n + 5][m + 5];
for (int i = 0; i < n; ++i) {
a[i] = n();
}
int d[][] = new int[n][m];
int ci, cj;
int cn = 1;
Stack<Pair> st = new Stack<>();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if(d[i][j]!=0)continue;
st.add(new Pair(i, j));
int dp=0;
while (true) {
int x = st.peek().x;
int y = st.peek().y;
if (x < 0 || y < 0 || x >= n || y >= m || d[x][y] != 0) {
break;
}
d[x][y]=--dp;
if (a[x][y] == 'R')
st.add(new Pair(x, y + 1));
else if (a[x][y] == 'L')
st.add(new Pair(x, y - 1));
else if (a[x][y] == 'U')
st.add(new Pair(x - 1, y));
else
st.add(new Pair(x + 1, y));
}
Pair p = st.pop();
if ((p.x >= 0 && p.y >= 0 && p.x < n &&p.y < m) && d[p.x][p.y] < 0) {
int cL = d[p.x][p.y] - d[st.peek().x][st.peek().y] + 1;
while (true) {
Pair p1 = st.pop();
d[p1.x][p1.y] = cL;
if (p1.x == p.x && p1.y == p.y)
break;
}
}
if(p.x >= 0 && p.y >= 0 && p.x < n && p.y < m) {
int e=d[p.x][p.y];
while (st.size() > 0) {
Pair p1 = st.pop();
d[p1.x][p1.y]=++e;
}
}
else {
int e=0;
while (st.size() > 0) {
Pair p1 = st.pop();
d[p1.x][p1.y]=++e;
}
}
}
}
// out.println(iC[1][0]);
// pn(d[1][0]);
int max = 0;
int ai = 0, aj = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (d[i][j] > max) {
ai = i + 1;
aj = j + 1;
max = d[i][j];
}
}
}
out.println(ai + " " + aj + " " + max);
}
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
char[] n() {
return in.next().toCharArray();
}
int ni() {
return in.nextInt();
}
long nl() {
return in.nextLong();
}
long[] nal(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
void pn(long zx) {
out.println(zx);
}
void pn(String sz) {
out.println(sz);
}
void pn(double dx) {
out.println(dx);
}
void pn(long ar[]) {
for (int i = 0; i < ar.length; i++)
out.print(ar[i] + " ");
out.println();
}
void pn(String ar[]) {
for (int i = 0; i < ar.length; i++)
out.println(ar[i]);
}
}
/* ......................Just Input............................. */
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());
}
}
/* ......................Just Input............................. */
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 8395c3e579f6bcba3d1b8aa52f10ce50 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | //package com.company;
//Avoid division by decimal digits :). Always try to multiply with whole numbers or fractions instead.
//if getting wrong answer then use long/double instead of int/float
//e + e = o; o + o = e; e + o = o;
//see stuff in a jugaad way... if you are being complicated you are doing it wrong
//If a=b+1 and b is even, then a∧b=1
//If there is a statement in the question that it can be proved that ... then it means there is a very very simple logic behind it, and is not a simulation or dp question.
//Be confident in Maths you are not that bad at it.
// add break statement where you are stopping. Do not forget that, as people may use that weakness to hack your solution.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner reader = new Scanner(System.in);
int test = reader.nextInt();
char[] arr = new char[]{'L', 'R', 'D', 'U'};
int[] x = new int[]{0, 0, 1, -1};
int[] y = new int[]{-1, 1, 0, 0};
HashMap<Character, Integer> hash = new HashMap<>();
hash.put('L', 0);hash.put('R', 1);hash.put('D', 2);hash.put('U', 3);
for (int o = 0; o < test; o++) {
int opt = 0; int ax = 0; int ay = 0;
int n = reader.nextInt();
int m = reader.nextInt();
char[][] grid = new char[n][m];
for (int i = 0; i < n; i ++){
grid[i] = reader.next().toCharArray();
}
int[][] dp = new int[n][m];
// boolean[][] vis = new boolean[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (dp[i][j] > 0 ){continue;}
int s1 =i; int s2 = j;
ArrayList<Integer> l1 = new ArrayList<Integer>();
ArrayList<Integer> l2 = new ArrayList<Integer>();
boolean isCycle = true;
Integer aj = 0;
int cycle_length = 0;
int ubub = 0;
while (true){
l1.add(s1); l2.add(s2); ubub += 1;
// vis[s1][s2] = true;
aj += 1;
dp[s1][s2] = -aj;
// System.out.println(s1 + " " + s2);
int test_s1 = s1; int test_s2 = s2;
test_s1 += x[hash.get(grid[s1][s2])];test_s2 += y[hash.get(grid[s1][s2])];
s1 = test_s1; s2 = test_s2;
if (s1 >= n || s2 >= m || s1 < 0 || s2 < 0){isCycle = false; break; }
if (dp[s1][s2] > 0){
isCycle =false;
aj += dp[s1][s2]; break;
}
if (dp[s1][s2] < 0){
cycle_length = aj + 1 + dp[s1][s2];
break;
}
}
for (int op = 0; op < ubub - cycle_length; op++){
dp[l1.get(op)][l2.get(op)] = aj;
aj -= 1;
}
for (int op = ubub - cycle_length; op < ubub; op++){
dp[l1.get(op)][l2.get(op)] = cycle_length;
}
if (opt < dp[i][j]){
opt = dp[i][j];
ax = i + 1; ay = j + 1;
}
} }
// for (int i =0; i < n; i++){
// System.out.println(Arrays.toString(dp[i]));
// }
System.out.println(ax + " " + ay + " " + opt);
}
}
}
//------------------------------------XX---Templatecode---XX--------------------------------------
class Template{
public static long gcd(long a, long b){
if (b == 0)
return a;
return gcd(b, a % b);
}
public static long lcm(long a,long 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;
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 43b5fda0d75a8fc2c89dcfed9097a69a | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Stack;
public class Main {
private static int n, m;
private static char[][] map;
private static int[] dp, visited, deep;
private static int round;
private static int ans, ans_x, ans_y;
private static void run() throws IOException {
n = in.nextInt();
m = in.nextInt();
map = new char[n][];
for (int i = 0; i < n; i++) {
map[i] = in.next().toCharArray();
}
round = 0;
dp = new int[n * m];
visited = new int[n * m];
deep = new int[n * m];
ans = 1;
ans_x = 0;
ans_y = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dp[i * m + j] == 0) {
round++;
dfs(new Point(i, j));
}
}
}
out.print(ans_x + 1);
out.print(' ');
out.print(ans_y + 1);
out.print(' ');
out.print(ans);
out.println();
out.flush();
}
private static void dfs(Point p) {
Stack<Point> stack = new Stack<>();
stack.add(p);
deep[p.x * m + p.y] = 1;
int cycle_size = 0;
int cycle_flag = 0;
Point now;
Point next;
while (true) {
now = stack.peek();
visited[now.x * m + now.y] = round;
next = get_next(now, map[now.x][now.y]);
if (next.x < 0 || next.x >= n || next.y < 0 || next.y >= m) {
dp[now.x * m + now.y] = 1;
next = now;
stack.pop();
break;
} else if (visited[next.x * m + next.y] == round) {
cycle_flag = cycle_size = deep[now.x * m + now.y] - deep[next.x * m + next.y] + 1;
break;
} else if (visited[next.x * m + next.y] == 0) {
deep[next.x * m + next.y] = deep[now.x * m + now.y] + 1;
stack.add(next);
} else {
break;
}
}
while (!stack.isEmpty()) {
now = stack.pop();
if (cycle_flag == 0) {
dp[now.x * m + now.y] = dp[next.x * m + next.y] + 1;
} else {
dp[now.x * m + now.y] = cycle_size;
cycle_flag--;
}
next = now;
if (ans < dp[now.x * m + now.y]) {
ans = dp[now.x * m + now.y];
ans_x = now.x;
ans_y = now.y;
}
}
}
private static Point get_next(Point p, char d) {
switch (d) {
case 'U':
return new Point(p.x - 1, p.y);
case 'D':
return new Point(p.x + 1, p.y);
case 'L':
return new Point(p.x, p.y - 1);
case 'R':
return new Point(p.x, p.y + 1);
}
return p;
}
private static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | a5ecfc6595fc2dc964dd616bf5de7826 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Stack;
public class Main {
private static int n, m;
private static char[][] map;
private static int[] dp, visited, deep;
private static int round;
private static int ans, ans_x, ans_y;
private static void run() throws IOException {
n = in.nextInt();
m = in.nextInt();
map = new char[n][];
for (int i = 0; i < n; i++) {
map[i] = in.next().toCharArray();
}
round = 0;
dp = new int[n * m];
visited = new int[n * m];
deep = new int[n * m];
ans = 1;
ans_x = 0;
ans_y = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dp[i * m + j] == 0) {
round++;
dfs(new Point(i, j));
}
}
}
out.print(ans_x + 1);
out.print(' ');
out.print(ans_y + 1);
out.print(' ');
out.print(ans);
out.println();
out.flush();
}
private static void dfs(Point p) {
Stack<Point> stack = new Stack<>();
stack.add(p);
deep[p.x * m + p.y] = 1;
int cycle_flag = 0;
Point now;
Point next;
while (true) {
now = stack.peek();
visited[now.x * m + now.y] = round;
next = get_next(now, map[now.x][now.y]);
if (next.x < 0 || next.x >= n || next.y < 0 || next.y >= m) {
dp[now.x * m + now.y] = 1;
next = now;
stack.pop();
break;
} else if (visited[next.x * m + next.y] == round) {
cycle_flag = deep[now.x * m + now.y] - deep[next.x * m + next.y] + 1;
break;
} else if (visited[next.x * m + next.y] == 0) {
deep[next.x * m + next.y] = deep[now.x * m + now.y] + 1;
stack.add(next);
} else {
break;
}
}
int cycle_size = cycle_flag;
while (!stack.isEmpty()) {
now = stack.pop();
if (cycle_flag == 0) {
dp[now.x * m + now.y] = dp[next.x * m + next.y] + 1;
} else {
dp[now.x * m + now.y] = cycle_size;
cycle_flag--;
}
next = now;
if (ans < dp[now.x * m + now.y]) {
ans = dp[now.x * m + now.y];
ans_x = now.x;
ans_y = now.y;
}
}
}
private static Point get_next(Point p, char d) {
switch (d) {
case 'U':
return new Point(p.x - 1, p.y);
case 'D':
return new Point(p.x + 1, p.y);
case 'L':
return new Point(p.x, p.y - 1);
case 'R':
return new Point(p.x, p.y + 1);
}
return p;
}
private static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 5905630a451fac99add348c9679d48a2 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Stack;
public class Main {
private static int n, m;
private static String[] map;
private static int[] dp, visited, deep;
private static int round;
private static int ans, ans_x, ans_y;
private static void run() throws IOException {
n = in.nextInt();
m = in.nextInt();
map = new String[n];
for (int i = 0; i < n; i++) {
map[i] = in.next();
}
round = 0;
dp = new int[n * m];
visited = new int[n * m];
deep = new int[n * m];
ans = 1;
ans_x = 0;
ans_y = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (dp[i * m + j] == 0) {
round++;
dfs(i, j);
}
}
}
out.print(ans_x + 1);
out.print(' ');
out.print(ans_y + 1);
out.print(' ');
out.print(ans);
out.println();
out.flush();
}
private static void dfs(int x, int y) {
Stack<int[]> stack = new Stack<>();
stack.add(new int[]{x, y});
deep[x * m + y] = 1;
int cycle_flag = 0;
int[] now;
int[] next;
while (true) {
now = stack.peek();
visited[now[0] * m + now[1]] = round;
switch (map[now[0]].charAt(now[1])) {
case 'U':
next = new int[]{now[0] - 1, now[1]};
break;
case 'D':
next = new int[]{now[0] + 1, now[1]};
break;
case 'L':
next = new int[]{now[0], now[1] - 1};
break;
case 'R':
default:
next = new int[]{now[0], now[1] + 1};
break;
}
if (next[0] < 0 || next[0] >= n || next[1] < 0 || next[1] >= m) {
dp[now[0] * m + now[1]] = 1;
next = now;
stack.pop();
break;
} else if (visited[next[0] * m + next[1]] == round) {
cycle_flag = deep[now[0] * m + now[1]] - deep[next[0] * m + next[1]] + 1;
break;
} else if (visited[next[0] * m + next[1]] == 0) {
deep[next[0] * m + next[1]] = deep[now[0] * m + now[1]] + 1;
stack.add(next);
} else {
break;
}
}
int cycle_size = cycle_flag;
while (!stack.isEmpty()) {
now = stack.pop();
if (cycle_flag == 0) {
dp[now[0] * m + now[1]] = dp[next[0] * m + next[1]] + 1;
} else {
dp[now[0] * m + now[1]] = cycle_size;
cycle_flag--;
}
next = now;
if (ans < dp[now[0] * m + now[1]]) {
ans = dp[now[0] * m + now[1]];
ans_x = now[0];
ans_y = now[1];
}
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final 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();
}
final 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();
}
final 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 {
din.close();
}
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | d47c07d104a74d4479d694b212a1bb8e | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.io.*;
public class _1607_F {
static int[][] dirs = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
in.readLine();
StringTokenizer line = new StringTokenizer(in.readLine());
int n = Integer.parseInt(line.nextToken());
int m = Integer.parseInt(line.nextToken());
int[][] grid = new int[n][m];
for(int i = 0; i < n; i++) {
String line2 = in.readLine();
for(int j = 0; j < m; j++) {
if(line2.charAt(j) == 'U') {
grid[i][j] = 0;
}else if(line2.charAt(j) == 'R') {
grid[i][j] = 1;
}else if(line2.charAt(j) == 'D') {
grid[i][j] = 2;
}else {
grid[i][j] = 3;
}
}
}
int[][] marks = new int[n][m];
int[][] dists = new int[n][m];
int d = 0;
int row = 0;
int col = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int r = i;
int c = j;
int dist = 0;
while(r >= 0 && r < n && c >= 0 && c < m) {
if(marks[r][c] > 0) {
dists[r][c] = dist - marks[r][c] + 1;
break;
}else if(dists[r][c] > 0) {
dist += dists[r][c];
break;
}
dist++;
marks[r][c] = dist;
int[] dir = dirs[grid[r][c]];
r += dir[0];
c += dir[1];
}
r = i;
c = j;
int cyc_len = -1;
while(r >= 0 && r < n && c >= 0 && c < m) {
if(marks[r][c] == 0) {
break;
}
marks[r][c] = 0;
if(dists[r][c] > 0) {
cyc_len = dists[r][c];
}
if(cyc_len == -1) {
dists[r][c] = dist;
}else {
dists[r][c] = cyc_len;
}
if(dists[r][c] > d) {
d = dists[r][c];
row = r + 1;
col = c + 1;
}
int[] dir = dirs[grid[r][c]];
r += dir[0];
c += dir[1];
dist--;
}
}
}
out.println(row + " " + col + " " + d);
}
in.close();
out.close();
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 8797c0b86d28b453dc2b2c7132a099ef | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.io.*;
public class _1607_F {
static int[][] dirs = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
in.readLine();
StringTokenizer line = new StringTokenizer(in.readLine());
int n = Integer.parseInt(line.nextToken());
int m = Integer.parseInt(line.nextToken());
int[][] grid = new int[n][m];
for(int i = 0; i < n; i++) {
String line2 = in.readLine();
for(int j = 0; j < m; j++) {
if(line2.charAt(j) == 'U') {
grid[i][j] = 0;
}else if(line2.charAt(j) == 'R') {
grid[i][j] = 1;
}else if(line2.charAt(j) == 'D') {
grid[i][j] = 2;
}else {
grid[i][j] = 3;
}
}
}
int[][] marks = new int[n][m];
int[][] dists = new int[n][m];
int d = 0;
int row = 0;
int col = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int r = i;
int c = j;
int dist = 0;
while(r >= 0 && r < n && c >= 0 && c < m) {
dist++;
if(marks[r][c] > 0) {
dists[r][c] = dist - marks[r][c];
dist--;
break;
}else if(dists[r][c] > 0) {
dist += dists[r][c];
dist--;
break;
}
marks[r][c] = dist;
int[] dir = dirs[grid[r][c]];
r += dir[0];
c += dir[1];
}
r = i;
c = j;
int cyc_len = -1;
while(r >= 0 && r < n && c >= 0 && c < m) {
if(marks[r][c] == 0) {
break;
}
marks[r][c] = 0;
if(dists[r][c] > 0) {
cyc_len = dists[r][c];
}
if(cyc_len == -1) {
dists[r][c] = dist;
}else {
dists[r][c] = cyc_len;
}
if(dists[r][c] > d) {
d = dists[r][c];
row = r + 1;
col = c + 1;
}
int[] dir = dirs[grid[r][c]];
r += dir[0];
c += dir[1];
dist--;
}
}
}
out.println(row + " " + col + " " + d);
}
in.close();
out.close();
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 397dc24bdacca0328d0d57ec67d5fe09 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.io.*;
public class _1607_F {
static int[][] dirs = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
in.readLine();
StringTokenizer line = new StringTokenizer(in.readLine());
int n = Integer.parseInt(line.nextToken());
int m = Integer.parseInt(line.nextToken());
int[][] grid = new int[n][m];
for(int i = 0; i < n; i++) {
String line2 = in.readLine();
for(int j = 0; j < m; j++) {
if(line2.charAt(j) == 'U') {
grid[i][j] = 0;
}else if(line2.charAt(j) == 'R') {
grid[i][j] = 1;
}else if(line2.charAt(j) == 'D') {
grid[i][j] = 2;
}else {
grid[i][j] = 3;
}
}
}
int[][] marks = new int[n][m];
int[][] dists = new int[n][m];
int d = 0;
int row = 0;
int col = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int r = i;
int c = j;
int dist = 1;
while(r >= 0 && r < n && c >= 0 && c < m) {
if(marks[r][c] > 0) {
dists[r][c] = dist - marks[r][c];
break;
}else if(dists[r][c] > 0) {
dist += dists[r][c];
break;
}
marks[r][c] = dist;
int[] dir = dirs[grid[r][c]];
r += dir[0];
c += dir[1];
dist++;
}
dist--;
r = i;
c = j;
int cyc_len = -1;
while(r >= 0 && r < n && c >= 0 && c < m) {
if(marks[r][c] == 0) {
break;
}
marks[r][c] = 0;
if(dists[r][c] > 0) {
cyc_len = dists[r][c];
}
if(cyc_len == -1) {
dists[r][c] = dist;
}else {
dists[r][c] = cyc_len;
}
if(dists[r][c] > d) {
d = dists[r][c];
row = r + 1;
col = c + 1;
}
int[] dir = dirs[grid[r][c]];
r += dir[0];
c += dir[1];
dist--;
}
}
}
out.println(row + " " + col + " " + d);
}
in.close();
out.close();
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 9c69b3183c5caccd2b41af1204d37c6d | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | //package Codeforces;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class RobotOnTheBoard2 {
static class Cell{
int x;
int y;
int val;
Cell parent;
boolean isVisited;
boolean inStack;
Cell(int x, int y){
this.x = x;
this.y = y;
this.parent = null;
this.val = 0;
this.isVisited = false;
this.inStack = false;
}
}
static boolean isValid(int n, int m, int x, int y){
return 0<=x && x<n && 0<=y && y<m;
}
static void dfs(Cell source){
Stack<Cell> stk = new Stack<>();
stk.push(source);
source.inStack = true;
source.isVisited = true;
while (!stk.isEmpty()){
Cell cur = stk.peek();
if(cur.parent!=null){
Cell next = cur.parent;
if(!next.isVisited){
stk.push(next);
next.isVisited = true;
next.inStack = true;
}
else {
if(next.inStack){
ArrayList<Cell> cyclelist = new ArrayList<>();
while(stk.peek()!=next){
Cell cell = stk.pop();
cyclelist.add(cell);
cell.inStack = false;
}
Cell cell = stk.pop();
cyclelist.add(cell);
cell.inStack = false;
for(Cell cycell : cyclelist){
cycell.val = cyclelist.size();
}
int v = cyclelist.size()+1;
while(!stk.isEmpty()){
Cell cell1 = stk.pop();
cell1.val = v;
cell1.inStack = false;
v++;
}
}
else{
int v = next.val+1;
while(!stk.isEmpty()){
Cell cell = stk.pop();
cell.val = v;
cell.inStack = false;
v++;
}
}
}
}
else{
int v = 1;
while(!stk.isEmpty()){
Cell cell = stk.pop();
cell.val = v;
cell.inStack = false;
v++;
}
}
}
}
public static void main(String[] args) throws FileNotFoundException {
/*long start = System.currentTimeMillis();
Scanner sc = new Scanner(new File("Input.txt"));*/
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
char[][] mat = new char[n][m];
for(int i=0;i<n;i++){
mat[i] = sc.next().toCharArray();
}
Cell[][] grid = new Cell[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
grid[i][j] = new Cell(i, j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]=='U'){
if(isValid(n, m, i-1, j)){
grid[i][j].parent = grid[i-1][j];
}
}
if(mat[i][j]=='D'){
if(isValid(n, m, i+1, j)){
grid[i][j].parent = grid[i+1][j];
}
}
if(mat[i][j]=='L'){
if(isValid(n, m, i, j-1)){
grid[i][j].parent = grid[i][j-1];
}
}
if(mat[i][j]=='R'){
if(isValid(n, m, i, j+1)){
grid[i][j].parent = grid[i][j+1];
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!grid[i][j].isVisited){
dfs(grid[i][j]);
}
}
}
int max = -1;
Cell best = grid[0][0];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j].val>max){
best = grid[i][j];
max = grid[i][j].val;
}
}
}
sb.append(best.x+1).append(" ").append(best.y+1)
.append(" ").append(max).append("\n");
}
System.out.println(sb);
/*long end = System.currentTimeMillis();
System.out.println((end-start)/1000.0);*/
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 6b390ada63d6e4581778df2613fd0e74 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.*;
public class Practice
{
// static final long mod=7420738134811L;
static int mod=1000000007;
static final int size=501;
static FastReader sc=new FastReader(System.in);
// static Reader sc=new Reader();
static PrintWriter out=new PrintWriter(System.out);
static long[] factorialNumInverse;
static long[] naturalNumInverse;
static int[] sp;
static long[] fact;
static ArrayList<Integer> pr;
public static void main(String[] args) throws IOException
{
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream("output.txt"));
// factorial(mod);
// InverseofNumber(mod);
// InverseofFactorial(mod);
// make_seive();
int t=1;
t=sc.nextInt();
while(t-->0)
solve();
out.close();
out.flush();
}
static void solve() throws IOException
{
int n=sc.nextInt(),m=sc.nextInt();
char a[][]=new char[n][m];
for(int i=0;i<n;i++)
a[i]=sc.next().toCharArray();
int dis[][]=new int[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(dis[i][j]==0)
{
int ii=i,jj=j;
int d=0;
while(i>=0 && i<n && j>=0 && j<m && dis[i][j]==0)
{
d++;
dis[i][j]=-1;
if(a[i][j]=='U')
i--;
else if(a[i][j]=='D')
i++;
else if(a[i][j]=='R')
j++;
else
j--;
}
if(i<0 || i>=n || j<0 || j>=m)
d+=0;
else if(dis[i][j]!=-1)
d+=dis[i][j];
else
{
int c=0;
while(i>=0 && i<n && j>=0 && j<m && dis[i][j]==-1)
{
c++;
dis[i][j]=-2;
if(a[i][j]=='U')
i--;
else if(a[i][j]=='D')
i++;
else if(a[i][j]=='R')
j++;
else
j--;
}
while(i>=0 && i<n && j>=0 && j<m && dis[i][j]==-2)
{
dis[i][j]=c;
if(a[i][j]=='U')
i--;
else if(a[i][j]=='D')
i++;
else if(a[i][j]=='R')
j++;
else
j--;
}
}
i=ii;j=jj;
while(i>=0 && i<n && j>=0 && j<m && dis[i][j]==-1)
{
dis[i][j]=d--;
if(a[i][j]=='U')
i--;
else if(a[i][j]=='D')
i++;
else if(a[i][j]=='R')
j++;
else
j--;
}
i=ii;j=jj;
}
int ai=-1,aj=-1;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(ai==-1 && aj==-1 || dis[ai][aj]<dis[i][j])
{
ai=i;aj=j;
}
}
ai++;
aj++;
out.println(ai+" "+aj+" "+dis[ai-1][aj-1]);
}
static class Pair implements Cloneable, Comparable<Pair>
{
int x,y;
Pair(int a,int b)
{
this.x=a;
this.y=b;
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Pair)
{
Pair p=(Pair)obj;
return p.x==this.x && p.y==this.y;
}
return false;
}
@Override
public int hashCode()
{
return Math.abs(x)+500*Math.abs(y);
}
@Override
public String toString()
{
return "("+x+" "+y+")";
}
@Override
protected Pair clone() throws CloneNotSupportedException {
return new Pair(this.x,this.y);
}
@Override
public int compareTo(Pair a)
{
int t= this.x-a.x;
if(t!=0)
return t;
else
return this.y-a.y;
}
public void swap()
{
this.y=this.y+this.x;
this.x=this.y-this.x;
this.y=this.y-this.x;
}
}
static class Tuple implements Cloneable, Comparable<Tuple>
{
int x,y,z;
Tuple(int a,int b,int c)
{
this.x=a;
this.y=b;
this.z=c;
}
public boolean equals(Object obj)
{
if(obj instanceof Tuple)
{
Tuple p=(Tuple)obj;
return p.x==this.x && p.y==this.y && p.z==this.z;
}
return false;
}
@Override
public int hashCode()
{
return (this.x+501*this.y);
}
@Override
public String toString()
{
return "("+x+","+y+","+z+")";
}
@Override
protected Tuple clone() throws CloneNotSupportedException {
return new Tuple(this.x,this.y,this.z);
}
@Override
public int compareTo(Tuple a)
{
int x=this.z-a.z;
if(x!=0)
return x;
int X= this.x-a.x;
if(X!=0)
return X;
return a.y-this.y;
}
}
static void arraySort(int arr[])
{
ArrayList<Integer> a=new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
a.add(arr[i]);
}
Collections.sort(a);
for (int i = 0; i < arr.length; i++) {
arr[i]=a.get(i);
}
}
static void arraySort(long arr[])
{
ArrayList<Long> a=new ArrayList<Long>();
for (int i = 0; i < arr.length; i++) {
a.add(arr[i]);
}
Collections.sort(a);
for (int i = 0; i < arr.length; i++) {
arr[i]=a.get(i);
}
}
static HashSet<Integer> primeFactors(int n)
{
HashSet<Integer> ans=new HashSet<Integer>();
if(n%2==0)
{
ans.add(2);
while((n&1)==0)
n=n>>1;
}
for(int i=3;i*i<=n;i+=2)
{
if(n%i==0)
{
ans.add(i);
while(n%i==0)
n=n/i;
}
}
if(n!=1)
ans.add(n);
return ans;
}
static void make_seive()
{
sp=new int[size];
pr=new ArrayList<Integer>();
for (int i=2; i<size; ++i) {
if (sp[i] == 0) {
sp[i] = i;
pr.add(i);
}
for (int j=0; j<(int)pr.size() && pr.get(j)<=sp[i] && i*pr.get(j)<size; ++j)
sp[i * pr.get(j)] = pr.get(j);
}
}
public static void InverseofNumber(int p)
{
naturalNumInverse=new long[size];
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i < size; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p;
}
// Function to precompute inverse of factorials
public static void InverseofFactorial(int p)
{
factorialNumInverse=new long[size];
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// pre-compute inverse of natural numbers
for(int i = 2; i < size; i++)
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to 200001
public static void factorial(int p)
{
fact=new long[size];
fact[0] = 1;
for(int i = 1; i < size; i++)
fact[i] = (fact[i - 1] * (long)i) % p;
}
// Function to return nCr % p in O(1) time
public static long Binomial(int N, int R)
{
if(R<0)
return 1;
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) % mod * factorialNumInverse[N - R]) % mod;
return ans;
}
static int findXOR(int x) //from 0 to x
{
if(x<0)
return 0;
if(x%4==0)
return x;
if(x%4==1)
return 1;
if(x%4==2)
return x+1;
return 0;
}
static boolean isPrime(long x)
{
if(x==1)
return false;
if(x<=3)
return true;
if(x%2==0 || x%3==0)
return false;
for(int i=5;i<=Math.sqrt(x);i+=2)
if(x%i==0)
return false;
return true;
}
static long gcd(long a,long b)
{
return (b==0)?a:gcd(b,a%b);
}
static int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
static class Node
{
int vertex;
HashSet<Edge> adj;
int taxC,taxD;
Node(int ver)
{
vertex=ver;
taxD=0;
taxC=0;
adj=new HashSet<Edge>();
}
@Override
public String toString()
{
return vertex+" ";
}
}
static class Edge
{
Node to;
int cost;
Edge(Node t,int c)
{
this.to=t;
this.cost=c;
}
@Override
public String toString() {
return "("+to.vertex+","+cost+") ";
}
}
static long power(long x, long y)
{
if(x<=0)
return 1;
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1; // y = y/2
x = (x * x) % mod;
}
return res;
}
static long binomialCoeff(long n, long k)
{
if(n<k)
return 0;
long res = 1;
// if(k>n)
// return 0;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
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;
}
}
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') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
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 void printarray(int arr[])
{
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 3b7e1bbe830a3a91cd73250d725815c4 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
long mod=1000000007;
char[][]C;
int[][]V;
int[][]A;
int n,m,set,pari,parj;
boolean test=false;
void solve() throws IOException {
for (int tc=ni();tc>0;tc--) {
if (test) {
n=2000;
m=2000;
C=new char[n][m];
Random r=new Random();
char[]CC={'R','D','L','U'};
/*for (int i=0;i<n;i++) for (int j=0;j<m;j++) {
int u=r.nextInt(4);
C[i][j]=CC[u];
}*/
int[][]V2=new int[n][m];
int i=0;
int j=0;
char u='R';
while (true) {
//System.out.print("HI");
if (V2[i][j]>0) break;
//out.println(i+" "+j);
V2[i][j]=1;
if (u=='R') { C[i][j]='R'; j++; if (j>=m || V2[i][j]>0) { u='D'; j--; C[i][j]=u; i++; }}
else if (u=='D') { C[i][j]='D'; i++; if (i>=n || V2[i][j]>0) { u='L'; i--; C[i][j]=u; j--; }}
else if (u=='L') { C[i][j]='L'; j--; if (j<0 || V2[i][j]>0) { u='U'; j++; C[i][j]=u; i--; }}
else if (u=='U') { C[i][j]='U'; i--; if (i<0 || V2[i][j]>0) { u='R'; i++; C[i][j]=u; j++; }}
}
/*for (int k=0;k<n;k++) {
for (int l=0;l<m;l++) out.print(C[k][l]);
out.println();
}*/
}
else {
n=ni();
m=ni();
C=new char[n][];
for (int i=0;i<n;i++) C[i]=next().toCharArray();
}
V=new int[n][m];
A=new int[n][m];
set=1;
pari=-1;
parj=-1;
for (int x=0;x<n;x++) {
for (int y=0;y<m;y++) {
if (V[x][y]>0) continue;
Stack<Integer>I=new Stack();
Stack<Integer>J=new Stack();
int step=0;
int i=x;
int j=y;
while (true) {
V[i][j]=set;
A[i][j]=step;
int newi=i,newj=j;
if (C[i][j]=='U') newi--;
if (C[i][j]=='D') newi++;
if (C[i][j]=='L') newj--;
if (C[i][j]=='R') newj++;
if (newi<0 || newi>=n) { A[i][j]=1; break; }
if (newj<0 || newj>=m) { A[i][j]=1; break; }
if (V[newi][newj]==set) { A[i][j]=step-A[newi][newj]+1; pari=newi; parj=newj; break; }
if (V[newi][newj]>0) { A[i][j]=A[newi][newj]+1; break; }
I.push(i);
J.push(j);
i=newi;
j=newj;
step++;
}
while (!I.empty()) {
i=I.pop();
j=J.pop();
int newi=i,newj=j;
if (C[i][j]=='U') newi--;
if (C[i][j]=='D') newi++;
if (C[i][j]=='L') newj--;
if (C[i][j]=='R') newj++;
if (pari>-1) {
A[i][j]=A[newi][newj];
if (i==pari && j==parj) { pari=-1; parj=-1; }
}
else A[i][j]=A[newi][newj]+1;
}
set++;
}
}
int max=0;
int ai=-1;
int aj=-1;
for (int i=0;i<n;i++) for (int j=0;j<m;j++)
if (A[i][j]>max) { max=A[i][j]; ai=i+1; aj=j+1; }
out.println(ai+" "+aj+" "+max);
}
out.flush();
}
void dfs(int i,int j,int step) {
V[i][j]=set;
A[i][j]=step;
int newi=i,newj=j;
if (C[i][j]=='U') newi--;
if (C[i][j]=='D') newi++;
if (C[i][j]=='L') newj--;
if (C[i][j]=='R') newj++;
if (newi<0 || newi>=n) { A[i][j]=1; return; }
if (newj<0 || newj>=m) { A[i][j]=1; return; }
if (V[newi][newj]==set) { A[i][j]=step-A[newi][newj]+1; pari=newi; parj=newj; return; }
if (V[newi][newj]>0) { A[i][j]=A[newi][newj]+1; return; }
dfs(newi,newj,step+1);
if (pari>-1) {
A[i][j]=A[newi][newj];
if (i==pari && j==parj) { pari=-1; parj=-1; }
}
else A[i][j]=A[newi][newj]+1;
}
int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); }
long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); }
long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; }
public static void main(String[] args) throws IOException {
new Main().solve();
}
} | Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 32125591ed8312bfb03b1800b176a4b1 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes |
import java.util.Scanner;
public class F {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int lines = s.nextInt();
s.nextLine();
for (int i = 0; i < lines; i += 1) {
solve(s.nextInt(), s.nextInt(), s);
}
}
public static void solve(int h, int w, Scanner s) {
int a[][] = new int[h + 2][w + 2];
String line;
for (int i = 1; i <= h; i++) {
line = s.next();
for (int j = 1; j <= w; j++) {
a[i][j] = line.charAt(j - 1);
}
}
int p[][][] = new int[h + 2][w + 2][2]; //dist type
for (int i = 0; i < w + 2; i++) {
p[0][i] = new int[]{0, 'P'};
p[h + 1][i] = new int[]{0, 'P'};
}
for (int i = 0; i < h + 2; i++) {
p[i][0] = new int[]{0, 'P'};
p[i][w + 1] = new int[]{0, 'P'};
}
int x;
int y;
int len;
int id = 0;
int was[][] = new int[h + 2][w + 2];
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (p[i][j][1] == 0) {
x = j;
y = i;
len = 1;
id++;
was[y][x] = id;
while (true) {
switch (a[y][x]) {
case 'U':
y--;
break;
case 'D':
y++;
break;
case 'L':
x--;
break;
case 'R':
x++;
break;
}
int stopX;
int stopY;
boolean change = false;
int minusDist = 0;
if (was[y][x] == id) {
stopX = x;
stopY = y;
x = j;
y = i;
for (int k = 0; k < len; k++) {
if (x == stopX && y == stopY) {
change = true;
minusDist = k;
}
if (change) p[y][x] = new int[]{len - minusDist, 'C'}; else p[y][x] = new int[]{len - k, 'C'};
switch (a[y][x]) {
case 'U':
y--;
break;
case 'D':
y++;
break;
case 'L':
x--;
break;
case 'R':
x++;
break;
}
}
break;
}
int extra;
if (p[y][x][1] != 0) {
extra = p[y][x][0];
x = j;
y = i;
for (int k = 0; k < len; k++) {
p[y][x] = new int[]{len + extra - k, 'P'};
switch (a[y][x]) {
case 'U':
y--;
break;
case 'D':
y++;
break;
case 'L':
x--;
break;
case 'R':
x++;
break;
}
}
break;
}
was[y][x] = id;
len++;
}
}
}
}
int maxX = 0;
int maxY = 0;
for (int i = 0; i < h + 2; i++) {
for (int j = 0; j < w + 2; j++) {
if (p[i][j][0] > p[maxY][maxX][0]) {
maxX = j;
maxY = i;
}
}
}
System.out.println(maxY + " " + maxX + " " + p[maxY][maxX][0]);
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 9598842b10dc24e7233793a52e738f22 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | // package c1607;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
//
// Codeforces Round #753 (Div. 3) 2021-11-02 06:35
// F. Robot on the Board 2
// https://codeforces.com/contest/1607/problem/F
// time limit per test 2 seconds; memory limit per test 256 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// The robot is located on a checkered rectangular board of size n x m (n rows, m columns). The rows
// in the board are numbered from 1 to n from top to bottom, and the columns-- from 1 to m from left
// to right.
//
// The robot is able to move from the current cell to one of the four cells adjacent by side.
//
// Each cell has one of the symbols '', '', '' or '' written on it, indicating the direction in
// which the robot will move when it gets in that cell-- left, right, down or up, respectively.
//
// The robot can start its movement in any cell. He then moves to the adjacent square in the
// direction indicated on the current square in one move.
// * If the robot moves beyond the edge of the board, it falls and breaks.
// * If the robot appears in the cell it already visited before, it breaks (it stops and doesn't
// move anymore).
//
// Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps
// before it breaks or stops.
//
// Determine from which square the robot should start its movement in order to execute as many
// commands as possible. A command is considered successfully completed if the robot has moved from
// the square on which that command was written (it does not matter whether to another square or
// beyond the edge of the board).
//
// Input
//
// The first line contains an integer t (1 <= t <= 10000)-- the number of test cases in the test.
//
// Each test case's description is preceded by a blank line. Next is a line that contains integers n
// and m (1 <= n <= 2000; 1 <= m <= 2000)-- the height and width of the board. This line followed by
// n lines, the i-th of which describes the i-th line of the board. Each of them is exactly m
// letters long and consists of symbols '', '', '' and ''.
//
// It is guaranteed that the sum of sizes of all boards in the input does not exceed 4*10^6.
//
// Output
//
// For each test case, output three integers r, c and d (1 <= r <= n; 1 <= c <= m; d >= 0), which
// denote that the robot should start moving from cell (r, c) to make the maximum number of moves d.
// If there are several answers, output any of them.
//
// Example
/*
input:
7
1 1
R
1 3
RRL
2 2
DL
RU
2 2
UD
RU
3 2
DL
UL
RU
4 4
RRRD
RUUD
URUD
ULLR
4 4
DDLU
RDDU
UUUU
RDLD
output:
1 1 1
1 1 3
1 1 4
2 1 3
3 1 5
4 3 12
1 1 4
*/
//
public class C1607F {
static final int MOD = (int)1e9+7;
static final Random RAND = new Random();
static int[] solve(char[][] board) {
// n rows, m columns
int n = board.length;
int m = board[0].length;
// m columns
// ------------------------
// | U |
// | ^ |
// | L <- o -> R | n rows
// | v |
// | D |
// ------------------------
//
int[][] arr = new int[n][m];
final int tid = -1; // negative value indicate current trip
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] != 0) {
continue;
}
List<int[]> points = new ArrayList<>();
// Note that i and r are for row
int r = i;
int c = j;
arr[i][j] = tid;
while (true) {
points.add(new int[] {r,c});
char ch = board[r][c];
if (ch == 'L') {
// L decreases column
c--;
} else if (ch == 'R') {
// R increases column
c++;
} else if (ch == 'U') {
// U decreases row
r--;
} else {
// D increases row
r++;
}
if (r < 0 || r >= n || c < 0 || c >= m) {
// off the boarder
Collections.reverse(points);
int d = 1;
for (int[] point : points) {
arr[point[0]][point[1]] = d;
d++;
}
break;
} else if (arr[r][c] == tid) {
// detected loop to self
Collections.reverse(points);
int k = 0;
while (points.get(k)[0] != r || points.get(k)[1] != c) {
k++;
}
// System.out.format(" loop [%d,%d] k:%d\n", r, c, k);
for (int h = 0; h < points.size(); h++) {
int[] point = points.get(h);
arr[point[0]][point[1]] = h <= k ? k + 1 : h + 1;
}
break;
} else if (arr[r][c] > 0) {
// join a previous path/loop
Collections.reverse(points);
int d = arr[r][c] + 1;
for (int[] point : points) {
arr[point[0]][point[1]] = d;
d++;
}
break;
}
arr[r][c] = tid;
}
}
}
int[] ans = new int[3];
int maxv = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] > maxv) {
maxv = arr[i][j];
ans[0] = i + 1;
ans[1] = j + 1;
ans[2] = maxv;
}
}
}
return ans;
}
static String trace(int[] a) {
StringBuilder sb = new StringBuilder();
for (int v : a) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(v);
}
return sb.toString();
}
public static void main(String[] args) {
Scanner in = getInputScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int m = in.nextInt();
char[][] board = new char[n][m];
for (int i = 0; i < n; i++) {
String s = in.next();
for (int j = 0; j < m; j++) {
board[i][j] = s.charAt(j);
}
}
int[] ans = solve(board);
System.out.format("%d %d %d\n", ans[0], ans[1], ans[2]);
}
in.close();
}
static Scanner getInputScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
} catch (Exception e) {
return new Scanner(System.in);
}
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output | |
PASSED | 5d7835a46e234d4df47e5667ca38c389 | train_108.jsonl | 1635863700 | The robot is located on a checkered rectangular board of size $$$n \times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.Each cell has one of the symbols 'L', 'R', 'D' or 'U' written on it, indicating the direction in which the robot will move when it gets in that cell — left, right, down or up, respectively.The robot can start its movement in any cell. He then moves to the adjacent square in the direction indicated on the current square in one move. If the robot moves beyond the edge of the board, it falls and breaks. If the robot appears in the cell it already visited before, it breaks (it stops and doesn't move anymore). Robot can choose any cell as the starting cell. Its goal is to make the maximum number of steps before it breaks or stops.Determine from which square the robot should start its movement in order to execute as many commands as possible. A command is considered successfully completed if the robot has moved from the square on which that command was written (it does not matter whether to another square or beyond the edge of the board). | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
public class codeforces_753_E2 {
private static void solve(FastIOAdapter io) {
int n = io.nextInt();
int m = io.nextInt();
char[][] a = new char[n][];
for (int i = 0; i < n; i++) {
a[i] = io.next().toCharArray();
}
int[][] lengths = new int[n][m];
int[][] indices = new int[n][m];
int x = 0, y = 0;
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (lengths[i][j] == 0) { // we did not check this
Stack<int[]> stack = new Stack<>();
stack.add(new int[]{i, j});
lengths[i][j] = -1; // we were here
indices[i][j] = 0;
int length = 1;
int cycle = 0;
int cycX = -1, cycY = -1;
while (true) {
int[] xy = stack.peek();
int xc = xy[0], yc = xy[1];
if (a[xc][yc] == 'U') {
xc--;
} else if (a[xc][yc] == 'D') {
xc++;
} else if (a[xc][yc] == 'L') {
yc--;
} else yc++;
if (xc < 0 || xc >= n || yc < 0 || yc >= m) {
break;
} else if (lengths[xc][yc] < 0) {
cycle -= indices[xc][yc] - 1;
length = cycle + 1;
cycX = xc;
cycY = yc;
break;
} else if (lengths[xc][yc] > 0) {
length = lengths[xc][yc] + 1;
break;
} else {
lengths[xc][yc] = -1;
indices[xc][yc] = ++cycle;
stack.add(new int[]{xc, yc});
}
}
boolean isCycle = cycX != -1;
int len = isCycle ? cycle : length;
while (!stack.isEmpty()) {
int[] xy = stack.pop();
lengths[xy[0]][xy[1]] = len;
if (isCycle) {
if (xy[0] == cycX && xy[1] == cycY) {
isCycle = false;
len = length;
}
} else len++;
}
if (max < lengths[i][j]) {
max = lengths[i][j];
x = i + 1;
y = j + 1;
}
}
}
}
io.out.println(x + " " + y + " " + max);
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.close();
}
}
}
| Java | ["7\n\n1 1\nR\n\n1 3\nRRL\n\n2 2\nDL\nRU\n\n2 2\nUD\nRU\n\n3 2\nDL\nUL\nRU\n\n4 4\nRRRD\nRUUD\nURUD\nULLR\n\n4 4\nDDLU\nRDDU\nUUUU\nRDLD"] | 2 seconds | ["1 1 1\n1 1 3\n1 1 4\n2 1 3\n3 1 5\n4 3 12\n1 1 4"] | null | Java 11 | standard input | [
"brute force",
"dfs and similar",
"graphs",
"implementation"
] | 67c748999e681fa6f60165f411e5149d | The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the test. Each test case's description is preceded by a blank line. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$; $$$1 \le m \le 2000$$$) — the height and width of the board. This line followed by $$$n$$$ lines, the $$$i$$$-th of which describes the $$$i$$$-th line of the board. Each of them is exactly $$$m$$$ letters long and consists of symbols 'L', 'R', 'D' and 'U'. It is guaranteed that the sum of sizes of all boards in the input does not exceed $$$4\cdot10^6$$$. | 2,300 | For each test case, output three integers $$$r$$$, $$$c$$$ and $$$d$$$ ($$$1 \le r \le n$$$; $$$1 \le c \le m$$$; $$$d \ge 0$$$), which denote that the robot should start moving from cell $$$(r, c)$$$ to make the maximum number of moves $$$d$$$. If there are several answers, output any of them. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.