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
|
2eb47f53917286c6b2ee797f4405a02f
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class codeforces {
static int mod = 1000000007;
public static void main(String[] args) {
FastReader sc = new FastReader();
try {
// System.out.println(al);
StringBuilder ss = new StringBuilder();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Long> al = new ArrayList<>();
if(n%2 == 0 && m%2 == 0) {
long mid = n/2;
long mid1 = n/2 - 1;
long mid2 = m/2;
long mid3 = m/2 - 1;
long max = (n - n/2 + m - m/2);
for(int i = 0 ; i <n ; i++) {
for(int j = 0 ; j <m ; j++) {
long dist = Math.abs(mid1 - i ) + Math.abs(mid3 - j);
long dist1 = Math.abs(mid - i) + Math.abs(mid3 - j);
long dist2 = Math.abs(mid1 - i ) + Math.abs(mid2 - j);
long dist3 = Math.abs(mid - i ) + Math.abs(mid2 - j);
long temp = Math.min(dist, Math.min(dist1, Math.min(dist2, dist3)));
long c = temp + max;
al.add(c);
}
}
}
else if(n%2 != 0 && m%2 != 0) {
long mid = n/2;
long mid1 = m/2;
long max = (n - n/2 + m - m/2) -2;
for(int i = 0 ;i <n;i++) {
for(int j = 0;j<m ; j++) {
long dist = Math.abs(mid-i) + Math.abs(mid1 - j);
long temp = dist + max;
al.add(temp);
}
}
}
else if( n%2 == 0) {
long mid = n/2;
long mid1 = n/2 - 1;
long mid2 = m/2;
long max = (n - n/2 + m - m/2) - 1;
for(int i = 0 ;i <n;i++) {
for(int j = 0;j<m ; j++) {
long dist = Math.abs(mid-i) + Math.abs(mid2 - j);
long dist1 = Math.abs(mid1 - i ) + Math.abs(mid2 - j);
long temp = Math.min(dist, dist1) + max;
al.add(temp);
}
}
}
else if(m%2 == 0) {
long mid = n/2;
long mid1 = m/2 - 1;
long mid2 = m/2;
long max = (n - n/2 + m - m/2)-1;
for(int i = 0 ;i <n;i++) {
for(int j = 0;j<m ; j++) {
long dist = Math.abs(mid-i) + Math.abs(mid1 - j);
long dist1 = Math.abs(mid - i ) + Math.abs(mid2 - j);
long temp = Math.min(dist, dist1) + max;
al.add(temp);
}
}
}
Collections.sort(al);
for(int i = 0 ; i<al.size() ; i++) {
ss.append(al.get(i) +" ");
}
ss.append("\n");
}
System.out.println(ss);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
static long pow(long a, long b) {
long ans = 1;
long temp = a;
while(b>0) {
if((b&1) == 1) {
ans*=temp;
}
temp = temp*temp;
b = b>>1;
}
return ans;
}
static long ncr(int n, int r) {
return fact(n) / (fact(r) *
fact(n - r));
}
static long fact(long n)
{
long res = 1;
for (int i = 2; i <= n; i++) {
res = (res * i);
}
return res;
}
static long gcd(long a, long b) {
if(b == 0) {
return a;
}
return gcd(b , a%b);
}
static ArrayList<Integer> factor(long n) {
ArrayList<Integer> al = new ArrayList<>();
for(int i = 1 ; i*i<=n;i++) {
if(n%i == 0) {
if(n/i == i) {
al.add(i);
}
else {
al.add(i);
al.add((int) (n/i));
}
}
}
return al;
}
static class Pair implements Comparable<Pair>{
long a;
int b ;
Pair(long a, int b){
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
return (int)(this.a - o.a);
}
}
static ArrayList<Integer> sieve(int n) {
boolean a[] = new boolean[n+1];
Arrays.fill(a, false);
for(int i = 2 ; i*i <=n ; i++) {
if(!a[i]) {
for(int j = 2*i ; j<=n ; j+=i) {
a[j] = true;
}
}
}
ArrayList<Integer> al = new ArrayList<>();
for(int i = 2 ; i <=n;i++) {
if(!a[i]) {
al.add(i);
}
}
return al;
}
static ArrayList<Long> pf(long n) {
ArrayList<Long> al = new ArrayList<>();
while(n%2 == 0) {
al.add(2l);
n = n/2;
}
for(long i = 3 ; i*i<=n ; i+=2) {
while(n%i == 0) {
al.add(i);
n = n/i;
}
}
if(n>2) {
al.add( n);
}
return al;
}
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
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
acbf21a44657b3017e1b9e6c712c6e86
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
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++) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] distances = new int[n*m];
for (int x = 0; x<n; x++) {
for (int y = 0; y<m; y++) {
distances[x*m+y] = Math.max(x, n-x-1) + Math.max(y, m-y-1);
}
}
Arrays.sort(distances);
for (int k=0; k<n*m; k++) {
System.out.print((distances[k]) + " ");
}
}
System.out.println("");
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
7e4483c6bec9609a8ea78daf4ab2ccfb
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class A {
public static void main(String[] args) throws IOException {
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
int t = Integer.parseInt(br.readLine());
int i,j,k;
for(i=0;i<t;i++) {
String s = br.readLine();
String c[] = s.split(" ");
int n = Integer.parseInt(c[0]);
int m= Integer.parseInt(c[1]);
int num = n*m;
int[]res = new int[num];
int in = 0;
for(j=1;j<=n;j++) {
for(k=1;k<=m;k++) {
if(j<=n/2) {
if(k<=m/2)
res[in] = (n-j)+(m-k);
else
res[in] = (n-j)+(k-1);
in++;
//System.out.println(j+" "+k+" "+res[in-1]);
}
else {
if(k<=m/2)
res[in] = (j-1)+(m-k);
else
res[in] = (j-1)+(k-1);
in++;
//System.out.println(j+" "+k+" "+res[in-1]);
}
}
}
Arrays.sort(res);
for(j=0;j<num;j++)
System.out.print(res[j]+" ");
System.out.println();
}
}
static long solve(long[] arr,long l,long r, int n,int k) {
int j;
if(l<=r) {
long m = l+(r-l)/2;
long sum = arr[0]+m;
for(j=1;j<n;j++) {
if((double)arr[j]> (double)(sum*k)/(double)100)
break;
sum+=arr[j];
}
if(j==n)
return solve(arr,l,m-1,n,k);
else
return solve(arr,m+1,r,n,k);
}
return l;
}
static long setbitNumber(long n)
{
long k = (int)(Math.log(n) / Math.log(2));
return 1 << k;
}
static void leftrotate(int arr[],int i, int n)
{
int t = arr[i];
int f;
int j = i+1;
for(;j<=n;j++) {
f = arr[j];
arr[j] = t;
t = f;
}
arr[i] = t;
}
static int search(int l, int r,int x, int[]arr) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return search( l, mid - 1, x,arr);
return search( mid + 1, r, x,arr);
}
return -1;
}
static long coeff(int n, int k){ // nCr combination
long res = 1;
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
static long modpower(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static int xor(int n){
// If n is a multiple of 4
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
return 0;
}
static int getsum(int n) // sum of digits
{
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static boolean isPrime(int n) // check prime
{
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
94af6d2341dbb99936fb265de1ace27d
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
/**
__ __
( _) ( _)
/ / \\ / /\_\_
/ / \\ / / | \ \
/ / \\ / / |\ \ \
/ / , \ , / / /| \ \
/ / |\_ /| / / / \ \_\
/ / |\/ _ '_| \ / / / \ \\
| / |/ 0 \0\ / | | \ \\
| |\| \_\_ / / | \ \\
| | |/ \.\ o\o) / \ | \\
\ | /\\`v-v / | | \\
| \/ /_| \\_| / | | \ \\
| | /__/_ `-` / _____ | | \ \\
\| [__] \_/ |_________ \ | \ ()
/ [___] ( \ \ |\ | | //
| [___] |\| \| / |/
/| [____] \ |/\ / / ||
( \ [____ / ) _\ \ \ \| | ||
\ \ [_____| / / __/ \ / / //
| \ [_____/ / / \ | \/ //
| / '----| /=\____ _/ | / //
__ / / | / ___/ _/\ \ | ||
(/-(/-\) / \ (/\/\)/ | / | /
(/\/\) / / //
_________/ / /
\____________/ (
*/
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int m=sc.nextInt();
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++){
list.add(Math.max(i, n-i-1)+Math.max(j, m-j-1));
}
}
Collections.sort(list);
for(int i=0;i<=n*m-1;i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
c5011ab9d9dc5adc14df7557cc5627fc
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
static class scanner {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {return Integer.parseInt(next());}
static double nextDouble() throws IOException {return Double.parseDouble(next());}
static long nextLong() throws IOException {return Long.parseLong(next());}
}
static long min(long a, long b) {return Math.min(a, b);}
static long min(long a, long b, long c) {return min(a, min(b, c));}
static int min(int a, int b) {return Math.min(a, b);}
static int min(int a, int b, int c) {return min(a, min(b, c));}
static long max(long a, long b) {return Math.max(a, b);}
static long max(long a, long b, long c) {return max(a, max(b, c));}
static int max(int a, int b) {return Math.max(a, b);}
static int max(int a, int b, int c) {return max(a, max(b, c));}
static int abs(int x) {return Math.abs(x);}
static long abs(long x) {return Math.abs(x);}
static long ceil(double x) {return (long) Math.ceil(x);}
static long floor(double x) {return (long) Math.floor(x);}
static int ceil(int x) {return (int) Math.ceil(x);}
static int floor(int x) {return (int) Math.floor(x);}
static double sqrt(double x) {return Math.sqrt(x);}
static double cbrt(double x) {return Math.cbrt(x);}
static long sqrt(long x) {return (long) Math.sqrt(x);}
static long cbrt(long x) {return (long) Math.cbrt(x);}
static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);}
static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);}
static double pow(double n, double power) {return Math.pow(n, power);}
static long pow(long n, double power) {return (long) Math.pow(n, power);}
public static void main(String[] args) throws IOException {
scanner.init(System.in);
int t = 1;
t = scanner.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = scanner.nextInt();
int m = scanner.nextInt();
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dis = max(i, n-1-i) + max(j, m-1-j);
res.add(dis);
}
}
Collections.sort(res);
for (int i : res) {
System.out.print(i + " ");
}
System.out.println();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
f7abd4d1f4409b80d445407d9168537a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
static class scanner {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {return Integer.parseInt(next());}
static double nextDouble() throws IOException {return Double.parseDouble(next());}
static long nextLong() throws IOException {return Long.parseLong(next());}
}
static long min(long a, long b) {return Math.min(a, b);}
static long min(long a, long b, long c) {return min(a, min(b, c));}
static int min(int a, int b) {return Math.min(a, b);}
static int min(int a, int b, int c) {return min(a, min(b, c));}
static long max(long a, long b) {return Math.max(a, b);}
static long max(long a, long b, long c) {return max(a, max(b, c));}
static int max(int a, int b) {return Math.max(a, b);}
static int max(int a, int b, int c) {return max(a, max(b, c));}
static int abs(int x) {return Math.abs(x);}
static long abs(long x) {return Math.abs(x);}
static long ceil(double x) {return (long) Math.ceil(x);}
static long floor(double x) {return (long) Math.floor(x);}
static int ceil(int x) {return (int) Math.ceil(x);}
static int floor(int x) {return (int) Math.floor(x);}
static double sqrt(double x) {return Math.sqrt(x);}
static double cbrt(double x) {return Math.cbrt(x);}
static long sqrt(long x) {return (long) Math.sqrt(x);}
static long cbrt(long x) {return (long) Math.cbrt(x);}
static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);}
static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);}
static double pow(double n, double power) {return Math.pow(n, power);}
static long pow(long n, double power) {return (long) Math.pow(n, power);}
public static void main(String[] args) throws IOException {
scanner.init(System.in);
int t = 1;
t = scanner.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = scanner.nextInt();
int m = scanner.nextInt();
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res.add(max(i, n-1-i) + max(j, m-1-j));
}
}
Collections.sort(res);
for (int i : res) {
System.out.print(i + " ");
}
System.out.println();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
5445c734eaa15ec557f0cead685cf48f
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
static class scanner {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {return Integer.parseInt(next());}
static double nextDouble() throws IOException {return Double.parseDouble(next());}
static long nextLong() throws IOException {return Long.parseLong(next());}
}
static long min(long a, long b) {return Math.min(a, b);}
static long min(long a, long b, long c) {return min(a, min(b, c));}
static int min(int a, int b) {return Math.min(a, b);}
static int min(int a, int b, int c) {return min(a, min(b, c));}
static long max(long a, long b) {return Math.max(a, b);}
static long max(long a, long b, long c) {return max(a, max(b, c));}
static int max(int a, int b) {return Math.max(a, b);}
static int max(int a, int b, int c) {return max(a, max(b, c));}
static int abs(int x) {return Math.abs(x);}
static long abs(long x) {return Math.abs(x);}
static long ceil(double x) {return (long) Math.ceil(x);}
static long floor(double x) {return (long) Math.floor(x);}
static int ceil(int x) {return (int) Math.ceil(x);}
static int floor(int x) {return (int) Math.floor(x);}
static double sqrt(double x) {return Math.sqrt(x);}
static double cbrt(double x) {return Math.cbrt(x);}
static long sqrt(long x) {return (long) Math.sqrt(x);}
static long cbrt(long x) {return (long) Math.cbrt(x);}
static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);}
static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);}
static double pow(double n, double power) {return Math.pow(n, power);}
static long pow(long n, double power) {return (long) Math.pow(n, power);}
public static void main(String[] args) throws IOException {
scanner.init(System.in);
int t = 1;
t = scanner.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] mat = new int[n][m];
int max = m+n-2;
for (int i = 0; i < (n+1)/2; i++) {
int k=0;
for (int j = 0; j < (m+1)/2; j++) {
mat[i][j] = max - k++;
}
max--;
}
max = m+n-2;
for (int i = n-1; i >= (n+1)/2; i--) {
int k=0;
for (int j = 0; j < (m+1)/2; j++) {
mat[i][j] = max - k++;
}
max--;
}
max = m+n-2;
for (int i = 0; i < (n+1)/2; i++) {
int k=0;
for (int j = m-1; j >= (m+1)/2; j--) {
mat[i][j] = max - k++;
}
max--;
}
max = m+n-2;
for (int i = n-1; i >= (n+1)/2; i--) {
int k=0;
for (int j = m-1; j >= (m+1)/2; j--) {
mat[i][j] = max - k++;
}
max--;
}
List<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res.add(mat[i][j]);
}
}
Collections.sort(res);
for (int i : res) {
System.out.print(i + " ");
}
System.out.println();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
a1825ab737db27dd1f1cc9d23e21274b
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static class JoinSet {
int[] fa;
JoinSet(int n) {
fa = new int[n];
for (int i = 0; i < n; i++) fa[i] = i;
}
int find(int t) {
if (t != fa[t]) fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int mod = (int)998244353;
static int[][] dirs = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
static boolean[] prime = new boolean[10];
static {
for (int i = 2; i < prime.length; i++) prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i +"");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int) ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++) b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++) a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++) b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++) a[i] = b[i];
}
static int max(int a, int b) {
return Math.max(a,b);
}
static int min(int a, int b) {
return Math.min(a,b);
}
static long max(long a, long b) {
return Math.max(a,b);
}
static long min(long a, long b) {
return Math.min(a,b);
}
static int max(int[] a) {
int max = a[0];
for(int i : a) max = max(max,i);
return max;
}
static int min(int[] a) {
int min = a[0];
for(int i : a) min = min(min,i);
return min;
}
static long max(long[] a) {
long max = a[0];
for(long i : a) max = max(max,i);
return max;
}
static long min(long[] a) {
long min = a[0];
for(long i : a) min = min(min,i);
return min;
}
static int query(int x) throws Exception {
print("? " + x);
bw.flush();
return get();
}
public static void main(String[] args) throws Exception {
int t = get();
while (t-- > 0){
int f[] = getint();
int n = f[0], m = f[1];
int a[] = new int[n*m];
int idx = 0;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
a[idx++] = max(new int[]{i+j,n-1-i+j,i+m-1-j,n-1-i+m-1-j});
}
}
sort(a);
print(a);
}
bw.flush();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
c01efe98c82e878e08a2883a3c47cfec
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Collections;
public class Main{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[]args){
FastReader at = new FastReader();
int t = at.nextInt();
while(t-->0){
int n = at.nextInt();
int m = at.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
int distance = Math.max(i,n-i-1)+Math.max(j,m-j-1);
ans.add(distance);
}
}
Collections.sort(ans);
for(int i = 0;i<ans.size();i++){
System.out.print(ans.get(i)+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
df160045c971c929df3c231d57112374
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class GFG {
static class Pair{
int x,y;
Pair(int x1,int y1){
x=x1;y=y1;
}
}
public static int n,m;
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t!=0){
t--;
n = sc.nextInt(); m = sc.nextInt();
boolean[][] vis = new boolean[n][m];
for(int i=0;i<n;i++)
Arrays.fill(vis[i],false);
int min = (n/2)+(m/2);
Queue<Pair> q = new LinkedList<>();
if(n%2!=0 && m%2!=0){
q.add(new Pair(n/2,m/2));
vis[n/2][m/2]=true;
}else if(n%2==0 && m%2==0){
q.add(new Pair((n/2)-1,(m/2)-1));vis[(n/2)-1][(m/2)-1]=true;
q.add(new Pair((n/2)-1,m/2));vis[(n/2)-1][m/2]=true;
q.add(new Pair(n/2,(m/2)-1));vis[n/2][(m/2)-1]=true;
q.add(new Pair(n/2,m/2));vis[n/2][m/2]=true;
}else{
if(n%2==0){
q.add(new Pair((n/2)-1,m/2));vis[(n/2)-1][m/2]=true;
q.add(new Pair(n/2,m/2));vis[n/2][m/2]=true;
}else{
q.add(new Pair(n/2,(m/2)-1));vis[n/2][(m/2)-1]=true;
q.add(new Pair(n/2,m/2));vis[n/2][m/2]=true;
}
}
while(!q.isEmpty()){
int size = q.size();
for(int i=0;i<size;i++)
System.out.print(min+" ");
while(size!=0){
size--;
Pair p = q.poll();
if(isValid(p.x-1,p.y) && !vis[p.x-1][p.y]){vis[p.x-1][p.y]=true;q.add(new Pair(p.x-1,p.y));}
if(isValid(p.x+1,p.y) && !vis[p.x+1][p.y]){vis[p.x+1][p.y]=true;q.add(new Pair(p.x+1,p.y));}
if(isValid(p.x,p.y-1) && !vis[p.x][p.y-1]){vis[p.x][p.y-1]=true;q.add(new Pair(p.x,p.y-1));}
if(isValid(p.x,p.y+1) && !vis[p.x][p.y+1]){vis[p.x][p.y+1]=true;q.add(new Pair(p.x,p.y+1));}
}
min++;
}
System.out.println("");
}}
public static boolean isValid(int r,int c){
if(r>=0 && c>=0 && r<n && c<m)
return true;
return false;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
c1c89d709113f07ef865426dcfe092aa
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class NotSitting {
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0)
{
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
if(n%2!=0 && m%2!=0)
{
int ans=n/2+m/2;
int i=n/2,j=m/2;
pw.print(ans+" ");
int c=0,co1=-2,co2=-2;
while(true)
{
i--;
j++;
if(i<0)
co1+=4;
if(j>=m)
co2+=4;
c+=4;
int tempc=c;
if(co1>0)
tempc-=co1;
if(co2>0)
tempc-=co2;
if(tempc<=0)
break;
ans++;
for(int k=1;k<=tempc;k++)
pw.print(ans+" ");
}
}
else if(n%2!=0 || m%2!=0)
{
int ans=n/2+m/2;
int i=n/2,j=m/2;
pw.print(ans+" "+ans+" ");
int c=2,co1=-2,co2=-2;
if(n%2!=0)
{
co1=0;
}
else
{
co2=0;
i--;
}
while(true)
{
i--;
j++;
if(i<0)
co1+=4;
if(j>=m)
co2+=4;
c+=4;
int tempc=c;
if(co1>0)
tempc-=co1;
if(co2>0)
tempc-=co2;
if(tempc<=0)
break;
ans++;
for(int k=1;k<=tempc;k++)
pw.print(ans+" ");
}
}
else
{
int ans=n/2+m/2;
int i=n/2-1,j=m/2;
pw.print(ans+" "+ans+" "+ans+" "+ans+" ");
int c=4,co1=0,co2=0;
while(true)
{
i--;
j++;
if(i<0)
co1+=4;
if(j>=m)
co2+=4;
c+=4;
int tempc=c;
if(co1>0)
tempc-=co1;
if(co2>0)
tempc-=co2;
if(tempc<=0)
break;
ans++;
for(int k=1;k<=tempc;k++)
{
pw.print(ans+" ");
}
}
}
pw.println();
pw.flush();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
db168b813a657121ac476aadacc4eddb
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
// static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
int n = sc.nextInt();
int m = sc.nextInt();
PriorityQueue<Integer> st = new PriorityQueue<>();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int maxi = Math.max(i , n - i - 1) + Math.max(j , m - j - 1);
st.add(maxi);
}
}
while(!st.isEmpty())
{
System.out.print(st.peek()+" ");
st.poll();
}
System.out.println();
}
}
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 void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
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 class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
int second;
Pair(int first , int second)
{
this.first = first;
this.second = second;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
652876b369349ac6bf54cb0009438505
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t= sc.nextInt();
while (t-->0) {
double n, m;
n = sc.nextDouble();
m = sc.nextDouble();
double[] num=new double[(int)Math.ceil(n*m)];
int index=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
num[index++]=(n-1.0)/2.0+(m-1.0)/2.0+Math.abs(i-(n-1.0)/2.0)+Math.abs(j-(m-1.0)/2.0);
}
}
Arrays.sort(num);
for(int i=0;i<num.length;i++){
if(i!=0) System.out.print(" ");
System.out.print(Math.round(num[i]));
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d31c7d904b00998656af7b0e3ca80a70
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
List<int[]> corners = new ArrayList<>();
corners.add(new int[]{1, 1});
corners.add(new int[]{n, 1});
corners.add(new int[]{1, m});
corners.add(new int[]{n, m});
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int distance = getMaxDistance(corners, i, j);
pq.add(new int[]{i, j, distance});
}
}
int count = 0;
while (count < n * m) {
out.print(pq.poll()[2] + " ");
count++;
}
out.println();
}
private static int getMaxDistance(List<int[]> corners, int i, int j) {
int distance = 0;
for (int[] point : corners) {
distance = Math.max(distance, Math.abs(i - point[0]) + Math.abs(j - point[1]));
}
return distance;
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException end)
{
end.printStackTrace();
}
}
return str.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 end)
{
end.printStackTrace();
}
return str;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
838c508c37194fdbf74ae69dc07e41a2
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class B {
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());
}
}
public static void solve(int h, int w) {
Map<Integer, Integer> map = new TreeMap<>();
int dist;
int[][] f = new int[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dist = Math.max(dist(j, i, 0, 0), Math.max(dist(j, i, w - 1, 0), Math.max(dist(j, i, 0, h - 1), dist(j, i, w - 1, h - 1))));
f[i][j] = dist;
map.put(dist, map.getOrDefault(dist, 0) + 1);
}
}
StringBuilder erg = new StringBuilder();
for (Map.Entry<Integer, Integer> e: map.entrySet()) erg.append((e.getKey() + " ").repeat(e.getValue()));
System.out.println(erg);
}
public static int dist(int x1, int y1, int x2, int y2) {
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
698421bb6059274fbea6b00c3fbf745a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int tc = scn.nextInt();
while(tc-->0){
int n = scn.nextInt();
int m = scn.nextInt();
int[][] c = new int[n][m];
Queue<int[]> q = new ArrayDeque<>();
if(n%2==1&&m%2==1){
c[n/2][m/2]=n/2+m/2;
q.add(new int[]{n/2,m/2});
}else if(n%2==1){
c[n/2][m/2-1]=n/2+m/2;
q.add(new int[]{n/2,m/2-1});
c[n/2][m/2]=n/2+m/2;
q.add(new int[]{n/2,m/2});
}else if(m%2==1){
c[n/2-1][m/2]=n/2+m/2;
q.add(new int[]{n/2-1,m/2});
c[n/2][m/2]=n/2+m/2;
q.add(new int[]{n/2,m/2});
}else{
c[n/2][m/2-1]=n/2+m/2;
q.add(new int[]{n/2,m/2-1});
c[n/2-1][m/2]=n/2+m/2;
q.add(new int[]{n/2-1,m/2});
c[n/2][m/2]=n/2+m/2;
q.add(new int[]{n/2,m/2});
c[n/2-1][m/2-1]=n/2+m/2;
q.add(new int[]{n/2-1,m/2-1});
}
int[][] d = {{-1,0},{0,-1},{1,0},{0,1}};
while(!q.isEmpty()){
int[] t = q.remove();
System.out.print(c[t[0]][t[1]]+" ");
for(int i=0;i<d.length;i++){
if(t[0]+d[i][0]>=0&&t[0]+d[i][0]<n&&t[1]+d[i][1]>=0&&t[1]+d[i][1]<m&&c[t[0]+d[i][0]][t[1]+d[i][1]]==0){
c[t[0]+d[i][0]][t[1]+d[i][1]]=c[t[0]][t[1]]+1;
q.add(new int[]{t[0]+d[i][0],t[1]+d[i][1]});
}
}
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
06c855acb459952fc97bedf98f3c96fe
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
public class Solution{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int val;
char c;
public Pair(int v, char ch) {
val=v;
c = ch;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
public static void main(String[] args) throws Exception
{
FastReader scn = new FastReader();
int t = scn.nextInt();
while(t>0) {
int n = scn.nextInt();
int m = scn.nextInt();
int[][] a = new int[n][m];
int[] ans = new int[m*n];
int k = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j] = Math.max(j+i,Math.max(m-j-1+n-i-1, Math.max(m-j-1+i,n-i-1+j)));
if(m==n && m%2!=0) {
if(a[i][j]>=m-1) {
ans[k++]=a[i][j];
}
}else{
if(a[i][j]>= Math.min(n,m)) {
ans[k++]=a[i][j];
}
// System.out.println(k+" "+a[i][j]);
}
}
}
Arrays.sort(ans);
for(int i=0;i<ans.length;i++) {
System.out.print(ans[i]+" ");
}
System.out.println();
t--;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
9a4c406a784b13efc317a8ad01fa1aae
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/*
It's always like another kick for me to make sure I get what I want to get done,
because honestly you never know.
*/
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main extends PrintWriter {
Main() { super(System.out); }
static boolean cases = true;
void solve() {
int r = sc.nextInt();
int c = sc.nextInt();
int x = (r + 1) / 2, y = (c + 1) / 2;
int st = abs(r - x) + abs(c - y);
int a[][] = new int[r + 1][c + 1];
boolean[][] vis = new boolean[r + 1][c + 1];
Queue<int[]> q = new LinkedList<>();
if (r % 2 == 0 && c % 2 == 0) {
a[r / 2][c / 2] = st;
a[(r / 2) + 1][(c / 2) + 1] = st;
a[r / 2][(c / 2) + 1] = st;
a[(r / 2) + 1][c / 2] = st;
q.add(new int[] { r / 2, c / 2 });
q.add(new int[] { (r / 2) + 1, (c / 2) + 1 });
q.add(new int[] { r / 2, (c / 2) + 1 });
q.add(new int[] { (r / 2) + 1, c / 2 });
vis[r / 2][c / 2] = true;
vis[(r / 2) + 1][(c / 2) + 1] = true;
vis[r / 2][(c / 2) + 1] = true;
vis[(r / 2) + 1][c / 2] = true;
} else if (r % 2 != 0 && c % 2 != 0) {
a[(r + 1) / 2][(c + 1) / 2] = st;
q.add(new int[] { (r + 1) / 2, (c + 1) / 2 });
vis[(r + 1) / 2][(c + 1) / 2] = true;
} else if (r % 2 == 0 && c % 2 != 0) {
a[r / 2][(c + 1) / 2] = st;
a[(r / 2) + 1][(c + 1) / 2] = st;
q.add(new int[] { r / 2, (c + 1) / 2 });
q.add(new int[] { (r / 2) + 1, (c + 1) / 2 });
vis[r / 2][(c + 1) / 2] = true;
vis[(r / 2) + 1][(c + 1) / 2] = true;
} else if (r % 2 != 0 && c % 2 == 0) {
a[(r + 1) / 2][(c / 2) + 1] = st;
a[(r + 1) / 2][c / 2] = st;
q.add(new int[] { (r + 1) / 2, (c / 2) + 1 });
q.add(new int[] { (r + 1) / 2, c / 2 });
vis[(r + 1) / 2][(c / 2) + 1] = true;
vis[(r + 1) / 2][c / 2] = true;
}
int dir[][] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
ArrayList<Integer> ans = new ArrayList<>();
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
int[] curr = q.remove();
ans.add(a[curr[0]][curr[1]]);
for (int d[] : dir) {
int newX = d[0] + curr[0];
int newY = d[1] + curr[1];
if (newX <= 0 || newY <= 0 || newX > r || newY > c) continue;
if (vis[newX][newY]) continue;
a[newX][newY] = a[curr[0]][curr[1]] + 1;
q.add(new int[] { newX, newY });
vis[newX][newY] = true;
}
}
}
Collections.sort(ans);
for (int i : ans) print(i + " ");
println();
}
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);
}
public static void main(String[] args) {
Main obj = new Main();
int c = 1;
for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) obj.solve();
obj.solve();
obj.flush();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
char[] readCharArray(int n) {
char a[] = new char[n];
String s = sc.next();
for (int i = 0; i < n; i++) { a[i] = s.charAt(i); }
return a;
}
long[] readLongArray(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()); }
}
final int ima = Integer.MAX_VALUE;
final int imi = Integer.MIN_VALUE;
final long lma = Long.MAX_VALUE;
final long lmi = Long.MIN_VALUE;
static final long mod = (long) 1e9 + 7;
private static final FastScanner sc = new FastScanner();
private PrintWriter out = new PrintWriter(System.out);
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
3058b8406b563e4376448ed33175b904
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/*
*
*
* *** *** ******* ****
* *** *** **** **** *****
* *** *** *** *** ***
* **** *** ***
* *** *** *** ***
* *** *** ********** ***
* *** *** *********** ***
*
*
* */
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
try {
int t = sc.nextInt();
while (t-- > 0)
solve();
} catch (Exception e) {
return;
}
out.flush();
}
// SOLUTION STARTS HERE
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
static void solve() {
int r = sc.nextInt();
int c = sc.nextInt();
int x = (r + 1) / 2, y = (c + 1) / 2;
int st = Math.abs(r - x) + Math.abs(c - y);
int a[][] = new int[r + 1][c + 1];
boolean[][] vis = new boolean[r + 1][c + 1];
Queue<int[]> q = new LinkedList<>();
if (r % 2 == 0 && c % 2 == 0) {
a[r / 2][c / 2] = st;
a[(r / 2) + 1][(c / 2) + 1] = st;
a[r / 2][(c / 2) + 1] = st;
a[(r / 2) + 1][c / 2] = st;
q.add(new int[] { r / 2, c / 2 });
q.add(new int[] { (r / 2) + 1, (c / 2) + 1 });
q.add(new int[] { r / 2, (c / 2) + 1 });
q.add(new int[] { (r / 2) + 1, c / 2 });
vis[r / 2][c / 2] = true;
vis[(r / 2) + 1][(c / 2) + 1] = true;
vis[r / 2][(c / 2) + 1] = true;
vis[(r / 2) + 1][c / 2] = true;
} else if (r % 2 != 0 && c % 2 != 0) {
a[(r + 1) / 2][(c + 1) / 2] = st;
q.add(new int[] { (r + 1) / 2, (c + 1) / 2 });
vis[(r + 1) / 2][(c + 1) / 2] = true;
} else if (r % 2 == 0 && c % 2 != 0) {
a[r / 2][(c + 1) / 2] = st;
a[(r / 2) + 1][(c + 1) / 2] = st;
q.add(new int[] { r / 2, (c + 1) / 2 });
q.add(new int[] { (r / 2) + 1, (c + 1) / 2 });
vis[r / 2][(c + 1) / 2] = true;
vis[(r / 2) + 1][(c + 1) / 2] = true;
} else if (r % 2 != 0 && c % 2 == 0) {
a[(r + 1) / 2][(c / 2) + 1] = st;
a[(r + 1) / 2][c / 2] = st;
q.add(new int[] { (r + 1) / 2, (c / 2) + 1 });
q.add(new int[] { (r + 1) / 2, c / 2 });
vis[(r + 1) / 2][(c / 2) + 1] = true;
vis[(r + 1) / 2][c / 2] = true;
}
int dir[][] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
ArrayList<Integer> ans = new ArrayList<>();
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
int[] curr = q.remove();
ans.add(a[curr[0]][curr[1]]);
for (int d[] : dir) {
int newX = d[0] + curr[0];
int newY = d[1] + curr[1];
if (newX <= 0 || newY <= 0 || newX > r || newY > c)
continue;
if (vis[newX][newY])
continue;
a[newX][newY] = a[curr[0]][curr[1]] + 1;
q.add(new int[] { newX, newY });
vis[newX][newY] = true;
}
}
}
Collections.sort(ans);
for (int i : ans)
System.out.print(i + " ");
System.out.println();
}
// SOLUTION ENDS HERE
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
static class DSU {
int rank[];
int parent[];
DSU(int n) {
rank = new int[n + 1];
parent = new int[n + 1];
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
}
int findParent(int node) {
if (parent[node] == node)
return node;
return parent[node] = findParent(parent[node]);
}
boolean union(int x, int y) {
int px = findParent(x);
int py = findParent(y);
if (px == py)
return false;
if (rank[px] < rank[py]) {
parent[px] = py;
} else if (rank[px] > rank[py]) {
parent[py] = px;
} else {
parent[px] = py;
rank[py]++;
}
return true;
}
}
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 boolean[] seiveOfEratosthenes(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean isPrime(long n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
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());
}
char[][] readCharMatrix(int n, int m) {
char a[][] = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
a[i][j] = s.charAt(j);
}
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
int[] readIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(next());
}
return a;
}
void printIntArray(int a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
long[] readLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(next());
}
return a;
}
void printLongArray(long a[]) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
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();
}
}
private static final FastReader sc = new FastReader();
private static final FastWriter out = new FastWriter(System.out);
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
3fc3dd08f0d72860894d83e9cbe90f9a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int n = scn.nextInt();
int m = scn.nextInt();
ArrayList<Integer>list = new ArrayList<>();
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
int d = Math.max(n-i,i-1) + Math.max(m-j,j-1);
list.add(d);
}
}
Collections.sort(list);
for(int i = 0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
5c028ddae0998221b3eed0f4253a63e5
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
// Working program with FastReader
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class B_Not_Sitting {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = new int[n * m];
int idx = 0;
// arr stores distance of Tina with respect to every block i,j
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[idx++] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1);
}
}
// Sorting this array as when k is less then rahul will be closer to Tinal and
// hence the overall distance b/w
// them would be less. As the k increases the distance might increase b/w Tina &
// Rahul
Arrays.sort(arr);
for (int i : arr)
System.out.print(i + " ");
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
170362cf97096d9de353bf612f8165ca
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
// Working program with FastReader
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class B_Not_Sitting {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = new int[n * m];
int idx = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[idx++] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1);
}
}
Arrays.sort(arr);
// for(int i=0;i<n*m;i++){
// Sysout
// }
for (int i : arr)
System.out.print(i + " ");
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
e83bcdc568e74dff11fb56ea250a9395
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.concurrent.PriorityBlockingQueue;
//* --> number of prime numbers less then or equal to x are --> x/ln(x)
//* --> String concatenation using the + operator within a loop should be avoided. Since the String object is immutable, each call for concatenation will
// result in a new String object being created.
// THE SIEVE USED HERE WILL RETURN A LIST CONTAINING ALL THE PRIME NUMBERS TILL N
public class name766b{
static class FastIO extends PrintWriter{private InputStream stream;private byte[] buf = new byte[1 << 16];
private int curChar, numChars;public FastIO() {this(System.in, System.out);}public FastIO(InputStream i, OutputStream o){super(o);stream=i;}
public FastIO(String i, String o) throws IOException{super(new FileWriter(o));stream = new FileInputStream(i);}
private int nextByte(){if (numChars == -1)throw new InputMismatchException();if(curChar >= numChars) {
curChar = 0;try{numChars = stream.read(buf);}catch (IOException e) {throw new InputMismatchException();}
if (numChars == -1)return -1;}return buf[curChar++];}public String next(){int c;do{c=nextByte();}while (c <= ' ');StringBuilder res = new StringBuilder();do {
res.appendCodePoint(c);c = nextByte();} while (c > ' ');return res.toString();}
public int nextInt() { int c;do {c = nextByte();} while (c <= ' ');int sgn = 1;if (c == '-') {sgn = -1;c = nextByte();}
int res = 0;do {if (c < '0' || c > '9')throw new InputMismatchException();res = 10 * res + c - '0';c = nextByte();
} while (c > ' ');return res * sgn;}public int nextLong() {int c;do {c = nextByte();} while (c <= ' ');int sgn = 1;if (c == '-') {sgn = -1;c = nextByte();}int res = 0;do {if (c < '0' || c > '9')throw new InputMismatchException();
res = 10 * res + c - '0';c = nextByte();} while (c > ' ');return res * sgn;}public double nextDouble() {return Double.parseDouble(next());}}
public static void print(int ans, int t) {System.out.println("Case" + " " + "#" + t + ":" + " " + ans);}
static long mod = 1000000007;static int max = Integer.MIN_VALUE;static int min = Integer.MAX_VALUE;
public static void sort(long[] arr) {ArrayList<Long> ls = new ArrayList<Long>();for (long x : arr)ls.add(x);Collections.sort(ls);for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);}public static long fciel(long a, long b) {if (a == 0)return 0;return (a - 1) / b + 1;}
static boolean[] is_prime = new boolean[1000001];static ArrayList<Integer> list = new ArrayList<>();static long n = 1000000;
public static void sieve() {Arrays.fill(is_prime, true);is_prime[0] = is_prime[1] = false;for (int i = 2; i * i <= n; i++) {
if (is_prime[i]){for (int j = i * i; j <= n; j += i)is_prime[j] = false;}}for (int i = 2; i <= n; i++) {if (is_prime[i]){list.add(i);}}
}
// ---------- NCR ---------- \
static int NC = 100005;static long inv[] = new long[NC];static long fac_inv[] = new long[NC];static long fac[] = new long[NC];
public static void initialize(){long MOD = mod;int i;inv[1] = 1;for (i = 2; i <= NC - 2; i++)inv[i] = (MOD - MOD / i) * inv[(int) MOD % i] % MOD;
fac[0] = fac[1] = 1;for (i = 2; i <= NC - 2; i++)fac[i] = i * fac[i - 1] % MOD;fac_inv[0] = fac_inv[1] = 1;
for (i = 2; i <= NC - 2; i++)fac_inv[i] = inv[i] * fac_inv[i - 1] % MOD;}public static long ncr(int n, int r) {
long MOD = mod;if (n < r)return 0;return (fac[n] * fac_inv[r] % MOD) * fac_inv[n - r] % MOD;}
// ---------- NCR ---------- \
public static void main(String args[]) throws java.lang.Exception {FastIO io = new FastIO();StringBuilder s = new StringBuilder();
int t=io.nextInt();while(t-->0){
int n=io.nextInt();
int m=io.nextInt();
int ar[][]=new int[n][m];
int i=0;
int j=n-1;
int steps=Math.abs(n+m-2);
ArrayList<Integer> list=new ArrayList<>();
while(i<=j)
{
int cst=steps;
int x=0;
int y=m-1;
while(x<=y)
{
ar[i][x]=ar[i][y]=cst;
x++;
y--;
cst--;
}
x=0;
y=m-1;
cst=steps;
while(x<=y)
{
ar[j][x]=ar[j][y]=cst;
x++;
y--;
cst--;
}
i++;
j--;
steps--;
}
for( i=0;i<n;i++)
{
for( j=0;j<m;j++)
{
list.add(ar[i][j]);
}
}
Collections.sort(list);
for(int k :list)
{
s.append(k+" ");
}
if(t>0){s.append("\n");}}
io.print(s);io.close();}}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
cc82ff99bee60013976b7039b93ea48c
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/*
Bissmillah_send
IN SHA ALLAH ACCEPTED
*/
import java.lang.*;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class MyClass {
static Scanner in = new Scanner(System.in);
static void solve()
{
int a = in.nextInt();
int b = in.nextInt();
int[][] arr = new int[a][b];
int l = a+b-2;
for(int i=0;i<b;i++){
if(l>arr[0][i]){
arr[0][i]=l;
}
l--;
}
for(int j=0;j<b;j++){
int s = arr[0][j]-1;
for(int i=1;i<a;i++){
if(s>arr[i][j]){
arr[i][j]=s;
}
s--;
}
}
l = a+b-2;
for(int i=b-1;i>=0;i--){
if(l>arr[0][i]){
arr[0][i]=l;
}
l--;
}
for(int j=0;j<b;j++){
int s = arr[0][j]-1;
for(int i=1;i<a;i++){
if(s>arr[i][j]){
arr[i][j]=s;
}
s--;
}
}
l = a+b-2;
for(int i=b-1;i>=0;i--){
if(l>arr[a-1][i]){
arr[a-1][i]=l;
}
l--;
}
for(int j=0;j<b;j++){
int s = arr[a-1][j]-1;
for(int i=a-2;i>=0;i--){
if(s>arr[i][j]){
arr[i][j]=s;
}
s--;
}
}
l = a+b-2;
for(int i=0;i<b;i++){
if(l>arr[a-1][i]){
arr[a-1][i]=l;
}
l--;
}
for(int j=0;j<b;j++){
int s = arr[a-1][j]-1;
for(int i=a-2;i>=0;i--){
if(s>arr[i][j]){
arr[i][j]=s;
}
s--;
}
}
ArrayList<Integer> ar = new ArrayList<>();
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
ar.add(arr[i][j]);
}
}
Collections.sort(ar);
for(int i=0;i<ar.size();i++){
System.out.print(ar.get(i)+" ");
}
System.out.println();
}
public int gcd(int a, int b) {
if (b==0) return a;
return gcd(b,a%b);
}
public static void main(String args[]) {
// String s =in.next();
int n = in.nextInt();
for(int j=0;j<n;j++){
solve();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
95005d7d8b8e3a5aa9cbc7839ccdf0c5
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.Math.sqrt;
import static java.lang.System.out;
import static java.lang.System.err;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc;
// static FastWriter out;
public static void main(String hi[]){
initializeIO();
sc=new FastReader();
// FastWriter out=new FastWriter();
int t=sc.nextInt();
// boolean[] seave=sieveOfEratosthenes((int)(1e6));
// int t=1;
while(t--!=0){
int n =sc.nextInt();
int m =sc.nextInt();
List<Integer> li=new ArrayList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int d1=abs(i-0)+abs(j-0);
int d2=abs(i-(n-1))+abs(j-0);
int d3=abs(i-(n-1))+abs(j-(m-1));
int d4=abs(i-0)+abs(j-(m-1));
int v=max(max(d1,d4),max(d2,d3));
li.add(v);
}
}
Collections.sort(li);
for (int x : li) {
out.print(x+" ");
}
out.println();
// print(solve(nums,n,m,it,jt));
// System.out.println(String.format("%.10f", max));
}
}
private static int solve(String[] nums,int n,int m,int it,int jt){
boolean see=false;
debug(n+" "+m+" "+it+" "+jt);
int max=4;
for(int i=0;i<n;i++){
char[] arr=nums[i].toCharArray();
// debug(nums[i]+" "+i);
for(int j=0;j<m;j++){
if(arr[j]=='B'){
// debug(i+" "+j);
see=true;
if((i==it&&j==jt))max=0;
if((i==it||j==jt))max=min(max,1);
}
}
}
if(max<4)return max;
if(see)return 2;
return -1;
}
/*
************************************see constraints carefully***************************
//pallindrome
1:- agar palllindrome related hai to dkho equal character k pair koonse konse hain kyunki bo humeasaa laag jaynge
2:- phir usme ek char add kardo agar kaar sakte ho too
*****if lost****
try to think of prefix and postfix array
//cloring
1->devide on no of colors
2->bipartrate graph
//bit manupulation
1-> agar do no ka xor min karna hai to dusra bo no ho jiski last bit set ho
for example{
3 ka xor esse no se karao jiski last bit (11)3 set ho jo 2^k
^
| last bit
hogi
}
2-> A || b >max(a,b)
//game theory
1;- jo pahle game start karega uske paas advantage hoga
jo ki hai ki agar sath mai finish kiya too jisne pahle start kiya thaa bo jittega kyunki usne start pahle kiya thaa thsts it
//MEX concept
Mex of array is smallest +ve integer not present in array
orr
ek chiz orr
0 2 1 1 0
| |
mex1
| ^=<mex2|
*visited array ka consept use karna hai lowest no find karne k liye*
// lexograpgical greater
array x is lexographicallly greater then y first position where element differ there xi>yi
or
sizeof(x)>size(y) and y is prefix of x
*/
private static void print(String s){
out.println(s);
}
private static void debug(String s){
err.println(s);
}
private static int charToInt(char c){
return ((((int)(c-'0'))%48));
}
private static void print(double s){
out.println(s);
}
private static void print(float s){
out.println(s);
}
private static void print(long s){
out.println(s);
}
private static void print(int s){
out.println(s);
}
private static void debug(double s){
err.println(s);
}
private static void debug(float s){
err.println(s);
}
private static void debug(long s){
err.println(s);
}
private static void debug(int s){
err.println(s);
}
private static boolean isPrime(int n){
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static String[] readStringArray(int n){
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.next();
}
return arr;
}
private static Map<Character,Integer> freq(String s){
Map<Character,Integer> map=new HashMap<>();
for(char c:s.toCharArray()){
map.put(c,map.getOrDefault(c,0)+1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n){
boolean prime[] = new boolean[(int)n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true){
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static long maxProfit(List<Long> prices) {
long sofar=0;
long max_v=0;
for(int i=0;i<prices.size();i++){
sofar+=prices.get(i);
if (sofar<0) {
sofar=0;
}
max_v=Math.max(max_v,sofar);
}
return max_v;
}
static boolean isMemberAC(int a, int d, int x){
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static long sum(int[] arr){
long sum=0;
for(int x:arr){
sum+=x;
}
return sum;
}
private static long[] readLongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static long evenSumFibo(long n){
long l1=0,l2=2;
long sum=0;
while (l2<n) {
long l3=(4*l2)+l1;
sum+=l2;
if(l3>n)break;
l1=l2;
l2=l3;
}
return sum;
}
private static void initializeIO(){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr){
int max=Integer.MIN_VALUE;
for(int x:arr){
max=Math.max(max,x);
}
return max;
}
private static long maxOfArray(long[] arr){
long max=Long.MIN_VALUE;
for(long x:arr){
max=Math.max(max,x);
}
return max;
}
private static int[][] getIntervals(int n,int m){
int[][] arr=new int[n][m];
for(int j=0;j<n;j++){
for(int i=0;i<m;i++){
arr[j][m]=sc.nextInt();
}
}
return arr;
}
private static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int[] rintArray(int n){
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n){
double[] arr=new double[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextDouble();
}
return arr;
}
private static long[] rlongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static String[] rstringArray(int n){
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLine();
}
return arr;
}
private static void print(int[] arr){
out.println(Arrays.toString(arr));
}
private static void print(long[] arr){
out.println(Arrays.toString(arr));
}
private static void print(String[] arr){
out.println(Arrays.toString(arr));
}
private static void print(double[] arr){
out.println(Arrays.toString(arr));
}
private static void debug(String[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(int[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr){
err.println(Arrays.toString(arr));
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
return true;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
78dc12630128ba089e43b24ddb1a7154
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
/**
* Provide prove of correctness before implementation. Implementation can cost a lot of time.
* Anti test that prove that it's wrong.
* <p>
* Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation
* <p>
* Will program ever exceed limit?
* Try all approaches with prove of correctness if task is not obvious.
* If you are given formula/rule: Try to play with it.
* Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data
* Number theory
* Game theory (optimal play) that consider local and global strategy.
*/
public class MS_4_C {
int n;
int m;
private void solveOne() {
n = nextInt();
m = nextInt();
List<PairIntInt> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
list.add(new PairIntInt(i, j));
}
}
list.sort(
(pair1, pair2) -> {
return Integer.compare(getMaxDist(pair1), getMaxDist(pair2));
}
);
int cnt = n * m;
for (int i = 0; i < cnt; i++) {
System.out.print(getMaxDist(list.get(i)));
System.out.print(' ');
}
System.out.println();
}
int getMaxDist(PairIntInt pair) {
return max(distTo(new PairIntInt(1, 1), pair),
distTo(new PairIntInt(n, 1), pair),
distTo(new PairIntInt(1, m), pair),
distTo(new PairIntInt(n, m), pair));
}
int max(int a, int b, int c, int d) {
return Math.max(Math.max(a, b), Math.max(c, d));
}
int distTo(PairIntInt f, PairIntInt s) {
return distTo(f.x, f.y, s.x, s.y);
}
int distTo(int a, int b, int c, int d) {
return Math.abs(a - c) + Math.abs(b - d);
}
static class PairIntInt {
int x;
int y;
public PairIntInt(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public PairIntInt setX(int x) {
this.x = x;
return this;
}
public int getY() {
return y;
}
public PairIntInt setY(int y) {
this.y = y;
return this;
}
@Override
public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
PairIntInt rational = (PairIntInt) o;
return x == rational.x && y == rational.y;
}
@Override
public int hashCode() {
// return Objects.hash(x, y);
return x * 31 + y;
}
}
private void solve() {
int t = nextInt();
for (int tt = 0; tt < t; tt++) {
solveOne();
}
}
class AssertionRuntimeException extends RuntimeException {
AssertionRuntimeException(Object expected,
Object actual, Object... input) {
super("expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input));
}
}
private int nextInt() {
return System.in.readInt();
}
private long nextLong() {
return System.in.readLong();
}
private String nextString() {
return System.in.readString();
}
private int[] nextIntArr(int n) {
return System.in.readIntArray(n);
}
private long[] nextLongArr(int n) {
return System.in.readLongArray(n);
}
public static void main(String[] args) {
new MS_4_C().run();
}
static class System {
private static FastInputStream in;
private static FastPrintStream out;
}
private void run() {
System.in = new FastInputStream(java.lang.System.in);
System.out = new FastPrintStream(java.lang.System.out);
solve();
System.out.flush();
}
private static class FastPrintStream {
private static final int BUF_SIZE = 8192;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastPrintStream() {
this(java.lang.System.out);
}
public FastPrintStream(OutputStream os) {
this.out = os;
}
public FastPrintStream(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastPrintStream print(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastPrintStream print(char c) {
return print((byte) c);
}
public FastPrintStream print(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastPrintStream print(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
//can be optimized
public FastPrintStream print0(char[] s) {
if (ptr + s.length < BUF_SIZE) {
for (char c : s) {
buf[ptr++] = (byte) c;
}
} else {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
}
return this;
}
//can be optimized
public FastPrintStream print0(String s) {
if (ptr + s.length() < BUF_SIZE) {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
}
} else {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
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 FastPrintStream print(int x) {
if (x == Integer.MIN_VALUE) {
return print((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
print((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 FastPrintStream print(long x) {
if (x == Long.MIN_VALUE) {
return print("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
print((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 FastPrintStream print(double x, int precision) {
if (x < 0) {
print('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
print((long) x).print(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
print((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastPrintStream println(int[] a, int from, int upTo, char separator) {
for (int i = from; i < upTo; i++) {
print(a[i]);
print(separator);
}
print('\n');
return this;
}
public FastPrintStream println(int[] a) {
return println(a, 0, a.length, ' ');
}
public FastPrintStream println(char c) {
return print(c).println();
}
public FastPrintStream println(int x) {
return print(x).println();
}
public FastPrintStream println(long x) {
return print(x).println();
}
public FastPrintStream println(String x) {
return print(x).println();
}
public FastPrintStream println(double x, int precision) {
return print(x, precision).println();
}
public FastPrintStream println() {
return print((byte) '\n');
}
public FastPrintStream printf(String format, Object... args) {
return print(String.format(format, args));
}
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");
}
}
}
private static class FastInputStream {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastInputStream(InputStream stream) {
this.stream = stream;
}
public double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = readDouble();
}
return array;
}
public String[] readStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = readString();
}
return array;
}
public char[] readCharArray(int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++) {
array[i] = readCharacter();
}
return array;
}
public void readIntArrays(int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readInt();
}
}
}
public void readLongArrays(long[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readLong();
}
}
}
public void readDoubleArrays(double[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readDouble();
}
}
}
public char[][] readTable(int rowCount, int columnCount) {
char[][] table = new char[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readCharArray(columnCount);
}
return table;
}
public int[][] readIntTable(int rowCount, int columnCount) {
int[][] table = new int[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readIntArray(columnCount);
}
return table;
}
public double[][] readDoubleTable(int rowCount, int columnCount) {
double[][] table = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readDoubleArray(columnCount);
}
return table;
}
public long[][] readLongTable(int rowCount, int columnCount) {
long[][] table = new long[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readLongArray(columnCount);
}
return table;
}
public String[][] readStringTable(int rowCount, int columnCount) {
String[][] table = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readStringArray(columnCount);
}
return table;
}
public String readText() {
StringBuilder result = new StringBuilder();
while (true) {
int character = read();
if (character == '\r') {
continue;
}
if (character == -1) {
break;
}
result.append((char) character);
}
return result.toString();
}
public void readStringArrays(String[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readString();
}
}
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = readLong();
}
return array;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int peekNonWhitespace() {
while (isWhitespace(peek())) {
read();
}
return peek();
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return readString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d22a79df5b31ce74de7954117cb43960
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();
ArrayList<Integer>ans=new ArrayList<>();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int x=Math.abs(i-1)+Math.abs(j-1);
x=Math.max(x,Math.abs(i-1)+Math.abs(j-m));
x=Math.max(x,Math.abs(n-i)+Math.abs(j-1));
x=Math.max(x,Math.abs(i-n)+Math.abs(j-m));
ans.add(x);
}
}
Collections.sort(ans);
for(int val:ans){
System.out.print(val+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
17e9306b4805b58e91d96814e50d83ee
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package practice;
import java.io.*;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
FastReader fr=new FastReader();
int t=fr.nextInt();
while(t-->0) {
int n=fr.nextInt();
int m=fr.nextInt();
List<Integer> dist=new ArrayList<>();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int d1=i+j;
int d2= i+m-1-j;
int d3=n-i-1+j;
int d4=n-i-1+m-1-j;
dist.add(Math.max(Math.max(d1, d2),Math.max(d3, d4)));
}
}
Collections.sort(dist);
for(int i=0;i<n*m;i++) {
System.out.print(dist.get(i)+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
4da9b5e2620780337f1b88e5351099d7
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
public static void main (String[] args) throws IOException {
Kattio io = new Kattio();
int t = io.nextInt();
for (int ii=0; ii<t; ii++) {
long n = io.nextLong();
long m = io.nextLong();
PriorityQueue<Long> pq = new PriorityQueue<Long>();
for (long i=0; i<n; i++) {
for (long j=0; j<m; j++) {
long dist = i + j;
dist = Math.max(dist, (n-1-i)+j);
dist = Math.max(dist, i+(m-1-j));
dist = Math.max(dist, (n-1-i) + (m-1-j));
pq.add(dist);
}
}
while (pq.size() >= 1) {
io.print(pq.poll());
io.print(" ");
}
io.println();
}
io.close();
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
a8ec5ac66f20868ff260ae8ece50454d
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.sql.SQLSyntaxErrorException;
import java.util.*;
import java.io.*;
import java.util.stream.StreamSupport;
public class Solution {
static int mod = 998244353;
public static void main(String str[]) throws IOException{
// Reader sc = new Reader();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
Stack<Node> s= new Stack<>();
int k = 1;
int x = (n+1)/2;
int y = (m+1)/2;
int curr = (n+m-2);
int times = 0;
while(k<Math.min(x,y)){
Node d = new Node(times, curr--,k++);
s.add(d);
}
if(Math.min(n,m)%2!=0){
times++;
}
for(int i=0;i<(Math.max(x,y)-Math.min(x,y));i++){
Node d = new Node(times,curr--,k);
s.add(d);
}
if(Math.max(n,m)%2!=0){
times++;
}
while(k>0){
Node d = new Node(times,curr--,k);
s.add(d);
k--;
}
while(!s.isEmpty()){
Node d = s.pop();
int count = 0;
if(d.times<=d.count){
count = (d.count-d.times)*4 + d.times*2;
}
else{
count = 1;
}
while(count-->0){
output.write(d.value+" ");
}
}
output.write("\n");
}
output.flush();
}
static class Node{
int times;
int value;
int count;
Node( int t, int c, int b){
times = t;
value = c;
count = b;
}
}
static int dp(ArrayList<Integer> arr, ArrayList<Integer> num, HashMap<Double, Integer> memo, double curr, int ans){
if(memo.containsKey((Math.pow(ans,4)*Math.pow(curr,3)))) return memo.get(curr);
int max = 0;
int fin = 0;
for(int i=0;i<arr.size();i++){
int x = arr.get(i);
arr.remove(i);
int y = num.get(i);
num.remove(i);
int pp = x;
if(ans!=-1) pp = (int)gcd(ans, x);
if(pp==1){
int temp = pp+arr.size();
if(max<=temp){
max = temp;
fin = pp;
}
continue;
}
int temp = pp + dp(arr, num, memo, curr+(Math.pow(x,7)/Math.pow(x,4)), pp);
if(max<=temp){
max = temp;
fin = pp;
}
num.add(i,y);
arr.add(i,x);
}
memo.put(curr, max);
return max+fin;
}
static void add(ArrayList<Pair> al, Pair x){
int ind = Collections.binarySearch(al, x, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.val-o2.val;
}
});
if(ind<0){
ind++;
ind*=-1;
}
al.add(ind, x);
}
static int max(ArrayList<Integer> al){
int i=0;
int max =Integer.MIN_VALUE;
for(int j=0;j<al.size();j++){
int k = al.get(j);
if(k>max){
i = j;
max = k;
}
}
return i;
}
static class Pair {
int val;
int ind;
// int e=0;
Pair(int v, int ss) {
val = v;
ind = ss;
}
}
//Collections.sort(al, new Comparator<Object>() {
// @Override
// public int compare(Object o1, Object o2) {
// return 0;
// }
//});
// static class Node{
// int ind;
// int tot = 1;
// ArrayList<Node> parent = new ArrayList<>();
// ArrayList<Node> child = new ArrayList<>();
// Node(int i){
// ind =i;
// }
// }
// static void dfs(Node[] Nodes, Node ind, Node parent){
// if(parent!=null){
// ind.parent.remove(parent);
// int x = parent.tot;
// if(parent.ind>ind.ind){
// x++;
// }
// ind.tot = Math.max(x, ind.tot);
// }
// if(ind.parent.isEmpty()){
// for(Node n: ind.child){
// dfs(Nodes, n, ind);
// }
// }
//
// }
static int maxHeight(List<Integer> wallPositions, List<Integer> wallHeights){
int ans = 0;
int n = wallHeights.size();
for(int i=1;i<n;i++){
int ind1 = wallPositions.get(i-1);
int ind2 = wallPositions.get(i);
if(ind2-ind1==1) continue;
int x = wallHeights.get(i-1);
int y = wallHeights.get(i);
int index = (y-x+ind1+ind2)/2;
if(index<=ind1){
index = ind1+1;
}
else if(index>=ind2){
index = ind2-1;
}
ans = Math.max(ans, Math.min(x+(index-ind1),y+(ind2-index)));
}
return ans;
}
// 3
// 2 1 1
// 2 3 11
// 3 4 1
// 4
// 2
public static ArrayList<Integer> primeFactors(int n)
{
// Print the number of 2s that divide n
ArrayList<Integer> al = new ArrayList<>();
while (n%2==0)
{
al.add(2);
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
al.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
al.add(n);
return al;
}
static void bfs(Graph[] g, int ind, boolean vis[], ArrayList<Node> al, Set<Integer> set){
vis[ind] = true;
set.add(ind);
for(int i: g[ind].pq){
if(!vis[i]) bfs(g,i,vis,al,set);
}
g[ind].set = set;
}
// static class tempSort implements Comparator<Node> {
// // Used for sorting in ascending order of
// // roll number
// public int compare(Node a, Node b) {
// return a.size - b.size;
// }
// }
static long divide(long p, long q, long mod)
{
long expo = mod - 2;
while (expo != 0)
{
if ((expo & 1) == 1)
{
// long temp = p;
// System.out.println("zero--> "+temp+" "+q);
p = (p * q) % mod;
// if(p<0){
// System.out.println("one--> "+temp+" "+q);
// }
}
q = (q * q) % mod;
// if(q<0){
// System.out.println("two--> "+p+" "+q);
// }
expo >>= 1;
}
return p;
}
static class Graph{
int ind;
ArrayList<Integer> pq = new ArrayList<>();
Set<Integer> set;
boolean b = false;
public Graph(int a){
ind = a;
}
}
//
// static class Pair{
// int a=0;
// int b=0;
// int in = 0;
// int ac = 0;
// int ex = 0;
// }
long fun2(ArrayList<Integer> arr, int x){
ArrayList<ArrayList> al = new ArrayList<>();
ArrayList<Integer> curr = new ArrayList<>();
fun(arr, x, al, curr, 0);
if(al.size()==0) return 0;
int max = 0;
for(ArrayList<Integer> i: al){
if(i.size()>max) max = i.size();
}
for(int i=0;i<al.size();i++){
if(al.get(i).size()!=max){
al.remove(i);
i--;
}
}
for(ArrayList<Integer> i: al){
Collections.sort(al, Collections.reverseOrder());
}
long ans = 0;
for(ArrayList<Integer> i: al){
long temp = 0;
for(int j: i){
temp*=10;
temp+=j;
}
if(ans<temp) ans = temp;
}
return ans;
}
void fun(ArrayList<Integer> arr, int x, ArrayList<ArrayList> al, ArrayList<Integer> curr, int i){
if(x<0) return ;
if(x==0) {
al.add(curr);
return;
}
for(int j=i;j<arr.size();j++){
ArrayList<Integer> temp = new ArrayList<>(curr);
fun(arr, x-arr.get(j), al, temp, j);
}
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return (long)power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
int p, long[] fac)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long x = modInverse(fac[r], p);
long y = modInverse(fac[n - r], p);
return (fac[n] * x
% p * y
% p)
% p;
}
static long[] sum(String[] str){
int n = str[0].length();
long ans[] = new long[n];
for(String s: str){
for(int i=0;i<n;i++) ans[i]+=s.charAt(i);
}
return ans;
}
// static class tSort implements Comparator<Pair>{
//
// public int compare(Pair s1, Pair s2) {
// if (s1.b < s2.b)
// return -1;
// else if (s1.b > s2.b)
// return 1;
// return 0;
// }
// }
// static boolean checkCycle(Tree[] arr, boolean[] visited, int curr, int Node){
// if(curr==Node && visited[curr]) return true;
// if(visited[curr]) return false;
// visited[curr] = true;
// for(int i: arr[curr].al){
// if(checkCycle(arr, visited, i, Node)) return true;
// }
// return false;
// }
// static boolean allCombinations(int n){ //Global round 15
// int three2n = 1;
// for (int i = 1; i <= n; i++)
// three2n *= 3;
//
// for (int k = 1; k < three2n; k++) {
// int k_cp = k;
// int sum = 0;
// for (int i = 1; i <= n; i++) {
// int s = k_cp % 3;
// k_cp /= 3;
// if (s == 2) s = -1;
// sum += s * a[i];
// }
// if (sum == 0) {
// return true;
// }
// }
// return false;
// }
static ArrayList<String> fun( int curr, int n, char c){
int len = n-curr;
if(len==0) return null;
ArrayList<String> al = new ArrayList<>();
if(len==1){
al.add(c+"");
return al;
}
String ss = "";
for(int i=0;i<len/2;i++){
ss+=c;
}
ArrayList<String> one = fun(len/2+curr, n, (char)(c+1));
for(String str: one){
al.add(str+ss);
al.add(ss+str);
}
return al;
}
static ArrayList convert(int x, int k){
ArrayList<Integer> al = new ArrayList<>();
if(x>0) {
while (x > 0) {
al.add(x % k);
x /= k;
}
}
else al.add(0);
return al;
}
static int max(int x, int y, int z){
int ans = Math.max(x,y);
ans = Math.max(ans, z);
return ans;
}
static int min(int x, int y, int z){
int ans = Math.min(x,y);
ans = Math.min(ans, z);
return ans;
}
// static long treeTraversal(Tree arr[], int parent, int x){
// long tot = 0;
// for(int i: arr[x].al){
// if(i!=parent){
// tot+=treeTraversal(arr, x, i);
// }
// }
// arr[x].child = tot;
// if(arr[x].child==0) arr[x].child = 1;
// return tot+1;
// }
public static int primeFactors(int n, int k)
{
int ans = 0;
while (n%2==0)
{
ans++;
if(ans>=k) return k;
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
while (n%i == 0)
{
ans++;
n /= i;
if(ans>=k) return k;
}
}
if (n > 2) ans++;
return ans;
}
static int binaryLow(ArrayList<Integer> arr, int x, int s, int e){
if(s>=e){
if(arr.get(s)>=x) return s;
else return s+1;
}
int m = (s+e)/2;
if(arr.get(m)==x) return m;
if(arr.get(m)>x) return binaryLow(arr,x,s,m);
if(arr.get(m)<x) return binaryLow(arr,x,m+1,e);
return 0;
}
static int binaryLow(int[] arr, int x, int s, int e){
if(s>=e){
if(arr[s]>=x) return s;
else return s+1;
}
int m = (s+e)/2;
if(arr[m]==x) return m;
if(arr[m]>x) return binaryLow(arr,x,s,m);
if(arr[m]<x) return binaryLow(arr,x,m+1,e);
return 0;
}
static int binaryHigh(int[] arr, int x, int s, int e){
if(s>=e){
if(arr[s]<=x) return s;
else return s-1;
}
int m = (s+e)/2;
if(arr[m]==x) return m;
if(arr[m]>x) return binaryHigh(arr,x,s,m-1);
if(arr[m]<x) return binaryHigh(arr,x,m+1,e);
return 0;
}
// static void arri(int arr[], int n, Reader sc) throws IOException{
// for(int i=0;i<n;i++){
// arr[i] = sc.nextInt();
// }
// }
// static void arrl(long arr[], int n, Reader sc) throws IOException{
// for(int i=0;i<n;i++){
// arr[i] = sc.nextLong();
// }
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res%p;
}
// static class SortbyI implements Comparator<Pair> {
// // Used for sorting in ascending order of
// // roll number
// public int compare(Pair a, Pair b)
// {
// if(a.a>=b.a) return 1;
// else return -1;
// }
// }
// static class SortbyD implements Comparator<Pair> {
// // Used for sorting in ascending order of
// // roll number
// public int compare(Pair a, Pair b)
// {
// if(a.a<b.a) return 1;
// else if(a.a==b.b && a.b>b.b) return 1;
// else return -1;
// }
// }
// static int binarySearch(ArrayList<Pair> a, int x, int s, int e){
// if(s>=e){
// if(x<=a.get(s).b) return s;
// else return s+1;
// }
// int mid = (e+s)/2;
// if(a.get(mid).b<x){
// return binarySearch(a, x, mid+1, e);
// }
// else return binarySearch(a,x,s, mid);
// }
// static class Edge{
// int a;
// int b;
// int c;
// int sec;
// Edge(int a, int b, int c, int sec){
// this.a = a;
// this.b = b;
// this.c = c;
// this.sec = sec;
// }
//
// }
static class Tree{
int a;
ArrayList<Tree> al = new ArrayList<>();
Tree(int a){
this.a = a;
}
}
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();
}
}
static boolean isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 ||
n % 3 == 0)
return false;
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return false;
return true;
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
ArrayList<Integer> al = new ArrayList<>();
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
al.add(i);
}
return al;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
9f952ed58545db089c1196af152b3b01
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package com.shroom;
import java.util.*;
public class dec {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-->0) {
LinkedList<Character> ll = new LinkedList<>();
int n = in.nextInt();
int m = in.nextInt();
int[] h = new int[m * n];
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a = Math.abs(i-1) + Math.abs(j-1);
b = Math.abs(i - n) + Math.abs(j-1);
c = Math.abs(i-1) + Math.abs(j - m);
d = Math.abs(i - n) + Math.abs(j - m);
h[cnt] = Math.max(Math.max(a, b), Math.max(c, d));
cnt++;
}
}
Arrays.sort(h);
for (int j : h) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
051a3df55724f010736b9fd8db1de10d
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class B_Not_Sitting {
static class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
InputReader sc = new InputReader(inputStream);
int t = sc.nextInt();
while (--t >= 0) {
// Write your code here
int r = sc.nextInt();
int c = sc.nextInt();
boolean[][] visited = new boolean[r][c];
Queue<Node> q = new LinkedList<Node>();
List<Integer> xs = new ArrayList<Integer>();
List<Integer> ys = new ArrayList<Integer>();
xs.add(r/2);
ys.add(c/2);
if(r % 2 == 0){
xs.add(r/2 -1);
}
if(c % 2 == 0){
ys.add(c/2 -1);
}
for(int i = 0; i < xs.size(); i++){
for(int j = 0 ; j < ys.size() ; j++){
q.add(new Node(xs.get(i),ys.get(j)));
visited[xs.get(i)][ys.get(j)] = true;
}
}
int[][] dirs = {{-1,0},{1,0},{0,1},{0,-1}};
List<Integer> ans = new ArrayList<Integer>();
int val = r/2 + c/2;
while(q.size() > 0){
int size = q.size();
for(int i = 0; i < size; i++){
Node cur = q.poll();
ans.add(val);
for(int[] dir : dirs){
int nx = cur.x + dir[0];
int ny = cur.y + dir[1];
if(nx >= 0 && ny >= 0 && nx < r && ny < c && !visited[nx][ny]){
q.add(new Node(nx, ny));
visited[nx][ny] = true;
}
}
}
val += 1;
}
for(int k : ans){
System.out.print(k+" ");
}
System.out.println();
}
}
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 double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public char nextChar() {
return next().charAt(0);
}
public String nextLine() throws IOException {
return reader.readLine().trim();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
7721a9a702b0d332bccf20b4bdc5c220
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class NotSit {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String nm[] = br.readLine().split(" ");
int n = Integer.parseInt(nm[0]);
int m = Integer.parseInt(nm[1]);
int val = n / 2 + m / 2;
if (n > m) {
int temp = n;
n = m;
m = temp;
}
int rh = n / 2, ch = m / 2;
if (n % 2 != 0)
rh++;
if (m % 2 != 0)
ch++;
int ar[] = new int[n * m];
int centre = 0;
int idx = 0;
if (n % 2 == 0 && m % 2 == 0) {
for (int i = 0; i < 4; i++) {
ar[idx] = val;
idx++;
}
centre += 4;
} else if (n % 2 == 0) {
for (int i = 0; i < 2; i++) {
ar[idx] = val;
idx++;
}
centre += 2;
} else if (m % 2 == 0) {
for (int i = 0; i < 2; i++) {
ar[idx] = val;
idx++;
}
centre += 2;
} else {
ar[idx] = val;
idx++;
centre += 1;
}
rh--;
ch--;
val++;
int times = 0;
while (rh > 0 && ch > 0) {
if (centre != 0) {
if (centre == 1) {
times = 4;
} else if (centre == 2) {
times = 6;
} else if (centre == 4) {
times = 8;
}
centre = 0;
}
for (int i = 0; i < times; i++) {
ar[idx] = val;
idx++;
}
times += 4;
val++;
rh--;
ch--;
}
while (ch > 0) {
times = 2 * n;
for (int i = 0; i < times; i++) {
ar[idx] = val;
idx++;
}
val++;
ch--;
}
int left = (n * m) - idx;
if (left > 0) {
ArrayList<Integer> list = new ArrayList<>();
int rem = 4;
while (left != 0) {
left -= rem;
list.add(rem);
rem += 4;
}
Collections.reverse(list);
for (int x : list) {
for (int i = 0; i < x; i++) {
ar[idx] = val;
idx++;
}
val++;
}
}
for (int i = 0; i < n * m; i++) {
bw.write(ar[i] + " ");
}
bw.newLine();
}
bw.flush();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
2af1317d8667e504f7288896bc5d29a8
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();
ArrayList<Integer>ans=new ArrayList<>();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int x=Math.abs(i-1)+Math.abs(j-1);
x=Math.max(x,Math.abs(i-1)+Math.abs(j-m));
x=Math.max(x,Math.abs(n-i)+Math.abs(j-1));
x=Math.max(x,Math.abs(i-n)+Math.abs(j-m));
ans.add(x);
}
}
Collections.sort(ans);
for(int val:ans){
System.out.print(val+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
2d85ccdf0329d3532bb8b2f63a1207ff
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class aj{
public static void main(String args[])
{
int t;
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
while(t>0)
{int n,m;
n=sc.nextInt();
m=sc.nextInt();
List<Integer> al=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{for(int j=0;j<m;j++)
{
Integer d=Math.max(i,n-1-i)+Math.max(j,m-1-j);
al.add(d);
}}
Collections.sort(al);
for (Integer s : al) {
System.out.print(s +" ");}
System.out.println();
t--;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
5cfb8628b1bdc07f2794aa2577b52afe
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int t=scn.nextInt();
while(t--!=0)
{
int n=scn.nextInt();
int m=scn.nextInt();
int[] arr=new int[n*m];
int a=0,b=0,c=0,d=0, ctr=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a=Math.abs(i-1)+Math.abs(j-1);
b=Math.abs(i-n)+Math.abs(j-1);
c=Math.abs(i-1)+Math.abs(j-m);
d=Math.abs(i-n)+Math.abs(j-m);
arr[ctr]=Math.max(Math.max(a,b), Math.max(c,d));
ctr++;
}
}
Arrays.sort(arr);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d60c96f87855b293373e14dfc80f5a61
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.math.*;
import java.util.*;
import java.io.*;
public class B766 {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static void solve(int n, int m) {
int a, b,c,d;
int arr[]=new int[n*m];
int cnt=0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a=Math.abs(i-1)+Math.abs(j-1);
b=Math.abs(i-n)+Math.abs(j-1);
c=Math.abs(i-1)+Math.abs(j-m);
d=Math.abs(i-n)+Math.abs(j-m);
arr[cnt]=Math.max(Math.max(a,b), Math.max(c,d));
cnt++;
}
}
Arrays.sort(arr);
for (int i = 0; i < n * m; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int m=sc.nextInt();
solve(n, m);
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
10db82f4300320ac07647a14de4bbf11
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.util.StringTokenizer;
public class Solve_766_div_2 {
public static void main(String[] args) {
var io = new Kattio(System.in, System.out);
int t = io.nextInt();
for(int i=0; i<t; i++){
solve_B(io);
}
io.close();
}
public static void solve_A(Kattio io){
int n, m, r, c;
n = io.nextInt();
m = io.nextInt();
r = io.nextInt()-1;
c = io.nextInt()-1;
boolean hasBlack = false;
boolean[][] grid = new boolean[n][m];
for(int i=0; i<n; i++){
String line = io.next();
for(int j=0; j<m; j++){
grid[i][j] = (line.charAt(j) == 'B');
hasBlack |= grid[i][j];
}
}
if(!hasBlack){
io.println(-1);
}
else if(grid[r][c]){
io.println(0);
}
else{
boolean flag = false;
for(int i=0; i<m; i++){
flag |= grid[r][i];
}
for(int i=0; i<n; i++){
flag |= grid[i][c];
}
if(flag){
io.println(1);
}
else{
io.println(2);
}
}
}
public static void solve_B(Kattio io){
int n = io.nextInt();
int m = io.nextInt();
int[] dist = new int[n*m];
int index = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
dist[index++] = cnt_dist(i, j, n, m);
}
}
quickSort(dist, 0, index-1);
for(int i=0; i<index; i++){
io.print(dist[i]+" ");
}
io.println();
}
public static int cnt_dist(int x, int y, int n, int m){
int dist = 0;
int x1 = 1, y1 = 1;
int x2 = 1, y2 = m;
int x3 = n, y3 = 1;
int x4 = n, y4 = m;
int d1 = 0, d2 = 0, d3 = 0, d4 = 0;
if(n>1){
d3 = Math.abs(x3-x)+Math.abs(y3-y);
}
if(m>1){
d2 = Math.abs(x2-x)+Math.abs(y2-y);
}
if(n>1 && m>1){
d4 = Math.abs(x4-x)+Math.abs(y4-y);
}
d1 = Math.abs(x1-x)+Math.abs(y1-y);
dist = Math.max(Math.max(d1, d2), Math.max(d3, d4));
return dist;
}
public static void quickSort(int[] arr,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
//temp就是基准位
temp = arr[low];
while (i<j) {
//先看右边,依次往左递减
while (temp<=arr[j]&&i<j) {
j--;
}
//再看左边,依次往右递增
while (temp>=arr[i]&&i<j) {
i++;
}
//如果满足条件则交换
if (i<j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = temp;
//递归调用左半数组
quickSort(arr, low, j-1);
//递归调用右半数组
quickSort(arr, j+1, high);
}
public static void solve_C(Kattio io){
}
public static void solve_D(Kattio io){
}
public static void solve_E(Kattio io){
}
public static void solve_F(Kattio io){
}
static class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
38ff6a77aa5466ae7e6c56059b83115e
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
static long m = 998244353;
// public static class Pair
// {
// long a;
// long b;
// Pair(long a , long b)
// {
// this.a=a;
// this.b=b;
// }
// }
public static void main (String[] args) throws java.lang.Exception
{
long t = sc.nextLong();
while(t-- >0)
{
long n = sc.nextLong();
long m = sc.nextLong();
// ArrayList<Pair> arr = new ArrayList<>();
//
// arr.add(new Pair(1,1));
// arr.add(new Pair(1,m));
// arr.add(new Pair(n,1));
// arr.add(new Pair(n,m));
ArrayList<Long> ans = new ArrayList<>();
for( int i=1 ; i<=n ; i++)
{
for(int j=1 ; j<=m ; j++)
{
long a = Math.abs(1-i)+Math.abs(1-j);
long b = Math.abs(1-i)+Math.abs(m-j);
long c = Math.abs(n-i)+Math.abs(1-j);
long d = Math.abs(n-i)+Math.abs(m-j);
long max1 = Math.max(a, b); long max2 = Math.max(c, d);
long max = Math.max(max1, max2);
ans.add(max);
}
}
Collections.sort(ans);
for(int i=0 ; i<ans.size(); i++)
{
out.print(ans.get(i)+" ");
}
out.println();
}
out.flush();
}
static long gcd(long a , long b)
{
if(b==0) return a;
return gcd(b,a%b);
}
static long lcm(long a , long b)
{
return (a*b)/gcd(a,b);
}
static long fastPower(long a , long b )
{
long res=1;
while(b>0)
{
if((b&1)!=0)
{
res = (res%m * a%m)%m;
}
a = (a%m * a%m)%m;
b=b>>1;
}
return res;
}
static long modexp(long x, long n)
{
if (n == 0) {
return 1;
}
else if (n % 2 == 0) {
return modexp((x * x) % m, n / 2);
}
else {
return (x * modexp((x * x) % m, (n - 1) / 2) % m);
}
}
static long getFractionModulo(long a, long b)
{
long c = gcd(a, b);
a = a / c;
b = b / c;
long d = modexp(b, m - 2);
long ans = ((a % m) * (d % m)) % m;
return ans;
}
public static long power(long x, long y)
{
long temp;
if (y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return modMult(temp,temp);
else {
if (y > 0)
return modMult(x,modMult(temp,temp));
else
return (modMult(temp,temp)) / x;
}
}
static long modMult(long a,long b) {
return a*b%m;
}
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
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
3353c2236343a6d37eda05ae9509fe66
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class taskb {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
taskb sol = new taskb();
sol.solve(in, out);
}
out.flush();
}
void solve(FastScanner in, PrintWriter out) {
int n = in.nextInt(), m = in.nextInt();
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int res = 0;
res = Math.max(res, i + j);
res = Math.max(res, i + Math.abs(m - 1 - j));
res = Math.max(res, Math.abs(n - 1 - i) + j);
res = Math.max(res, Math.abs(n - 1 - i) + Math.abs(m - 1 - j));
a.add(res);
}
}
Collections.sort(a);
for (int i = 0; i < n * m; i++) {
out.printf("%d ", a.get(i));
}
out.println("");
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
float nextFloat() { return Float.parseFloat(next()); }
double nextDouble() { return Double.parseDouble(next()); }
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
07a33ca1ec8a10ae23fe46a1feba3e60
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class Bwecanwin {
private static class pair{
int low,high;
pair(int low,int high){
this.low=low;this.high=high;
}
}
static int dist(int i,int j,int a,int b)
{ return Math.abs(i-a)+Math.abs(j-b);
}
static boolean issafe(int i,int j,int n,int m)
{
return i>=0 &&i<n && j>=0&&j<m;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- != 0) {int n=in.nextInt();int m=in.nextInt();
ArrayList<Integer>v=new ArrayList<>();
boolean vis[][]=new boolean[n][m];
Queue <pair>q=new LinkedList<>();
if(n%2==1 && m%2==1)
{
q.add(new pair(n/2,m/2));vis[n/2][m/2]=true;
}else if(n%2==1 && m%2==0)
{
q.add(new pair(n/2,m/2));
q.add(new pair(n/2,m/2 -1));
vis[n/2][m/2]=true;vis[n/2][m/2-1]=true;
}
else if(n%2==0 && m%2==1)
{
q.add(new pair(n/2,m/2));
q.add(new pair(n/2-1,m/2 ));
vis[n/2][m/2]=true;vis[n/2-1][m/2]=true;
}else{
q.add(new pair(n/2,m/2));
q.add(new pair(n/2-1,m/2 ));
vis[n/2][m/2]=true;vis[n/2-1][m/2]=true;
q.add(new pair(n/2-1,m/2-1));
q.add(new pair(n/2,m/2 -1));
vis[n/2-1][m/2-1]=true;vis[n/2][m/2-1]=true;
}
while(!q.isEmpty())
{
v.add(q.size());
int x=q.size();
for(int i=0;i<x;i++)
{pair arc=q.remove();int a=arc.low;int b=arc.high;
if(issafe(a+1,b,n,m)&&!vis[a+1][b])
{
q.add(new pair(a+1,b));vis[a+1][b]=true;
}
if(issafe(a-1,b,n,m)&&!vis[a-1][b])
{
q.add(new pair(a-1,b));vis[a-1][b]=true;
}
if(issafe(a,b+1,n,m)&&!vis[a][b+1])
{
q.add(new pair(a,b+1));vis[a][b+1]=true;
}
if(issafe(a,b-1,n,m)&&!vis[a][b-1])
{
q.add(new pair(a,b-1));vis[a][b-1]=true;
}
}
}
int x=n/2+m/2;
for(int i=0;i<v.size();i++)
{
for(int j=0;j<v.get(i);j++)
{
System.out.print(x+" ");
}x++;}
System.out.println();
}
}}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
3c7d21e7f9f02039bf49eca9d714ecb2
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Bwecanwin {
static int dist(int i,int j,int a,int b)
{ return Math.abs(i-a)+Math.abs(j-b);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- != 0) {int n=in.nextInt();int m=in.nextInt();
PriorityQueue<Integer>pq=new PriorityQueue<>();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int d1=dist(0,0,i,j); int d2=dist(0,m-1,i,j); int d3=dist(n-1,0,i,j);
int d4=dist(n-1,m-1,i,j);
pq.add(Math.max(Math.max(d1,d2),Math.max(d3,d4)));
}
}
for(int i=0;i<n*m-1;i++)
{
System.out.print(pq.poll()+" ");
}
System.out.print(pq.poll());
System.out.println();
}
}}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
988ad964c59a5c895f717b2eef162508
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
1. 枚举k,观察到k是0 ~ n * m - 1
2. 观察结果发现最远的距离是5,即是图中最远的距离(4 - 1 + 3 - 2)
想想一下如果是一维数组,如何安排,如果Rahul的距离是确定的,那么T要走到最远距离,理论上Rahul会走到所有的距离
因为摆放不能走的地方是可以枚举的,比如k = n * m - 1,也就是比
3. 枚举Rahul的位置,找到T最远的距离
5. 然后排序即可
* */
public class B {
static int n, m;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while (T-- > 0) {
n = sc.nextInt();
m = sc.nextInt();
solver();
}
}
static void solver() {
List<Integer> list = new ArrayList<>();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
int max = Math.abs(i - 1) + Math.abs(j - m);
max = Math.max(max, Math.abs(i - 1) + Math.abs(j - 1));
max = Math.max(max, Math.abs(i - n) + Math.abs(j - 1));
max = Math.max(max, Math.abs(i - n) + Math.abs(j - m));
list.add(max);
}
}
Collections.sort(list);
for(int i : list) System.out.print(i + " ");
System.out.println();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
69bb9ba75969af8b74dc76c11e189a52
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*; //PrintWriter
import java.math.*; //BigInteger, BigDecimal
import java.util.*; //StringTokenizer, ArrayList
public class R766_Div2_B
{
FastReader in;
PrintWriter out;
public static void main(String[] args) {
new R766_Div2_B().run();
}
void run()
{
in = new FastReader(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void solve()
{
int t = in.nextInt();
for (int T = 0; T < t; T++) {
int n = in.nextInt();
int m = in.nextInt();
int[] d = new int[n*m];
int d1,d2,d3,d4, maxd = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
d1 = i + j;
d2 = i + (m-1 - j);
d3 = (n-1 - i) + j;
d4 = (n-1 - i) + (m-1 - j);
maxd = d1;
maxd = Math.max(d2, maxd);
maxd = Math.max(d3, maxd);
maxd = Math.max(d4, maxd);
d[i*m+j] = maxd;
}
Arrays.sort(d);
for (int i = 0; i < n*m; i++)
out.print(d[i] + " ");
out.println();
}
}
//-----------------------------------------------------
void runWithFiles() {
in = new FastReader(new File("input.txt"));
try {
out = new PrintWriter(new File("output.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
solve();
out.close();
}
class FastReader
{
BufferedReader br;
StringTokenizer tokenizer;
public FastReader(InputStream stream)
{
br = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public FastReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
tokenizer = null;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
try {
return br.readLine();
}
catch(Exception e) {
throw(new RuntimeException());
}
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
929082bd498daa1a5ef42d7d3a90d458
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
long t = Long.parseLong(br.readLine());
while (t-- != 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
//
long n = Long.parseLong(st.nextToken());
long m = Long.parseLong(st.nextToken());
//
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < n ; i++) {
for (int j = 0; j < m; j++) {
// i is distance of cell from top left corner
// n-i-1 is the distance of cell from bottom left corner
// j is distance of cell from top right corner
// n-j-1 is distance of cell from bottom right corner
arr.add(Math.max(i,n-i-1)+Math.max(j,m-j-1));
}
}
Collections.sort(arr);
for (int i = 0; i < n*m ; i++) {
pr.print(arr.get(i)+" ");
}
pr.println();
}
pr.close();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
14b0247ff7477941e006e311311a9af9
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n=s.nextInt();
int m=s.nextInt();
int total= 0;
int si= 0, ei=4;
int size=0;
if(n%2==0&&m%2==0) {
si=4;
size= (n+m)/2-1;
}
if(n%2!=0&&m%2!=0) {
si=1;
size=(n+m)/2;
}
if(n%2==0&&m%2!=0||n%2!=0&&m%2==0)
{
si=2;
size= (n+m)/2;
}
int arr[]=new int[size];
int k= Math.min((n+1)/2, (m+1)/2);
int currsum=n*m;
for(int i=0;i<k;i++)
{
arr[i]= si;
currsum-=arr[i];
if(si==1)
si=4;
else
si+=4;
}
int q= Math.min(n/2, m/2);
int ind=size-1;
for(int i=0;i<q;i++)
{
arr[ind]= ei;
ei+=4;
currsum-=arr[ind];
ind--;
}
// if(currsum!=0) {
int zeroes=0;
for(int i=0;i<size;i++)
{
if(arr[i]==0)
zeroes++;
}
if(zeroes!=0) {
int val= currsum/zeroes;
for(int i=0;i<size;i++)
{
if(arr[i]==0)
arr[i]=val;
}
}
int p= m/2+n/2;
for(int i=0;i<size;i++)
{
for(int j=1;j<=arr[i];j++ )
{
System.out.print(p+" ");
}
p++;
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
6a4e847d8ca099cf0e4527cbb92d9a73
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(sc.readLine().trim());
for (int h = 0; h < t; h++) {
String sp[] = sc.readLine().split("\\ ");
int n = Integer.parseInt(sp[0]);
int m = Integer.parseInt(sp[1]);
ArrayList<Integer> ll = new ArrayList<>();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int g;
g = (i - 1) + (j - 1);
if (g < ((i - 1) + (m - j))) {
g = ((i - 1) + (m - j));
}
if (g < ((n - i) + (m - j))) {
g = ((n - i) + (m - j));
}
if (g < ((n - i) + (j - 1))) {
g = ((n - i) + (j - 1));
}
ll.add(g);
}
}
Collections.sort(ll);
for (int kl = 0; kl < ll.size(); kl++) {
System.out.print(ll.get(kl) + " ");
}
System.out.println("");
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
07c1ac7d165b65dc9593fa9995763bc2
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n=i();
int m=i();
int x=(n+1)/2;
int y=(m+1)/2;
int dist=Math.max(x+y-2,Math.abs(n-x)+Math.abs(m-y));
Queue<Pair> q=new LinkedList<>();
if(x==y){
q.add(new Pair(x,y,dist));
}
else{
q.add(new Pair(x,y,dist));
q.add(new Pair(y,x,dist));
}
boolean[][] vis=new boolean[n+1][m+1];
int pr=0;
ArrayList<Integer> list=new ArrayList<>();
while(q.size()>0){
int size=q.size();
while(size-->0){
Pair p=q.remove();
if(p.x<=0||p.x>n||p.y<=0||p.y>m)continue;
if(vis[p.x][p.y])continue;
dist=Math.max(p.x+p.y-2,Math.abs(n-p.x)+Math.abs(m-p.y));
dist=Math.max(Math.abs(1-p.x)+Math.abs(m-p.y),Math.max(Math.abs(n-p.x)+Math.abs(1-p.y),dist));
if(pr<n*m)list.add(dist);
pr++;
q.add(new Pair(p.x+1,p.y,p.dist+1));
q.add(new Pair(p.x-1,p.y,p.dist+1));
q.add(new Pair(p.x,p.y+1,p.dist+1));
q.add(new Pair(p.x,p.y-1,p.dist+1));
vis[p.x][p.y]=true;
}
}
Collections.sort(list);
for(int c1:list)sb.append(c1+" ");
sb.append("\n");
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
int y;
int dist;
Pair(int x, int y,int dist) {
this.x = x;
this.y = y;
this.dist=dist;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
78549045d2ef2a3c0c1d1275f156b74c
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.Scanner;
public class Solution {
public static int a[] = new int [111111];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int g = 0; g < t; g++) {
int n = sc.nextInt();
int m = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int dist = Math.min(Math.abs(i - (n + 1) / 2), Math.abs(i - (n / 2 + 1))) + Math.min(Math.abs(j - (m + 1) / 2), Math.abs(j - (m / 2 + 1)));
a[dist]++;
}
}
int baseDist = n / 2 + m / 2;
for (int i = 0; i < n * m; i++) {
for (int j = 0; j < a[i]; j++) {
System.out.print(baseDist + i + " ");
}
a[i] = 0;
}
System.out.println();
/* int baseA = 2 - n % 2;
int baseB = 2 - m % 2;
int r = baseA;
int c = baseB;
int baseDist = n / 2 + m / 2;
int i = 0;
while (i < n * m) {
int A = c > m ? 0 : baseA;
int B = r > n ? 0 : baseB;
int centerCount = (i == 0 ? A * B : (A + B) * 2);
for (int j = 0; j < centerCount; j++) {
i++;
System.out.print(baseDist + " ");
}
baseDist++;
int C = r > n ? 0 : 2;
int D = c > m ? 0 : 2;
int count = c > m ? (m - baseB) / 2 : (Math.min(r, n) - baseA) / 2;
for (int j = 0; j < count - 1; j++) {
for (int z = 0; z < (C + D) * 2; z++) {
i++;
System.out.print(baseDist + " ");
}
baseDist++;
}
if (count > 0) {
for (int j = 0; j < 4; j++) {
i++;
System.out.print(baseDist + " ");
}
baseDist++;
}
r += 2;
c += 2;
}
System.out.println();*/
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d8b950a1a1e93595517be3fcf293f173
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
static int[] dx = new int[] {0, 0, -1, 1};
static int[] dy = new int[] {-1, 1, 0, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = Integer.parseInt(br.readLine());
while(t --> 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
ArrayList<Integer> sort = new ArrayList<Integer>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
sort.add(Math.max(i+j, Math.max(i+(m-j-1), Math.max(j+(n-i-1), (m-j-1)+(n-i-1)))));
}
}
Collections.sort(sort);
for(Integer i : sort)
pw.print(i + " ");
pw.println();
}
pw.close();
}
static class Pair{
int a, b, c;
public Pair(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
8c25375138c9a0e99ec48592a894c3bf
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class quesB {
public static class Pair {
int r;
int c;
int d;
Pair(int r,int c,int d) {
this.r = r;
this.c = c;
this.d = d;
}
Pair() {
}
}
public static void main(String[] args) throws Exception {
// String[] parts=br.readLine().split(" ");
// int n=Integer.parseInt(parts[0]);
// int k=Integer.parseInt(parts[1]);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tests = Integer.parseInt(br.readLine());
for (int test = 1;test <= tests;test++) {
String[] parts = br.readLine().split(" ");
int n = Integer.parseInt(parts[0]);
int m = Integer.parseInt(parts[1]);
LinkedList<Pair> que = new LinkedList<>();
boolean[][] vis = new boolean[n][m];
if(n % 2 == 0 && m % 2 == 0) {
int dis = n / 2 + m / 2;
que.add(new Pair((n-1)/ 2, (m-1)/2, dis));
que.add(new Pair((n-1)/ 2, (m)/2, dis));
que.add(new Pair((n)/ 2, (m-1)/2, dis));
que.add(new Pair((n)/ 2, (m)/2, dis));
// vis[(n-1)/ 2][(m-1)/ 2] = true;
// vis[(n-1)/ 2][(m)/ 2] = true;
// vis[n/ 2][(m-1)/ 2] = true;
// vis[n/ 2][m/ 2] = true;
}else if(n % 2 == 0 && m % 2 != 0) {
int dis = (n / 2) + Math.abs((m-1) - m/2);
que.add(new Pair((n-1)/ 2, (m)/2, dis));
que.add(new Pair((n)/ 2, (m)/2, dis));
// vis[(n-1)/ 2][(m)/ 2] = true;
// vis[n/ 2][m/ 2] = true;
}else if(n % 2 != 0 && m % 2 == 0) {
int dis = (n / 2) + m/2;
que.add(new Pair((n)/ 2, (m-1)/2, dis));
que.add(new Pair((n)/ 2, (m)/2,dis));
// vis[n/ 2][(m-1)/ 2] = true;
// vis[n/ 2][m/ 2] = true;
}else {
int dis = (n / 2) + Math.abs((m-1) - m/2);
que.add(new Pair((n-1)/ 2, (m-1)/2, dis));
// vis[(n-1)/ 2][(m-1)/ 2] = true;
}
StringBuilder sb = new StringBuilder();
int[][] dir = {{0,-1},{-1,0},{1,0},{0,1}};
int[] ans = new int[n * m];
int k = 0;
while(que.size() > 0) {
int s = que.size();
for(int i = 0;i < s;i++) {
Pair rem = que.removeFirst();
if(vis[rem.r][rem.c]) continue;
vis[rem.r][rem.c] = true;
ans[k] = rem.d;
k++;
for(int d = 0;d < 4;d++) {
int ni = rem.r + dir[d][0];
int nj = rem.c + dir[d][1];
if(ni >= 0 && nj >= 0 && ni < n && nj < m) {
// vis[ni][nj] = true;
que.addLast(new Pair(ni,nj,rem.d + 1));
}
}
}
}
for(int val : ans) {
sb.append(val).append(" ");
}
sb.append("\n");
System.out.print(sb);
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d5183d824670058c86fc8727525f3094
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class B {
private static StringTokenizer st;
private static BufferedReader br;
private static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = nextInt();
// t = 1;
while (t-- > 0) {
t_main();
}
pw.close();
}
private static void t_main() throws IOException {
int n = nextInt();
int m = nextInt();
int[][] a = new int[n][m];
int mn = n / 2 + m / 2;
Queue<Point> q = new LinkedList<>();
if (n % 2 > 0) {
if (m % 2 > 0) {
q.add(new Point(n/2, m/2));
a[n/2][m/2] = mn;
} else {
q.add(new Point(n/2, (m-1)/2));
q.add(new Point(n/2, m/2));
a[n/2][m/2] = mn;
a[n/2][(m-1)/2] = mn;
}
} else {
if (m % 2 > 0) {
q.add(new Point((n-1)/2, m/2));
q.add(new Point(n/2, m/2));
a[n/2][m/2] = mn;
a[(n-1)/2][m/2] = mn;
} else {
q.add(new Point((n-1)/2, m/2));
q.add(new Point((n-1)/2, (m-1)/2));
q.add(new Point(n/2, m/2));
q.add(new Point(n/2, (m-1)/2));
a[(n-1)/2][(m-1)/2] = mn;
a[n/2][(m-1)/2] = mn;
a[n/2][m/2] = mn;
a[(n-1)/2][m/2] = mn;
}
}
int[] dx = {0, -1, 0, 1};
int[] dy = {1, 0, -1, 0};
for (int i = 0; i < n * m; i++) {
Point p = q.remove();
pw.print(a[p.x][p.y]);
pw.print(" ");
for (int j = 0; j < 4; j++) {
if (p.x+dx[j] < 0 || p.x+dx[j] >= n) continue;
if (p.y+dy[j] < 0 || p.y+dy[j] >= m) continue;
if (a[p.x+dx[j]][p.y+dy[j]] > 0) continue;
a[p.x+dx[j]][p.y+dy[j]] = a[p.x][p.y] + 1;
q.add(new Point(p.x+dx[j], p.y+dy[j]));
}
}
pw.println();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
}
class Point{
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
b03476d9ae3421cf4e790636634d673f
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
// Generated by Code Flattener.
// https://plugins.jetbrains.com/plugin/9979-idea-code-flattener
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static class Pair {
int i;
int j;
public Pair(int i, int j) {
this.i = i;
this.j = j;
}
}
public void solve(FastReader in, FastWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[(n + 1) / 2 + (m + 1) / 2 - 1];
for (int i = 0; i < (n + 1) / 2; i++) {
for (int j = 0; j < (m + 1) / 2; j++) {
int tmp = 1;
if (n % 2 == 0 || i > 0) tmp *= 2;
if (m % 2 == 0 || j > 0) tmp *= 2;
a[i + j] += tmp;
}
}
int ans = n / 2 + m / 2;
for (int el : a) {
for (int i = 0; i < el; i++) {
out.print(ans + " ");
}
ans++;
}
out.println();
}
public static void main(String[] args) {
Main main = new Main();
Problem problem = new Problem();
problem.setSolution(main::solve);
problem.enableMultiTest(true);
problem.useStdIn(System.getProperty("ONLINE_JUDGE") != null);
problem.useStdOut(true);
problem.execute();
}
private static class Problem {
private Executable solution;
private Executable init;
private boolean multiTest;
private String inputFileName;
private String outputFileName;
private boolean stdIn;
private boolean stdOut;
public Problem() {
this.multiTest = false;
this.inputFileName = "input.txt";
this.outputFileName = "output.txt";
this.stdIn = false;
this.stdOut = false;
}
public void execute() {
if (solution == null) {
throw new RuntimeException("please, set solution");
}
FastReader in = new FastReader(stdIn ? null : inputFileName);
FastWriter out = new FastWriter(stdOut ? null : outputFileName);
if (init != null) {
init.run(in, out);
}
if (!multiTest) {
solution.run(in, out);
} else {
int times = in.nextInt();
for (int i = 0; i < times; i++) {
solution.run(in, out);
}
}
out.close();
}
public void setSolution(Executable solution) {
this.solution = solution;
}
public void enableMultiTest(boolean multiTest) {
this.multiTest = multiTest;
}
public void useStdIn(boolean stdIn) {
this.stdIn = stdIn;
}
public void useStdOut(boolean stdOut) {
this.stdOut = stdOut;
}
}
private static class FastReader {
private final BufferedReader br;
private StringTokenizer st;
public FastReader(String fileName) {
if (fileName != null) {
try {
File file = new File(getClass().getClassLoader().getResource(fileName).getFile());
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() {
try {
String line = br.readLine();
if (line == null) {
throw new RuntimeException("empty line");
}
st = null;
return line;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static class FastWriter {
final private PrintWriter printWriter;
public FastWriter(String fileName) {
if (fileName != null) {
try {
printWriter = new PrintWriter(new FileWriter(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
printWriter = new PrintWriter(new BufferedOutputStream(System.out));
}
}
public void print(String s) {
printWriter.print(s);
}
public void println() {
printWriter.println();
}
public void close() {
printWriter.close();
}
}
private interface Executable {
void run(FastReader in, FastWriter out);
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
65bd19df07171ee233b5661d8e39d6c6
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class copy {
public static boolean checker(long[] arr, long K, long diff) {
long collect = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > diff)
collect += arr[i] - diff;
}
if (collect >= K)
return true;
else
return false;
}
public static long search(long[] arr, long K, long R) {
long l = 0;
long r = R;
while (l <= r) {
long mid = (l + r) / 2;
if (checker(arr, K, mid)) {
if (checker(arr, K, mid + 1))
l = mid + 1;
else
return mid;
} else
r = mid - 1;
}
return -1;
}
static void sieveOfEratosthenes(int n, ArrayList<Integer> arr, ArrayList<Integer> arr1) {
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
int x = 0;
for (int i = 2; i <= n; i++) {
if (prime[i] == true) {
arr.add(i);
}
}
System.out.println(arr.size());
}
public static boolean check(String s, int K, char ch) {
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ch)
arr.add(i);
}
// if(K==1 && ch=='z')
// System.out.println(arr);
if (arr.size() == 0)
return false;
if (arr.get(0) >= K)
return false;
if (s.length() - 1 - arr.get(arr.size() - 1) >= K)
return false;
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) - arr.get(i - 1) > K)
return false;
}
return true;
}
public static int check(long N) {
int sum = 0;
while (N > 0) {
sum += N % 10;
N = N / 10;
}
return sum;
}
public static long call(long N) {
long l = 1;
long r = (long) Math.floor(Math.sqrt(N));
while (l <= r) {
long mid = (l + r) / 2;
double left = N / (double) (mid) - mid;
int right = check(mid);
System.out.println(mid + " " + left + " " + right);
if (mid == 10)
System.out.println(left + " " + right);
if (left > right)
l = mid + 1;
else if (right > left)
r = mid - 1;
else
return mid;
}
return -1;
}
public static int path(ArrayList<ArrayList<Integer>> arr, int X, int parent, int[] gold, PriorityQueue<Integer> pq) {
int max = 0;
ArrayList<Integer> ch = new ArrayList<>();
for (int i : arr.get(X)) {
if (i != parent) {
ch.add(path(arr, i, X, gold, pq));
}
}
Collections.sort(ch, Collections.reverseOrder());
for (int i = 1; i < ch.size(); i++)
pq.add(ch.get(i));
if (ch.size() == 0)
return gold[X];
else
return ch.get(0) + gold[X];
}
public static long fac(long N, long mod) {
if (N == 0)
return 1;
if(N==1)
return 1;
return ((N % mod) * (fac(N - 1, mod) % mod)) % mod;
}
public static String form(char ch, int freq) {
String s = "";
for (int i = 1; i <= freq; i++)
s += ch;
return s;
}
static long power(long x, long y, long p) {
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(long n, long r,
long p) {
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
// System.out.println(modInverse(fac(r,p),p));
// System.out.println(modInverse(fac(n-r,p),p));
return ((fac(n, p) % p * (modInverse(fac(r, p), p)
% p)) % p * (modInverse(fac(n - r, p), p)
% p)) % p;
}
public static boolean check(long[] arr, long time, int M) {
long sum = 0;
int count = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
if (sum >= time) {
++count;
sum = 0;
}
}
if (sum >= time)
++count;
return count >= M;
}
public static int check(ArrayList<Integer> arr, ArrayList<Integer> arr2) {
int l = 0, r = 0, ans = 0;
while (l < arr.size() && r < arr2.size()) {
if (arr.get(l) < arr2.get(r))
++l;
else if (arr.get(l) > arr2.get(r))
++r;
else {
++ans;
++l;
++r;
}
}
return ans;
}
public static int find(int[] parent, int x) {
if (parent[x] == x)
return x;
return find(parent, parent[x]);
}
public static void merge(int[] parent, int[] rank, int x, int y) {
int x1 = find(parent, x);
int y1 = find(parent, y);
if (rank[x1] > rank[y1]) {
parent[y1] = x1;
} else if (rank[y1] > rank[x1]) {
parent[x1] = y1;
} else {
parent[y1] = x1;
rank[x1]++;
}
}
public static int bfs(ArrayList<ArrayList<Integer>> arr, int e1, int e2, int N) {
boolean[] visited = new boolean[N];
visited[0] = true;
int[] dist = new int[N];
Queue<Integer> queue = new LinkedList<>();
queue.add(0);
Arrays.fill(dist, -1);
dist[0] = 0;
while (queue.size() > 0) {
int x = queue.poll();
for (int i : arr.get(x)) {
if (!visited[i] || (e1 == x && e2 == i)) {
System.out.println(x + " " + dist[x]);
dist[i] = dist[x] + 1;
visited[i] = true;
queue.add(i);
}
}
}
return dist[N - 1];
}
public static int binarysearch(ArrayList<Integer> possible, int X, int start) {
int l = start, r = possible.size() - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (possible.get(mid) > X) {
if (mid - 1 >= start && possible.get(mid - 1) > X)
r = mid - 1;
else
return mid;
} else
l = mid + 1;
}
return -1;
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void leftRotatebyOne(int arr[], int n) {
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n - 1] = temp;
}
static int power(int x, int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
public static long[][] ncr(int n,int r)
{
long[][] dp=new long[n+1][r+1];
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=r;j++)
{
if(j>i)
continue;
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
}
return dp;
}
static boolean status=false;
static int x1=-1,y1=-1;
public static int dfs(ArrayList<ArrayList<Integer>> arr,int X,int[] ans,int value,int parent)
{
int xor=ans[X];
for(int i:arr.get(X))
{
if(i!=parent)
{
int x=dfs(arr,i,ans,value,X);
if(x==-1)
return -1;
if(x==value)
{
x1=X;
y1=i;
return -1;
}
xor=xor^x;
}
}
return xor;
}
public static boolean prime(long N)
{
int c=0;
for(int i=2;i*i<=N;i++)
{
if(N%i==0)
++c;
}
return c==0;
}
static int[] c;
static boolean sta=true;
public static int dfs(ArrayList<ArrayList<Integer>> arr,int N,int j,int[] status,boolean[] visited)
{
Queue<Integer> queue=new LinkedList<>();
queue.add(j);
status[j]=1;
visited[j]=true;
int[] color=new int[2];
while(queue.size()>0)
{
int temp=queue.poll();
if(temp<N)
color[status[temp]]++;
for(int i:arr.get(temp))
{
if(visited[i])
{
if(status[i]==status[temp])
return -1;
}
else
{
queue.add(i);
status[i]=1-status[temp];
visited[i]=true;
}
}
}
return Math.max(color[0],color[1]);
}
public static boolean check(String s,int N,long D,long C,long M)
{
int index=s.lastIndexOf('D');
for(int i=0;i<=index;i++)
{
if(s.charAt(i)=='D' && D>0)
{
D-=1;
C+=M;
}
else if(s.charAt(i)=='C' && C>0)
{
C-=1;
}
else
return false;
}
return true;
}
public static boolean checkbounds(int x,int y,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
public static void dfs(int x,int y,char[][] grid,boolean[][] visited,int N,int M)
{
visited[x][y]=true;
if(checkbounds(x+1,y,N,M) && !visited[x+1][y] && grid[x+1][y]=='.')
dfs(x+1,y,grid,visited,N,M);
if(checkbounds(x-1,y,N,M) && !visited[x-1][y] && grid[x-1][y]=='.')
dfs(x-1,y,grid,visited,N,M);
if(checkbounds(x,y+1,N,M) && !visited[x][y+1] && grid[x][y+1]=='.')
dfs(x,y+1,grid,visited,N,M);
if(checkbounds(x,y-1,N,M) && !visited[x][y-1] && grid[x][y-1]=='.')
dfs(x,y-1,grid,visited,N,M);
}
public static int bin(ArrayList<Integer> arr,int search)
{
int l=0,r=arr.size()-1;
if(search<arr.get(0))
return -1;
if(search>arr.get(r-1))
return r+1;
while(l<=r)
{
int mid=(l+r)/2;
if(arr.get(mid)>search)
{
if(mid-1>=0 && arr.get(mid-1)>search)
r=mid-1;
else
return mid;
}
else
l=mid+1;
}
return -1;
}
public static int mcbc(ArrayList<Long> arr,long search,int N,int lower)
{
if(search>=arr.get(N))
return N;
if(search<=arr.get(lower))
return lower;
int l=lower;
int r=N;
while(l<=r)
{
int mid=(l+r)/2;
if(arr.get(mid)==search)
{
return mid;
}
if(arr.get(mid)>search)
{
if(mid-1>=lower && arr.get(mid-1)>=search)
r=mid-1;
else
{
if(mid-1>=lower)
{
if (Math.abs(search - arr.get(mid)) < Math.abs(search - arr.get(mid - 1)))
return mid;
else
return mid - 1;
}
else
return mid;
}
}
else if(arr.get(mid)<search)
{
if(mid+1<=N && arr.get(mid+1)<=search)
l=mid+1;
else
{
if(mid+1<=N)
{
if (Math.abs(search - arr.get(mid)) < Math.abs(search - arr.get(mid + 1)))
return mid;
else
return mid + 1;
}
else
return mid;
}
}
}
return -1;
}
public static int check(String s,char ch)
{
int l=0,r=s.length()-1;
int c=0;
while(l<r)
{
if(s.charAt(l)!=s.charAt(r))
{
if(s.charAt(l)==ch)
{
++c;
++l;
}
else if(s.charAt(r)==ch)
{
++c;
--r;
}
else
{
return -1;
}
}
else
{
++l;
--r;
}
}
return c;
}
static boolean cycle=false;
static int connected=0;
public static int edges(ArrayList<ArrayList<Integer>> arr,int x,boolean[] visited)
{
++connected;
int sum=arr.get(x).size();
visited[x]=true;
for(int i:arr.get(x))
{
if(!visited[i])
sum+=edges(arr,i,visited);
}
return sum;
}
public static boolean getbounds(int x,int y,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
public static boolean drag(int[][] arr,int mid)
{
// System.out.println(mid);
for(int i=0;i<arr[0].length;i++)
{
boolean status=false;
for(int j=0;j<arr.length;j++)
{
status=status || arr[j][i]>=mid;
}
if(!status)
return false;
}
for(int i=0;i<arr.length;i++) {
int count=0;
for(int j=0;j<arr[0].length;j++)
{
if(arr[i][j]>=mid)
{
++count;
}
}
if(count>=2)
return true;
}
return false;
}
public static int search(int[][] arr)
{
int l=1;int r=(int)Math.pow(10,9);
while(l<=r)
{
int mid=(l+r)/2;
if(drag(arr,mid))
{
if(mid+1<=Math.pow(10,9) && drag(arr,mid+1))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
public static long check(long[][] dp,int x,int y,long mod,int H,int W)
{
if(x>=H || y>=W)
return 0;
if(dp[x][y]!=-1)
return dp[x][y];
long max=0;
max=((max%mod)+(check(dp,x+1,y,mod,H,W)%mod))%mod;
max=((max%mod)+(check(dp,x,y+1,mod,H,W)%mod))%mod;
dp[x][y]=max;
return max;
}
public static String recover(int[][] dp,String s,String t,int x,int y)
{
if(x>=1 && y>=1)
{
String temp="";
if(s.charAt(x-1)==t.charAt(y-1))
{
temp+=s.charAt(x-1);
temp+=recover(dp,s,t,x-1,y-1);
return temp;
}
else
{
if(dp[x-1][y]>dp[x][y-1])
return recover(dp,s,t,x-1,y);
else
return recover(dp,s,t,x,y-1);
}
}
return "";
}
public static long form(long[][] dp,int i,int j,long[] a,long[] b,long max)
{
if(dp[i][j]!=0)
return dp[i][j];
dp[i][j]=form(dp,i+1,j-1,a,b,max)+a[i]*(b[j]-b[i])+a[j]*(b[i]-b[j]);
return dp[i][j];
}
static long min;
public static boolean check(HashMap<Character,Integer> hp, int length,int K)
{
int pairs=0;
for(char ch:hp.keySet())
{
pairs+=hp.get(ch)/2;
}
int pairs_req=(length/2)*K;
return pairs>=pairs_req;
}
public static int solve_left(HashMap<Character,Integer> hp, int K,int N)
{
int l=1,r=N/K;
while(l<=r)
{
int mid=(l+r)/2;
if(check(hp,mid,K))
{
if(mid+1<=N/K && check(hp,mid+1,K))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
public static int child_calc(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child)
{
if(arr.get(x).size()==1 && x!=0)
return 1;
int count=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
count+=child_calc(arr,i,x,child);
}
}
child[x]=count;
return count+1;
}
public static void bipartite(ArrayList<ArrayList<Integer>> arr,int x,int parent,ArrayList<Integer> leaf,int d)
{
// System.out.println(x);
if(arr.get(x).size()==1 && x!=0)
{
leaf.add(d+1);
return;
}
for(int i:arr.get(x))
{
if(i!=parent)
{
bipartite(arr,i,x,leaf,d+1);
}
}
}
public static long fill(long[][] dp,int N,int i,int j,int M,int[] arr,long mod)
{
if(i>=N || j>M || j<1)
return 0;
if(arr[i]!=0 && arr[i]!=j)
{
return 0;
}
if(dp[i][j]!=-1)
return dp[i][j];
dp[i][j]=(fill(dp,N,i+1,j+1,M,arr,mod)%mod+fill(dp,N,i+1,j,M,arr,mod)%mod)%mod;
dp[i][j]=(dp[i][j]%mod+fill(dp,N,i+1,j-1,M,arr,mod)%mod)%mod;
return dp[i][j];
}
public static boolean check(ArrayList<Integer> keys,int X)
{
int l=0,r=keys.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(keys.get(mid)==X)
return true;
else if(X>keys.get(mid))
l=mid+1;
else
r=mid-1;
}
return false;
}
public static HashMap<Integer,Integer> shadow(int x,int y)
{
int[][] arr=new int[x][y];
for(int i=0;i<x;i++)
Arrays.fill(arr[i],-1);
HashMap<Integer,Integer> hp=new HashMap<>();
Queue<div> queue=new LinkedList<>();
if(x%2==0 && y%2==0)
{
arr[x/2][y/2]=0;
arr[x/2-1][y/2]=0;
arr[x/2][y/2-1]=0;
arr[x/2-1][y/2-1]=0;
hp.put(0,4);
queue.add(new div(x/2,y/2));
queue.add(new div(x/2-1,y/2));
queue.add(new div(x/2,y/2-1));
queue.add(new div(x/2-1,y/2-1));
}
else if(x%2==0 && y%2!=0)
{
arr[x/2][y/2]=0;
arr[x/2-1][y/2]=0;
hp.put(0,2);
queue.add(new div(x/2,y/2));
queue.add(new div(x/2-1,y/2));
}
else if(x%2!=0 && y%2==0)
{
arr[x/2][y/2]=0;
arr[x/2][y/2-1]=0;
hp.put(0,2);
queue.add(new div(x/2,y/2));
queue.add(new div(x/2,y/2-1));
}
else
{
arr[x/2][y/2]=0;
hp.put(0,1);
queue.add(new div(x/2,y/2));
}
int x1[]={1,-1,0,0};
int y1[]={0,0,1,-1};
queue.add(new div(-1,-1));
int d=0;
while(queue.size()>0)
{
div temp=queue.poll();
if(temp.x==-1 && temp.y==-1)
{
if(queue.size()>0)
{
d+=1;
hp.put(d,queue.size());
queue.add(new div(-1,-1));
}
else
continue;
}
int xco=temp.x;
int yco=temp.y;
for(int i=0;i<4;i++)
{
if(getbounds(xco+x1[i],yco+y1[i],x,y) && arr[xco+x1[i]][yco+y1[i]]==-1)
{
arr[xco+x1[i]][yco+y1[i]]=arr[xco][yco]+1;
queue.add(new div(xco+x1[i],yco+y1[i]));
}
}
}
return hp;
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int T=Reader.nextInt();
for(int m=1;m<=T;m++)
{
int N=Reader.nextInt();
int M=Reader.nextInt();
int ans=N/2+M/2;
HashMap<Integer,Integer> hp=shadow(N,M);
ArrayList<Integer> arr=new ArrayList<>(hp.keySet());
Collections.sort(arr);
// System.out.println(hp);
for(int i:arr)
{
for(int j=1;j<=hp.get(i);j++)
output.write(ans+i+" ");
}
output.write("\n");
}
output.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class sortat implements Comparator<div>
{
public int compare(div o1,div o2)
{
return o1.x-o2.x;
}
}
class div {
int x;
int y;
div(int x,int y) {
this.x = x;
this.y=y;
}
}
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data)
{
left=null;
right=null;
this.data=data;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
a75dd928bf8bf4000d6048384b85928c
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Test
{
final static FastReader fr = new FastReader();
final static PrintWriter out = new PrintWriter(System.out) ;
static int mod = 1000000007;
static long dp[][] ;
static void solve()
{
int n = fr.nextInt();
int m = fr.nextInt();
int arr[] = new int[n*m] ;
int k = 0 ;
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < m ; j++)
{
int a = Math.abs(n-1-i) + Math.abs(m-1-j) ;
int b = Math.abs(i) + Math.abs(j) ;
int c = Math.abs(i) + Math.abs(m-1-j);
int d = Math.abs(n-1-i) + Math.abs(j) ;
arr[k++] = Math.max(a, Math.max(b, Math.max(c, d))) ;
}
}
Arrays.parallelSort(arr);
for (int x: arr) out.print(x + " ");
out.println();
}
static long helper(long x, long m, long s)
{
return 0 ;
}
public static void main(String[] args)
{
int t = fr.nextInt();
while (t-- > 0)
{
solve() ;
}
out.close() ;
}
static class Pair{
int x;
int y ;
Pair(int x, int y){
this.x = x ;
this.y= y ;
}
}
static long gcd(long a, long b)
{
if (b == 0) return a ;
return gcd(b, a%b) ;
}
static long lcm(long a, long b)
{
long lcm = (a * b)/gcd(a, b) ;
return lcm ;
}
static boolean isVowel(char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return true ;
return false ;
}
static int upper_bound(int low,int high,int key,int a[])
{
while(low<high)
{
int mid=(low+high)/2;
if(a[mid]<=key)
{
low=mid+1;
}
else
high=mid;
}
return low;
}
static long fastpower(long a, long b)
{
long ans = 1 ;
while (b > 0)
{
if ((b & 1) != 0) ans *= a ;
a *= a ;
b >>= 1 ;
}
return ans ;
}
static int lower_bound(List<Long> arr, long key)
{
int pos = Collections.binarySearch(arr, key) ;
if (pos < 0)
{
pos = - (pos + 1) ;
}
return pos ;
}
static int upper_bound(List<Long> arr, long key)
{
int pos = Collections.binarySearch(arr, key);
pos++ ;
if (pos < 0)
{
pos = -(pos) ;
}
return pos ;
}
static void printArr(int arr[])
{
int n = arr.length ;
for (int i = 0 ; i < n ; i++) out.print(arr[i] + " ");
out.println() ;
}
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
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
c869b0d3bbdbb80e75439420bd841fa3
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
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 {
if (!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()); }
int[] na(int n) throws IOException { int[]A=new int[n]; for (int i=0;i<n;i++) A[i]=ni(); return A; }
long mod=1000000007;
void solve() throws IOException {
for (int tc=ni();tc>0;tc--) {
int n=ni(),m=ni();
int[]A=new int[n*m];
int p=0;
for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {
int x=i+j-2;
x=Math.max(x,i-1+m-j);
x=Math.max(x,n-i+j-1);
x=Math.max(x,n-i+m-j);
A[p]=x;
p++;
}
Arrays.sort(A);
for (int u:A) out.print(u+" ");
out.println();
}
out.flush();
}
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
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
4d4a86d28c03b2d54ee084ec85a06538
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main{
static class pair{
int x ;int y ;int z ;
pair(int x ,int y,int z )
{
this.x=x;
this.y =y;
this.z=z;
}
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void prlong(Object object) throws IOException {
bw.append("" + object);
}
public void prlongln(Object object) throws IOException {
prlong(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void help(int arr[], int vis[],int i , int n )
{
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
long testCases = in.nextLong();
while (testCases-- > 0) {
int m =in.nextInt() ,n =in.nextInt();
int arr[]=new int[m*n];int ind =0;
for(int i =1;i<m+1;i++)
for(int j =1;j<n+1;j++)
{
int op1 =Math.abs(m-i)+Math.abs(n-j);
int op2 =Math.abs(1-i)+Math.abs(n-j);
int op3 =Math.abs(1-i)+Math.abs(1-j);
int op4 =Math.abs(m-i)+Math.abs(1-j);
arr[ind]=Math.max(Math.max(op1,op2),Math.max(op3,op4));
ind++;
}
Arrays.sort(arr);
for(int i :arr)
System.out.print(i+" ");
System.out.println();
}
out.close();
} catch (Exception e) {
return;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
1c1457b77d8af18d36198b04eaca8f22
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
private static class FastReader {
public BufferedReader br;
public StringTokenizer st;
public FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader(new BufferedInputStream(System.in));
int times = sc.nextInt();
while (times-- > 0){
solve(sc);
}
}
public static void solve(FastReader sc){
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr = new int[n*m];
int k=0;
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <= m; j++) {
int op1 = Math.abs(1-i)+Math.abs(1-j);
int op2 = Math.abs(1-i)+Math.abs(m-j);
int op3 = Math.abs(n-i)+Math.abs(1-j);
int op4 = Math.abs(n-i)+Math.abs(m-j);
arr[k] = Math.max(op1,Math.max(op2,Math.max(op3,op4)));
k++;
}
}
Arrays.sort(arr);
for (int i = 0; i < (m*n); i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
680f31c0813f25b0cf1642c33420c72a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class NotSitting {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
for(int i = 0; i < n; i++){
String[] line = br.readLine().split(" ");
int r = Integer.parseInt(line[0]);
int c = Integer.parseInt(line[1]);
int[][] dir = new int[4][2];
dir[0] = new int[]{1, 0};
dir[1] = new int[]{0, 1};
dir[2] = new int[]{-1, 0};
dir[3] = new int[]{0, -1};
boolean[][] grid = new boolean[r][c];
Queue<Integer> pts = new LinkedList<Integer>();
if(r % 2 == 0 && c % 2 == 0){
int row = r/2;
int col = c/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
row = (r-1)/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
row = r/2;
col = (c-1)/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
row = (r-1)/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
}
else if(r % 2 == 0 || c % 2 == 0){
int row = r/2;
int col = c/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
if(r % 2 == 0) row = (r-1)/2;
else col = (c-1)/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
}
else{
int row = r/2;
int col = c/2;
pts.add(row);
pts.add(col);
grid[row][col] = true;
}
while(!pts.isEmpty()){
int row = pts.poll();
int col = pts.poll();
//Append dist
sb.append(maxDist(row, col, r, c)).append(" ");
for(int j = 0; j < dir.length; j++){
int nextX = row+dir[j][0];
int nextY = col+dir[j][1];
if(validPt(nextX, nextY, r, c) && !grid[nextX][nextY]){
grid[nextX][nextY] = true;
pts.add(nextX);
pts.add(nextY);
}
}
}
sb.append("\n");
}
System.out.println(sb);
}
static boolean validPt(int row, int col, int r, int c){
return row >= 0 && row != r && col >= 0 && col != c;
}
static int maxDist(int row, int col, int r, int c){
int max = row + col;
max = Math.max(max, r - row - 1 + col);
max = Math.max(max, row + c - col - 1);
max = Math.max(max, r - row + c - col - 2);
return max;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
028cbe70f845f42f8534e37da5f340e5
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
private static void run() throws IOException {
int n, m;
n = in.nextInt();
m = in.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int now = i + j;
now = Math.max(now, n - 1 - i + j);
now = Math.max(now, i + m - 1 - j);
now = Math.max(now, n - 1 - i + m - 1 - j);
ans.add(now);
}
}
ans.sort(Comparator.comparingInt(o -> o));
for (int now : ans) {
out.print(now);
out.print(' ');
}
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 add_mod(long... longs) {
long ans = 0;
for (long now : longs) {
ans = (ans + now) % mod;
if (ans < 0) ans += mod;
}
return ans;
}
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
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
7920f4b1c65f41d321052c32a4a70774
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package cp;
import java.io.*;
import java.util.*;
public class ProblemA{
static long mod = 1000000007L;
// map.put(a[i],map.getOrDefault(a[i],0)+1);
// map.putIfAbsent;
static MyScanner sc = new MyScanner();
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
int n = sc.nextInt();
int m = sc.nextInt();
int midN = (n-1)/2;
int midM = (m-1)/2;
// out.println(midN+" "+midM);
List<Integer> li = new ArrayList<>();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int newI;
int newJ;
if(i<=midN) {
newI = n-1;
}else {
newI = 0;
}
if(j<=midM) {
newJ = m-1;
}else {
newJ = 0;
}
int v = Math.abs(newJ-j) + Math.abs(newI-i);
// out.print(newI+" "+newJ+" "+v+" x ");
li.add(v);
}
}
// out.println();
Collections.sort(li);
for(int i: li) out.print(i+" ");
out.println();
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static void swap(int[] a,int i,int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LowerBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static void priArr(int[] a) {
for(int i=0;i<a.length;i++) {
out.print(a[i] + " ");
}
out.println();
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static void reverse(int[] arr){
int i = 0;
int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
static class Pair implements Comparable<Pair>{
int val;
int ind;
Pair(int v,int f){
val = v;
ind = f;
}
public int compareTo(Pair p){
if(this.val != p.val) return this.val-p.val;
return this.ind - p.ind;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
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;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
7f88b0574acfec17645630d3e1397d61
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class cd1627B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tt = in.nextInt();
for(int w= 0; w<tt;w++) {
int n = in.nextInt();
int m = in.nextInt();
String s ="";
long num = n*m;
boolean uslov = false;
int counter = Math.min(n, m);
boolean uslov1 = false;
int[] arr = new int[n*m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i * m + j] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1);
}
}
Arrays.sort(arr);
for(int i=0; i<n*m; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
4e348c9b4a62aa8eaf3d1e0c54da1a50
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package random;
import java.util.*;
import java.io.*;
public class CF {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int q = Integer.parseInt(st.nextToken());
while(q-->0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] ans = new int[n*m];
int ct = 0;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
ans[ct] = Math.max(Math.max(Math.abs(i-1)+Math.abs(j-1), Math.abs(i-n)+Math.abs(j-1)), Math.max(Math.abs(i-1)+Math.abs(j-m), Math.abs(i-n)+Math.abs(j-m)));
ct++;
}
}
Arrays.sort(ans);
for (int i: ans) {
System.out.print(i+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
16a4fef303ae8af8af08a3a522674908
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
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.*;
public class B_Not_Sitting {
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
while(t-- > 0){
solve(f, out);
}
out.close();
}
public static void solve(FastReader f, PrintWriter out) {
int n = f.nextInt();
int m = f.nextInt();
ArrayList<Integer> arrli = new ArrayList<>();
for(int r = 0; r < n; r++) {
for(int c = 0; c < m; c++) {
int dis1 = r + c;
int dis2 = n-1-r + c;
int dis3 = r + m-1-c;
int dis4 = n-1-r + m-1-c;
arrli.add(max(dis1, max(dis2, max(dis3, dis4))));
}
}
Collections.sort(arrli);
for(int i: arrli) {
out.print(i + " ");
}
out.println();
}
public static void sort(int arr[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i: arr) {
al.add(i);
}
Collections.sort(al);
for(int i = 0; i < arr.length; i++) {
arr[i] = al.get(i);
}
}
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
public static int gcd(int a, int b) {
int dividend = a > b ? a : b;
int divisor = a < b ? a : b;
while(divisor > 0) {
int reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
public static int lcm(int a, int b) {
int lcm = gcd(a, b);
int hcf = (a * b) / lcm;
return hcf;
}
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
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());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArray(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
ef297777638216bd50b104feccbfb6b4
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
public class B_Not_Sitting
{
public static void main(String[] args)throws Exception
{
new Solver().solve();
}
}
//* Success is not final, failure is not fatal: it is the courage to continue that counts.
class Solver {
void solve() throws Exception
{
int t = sc.nextInt();
while(t-->0){
int n =sc.nextInt();
int m =sc.nextInt();
ArrayList<Integer> A = new ArrayList<>();
for(int i =0;i<n;i++){
for(int j = 0;j<m;j++){
A.add(Math.max(n-i-1,i)+Math.max(m-j-1,j));
}
}
Collections.sort(A);
// System.out.println(A);
StringBuilder SB = new StringBuilder();
for(int i : A){
SB.append(i+" ");
}
String ANS = SB.toString();
System.out.println(ANS.substring(0, ANS.length()-1));
}
sc.flush();
}
final Helper sc;
final int MAXN = 1000_006;
final long MOD = (long) 1e9 + 7;
Solver() {
sc = new Helper(MOD, MAXN);
sc.initIO(System.in, System.out);
}
}
class Helper {
final long MOD;
final int MAXN;
final Random rnd;
public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}
public static int[] sieve;
public static ArrayList<Integer> primes;
public void setSieve() {
primes = new ArrayList<>();
sieve = new int[MAXN];
int i, j;
for (i = 2; i < MAXN; ++i)
if (sieve[i] == 0) {
primes.add(i);
for (j = i; j < MAXN; j += i) {
sieve[j] = i;
}
}
}
public static long[] factorial;
public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD;
}
public long getFactorial(int n) {
if (factorial == null) setFactorial();
return factorial[n];
}
public long ncr(int n, int r) {
if (r > n) return 0;
if (factorial == null) setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}
public long[] getLongArray(int size) throws Exception {
long[] ar = new long[size];
for (int i = 0; i < size; ++i) ar[i] = nextLong();
return ar;
}
public int[] getIntArray(int size) throws Exception {
int[] ar = new int[size];
for (int i = 0; i < size; ++i) ar[i] = nextInt();
return ar;
}
public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public long max(long[] ar) {
long ret = ar[0];
for (long itr : ar) ret = Math.max(ret, itr);
return ret;
}
public int max(int[] ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.max(ret, itr);
return ret;
}
public long min(long[] ar) {
long ret = ar[0];
for (long itr : ar) ret = Math.min(ret, itr);
return ret;
}
public int min(int[] ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.min(ret, itr);
return ret;
}
public long sum(long[] ar) {
long sum = 0;
for (long itr : ar) sum += itr;
return sum;
}
public long sum(int[] ar) {
long sum = 0;
for (int itr : ar) sum += itr;
return sum;
}
public void shuffle(int[] ar) {
int r;
for (int i = 0; i < ar.length; ++i) {
r = rnd.nextInt(ar.length);
if (r != i) {
ar[i] ^= ar[r];
ar[r] ^= ar[i];
ar[i] ^= ar[r];
}
}
}
public void shuffle(long[] ar) {
int r;
for (int i = 0; i < ar.length; ++i) {
r = rnd.nextInt(ar.length);
if (r != i) {
ar[i] ^= ar[r];
ar[r] ^= ar[i];
ar[i] ^= ar[r];
}
}
}
public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1) ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}
static byte[] buf = new byte[1000_006];
static int index, total;
static InputStream in;
static BufferedWriter bw;
public void initIO(InputStream is, OutputStream os) {
try {
in = is;
bw = new BufferedWriter(new OutputStreamWriter(os));
} catch (Exception e) {
}
}
public void initIO(String inputFile, String outputFile) {
try {
in = new FileInputStream(inputFile);
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFile)));
} catch (Exception e) {
}
}
private int scan() throws Exception {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public String nextLine() throws Exception {
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();
}
public String next() throws Exception {
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();
}
public int nextInt() throws Exception {
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;
}
public long nextLong() throws Exception {
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;
}
public void print(Object a) throws Exception {
bw.write(a.toString());
}
public void printsp(Object a) throws Exception {
print(a);
print(" ");
}
public void println() throws Exception {
bw.write("\n");
}
public void printArray(int[] arr) throws Exception{
for(int i = 0;i<arr.length;i++){
print(arr[i]+ " ");
}
println();
}
public void println(Object a) throws Exception {
print(a);
println();
}
public void flush() throws Exception {
bw.flush();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
dfaed99f5a75ea01df501f48ec4c0986
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.cert.X509CRL;
import java.util.*;
import java.lang.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static String OUTPUT = "";
//global
//private final static long BASE = 998244353L;
private final static int ALPHABET = (int)('z') - (int)('a') + 1;
private final static long BASE = 1000000007l;
private final static int INF_I = (1<<31)-1;
private final static long INF_L = (1l<<63)-1;
private final static int MAXN = 200100;
private final static int MAXK = 31;
private final static int[] DX = {-1,0,1,0};
private final static int[] DY = {0,1,0,-1};
static void solve() {
//int ntest = 1;
int ntest = readInt();
for (int test=0;test<ntest;test++) {
int N = readInt(), M = readInt();
List<Integer> ans = new ArrayList<>();
for (int i=0;i<N;i++)
for (int j=0;j<M;j++) {
int tmp = Math.max(i, N-1-i) + Math.max(j, M-1-j);
ans.add(tmp);
}
Collections.sort(ans);
for (int i=0;i<N*M;i++) out.print(ans.get(i) + " ");
out.println();
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
if (INPUT=="") {
is = System.in;
} else {
File file = new File(INPUT);
is = new FileInputStream(file);
}
if (OUTPUT == "") out = new PrintWriter(System.out);
else out = new PrintWriter(OUTPUT);
solve();
out.flush();
long G = System.currentTimeMillis();
}
private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {return x;}
public T getY() {return y;}
@Override
public int compareTo(Point<T> o) {
int cmp = x.compareTo(o.getX());
if (cmp==0) return y.compareTo(o.getY());
return cmp;
}
}
private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> {
public ClassComparator() {}
@Override
public int compare(T a, T b) {
return a.compareTo(b);
}
}
private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
public ListComparator() {}
@Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double readDouble() { return Double.parseDouble(readString()); }
private static char readChar() { return (char)skip(); }
private static String readString()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] readChar(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readTable(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = readChar(m);
return map;
}
private static int[] readIntArray(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = readInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i=0;i<n;i++) a[i] = readLong();
return a;
}
private static int readInt()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long readLong()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
409b5882e2e9dddb7e769a200e8b3e98
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n, m;
public static void main(String[] args) throws IOException {
t = in.iscan();
outer : while (t-- > 0) {
n = in.iscan(); m = in.iscan();
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int cost = 0;
cost = Math.max(cost, i-1+j-1);
cost = Math.max(cost, n-i+j-1);
cost = Math.max(cost, n-i+m-j);
cost = Math.max(cost, i-1+m-j);
arr.add(cost);
}
}
Collections.sort(arr);
int mx = n * m;
for (int k = 0; k < mx; k++) {
out.print(arr.get(k) + " ");
}
out.println();
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
a4d3fc3f66a8b60ae7e37e7c2ea4c2a8
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int test = input.nextInt();
for(int i = 0;i < test;i++) {
int n = input.nextInt();
int m = input.nextInt();
List<Integer> distances = new ArrayList<Integer>();
for(int r = 0;r < n;r++) {
for(int c = 0;c < m;c++) {
int D = Math.max(r, n - 1 - r) + Math.max(c, m - 1 - c);
distances.add(D);
}
}
Collections.sort(distances);
for(int k = 0;k < distances.size();k++) {
System.out.print(distances.get(k));
if(k != distances.size() - 1) {
System.out.print(" ");
}
else {
System.out.print("\n");
}
}
}
input.close();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
366e75d141b9ce06377f1835e0c3305b
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.Collection;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Queue;
import java.util.LinkedList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Manav
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
BNotSitting solver = new BNotSitting();
solver.solve(1, in, out);
out.close();
}
static class BNotSitting {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.nextInt();
for (int it = 0; it < t; it++) {
int n = in.nextInt(), m = in.nextInt();
int[][] a = new int[n][m];
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{0, 0, 1});
queue.add(new int[]{0, m - 1, 1});
queue.add(new int[]{n - 1, 0, 1});
queue.add(new int[]{n - 1, m - 1, 1});
while (queue.isEmpty() == false) {
int[] top = queue.remove();
int r = top[0], c = top[1], v = top[2];
if (a[r][c] > 0) {
continue;
}
a[r][c] = v;
int[] p = {0, 0, 1, -1};
int[] q = {1, -1, 0, 0};
for (int i = 0; i < 4; i++) {
if (r + p[i] < n && r + p[i] >= 0 && c + q[i] < m && c + q[i] >= 0 && a[r + p[i]][c + q[i]] == 0) {
queue.add(new int[]{r + p[i], c + q[i], v + 1});
}
}
// out.println(r,c,v);
}
int[] distance = new int[n + m - 1];
for (int i = 0; i < n; i++) {
// out.println(a[i]);
for (int j = 0; j < m; j++) {
distance[a[i][j]]++;
}
}
int[] ans = new int[n * m];
int j = ans.length - 1;
for (int i = 1; i < distance.length; i++) {
for (int k = 0; k < distance[i]; k++) {
ans[j--] = n + m - 1 - i;
}
}
out.println(ans);
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
9786b93185324a3ac3dcdf840d286d6b
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
/*
2
4 3
1 2
*/
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
int m = scan.nextInt();
ArrayList<Integer>list = new ArrayList<>();
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
int d = Math.max(n-i,i-1) + Math.max(m-j,j-1);
list.add(d);
}
}
Collections.sort(list);
for(int i = 0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
98868de3a425c351ce7429815b89fc5d
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
// package codeforce;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.io.*;
public class A {
static class Node {
int id1;
int id2;
Node(int v1, int w1){
this.id1= v1;
this.id2=w1;
}
Node(){}
}
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;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
static boolean[] seiveofEratoSthenes(int n) {
boolean[] isPrime= new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0]=false;
isPrime[1]= false;
for(int i=2;i*i<n;i++) {
for(int j=2*i; j<=n;j++) {
isPrime[j]= false;
}
}
return isPrime;
}
static int i = 2;
// Function check whether a number
// is prime or not
public static boolean isPrime(int n)
{
// Corner cases
if (n == 0 || n == 1)
{
return false;
}
// Checking Prime
if (n == i)
return true;
// Base cases
if (n % i == 0)
{
return false;
}
i++;
return isPrime(n);
}
static class SortingComparator implements Comparator<Node>{
@Override
public int compare(Node p1, Node p2) {
int n = p1.id1-p2.id1;
if(n!=0)return n;
return p2.id2-p1.id2;
}
}
public static void main(String[] args) {
FastReader sc=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int t = sc.nextInt();
int mod = 1000000007;
while(t--!=0) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> al = new ArrayList<>();
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
int d1 = Math.max(Math.abs(i-n+1)+Math.abs(j-m+1),
Math.max(Math.abs(i)+Math.abs(j), Math.max(Math.abs(i)+Math.abs(j-m+1),
Math.abs(i-n+1)+Math.abs(j))));
al.add(d1);
}
}
Collections.sort(al);
for(int e: al)out.print(e+" ");
out.println();
}
out.close();
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l, Collections.reverseOrder());
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPalindrome(char[] arr, int i, int j) {
while(i<j) {
if(arr[i]!=arr[j])return false;
i++;
j--;
}
return true;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int max =0;
static void dfs(int i, boolean[] vis , ArrayList<ArrayList<Integer>> adj) {
max = Math.max(max, i);
vis[i]= true;
for(int e: adj.get(i)) {
if(vis[e]==false) {
dfs(e, vis, adj);
}
}
}
public static boolean isValid(int x, int y, char[][] arr , boolean[][] vis) {
if(x<0 || y<0 || x>= arr.length || y>=arr.length || vis[x][y]==true || arr[x][y]=='1')return false;
return true;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static double pow(int a, int b) {
long res= 1;
while(b>0) {
if((b&1)!=0) {
res= (res*a);
}
a= (a*a);
b= b>>1;
}
return res;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
68001ac495dd80c10f6d78ece76a210f
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class NotSitting {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i > n/2 - 1 && j <= m/2 - 1) {
a.add(Math.abs(1 - (i+1)) + Math.abs(m - (j+1)));
} else if (i <= n/2 - 1 && j <= m/2 - 1) {
a.add(Math.abs(n - (i+1)) + Math.abs(m - (j+1)));
} else if (i <= n/2 - 1 && j > m/2 - 1) {
a.add(Math.abs(n - (i+1)) + Math.abs(1 - (j+1)));
} else if (i > n/2 - 1 && j > m/2 - 1) {
a.add(Math.abs(1 - (i+1)) + Math.abs(1 - (j+1)));
}
}
}
Collections.sort(a);
for (int i = 0; i < a.size(); i++) {
if (i == a.size() - 1) {
System.out.println(a.get(i));
break;
}
System.out.print(a.get(i) + " ");
}
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
8ccb5b7ff4689a5ee23b4a698702376a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
// when can't think of anything -->>
// 1. In sorting questions try to think about all possibilities like sorting from start, end, middle.
// 2. Two pointers, brute force.
// 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse.
// 4. If order does not matter then just sort the data if constraints allow. It'll never harm.
// 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with.
// 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve.
// 7. Try to solve it from back or reversely.
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int first = Math.abs(i-0)+Math.abs(j-0);
int second = Math.abs(i-0)+Math.abs(j-m+1);
int third = Math.abs(i-n+1)+Math.abs(j-m+1);
int fourth = Math.abs(i-n+1)+Math.abs(j-0);
list.add(Math.max(first, Math.max(second, Math.max(third, fourth))));
}
}
Collections.sort(list);
for (int i : list)writer.print(i + " ");
writer.println();
}
writer.flush();
writer.close();
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[a] = find(arr[a], arr);
return arr[a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
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;
}
}
}
class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b);
}
@Override
public int hashCode()
{
return Objects.hash(a,b);
}
@Override
public int compareTo(Pair o) {
if(this.a == o.a) {
return Integer.compare(this.b, o.b);
}else {
return Integer.compare(this.a, o.a);
}
}
@Override
public String toString() {
return this.a + " " + this.b;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
05e5eb984a0bbdd64357b4ac22635c67
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.rmi.ConnectIOException;
import java.text.DecimalFormat;
import java.io.*;
public class Eshan {
static private final String INPUT = "input.txt";
static private final String OUTPUT = "output.txt";
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.0000000");
final static int mod = (int) (1e9 + 7);
final static int MAX = Integer.MAX_VALUE;
final static int MIN = Integer.MIN_VALUE;
final static long INF = Long.MAX_VALUE;
final static long NEG_INF = Long.MIN_VALUE;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
input();
// int t = 1;
int t = readInt();
preprocess();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt(), m = readInt();
Queue<int[]> q = new ArrayDeque<>();
q.add(new int[] { n / 2, m / 2 });
if ((n & 1) == 0)
q.add(new int[] { n / 2 - 1, m / 2 });
if ((m & 1) == 0)
q.add(new int[] { n / 2, m / 2 - 1 });
if ((n & 1) == 0 && (m & 1) == 0)
q.add(new int[] { n / 2 - 1, m / 2 - 1 });
int[][] dirs = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };
boolean[][] visited = new boolean[n][m];
int ans = n / 2 + m / 2;
while (!q.isEmpty()) {
int size = q.size();
while (size-- > 0) {
int[] rem = q.remove();
if (visited[rem[0]][rem[1]])
continue;
visited[rem[0]][rem[1]] = true;
out.print(ans + " ");
for (int[] d : dirs) {
int x = rem[0] + d[0], y = rem[1] + d[1];
if (x >= 0 && x < n && y >= 0 && y < m)
q.add(new int[] { x, y });
}
}
ans++;
}
out.println();
}
private static void preprocess() {
}
// cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces
// javac Eshan.java
// java Eshan
// javac Eshan.java && java Eshan
// ==================== CUSTOM CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair o) {
if (this.first != o.first)
return this.second - o.second;
return this.first - o.first;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
private static void input() {
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) {
System.err.println("Error Occurred.");
}
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return br.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray(int n) throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray(m);
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static long power(long a, long b) {
if (b == 0)
return 1L;
long ans = power(a, b >> 1);
ans *= ans;
if ((b & 1) == 1)
ans *= a;
return ans;
}
private static int mod_power(int a, int b, int mod) {
if (b == 0)
return 1;
int temp = mod_power(a, b >> 1, mod);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static int multiply(int a, int b) {
return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod);
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(List<Long> list, long val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(List<Long> list, long val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ==================== SEGMENT TREE (RANGE SUM) =====================
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
public void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
public boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ==================== FENWICK TREE ================================
static class FT {
long[] tree;
int n;
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
c11d055048dc5e86450f454eb5488ee6
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
private static void run() throws IOException {
int n, m;
n = in.nextInt();
m = in.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int now = i + j;
now = Math.max(now, n - 1 - i + j);
now = Math.max(now, i + m - 1 - j);
now = Math.max(now, n - 1 - i + m - 1 - j);
ans.add(now);
}
}
ans.sort(Comparator.comparingInt(o -> o));
for (int now : ans) {
out.print(now);
out.print(' ');
}
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 add_mod(long... longs) {
long ans = 0;
for (long now : longs) {
ans = (ans + now) % mod;
if (ans < 0) ans += mod;
}
return ans;
}
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
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
dbd2678124ecef3796dc872485421784
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.math.*;
//import java.io.*;
public class Experiment {
static Scanner in=new Scanner(System.in);
static int global=0;
// static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static class Pair implements Comparable<Pair>{
String s;char ch;int swap;
Pair(String s,char ch,int swap){
this.s=s;
this.ch=ch;
this.swap=swap;
}
public int compareTo(Pair o){
if(swap%2==0)
return o.ch-this.ch;
else
return this.ch-o.ch;
// Sort return this.first-o.first; pair on the basis of first parameter in ascending order
// return o.first-this.first to sort on basis of first parameter in descending order
// return this.second -o.second to Sort pair on the basis of second parameter in ascending order
//return o.second-this.second to sort on basis of second parameter in descending order
}
}
public static void sort(int ar[],int l,int r) {
if(l<r) {
int m=l+(r-l)/2;
sort(ar,l,m);
sort(ar,m+1,r);
merge(ar,l,m,r);
}
}
public static int hcf(int x,int y) {
if(y==0)
return x;
return hcf(y,x%y);
}
public static void merge(int ar[],int l,int m,int r) {
int n1=m-l+1;int n2=r-m;
int L[]=new int[n1];
int R[]=new int[n2];
for(int i=0;i<n1;i++)
L[i]=ar[l+i];
for(int i=0;i<n2;i++)
R[i]=ar[m+i+1];
int i=0;int j=0;int k=l;
while(i<n1 && j<n2) {
if(L[i]<=R[j]) {
ar[k++]=L[i++];
}else {
ar[k++]=R[j++];
}
}
while(i<n1)
ar[k++]=L[i++];
while(j<n2)
ar[k++]=R[j++];
}
public static int[] sort(int ar[]) {
sort(ar,0,ar.length-1);
//Array.sort uses O(n^2) in its worst case ,so better use merge sort
return ar;
}
public static long rec(long n) {
if(n==1 || n==0)
return 1;
return n*rec(n-1);
}
public static void func() {
int n=in.nextInt();int m=in.nextInt();
int midr=(n-1)/2;int midc=(m-1)/2;
ArrayList<Integer> list=new ArrayList<Integer>();
boolean mark[][]=new boolean[n][m];
if(n%2==1 && m%2==1) {
mark[midr][midc]=true;list.add((n/2)+(m/2));
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(!mark[i][j]) {
list.add(list.get(0)+Math.abs(i-midr)+Math.abs(j-midc));
}
}
}
}
else if(n%2==0 && m%2==0) {
mark[midr][midc]=mark[midr+1][midc]=mark[midr][midc+1]=mark[midr+1][midc+1]=true;
int r1=midr;int c1=midc;int r2=midr+1;int c2=midc;
int r3=midr;int c3=midc+1;int r4=midr+1;int c4=midc+1;
list.add((n/2)+(m/2));list.add((n/2)+(m/2));list.add((n/2)+(m/2));list.add((n/2)+(m/2));
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(!mark[i][j]) {
int t1=Math.abs(i-r1)+Math.abs(j-c1);int t2=Math.abs(i-r2)+Math.abs(j-c2);
int t3=Math.abs(i-r3)+Math.abs(j-c3);int t4=Math.abs(i-r4)+Math.abs(j-c4);
list.add(list.get(0)+Math.min(t1, Math.min(t2, Math.min(t3, t4))));
}
}
}
}else {
int even=0,odd=0;
if(n%2==0) {
even=n;odd=m;
}else {
even=m;odd=n;
}
mark=new boolean[odd][even];midr=(odd-1)/2;midc=(even-1)/2;
mark[midr][midc]=true;mark[midr][midc+1]=true;list.add((n/2)+(m/2));list.add((n/2)+(m/2));
int r1=midr;int c1=midc;int r2=midr;int c2=midc+1;
for(int i=0;i<odd;i++) {
for(int j=0;j<even;j++) {
if(!mark[i][j]) {
int t1=Math.abs(i-r1)+Math.abs(j-c1);int t2=Math.abs(i-r2)+Math.abs(j-c2);
//int t3=Math.abs(i-r3)+Math.abs(j-c3);int t4=Math.abs(i-r4)+Math.abs(j-c4);
list.add(list.get(0)+Math.min(t1,t2));
}
}
}
}
Collections.sort(list);
for(int ele:list)
System.out.print(ele+" ");
System.out.println();
}
public static void main(String[] args) {
int t=in.nextInt();
while(t-->0) {
func();
global=0;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
496d100a37551c8fcd0b24e5490e1b1c
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int testNum = in.nextInt();
solver.solve(testNum, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
for (int z = 0; z < testNumber; z++) {
//out.println("FF");
int n = in.nextInt();
int m = in.nextInt();
int[][] sets = new int[n][m];
for (int k = 0; k < (int) Math.ceil((double)m/2); k++) {
sets[0][k] = k;
}
for (int i = 1; i < (int) Math.ceil((double)n/2); i++) {
for (int k = 0; k < (int) Math.ceil((double)m/2); k++) {
sets[i][k] = sets[i-1][k] + 1;
}
}
// left bottom
for (int k = 0; k < (int) Math.ceil((double)m/2); k++) {
sets[n-1][k] = k;
}
for (int i = n-2; i >= (int) Math.ceil((double)n/2); i--) {
for (int k = 0; k < (int) Math.ceil((double)m/2); k++) {
sets[i][k] = sets[i+1][k] + 1;
}
}
//right up
for (int k = m-1; k >= (int) Math.ceil((double)m/2); k--) {
sets[0][k] = m-1-k;
}
for (int i = 1; i < (int) Math.ceil((double)n/2); i++) {
for (int k = m-1; k >= (int) Math.ceil((double)m/2); k--) {
sets[i][k] = sets[i-1][k] + 1;
}
}
// right bottom
for (int k = m-1; k >= (int) Math.ceil((double)m/2); k--) {
sets[n-1][k] = m-1-k;
}
for (int i = n-2; i >= (int) Math.ceil((double)n/2); i--) {
for (int k = m-1; k >= (int) Math.ceil((double)m/2); k--) {
sets[i][k] = sets[i+1][k] + 1;
}
}
ArrayList<Integer> lis = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
for (int k = 0; k < m; k++) {
lis.add(sets[i][k]);
}
}
Collections.sort(lis);
int highestI = n/2;
int highestK = m/2;
int highestValue = sets[highestI][highestK];
for (int i = 0; i < n*m; i++) {
int curValue = lis.get(lis.size() - 1 - i);
int curI = highestI + (highestValue - curValue);
int curK = highestK;
out.print(curI + curK + " ");
}
/*
// print
for (int i = 0; i < n; i++) {
out.println();
for (int k = 0; k < m; k++) {
out.print(sets[i][k] + " ");
}
}*/
out.println();
}
}
}
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());
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
8064a7e1f7d753f378ee785f2cd14a2a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class F {
static int distance(int a, int b, int c, int d){
return Math.abs(c-a) + Math.abs(d-b);
}
public static void main(String[] args) throws IOException {
Soumit sc = new Soumit();
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
int[][] mat = new int[n][m];
List<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mat[i][j] = Math.max(Math.max(distance(i, j, 0, 0), distance(i, j, 0, m-1)),
Math.max(distance(i, j, n-1, 0), distance(i, j, n-1, m-1)));
list.add(mat[i][j]);
}
}
Collections.sort(list);
for (Integer integer : list) {
sb.append(integer).append(" ");
}
sb.append("\n");
}
System.out.println(sb);
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[3000064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public void sort(int[] arr) {
ArrayList<Integer> arlist = new ArrayList<>();
for (int i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public void sort(long[] arr) {
ArrayList<Long> arlist = new ArrayList<>();
for (long i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
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;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
87f6a6d4b955983f75b862f2821579ce
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.*;
//
public class Main{
public static void main(String[] args) {
TaskA solver = new TaskA();
// boolean[]prime=seive(3*100001);
int t = in.nextInt();
for (int i = 1; i <= t ; i++) {
solver.solve(1, in, out);
}
// solver.solve(1, in, out);
out.flush();
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n=in.nextInt();int m=in.nextInt();
int[]arr=new int[n*m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int d1=dis(i,j,0,0);
int d2=dis(i,j,n-1,0);
int d3=dis(i,j,0,m-1);
int d4=dis(i,j,n-1,m-1);
arr[m*i+j]=Math.max(Math.max(d1, d2),Math.max(d4, d3));
}
}
Arrays.sort(arr);
for(int i=0;i<n*m;i++) {
print(arr[i]+" ");
}
print("\n");
}
}
static int dis(int a,int b,int c,int d) {
return Math.abs(c-a)+Math.abs(d-b);
}
static long ceil(long a,long b) {
return (a/b + Math.min(a%b, 1));
}
static char[] rev(char[]ans,int n) {
for(int i=ans.length-1;i>=n;i--) {
ans[i]=ans[ans.length-i-1];
}
return ans;
}
static int countStep(int[]arr,long sum,int k,int count1) {
int count=count1;
int index=arr.length-1;
while(sum>k&&index>0) {
sum-=(arr[index]-arr[0]);
count++;
if(sum<=k) {
break;
}
index--;
// println("c");
}
if(sum<=k) {
return count;
}
else {
return Integer.MAX_VALUE;
}
}
static long pow(long b, long e) {
long ans = 1;
while (e > 0) {
if (e % 2 == 1)
ans = ans * b % mod;
e >>= 1;
b = b * b % mod;
}
return ans;
}
static void sortDiff(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.first==p2.first) {
return (p1.second-p1.first)-(p2.second-p1.first);
}
return (p1.second-p1.first)-(p2.second-p2.first);
}
});
}
static void sortF(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.first==p2.first) {
return p1.second-p2.second;
}
return p1.first - p2.first;
}
});
}
static int[] shift (int[]inp,int i,int j){
int[]arr=new int[j-i+1];
int n=j-i+1;
for(int k=i;k<=j;k++) {
arr[(k-i+1)%n]=inp[k];
}
for(int k=0;k<n;k++ ) {
inp[k+i]=arr[k];
}
return inp;
}
static long[] fac;
static long mod = (long) 1000000007;
static void initFac(long n) {
fac = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = (fac[i - 1] * i) % mod;
}
}
static long nck(int n, int k) {
if (n < k)
return 0;
long den = inv((int) (fac[k] * fac[n - k] % mod));
return fac[n] * den % mod;
}
static long inv(long x) {
return pow(x, mod - 2);
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) {
Queue<Integer>q=new LinkedList<>();
q.add(source);
visited[source]=true;
int distance=0;
while(!q.isEmpty()) {
int curr=q.poll();
distance++;
for(int neighbour:a[curr]) {
if(!visited[neighbour]) {
visited[neighbour]=true;
q.add(neighbour);
}
}
}
return distance;
}
public static Set<Integer>factors(int n){
Set<Integer>ans=new HashSet<>();
ans.add(1);
for(int i=2;i*i<=n;i++) {
if(n%i==0) {
ans.add(i);
ans.add(n/i);
}
}
return ans;
}
public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) {
boolean[]visited=new boolean[a.length];
int[]parent=new int[a.length];
Queue<Integer>q=new LinkedList<>();
int distance=0;
q.add(source);
visited[source]=true;
parent[source]=-1;
while(!q.isEmpty()) {
int curr=q.poll();
if(curr==destination) {
break;
}
for(int neighbour:a[curr]) {
if(neighbour!=-1&&!visited[neighbour]) {
visited[neighbour]=true;
q.add(neighbour);
parent[neighbour]=curr;
}
}
}
int cur=destination;
while(parent[cur]!=-1) {
distance++;
cur=parent[cur];
}
return distance;
}
static int bs(int size,int[]arr) {
int x = -1;
for (int b = size/2; b >= 1; b /= 2) {
while (!ok(arr));
}
int k = x+1;
return k;
}
static boolean ok(int[]x) {
return false;
}
public static int solve1(ArrayList<Integer> A) {
long[]arr =new long[A.size()+1];
int n=A.size();
for(int i=1;i<=A.size();i++) {
arr[i]=((i%2)*((n-i+1)%2))%2;
arr[i]%=2;
}
int ans=0;
for(int i=0;i<A.size();i++) {
if(arr[i+1]==1) {
ans^=A.get(i);
}
}
return ans;
}
public static String printBinary(long a) {
String str="";
for(int i=31;i>=0;i--) {
if((a&(1<<i))!=0) {
str+=1;
}
if((a&(1<<i))==0 && !str.isEmpty()) {
str+=0;
}
}
return str;
}
public static String reverse(long a) {
long rev=0;
String str="";
int x=(int)(Math.log(a)/Math.log(2))+1;
for(int i=0;i<32;i++) {
rev<<=1;
if((a&(1<<i))!=0) {
rev|=1;
str+=1;
}
else {
str+=0;
}
}
return str;
}
////////////////////////////////////////////////////////
static void sortS(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.second - p2.second;
}
});
}
static class Pair implements Comparable<Pair>
{
int first ;int second ;
public Pair(int x, int y)
{
this.first = x ;this.second = y ;
}
@Override
public boolean equals(Object obj)
{
if(obj == this)return true ;
if(obj == null)return false ;
if(this.getClass() != obj.getClass()) return false ;
Pair other = (Pair)(obj) ;
if(this.first != other.first)return false ;
if(this.second != other.second)return false ;
return true ;
}
@Override
public int hashCode()
{
return this.first^this.second ;
}
@Override
public String toString() {
String ans = "" ;ans += this.first ; ans += " "; ans += this.second ;
return ans ;
}
@Override
public int compareTo(Main.Pair o) {
{ if(this.first==o.first) {
return this.second-o.second;
}
return this.first - o.first;
}
}
}
//////////////////////////////////////////////////////////////////////////
static int nD(long num) {
String s=String.valueOf(num);
return s.length();
}
static int CommonDigits(int x,int y) {
String s=String.valueOf(x);
String s2=String.valueOf(y);
return 0;
}
static int lcs(String str1, String str2, int m, int n)
{
int L[][] = new int[m + 1][n + 1];
int i, j;
// Following steps build L[m+1][n+1] in
// bottom up fashion. Note that L[i][j]
// contains length of LCS of str1[0..i-1]
// and str2[0..j-1]
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (str1.charAt(i - 1)
== str2.charAt(j - 1))
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j],
L[i][j - 1]);
}
}
// L[m][n] contains length of LCS
// for X[0..n-1] and Y[0..m-1]
return L[m][n];
}
/////////////////////////////////
boolean IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
////////////////////////////////
static long power(long a,long b,long m ) {
long ans=1;
while(b>0) {
if(b%2==1) {
ans=((ans%m)*(a%m))%m; b--;
}
else {
a=(a*a)%m;b/=2;
}
}
return ans%m;
}
///////////////////////////////
public static boolean repeatedSubString(String string) {
return ((string + string).indexOf(string, 1) != string.length());
}
static int search(char[]c,int start,int end,char x) {
for(int i=start;i<end;i++) {
if(c[i]==x) {return i;}
}
return -2;
}
////////////////////////////////
static long gcd(long a, long b) {
while (b != 0) {
long t = b;
b = a % b;
a = t;
}
return a;
}
static long fac(long a)
{
if(a== 0L || a==1L)return 1L ;
return a*fac(a-1L) ;
}
static ArrayList al() {
ArrayList<Integer>a =new ArrayList<>();
return a;
}
static HashSet h() {
return new HashSet<Integer>();
}
static void debug(int[][]a) {
int n= a.length;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
out.print(a[i][j]+" ");
}
out.print("\n");
}
}
static void debug(int[]a) {
out.println(Arrays.toString(a));
}
static void debug(ArrayList<Integer>a) {
out.println(a.toString());
}
static boolean[]seive(int n){
boolean[]b=new boolean[n+1];
for (int i = 2; i <= n; i++)
b[i] = true;
for(int i=2;i*i<=n;i++) {
if(b[i]) {
for(int j=i*i;j<=n;j+=i) {
b[j]=false;
}
}
}
return b;
}
static int longestIncreasingSubseq(int[]arr) {
int[]sizes=new int[arr.length];
Arrays.fill(sizes, 1);
int max=1;
for(int i=1;i<arr.length;i++) {
for(int j=0;j<i;j++) {
if(arr[j]<arr[i]) {
sizes[i]=Math.max(sizes[i],sizes[j]+1);
max=Math.max(max, sizes[i]);
}
}
}
return max;
}
public static ArrayList primeFactors(long n)
{
ArrayList<Long>h= new ArrayList<>();
// Print the number of 2s that divide n
if(n%2 ==0) {h.add(2L);}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i <= Math.sqrt(n); i+= 2)
{
if(n%i==0) {h.add(i);}
}
if (n > 2)
h.add(n);
return h;
}
static boolean Divisors(long n){
if(n%2==1) {
return true;
}
for (long i=2; i<=Math.sqrt(n); i++){
if (n%i==0 && i%2==1){
return true;
}
}
return false;
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
public static void superSet(int[]a,ArrayList<String>al,int i,String s) {
if(i==a.length) {
al.add(s);
return;
}
superSet(a,al,i+1,s);
superSet(a,al,i+1,s+a[i]+" ");
}
public static long[] makeArr() {
long size=in.nextInt();
long []arr=new long[(int)size];
for(int i=0;i<size;i++) {
arr[i]=in.nextInt();
}
return arr;
}
public static long[] arr(int n) {
long []arr=new long[n+1];
for(int i=1;i<n+1;i++) {
arr[i]=in.nextLong();
}
return arr;
}
public static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void println(long x) {
out.println(x);
}
static void print(long c) {
out.print(c);
}
static void print(int c) {
out.print(c);
}
static void println(int x) {
out.println(x);
}
static void print(String s) {
out.print(s);
}
static void println(String s) {
out.println(s);
}
public static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
//Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
public static void reverse(int[] array)
{
int n = array.length;
for (int i = 0; i < n / 2; i++) {
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
public static long nCr(int n,int k)
{
long ans=1L;
k=k>n-k?n-k:k;
for( int j=1;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;
}
static int searchMax(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]>inp[index]) {
index+=1;
}
return index;
}
static int searchMin(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]<inp[index]) {
index+=1;
}
return index;
}
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
static class Pairr implements Comparable<Pairr>{
private int index;
private int cumsum;
private ArrayList<Integer>indices;
public Pairr(int index,int cumsum) {
this.index=index;
this.cumsum=cumsum;
indices=new ArrayList<Integer>();
}
public int compareTo(Pairr other) {
return Integer.compare(cumsum, other.cumsum);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
4a53ee6e4c48e4ee07dd635ead496b6e
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class level1300 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> al = new ArrayList<>();
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
int x = Math.max(n-i, i-1)+Math.max(m-j,j-1);
al.add(x);
}
}
Collections.sort(al);
for(int i=0;i<al.size();i++) {
if(i==al.size()-1)
sb.append(al.get(i)+"\n");
else
sb.append(al.get(i)+" ");
}
}
System.out.println(sb);
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
b2afc0d4b59bca0503a0f252fa8b8fa6
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static long mod = 1000000007;
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while(t-- > 0) {
int n = fs.nextInt();
int m = fs.nextInt();
ArrayList <Integer> list = new ArrayList<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int a1 = Math.abs((i+1)-1) + Math.abs((j+1)-1);
int a2 = Math.abs((i+1)-1) + Math.abs((j+1)-m);
int a3 = Math.abs((i+1)-n) + Math.abs((j+1)-1);
int a4 = Math.abs((i+1)-n) + Math.abs((j+1)-m);
int mx = Math.max(Math.max(a1,a2), Math.max(a3,a4));
list.add(mx);
}
}
Collections.sort(list);
for(Integer x : list) {
System.out.print(x + " ");
}
System.out.println();
}
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d6c5531f3b039fff8a2ea1c662e95f44
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
/*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.lang.Math.*;
public class KickStart2020 {
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;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static class Pair implements Comparable<Pair> {
public int index;
public int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
// multiplied to -1 as the author need descending sort order
if(other.index < this.index) return -1;
if(other.index > this.index) return 1;
if(other.value < this.value) return -1;
if(other.value > this.value) return 1;
return 0;
}
@Override
public String toString() {
return this.index + " " + value;
}
}
static boolean isPrime(long d) {
if (d == 1)
return false;
for (int i = 2; i <= (long) Math.sqrt(d); i++) {
if (d % i == 0)
return false;
}
return true;
}
static String decimalTob(int n, int k , String s) {
int x = n % k;
int y = n / k;
s += x;
if(y > 0) {
s = decimalTob(y, k, s);
}
return s;
}
static long powermod(long x, long y, long mod) {
long ans = 1;
x = x % mod;
if (x == 0)
return 0;
int i = 1;
while (y > 0) {
if ((y & 1) != 0)
ans = (ans * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return ans;
}
static long power(long x, long y)
{
long res = 1;
while (y > 0)
{
if ((y & 1) != 0)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
outerloop:
while(t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> ss = new ArrayList<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
ss.add((int)Math.max(i, n - i - 1) + (int)Math.max(j, m - j - 1));
}
}
Collections.sort(ss);
for(int e: ss) out.print(e + " ");
out.println();
}
out.close();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
64f124005fc7dcd83c9a62c62fbf3e1a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) {
while (N-- > 0) {
solve();
}
out.close();
}
public static void solve() {
int m = sc.nextInt();
int n = sc.nextInt();
int[] res = new int[m * n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res[i * n + j] = Math.max(Math.max(i + j, m - i - 1 + j),
Math.max(i + n - j - 1, m - i - 1 + n - j - 1));
}
}
betterSort(res);
for (int i : res) {
out.print(i + " ");
}
out.println();
}
private static void betterSort(int[] a) {
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int ran = r.nextInt(a.length);
int tmp = a[i];
a[i] = a[ran];
a[ran] = tmp;
}
Arrays.sort(a);
}
private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
private static MyScanner sc = new MyScanner();
private static int N = sc.nextInt();
private final static int MOD = 1000000007;
@SuppressWarnings("unused")
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
f5545c9f43e78780807dff6d0563ad9d
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class NotSitting1627B
{
static Scanner sc;
static void solve()
{
int n = sc.nextInt();
int m = sc.nextInt();
int array[] = new int[n*m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
array[i*m+j] = Math.max(i,n-1-i) + Math.max(j,m-1-j);
}
}
Arrays.sort(array);
for(int i=0;i<n*m;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
}
public static void main(String[] args)
{
sc = new Scanner(System.in);
int tests = sc.nextInt();
while (tests-->0)
{
solve();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
4f69a79d774cf7f262c51bcd8bd1b3a6
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
while(test-->0){
int n=sc.nextInt();
int m=sc.nextInt();
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int d1=i+j;
int d2=i+(m-j-1);
int d3=(n-i-1)+j;
int d4=(n-i-1)+(m-j-1);
al.add(Math.max(d1,Math.max(d2,Math.max(d3,d4))));
}
}
Collections.sort(al);
for(int i=0;i<al.size();i++){
System.out.print(al.get(i)+" ");
}
al.clear();
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
bb1501e6b9393f847739f740f204befb
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class NotSitting{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCases = Integer.parseInt(br.readLine());
StringBuilder result = new StringBuilder();
StringTokenizer str;
while(testCases-- > 0){
str = new StringTokenizer(br.readLine());
int n = Integer.parseInt(str.nextToken());
int m = Integer.parseInt(str.nextToken());
int s = n/2 + m/2;
int min = 0;
List<Integer> answer = new ArrayList<>();
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(n % 2 == 0 && m % 2== 0){
int a1 = n/2 ;
int a2 = a1 - 1;
int b1 = m/2;
int b2 = b1 - 1;
min = Math.min(Math.abs(a1 - i) + Math.abs(b1 - j), Math.abs(a2 - i) + Math.abs(b2 - j));
min = Math.min(min, Math.abs(a1 - i) + Math.abs(b2 - j));
min = Math.min(min, Math.abs(a2 - i) + Math.abs(b1 - j));
}else if(n % 2 == 1 && m % 2 == 1){
int a1 = n/2 ;
int b1 = m/2;
min = Math.abs(a1 - i) + Math.abs(b1 - j);
} else if(n % 2 == 0){
int a1 = n/2 ;
int a2 = a1 - 1;
int b1 = m/2;
min = Math.min(Math.abs(a1 - i) + Math.abs(b1 - j), Math.abs(a2 - i) + Math.abs(b1 - j));
} else{
int a1 = n/2 ;
int b1 = m/2;
int b2 = b1 - 1;
min = Math.min(Math.abs(a1 - i) + Math.abs(b1 - j), Math.abs(a1 - i) + Math.abs(b2 - j));
}
answer.add(min + s);
}
}
Collections.sort(answer);
for(int i : answer){
result.append(i + " ");
}
result.append("\n");
}
System.out.println(result);
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 11
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
0c86642b365bee77f8c46547251d2e75
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class ferrisWheel {
static PrintWriter pw = new PrintWriter(System.out);
static Scanner sc = new Scanner(System.in);
static int n,m;
static boolean [][]vis;
public static boolean vaild(int i,int j) {
if(i<0 || i>=n || j<0 || j>=m || vis[i][j])
return false;
return true;
}
public static void main(String[] args) throws IOException, InterruptedException {
int t=sc.nextInt();
while(t-->0) {
n=sc.nextInt();m=sc.nextInt();
int p1,p2,p3,p4;
int min=n/2+m/2;
if(n%2==0) {
p1=n/2-1;p2=n/2;
}
else {
p1=p2=n/2;
}
if(m%2==0) {
p3=m/2-1;p4=m/2;
}
else {
p3=p4=m/2;
}
vis=new boolean[n][m];
Queue<tuble> q=new LinkedList<>();
q.add(new tuble(p1, p3, min));
vis[p1][p3]=true;
if(!vis[p1][p4]) {
vis[p1][p4]=true;
q.add(new tuble(p1, p4, min));
}
if(!vis[p2][p3]) {
vis[p2][p3]=true;
q.add(new tuble(p2, p3, min));
}
if(!vis[p2][p4]) {
vis[p2][p4]=true;
q.add(new tuble(p2, p4, min));
}
while(!q.isEmpty()) {
tuble cur=q.poll();
pw.print(cur.z+" ");
if(vaild(cur.x+1, cur.y)) {
vis[cur.x+1][cur.y]=true;
q.add(new tuble(cur.x+1, cur.y, cur.z+1));
}
if(vaild(cur.x-1, cur.y)) {
vis[cur.x-1][cur.y]=true;
q.add(new tuble(cur.x-1, cur.y, cur.z+1));
}
if(vaild(cur.x, cur.y+1)) {
vis[cur.x][cur.y+1]=true;
q.add(new tuble(cur.x, cur.y+1, cur.z+1));
}
if(vaild(cur.x, cur.y-1)) {
vis[cur.x][cur.y-1]=true;
q.add(new tuble(cur.x, cur.y-1, cur.z+1));
}
}
pw.println();
}
pw.flush();
}
static class pair implements Comparable<pair> {
// long x,y;
int x,y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
d41fca887012e6e4c65985464a5faa4c
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import javax.management.Query;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
//Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int m=sc.nextInt();
//boolean[] vis=new boolean[n*m];
PriorityQueue<Integer> p=new PriorityQueue<Integer>();
// if(n*m==2) {
// pw.println("1 1");
// continue;
// }
for (int i = 0; i <m; i++) {
for (int j = 0; j < n; j++) {
p.add(max4(i+j,i+n-j-1,m-i+j-1,m+n-i-j-2));
}
}
for (int i = 0; i < n*m; i++) {
pw.print(p.remove()+" ");
}
pw.println();
}
pw.close();
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
e5da0c452a8acf6b2d80e09fda443393
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package com.company;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.Math;
public class Main
{
public static void main(String[]args) {
Scanner in = new Scanner(System.in);
//int n,m;
int t;
t=in.nextInt();
int m,n;
int r,c;
int dis[]=new int[100100];
while(t-->0)
{
int ans=0,a=0;
m=in.nextInt();
n=in.nextInt();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
dis[i*n+j]=Math.max(j,n-j-1)+Math.max(i,m-i-1);
}
}
Arrays.sort(dis,0,n*m-1);
for(int i=0;i<n*m;i++)
{
System.out.print(dis[i]+" ");
}
System.out.print("\n");
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
40a44f78d74dc0d0911915c4dbbd404a
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;
import java.lang.*;
public class Main{
private static FastReader fs;
public static void main(String[] args) throws Exception{
fs = new FastReader();
int test = fs.nextInt();
while(test-- > 0) solve();
}
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;
}
}
// use fs as scanner
// snippets -> sieve, power, Node , lowerbound, upperbound, readarray
public static int dis(int x, int y, int a, int b){
return Math.abs(x-a) + Math.abs(y-b);
}
public static void solve(){
int row = fs.nextInt();
int col = fs.nextInt();
int limit = row - col;
int disnumber[] = new int[(row*col - 2) + 5];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// she should fill the entire row/col (max) of them
// int dis[][] = new int[row][col];
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
int t1 = Math.max(dis(i,j,0,0), dis(i,j,0,col-1));
int t2 = Math.max(dis(i,j,row-1,0), dis(i,j,row-1,col-1));
int temp = Math.max(t1, t2);
disnumber[temp]++;
min = Math.min(min,temp);
max = Math.max(max,temp);
// 0,0
// 0,col
// row,0
// row,col
}
}
// System.out.println(min + " " + max);
// int start = 0;
// for(int ele : disnumber){
// System.out.println(start + " " + ele);
// start++;
// }
// System.out.println(disnumber[min] + " " + disnumber[max]);
StringBuilder ans = new StringBuilder();
int pre = 0;
int check;
for(int i=0; i<row*col;){
if( min == max ){
ans.append(min+" ");
i++;
}
else if( disnumber[min] > 0 ){
ans.append(min+" ");
i++;
disnumber[min]--;
}
else{
min++;
}
}
System.out.println(ans);
}
}
/*
do something when you are stuck
and be calm
*/
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
3679b8a67ff9ec07d9ce3c5f545bb0fc
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B_NotSitting_1300 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] ans = new int[n * m];
int b = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int corner1Distance = i + j;
int corner2Distance = i + (m - j - 1);
int corner3Distance = (n - i - 1) + j;
int corner4Distance = (n - i - 1) + (m - j - 1);
ans[b] = Math.max(Math.max(corner1Distance, corner2Distance), Math.max(corner3Distance, corner4Distance));
b++;
}
}
Arrays.sort(ans);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n * m; i++) {
sb.append(ans[i] + " ");
}
System.out.println(sb.toString().trim());
}
out.close();
}
public static PrintWriter out;
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
public int x,y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
5da4f0d97adbdbe85f097d58c17c0424
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Program {
public static void main(String[] args){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
// code
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int tt=0; tt<t; tt++) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr = new int[m*n];
int count = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int dist = Math.max(i,n-i-1) + Math.max(j,m-j-1);
arr[count] = dist;
count++;
}
}
Arrays.sort(arr);
for(int i=0;i<m*n;i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
}
return;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
c5e4e5d4dfda89662c2200027081f0c5
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import javax.swing.plaf.basic.BasicBorders;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Solution solve = new Solution();
int t = scan.nextInt();
// int t = 1;
for (int qq = 0; qq < t; qq++) {
solve.solve(scan, out);
out.println();
}
out.close();
}
static class Solution {
/*
* think and coding
*/
public void solve(FastReader scan, PrintWriter out) {
int n = scan.nextInt(), m = scan.nextInt();
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
if (i < n / 2) {
for (int j = 0; j < m; j++) {
if (j < m / 2) {
arr[i][j] = Math.abs(n - i - 1) + Math.abs(m - j - 1);
} else {
arr[i][j] = Math.abs(n - i - 1) + Math.abs(j);
}
}
} else {
for (int j = 0; j < m; j++) {
if (j < m / 2) {
arr[i][j] = Math.abs(i) + Math.abs(m - j - 1);
} else {
arr[i][j] = Math.abs(i) + Math.abs(j);
}
}
}
}
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
queue.add(arr[i][j]);
}
}
while (!queue.isEmpty()){
out.print(queue.poll() + " ");
}
}
}
static class FastReader {
private final BufferedReader br;
private StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
eab39678fbc20d7ec91d23a5f72f7485
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Codeforces {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
if(n > m) {
int temp = n;
n = m;
m = temp;
}
int dist_max = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
int dist1 = Math.abs(i-1)+Math.abs(j-1);
int dist2 = Math.abs(i-1)+Math.abs(j-m);
int dist3 = Math.abs(i-n)+Math.abs(j-1);
int dist4 = Math.abs(i-n)+Math.abs(j-m);
dist_max = Math.max(dist1, dist2);
dist_max = Math.max(dist_max, dist3);
dist_max = Math.max(dist_max, dist4);
}
}
int[] a = new int[dist_max+1];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
int dist1 = Math.abs(i-1)+Math.abs(j-1);
int dist2 = Math.abs(i-1)+Math.abs(j-m);
int dist3 = Math.abs(i-n)+Math.abs(j-1);
int dist4 = Math.abs(i-n)+Math.abs(j-m);
int dist = Math.min(dist1, dist2);
dist = Math.min(dist, dist3);
dist = Math.min(dist, dist4);
a[dist]++;
}
}
int ans = n+m-2-dist_max;
for(int i = dist_max; i >= 0; i--) {
for(int j = 0; j < a[i]; j++) {
writer.print(ans+" ");
}
ans++;
}
writer.println();
}
br.close();
writer.close();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
e3a2ebd1d0b737c840f185d3bc3f79ef
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package com.example.lib;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
public class Algorithm {
static int ans =0;
public static void main(String[] rgs) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\USER\\AndroidStudioProjects\\MyApplication\\lib\\src\\main\\java\\com\\example\\lib\\inpu"));
StringBuilder stringBuilder = new StringBuilder();
int t = Integer.parseInt(bufferedReader.readLine());
for (int i = 0; i < t; i++) {
String[] a= bufferedReader.readLine().split(" ");
int row= Integer.parseInt(a[0]);
int col= Integer.parseInt(a[1]);
StringBuilder ans = new StringBuilder();
int [] ansarray = new int[row*col];
int p=0;
int [] rowarray= {0,0,row-1,row-1};
int [] colarray= {0,col-1,0,col-1};
for (int j = 0; j < row; j++) {
for (int k = 0; k < col; k++) {
int dis=0;
for (int l = 0; l < 4; l++) {
dis= Math.max(dis, Math.abs(rowarray[l]-j)+Math.abs(colarray[l]-k));
}
ansarray[p++]=dis;
}
}
Arrays.sort(ansarray);
for (int j = 0; j < ansarray.length; j++) {
ans.append(ansarray[j]).append(" ");
}
stringBuilder.append(ans).append("\n");
}
System.out.println(stringBuilder);
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
b67e9e8838c85281880797a767851156
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class NotSitting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numTests = sc.nextInt();
for (int test = 0; test < numTests; test++) {
int numRows = sc.nextInt();
int numCols = sc.nextInt();
int[][] corners = {{0, 0}, {numRows - 1, 0}, {0, numCols - 1}, {numRows - 1, numCols - 1}};
PriorityQueue<Integer> solution = new PriorityQueue<>();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
int max = -1;
for (int[] corner : corners) {
max = Math.max(Math.abs(corner[0] - i) + Math.abs(corner[1] - j), max);
}
solution.add(max);
}
}
while (!solution.isEmpty()) {
System.out.print(solution.remove() + " ");
}
System.out.println();
}
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
46fecf0c7534901c3772717c046219b0
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
// package faltu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class Main {
static long LowerBound(long[] a2, long x) { // x is the target value or key
int l=-1,r=a2.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a2[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(int a[], int x) {// x is the key or target value
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static long getClosest(long val1, long val2,long target)
{
if (target - val1 >= val2 - target)
return val2;
else
return val1;
}
static void ruffleSort(long[] a) {
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
long oi=r.nextInt(n), temp=a[i];
a[i]=a[(int)oi];
a[(int)oi]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a){
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
Arrays.sort(a);
}
int ceilIndex(int input[], int T[], int end, int s){
int start = 0;
int middle;
int len = end;
while(start <= end){
middle = (start + end)/2;
if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){
return middle+1;
}else if(input[T[middle]] < s){
start = middle+1;
}else{
end = middle-1;
}
}
return -1;
}
public static int findIndex(long arr[], long t)
{
if (arr == null) {
return -1;
}
int len = arr.length;
int i = 0;
while (i < len) {
if (arr[i] == t) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
public static int[] swap(int a[], int left, int right)
{
int temp = a[left];
a[left] = a[right];
a[right] = temp;
return a;
}
public static void swap(long x,long max1)
{
long temp=x;
x=max1;
max1=temp;
}
public static int[] reverse(int a[], int left, int right)
{
// Reverse the sub-array
while (left < right) {
int temp = a[left];
a[left++] = a[right];
a[right--] = temp;
}
return a;
}
static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) {
int n =A.size();
int first = 0,second = n;
while(first <second) {
int mid = first + (second-first)/2;
if(A.get(mid) > B) {
second = mid;
}else {
first = mid+1;
}
}
if(first < n && A.get(first) < B) {
first++;
}
return first; //1 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;
}
}
// *******----segement tree implement---*****
// -------------START--------------------------
void buildTree (int[] arr,int[] tree,int start,int end,int treeNode)
{
if(start==end)
{
tree[treeNode]=arr[start];
return;
}
buildTree(arr,tree,start,end,2*treeNode);
buildTree(arr,tree,start,end,2*treeNode+1);
tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1];
}
void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value)
{
if(start==end)
{
arr[idx]=value;
tree[treeNode]=value;
return;
}
int mid=(start+end)/2;
if(idx>mid)
{
updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value);
}
else
{
updateTree(arr,tree,start,mid,2*treeNode,idx,value);
}
tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1];
}
// disjoint set implementation --start
static void makeSet(int n)
{
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++)
{
parent[i]=i;
rank[i]=0;
}
}
static void union(int u,int v)
{
u=findpar(u);
v=findpar(v);
if(rank[u]<rank[v])parent[u]=v;
else if(rank[v]<rank[u])parent[v]=u;
else
{
parent[v]=u;
rank[u]++;
}
}
private static int findpar(int node)
{
if(node==parent[node])return node;
return parent[node]=findpar(parent[node]);
}
static int parent[];
static int rank[];
// *************end
static Long MOD=(long) (1e9+7);
static boolean coprime(int a, long l){
return (gcd(a, l) == 1);
}
static long[][] dp;
static long[]dpp;
static ArrayList<Integer>arr;
static boolean[] vis;
static ArrayList<ArrayList<Integer>>adj;
public static void main(String[] args) throws IOException
{
// sieve();
FastReader s = new FastReader();
long tt = s.nextLong();
while(tt-->0) {
adj=new ArrayList<>();
int n=s.nextInt();
int m=s.nextInt();
long dis=(n/2+m/2);
ArrayList<Long>ans=new ArrayList<>();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(n%2==1&&m%2==1)
{
long a=n/2,b=m/2;
long extra=Math.abs(a-i)+Math.abs(b-j);
ans.add(dis+extra);
}
else if(n%2==0&&m%2==0)
{
long x1=n/2,y1=m/2,x2=n/2-1,y2=m/2-1;
long[]x=new long[] {x1,x1,x2,x2};
long[]y=new long[] {y1,y2,y1,y2};
long extra=Integer.MAX_VALUE;
for(int k=0;k<4;k++) {
extra=Math.min(extra, (Math.abs(x[k]-i)+Math.abs(y[k]-j)));
}
ans.add(extra+dis);
}
else if(n%2==0)
{
long r1=n/2,r2=n/2-1;
long c1=m/2;
long extra=Math.min((Math.abs(r1-i)+Math.abs(c1-j)),(Math.abs(r2-i)+Math.abs(c1-j)));
ans.add(dis+extra);
}
else if(m%2==0)
{
long c1=m/2,c2=m/2-1;
long r1=n/2;
long extra=Math.min((Math.abs(r1-i)+Math.abs(c1-j)),(Math.abs(r1-i)+Math.abs(c2-j)));
ans.add(dis+extra);
}
}
}
Collections.sort(ans);
for(Long i:ans)System.out.print(i+" ");
System.out.println();
}
}
static void DFSUtil(int v, boolean[] visited)
{
visited[v] = true;
Iterator<Integer> it = adj.get(v).iterator();
while (it.hasNext()) {
int n = it.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
static long DFS(int n)
{
boolean[] visited = new boolean[n+1];
long cnt=0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
DFSUtil(i, visited);
cnt++;
}
}
return cnt;
}
public static String revStr(String str){
String input = str;
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1.reverse();
return input1.toString();
}
public static String sortString(String inputString){
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static long myPow(long n, long i){
if(i==0) return 1;
if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD;
return (n%MOD* myPow(n,i-i)%MOD)%MOD;
}
static void palindromeSubStrs(String str) {
HashSet<String>set=new HashSet<>();
char[]a =str.toCharArray();
int n=str.length();
int[][]dp=new int[n][n];
for(int g=0;g<n;g++){
for(int i=0,j=g;j<n;j++,i++){
if(!set.contains(str.substring(i,i+1))&&g==0) {
dp[i][j]=1;
set.add(str.substring(i,i+1));
}
else {
if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) {
dp[i][j]=1;
set.add(str.substring(i,j+1));
}
}
}
}
int ans=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
System.out.print(dp[i][j]+" ");
if(dp[i][j]==1)ans++;
}
System.out.println();
}
System.out.println(ans);
}
static boolean isPalindrome(String str,int i,int j)
{
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean sign(long num) {
return num>0;
}
static boolean isSquare(long x){
if(x==1)return true;
long y=(long) Math.sqrt(x);
return y*y==x;
}
static long power1(long a,long b) {
if(b == 0){
return 1;
}
long ans = power(a,b/2);
ans *= ans;
if(b % 2!=0){
ans *= a;
}
return ans;
}
static void swap(StringBuilder sb,int l,int r)
{
char temp = sb.charAt(l);
sb.setCharAt(l,sb.charAt(r));
sb.setCharAt(r,temp);
}
// function to reverse the string between index l and r
static void reverse(StringBuilder sb,int l,int r)
{
while(l < r)
{
swap(sb,l,r);
l++;
r--;
}
}
// function to search a character lying between index l and r
// which is closest greater (just greater) than val
// and return it's index
static int binarySearch(StringBuilder sb,int l,int r,char val)
{
int index = -1;
while (l <= r)
{
int mid = (l+r)/2;
if (sb.charAt(mid) <= val)
{
r = mid - 1;
}
else
{
l = mid + 1;
if (index == -1 || sb.charAt(index) >= sb.charAt(mid))
index = mid;
}
}
return index;
}
// this function generates next permutation (if there exists any such permutation) from the given string
// and returns True
// Else returns false
static boolean nextPermutation(StringBuilder sb)
{
int len = sb.length();
int i = len-2;
while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1))
i--;
if (i < 0)
return false;
else
{
int index = binarySearch(sb,i+1,len-1,sb.charAt(i));
swap(sb,i,index);
reverse(sb,i+1,len-1);
return true;
}
}
private static int lps(int m ,int n,String s1,String s2,int[][]mat)
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1];
else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]);
}
}
return mat[m][n];
}
static String lcs(String X, String Y, int m, int n)
{
int[][] L = new int[m+1][n+1];
// Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i-1) == Y.charAt(j-1))
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
// Following code is used to print LCS
int index = L[m][n];
int temp = index;
// Create a character array to store the lcs string
char[] lcs = new char[index+1];
lcs[index] = '\u0000'; // Set the terminating character
// Start from the right-most-bottom-most corner and
// one by one store characters in lcs[]
int i = m;
int j = n;
while (i > 0 && j > 0)
{
// If current character in X[] and Y are same, then
// current character is part of LCS
if (X.charAt(i-1) == Y.charAt(j-1))
{
// Put current character in result
lcs[index-1] = X.charAt(i-1);
// reduce values of i, j and index
i--;
j--;
index--;
}
// If not same, then find the larger of two and
// go in the direction of larger value
else if (L[i-1][j] > L[i][j-1])
i--;
else
j--;
}
return String.valueOf(lcs);
// Print the lcs
// System.out.print("LCS of "+X+" and "+Y+" is ");
// for(int k=0;k<=temp;k++)
// System.out.print(lcs[k]);
}
static long lis(long[] aa2, int n)
{
long lis[] = new long[n];
int i, j;
long max = 0;
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1)
lis[i] = lis[j] + 1;
for (i = 0; i < n; i++)
if (max < lis[i])
max = lis[i];
return max;
}
static boolean isPalindrome(String str)
{
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean issafe(int i, int j, int r,int c, char ch)
{
if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false;
else return true;
}
static long power(long a, long b)
{
a %=MOD;
long out = 1;
while (b > 0) {
if((b&1)!=0)out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
a*=a;
}
return out;
}
static long[] sieve;
public static void sieve()
{
int nnn=(int) 1e6+1;
long nn=(int) 1e6;
sieve=new long[(int) nnn];
int[] freq=new int[(int) nnn];
sieve[0]=0;
sieve[1]=1;
for(int i=2;i<=nn;i++)
{
sieve[i]=1;
freq[i]=1;
}
for(int i=2;i*i<=nn;i++)
{
if(sieve[i]==1)
{
for(int j=i*i;j<=nn;j+=i)
{
if(sieve[j]==1)
{
sieve[j]=0;
}
}
}
}
}
}
class decrease implements Comparator<Long> {
// Used for sorting in ascending order of
// roll number
public int compare(long a, long b)
{
return (int) (b - a);
}
@Override
public int compare(Long o1, Long o2) {
// TODO Auto-generated method stub
return (int) (o2-o1);
}
}
class pair{
long x;
long y;
long c;
public pair(long x,long y) {
this.x=x;
this.y=y;
}
public pair(long x,long y,long c)
{
this.x=x;
this.y=y;
this.c=c;
}
}
class Pair {
int idx;
String str;
public Pair(String str,int idx) {
this.str=str;
this.idx=idx;
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
113cddb6676e9837ef8ce383e070b657
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
int first, second,third;
public Tuple(int first, int second, int third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader
{
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) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
for (int p = 2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i = p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p = 2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(int a[], int x)
{
int l=-1, r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int tt = sc.nextInt();
while(tt-- > 0)
{
int n = sc.nextInt(), m = sc.nextInt();
List<Integer> list = new ArrayList<>();
for(int i = 0;i<n;i++)
{
for(int j = 0;j<m;j++)
{
int w = i + j, x = (i + (m - j - 1)), y = j + (n - i - 1), z = (n - i - 1) + (m - j - 1);
int maxs = Math.max(Math.max(w,x),Math.max(y,z));
list.add(maxs);
}
}
Collections.sort(list);
for(int it : list) fout.print(it + " ");
fout.println();
}
fout.close();
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
7b15adde9eeebaf4b2d4aca0cf7d5dfb
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package Codeforces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class B {
public static void main (String[] Z) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder op = new StringBuilder();
StringTokenizer stz;
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
stz = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stz.nextToken());
int m = Integer.parseInt(stz.nextToken());
int[][] dp = new int[n+1][m+1];
int max = (n-1) + (m-1);
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 1, l = n ; i <= l ; i++ , --l) {
int curr = max;
for (int j = 1 , k = m ; j <= k ; j++, --k) {
dp[i][j] = dp[i][k] = curr;
dp[l][j] = dp[l][k] = curr;
--curr;
}
--max;
}
for (int i = 1 ; i <= n ; i++) for (int j = 1 ; j <= m ; j++) pq.add(dp[i][j]);
while (!pq.isEmpty()) op.append(pq.poll() + " ");
op.append("\n");
}
System.out.println(op);
// END OF CODE
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
| |
PASSED
|
3a6cffebf32b058597f701fdb5f2b796
|
train_109.jsonl
|
1642257300
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \dots, n \cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
256 megabytes
|
//package Codeforces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class B {
// public static void solve(int[][] dp, int n, int m, int i, int j, int x, int y, int val) {
//
// if(i < 1 || i > n || j < 1 || j > m || dp[i][j] != -1) return;
//
// if(i < n && dp[i+1][j] == -1) {
// dp[i+1][j] = val + 1;
// }
// if(i > 2 && dp[i-1][j] == -1) {
// dp[i-1][j] = val + 1;
// }
// if(j < m && dp[i][j+1] == -1) {
// dp[i][j+1] = val + 1;
// }
// if(j > 2 && dp[i][j-1] == -1) {
// dp[i][j-1] = val + 1;
// }
//
// solve(dp, n, m, i+1, j, val+1);
// solve(dp, n, m, i-1, j, val+1);
// solve(dp, n, m, i, j-1, val+1);
// solve(dp, n, m, i, j+1, val+1);
//
// }
public static void main (String[] Z) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder op = new StringBuilder();
StringTokenizer stz;
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
stz = new StringTokenizer(br.readLine());
int n = Integer.parseInt(stz.nextToken());
int m = Integer.parseInt(stz.nextToken());
int[][] dp = new int[n+2][m+2];
int r = (n+1) >> 1;
int c = (m+1) >> 1;
for(int[] a : dp) Arrays.fill(a, -1);
int dist = Math.abs(r-n) + Math.abs(c - m);
dp[r][c] = dist;
int max = (n-1) + (m-1);
for (int i = 1, l = n ; i <= l ; i++ , --l) {
int curr = max;
for (int j = 1 , k = m ; j <= k ; j++, --k) {
dp[i][j] = dp[i][k] = curr;
dp[l][j] = dp[l][k] = curr;
--curr;
}
--max;
}
//
// for (int i = 1 ; i <= n ; i++) {
// for (int j = 1 ; j <= m ; j++) {
// System.out.print(dp[i][j] + " ");
// }
// System.out.println();
// }
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 1 ; i <= n ; i++) {
for (int j = 1 ; j <= m ; j++) {
pq.add(dp[i][j]);
}
}
while (!pq.isEmpty()) {
op.append(pq.poll() + " ");
}
op.append("\n");
}
System.out.println(op);
// END OF CODE
}
}
|
Java
|
["2\n\n4 3\n\n1 2"]
|
1 second
|
["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"]
|
NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$.
|
Java 8
|
standard input
|
[
"games",
"greedy",
"sortings"
] |
dc1dc5e5dd17d19760c772739ce244a7
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n \cdot m \leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \cdot m$$$ across all test cases does not exceed $$$10^5$$$.
| 1,300
|
For each test case, output $$$n \cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \in [0, n \cdot m - 1]$$$.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.