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
|
8597f0718fc41b8fe639138834ab8ce4
|
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 {
static FScanner sc = new FScanner();
static PrintWriter out = new PrintWriter(System.out);
static final Random random = new Random();
static long mod = 1000000007L;
static HashMap<String, Integer> map = new HashMap<>();
public static void main(String args[]) throws IOException {
int T = sc.nextInt();
while (T-- > 0) {
int n=sc.nextInt();
int m=sc.nextInt();
int k=0;
int[] a=new int[n*m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int d=0;
d=max(d,Math.abs(n+m-i-j-2));
d=max(d,Math.abs(m+i-j-1));
d=max(d,Math.abs(n-i+j-1));
d=max(d,i+j);
a[k]=d;
k++;
}
}
Arrays.sort(a);
for(int i=0;i<a.length;i++)
out.print(a[i]+" ");
out.println();
}
out.close();
}
// TemplateCode
static int max(int a, int b) {
if (a < b)
return b;
return a;
}
static int min(int a, int b) {
if (a < b)
return a;
return b;
}
static void ruffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static <E> void print(E res) {
System.out.println(res);
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static int abs(int a) {
if (a < 0)
return -1 * a;
return a;
}
static class FScanner {
BufferedReader br;
StringTokenizer st;
public FScanner() {
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[] readintarray(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] readlongarray(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
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 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
|
a5186ea692f93316bf173cd7618accbf
|
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.round766div2;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class B_Editorial {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
//initReaderPrinter(true);
initReaderPrinter(false);
solve(in.nextInt());
//solve(1);
}
/*
General tips
1. It is ok to fail, but it is not ok to fail for the same mistakes over and over!
2. Train smarter, not harder!
3. If you find an answer and want to return immediately, don't forget to flush before return!
*/
/*
Read before practice
1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level;
2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else;
3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked;
4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list
and re-try in the future.
5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly;
*/
/*
Read before contests and lockout 1 v 1
Mistakes you've made in the past contests:
1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked;
2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA;
3. Forgot about possible integer overflow;
When stuck:
1. Understand problem statements? Walked through test examples?
2. Take a step back and think about other approaches?
3. Check rank board to see if you can skip to work on a possibly easier problem?
4. If none of the above works, take a guess?
*/
static void solve(int testCnt) {
for (int testNumber = 0; testNumber < testCnt; testNumber++) {
int n = in.nextInt(), m = in.nextInt();
int[][] d = new int[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
d[i][j] = max(max(i + j, i + m - 1 - j), max(n - 1 - i + j, n - 1 - i + m - 1 - j));
}
}
Integer[] x = new Integer[n * m];
int k = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
x[k] = d[i][j];
k++;
}
}
Arrays.sort(x);
for(int v : x) out.print(v + " ");
out.println();
}
out.close();
}
static void initReaderPrinter(boolean test) {
if (test) {
try {
in = new InputReader(new FileInputStream("src/input.in"));
out = new PrintWriter(new FileOutputStream("src/output.out"));
} catch (IOException e) {
e.printStackTrace();
}
} else {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
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();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
Integer[] nextIntArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitive(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitiveOneIndexed(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextInt();
return a;
}
Long[] nextLongArray(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitive(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitiveOneIndexed(int n) {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextLong();
return a;
}
String[] nextStringArray(int n) {
String[] g = new String[n];
for (int i = 0; i < n; i++) g[i] = next();
return g;
}
List<Integer>[] readGraphOneIndexed(int n, int m) {
List<Integer>[] adj = new List[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt();
int v = nextInt();
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
List<Integer>[] readGraphZeroIndexed(int n, int m) {
List<Integer>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
/*
A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) {
int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
edgeCntForEachNode[v]++;
end1[i] = u;
end2[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[end1[i]][idxForEachNode[end1[i]]] = end2[i];
idxForEachNode[end1[i]]++;
adj[end2[i]][idxForEachNode[end2[i]]] = end1[i];
idxForEachNode[end2[i]]++;
}
return adj;
}
/*
A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) {
int[] from = new int[edgeCnt], to = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
//from u to v: u -> v
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
from[i] = u;
to[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[from[i]][idxForEachNode[from[i]]] = to[i];
idxForEachNode[from[i]]++;
}
return adj;
}
}
}
|
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
|
23906bb6fea90355dc8dfbab1f4e6a51
|
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.round766div2;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class B {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
//initReaderPrinter(true);
initReaderPrinter(false);
solve(in.nextInt());
//solve(1);
}
/*
General tips
1. It is ok to fail, but it is not ok to fail for the same mistakes over and over!
2. Train smarter, not harder!
3. If you find an answer and want to return immediately, don't forget to flush before return!
*/
/*
Read before practice
1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level;
2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else;
3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked;
4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list
and re-try in the future.
5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly;
*/
/*
Read before contests and lockout 1 v 1
Mistakes you've made in the past contests:
1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked;
2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA;
3. Forgot about possible integer overflow;
When stuck:
1. Understand problem statements? Walked through test examples?
2. Take a step back and think about other approaches?
3. Check rank board to see if you can skip to work on a possibly easier problem?
4. If none of the above works, take a guess?
*/
static void solve(int testCnt) {
for (int testNumber = 0; testNumber < testCnt; testNumber++) {
int n = in.nextInt(), m = in.nextInt();
ArrayDeque<Integer> qx = new ArrayDeque<>(), qy = new ArrayDeque<>();
boolean[][] visited = new boolean[n + 1][m + 1];
if(n % 2 != 0 && m % 2 != 0) {
qx.addLast((n + 1) / 2);
qy.addLast((m + 1) / 2);
visited[(n + 1) / 2][(m + 1) / 2] = true;
}
else if(n % 2 == 0 && m % 2 !=0) {
qx.addLast(n / 2);
qx.addLast(n / 2 + 1);
qy.addLast(m / 2 + 1);
qy.addLast(m / 2 + 1);
visited[n / 2][m / 2 + 1] = true;
visited[n / 2 + 1][m /2 + 1] = true;
}
else if(n % 2 != 0 && m % 2 == 0) {
qx.addLast(n / 2 + 1);
qx.addLast(n / 2 + 1);
qy.addLast(m / 2);
qy.addLast(m / 2 + 1);
visited[n / 2 + 1][m / 2] = true;
visited[n / 2 + 1][m /2 + 1] = true;
}
else {
qx.addLast(n / 2);
qx.addLast(n / 2);
qx.addLast(n / 2 + 1);
qx.addLast(n / 2 + 1);
qy.addLast(m / 2);
qy.addLast(m / 2 + 1);
qy.addLast(m / 2);
qy.addLast(m / 2 + 1);
visited[n / 2][m / 2] = true;
visited[n / 2 + 1][m / 2] = true;
visited[n / 2][m / 2 + 1] = true;
visited[n / 2 + 1][m / 2 + 1] = true;
}
int d = n / 2 + m / 2;
int[] ans = new int[n * m];
int j = 0;
int[] dx = {-1,0,1,0},dy={0,1,0,-1};
while(!qx.isEmpty()) {
int sz = qx.size();
for(int i = 0; i < sz; i++) {
int x = qx.removeFirst(), y = qy.removeFirst();
ans[j] = d;
j++;
for(int k = 0; k < 4;k++) {
int x1 = x + dx[k], y1 = y + dy[k];
if(x1 >= 1 && x1 <= n && y1>=1 && y1<=m && !visited[x1][y1]) {
qx.addLast(x1);
qy.addLast(y1);
visited[x1][y1] = true;
}
}
}
d++;
}
for(int v : ans) out.print(v + " ");
out.println();
}
out.close();
}
static void initReaderPrinter(boolean test) {
if (test) {
try {
in = new InputReader(new FileInputStream("src/input.in"));
out = new PrintWriter(new FileOutputStream("src/output.out"));
} catch (IOException e) {
e.printStackTrace();
}
} else {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
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();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
Integer[] nextIntArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitive(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitiveOneIndexed(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextInt();
return a;
}
Long[] nextLongArray(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitive(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitiveOneIndexed(int n) {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextLong();
return a;
}
String[] nextStringArray(int n) {
String[] g = new String[n];
for (int i = 0; i < n; i++) g[i] = next();
return g;
}
List<Integer>[] readGraphOneIndexed(int n, int m) {
List<Integer>[] adj = new List[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt();
int v = nextInt();
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
List<Integer>[] readGraphZeroIndexed(int n, int m) {
List<Integer>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
/*
A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) {
int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
edgeCntForEachNode[v]++;
end1[i] = u;
end2[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[end1[i]][idxForEachNode[end1[i]]] = end2[i];
idxForEachNode[end1[i]]++;
adj[end2[i]][idxForEachNode[end2[i]]] = end1[i];
idxForEachNode[end2[i]]++;
}
return adj;
}
/*
A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) {
int[] from = new int[edgeCnt], to = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
//from u to v: u -> v
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
from[i] = u;
to[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[from[i]][idxForEachNode[from[i]]] = to[i];
idxForEachNode[from[i]]++;
}
return adj;
}
}
}
|
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
|
2c6e13829e085bba206def30bcecd2c7
|
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 in = new Scanner(System.in);
int T = in.nextInt();
for (int i = 1; i <= T; i++) {
int n = in.nextInt();
int m = in.nextInt();
List<Integer> dist = getDist(n, m);
dist.sort(Integer::compareTo);
// for (int i1 = dist.size()-1; i1 >= 0; i1--) {
// System.out.print(dist.get(i1) + " ");
// }
for (Integer integer : dist) {
System.out.print(integer + " ");
}
System.out.println();
}
}
private static List<Integer> getDist(int n, int m) {
int[][] matrix = new int[n][m];
int max = n + m -2;
matrix[0][0] = max;
matrix[n-1][m-1] = max;
matrix[n-1][0] = max;
matrix[0][m-1] = max;
int av = m/2;
if(m%2 == 1) av++;
for (int i = 1; i < av; i++) {
matrix[0][i] = matrix[0][i-1]-1;
matrix[0][m-1-i] = matrix[0][m-i]-1;
matrix[n-1][i] = matrix[n-1][i-1]-1;
matrix[n-1][m-1-i] = matrix[n-1][m-i]-1;
}
av = n/2;
if(n%2 == 1) av++;
for (int i = 1; i < av; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = matrix[i-1][j]-1;
matrix[n-1-i][j] = matrix[n-i][j]-1;
}
}
List<Integer> l = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
l.add(matrix[i][j]);
}
}
return l;
}
// private static List<Integer> getDist(int n, int m) {
// boolean[][] visited = new boolean[n][m];
// int maxDist = n - 1 + m - 1;
// List<Integer> res = new LinkedList<>();
//
// Set<String> v = new HashSet<>();
// v.add(getKey(0,0));
// v.add(getKey(n-1,0));
// v.add(getKey(0,m-1));
// v.add(getKey(n-1,m-1));
// LinkedList<String> toVisit = new LinkedList<>(v);
// visited[0][0] = true;
// visited[0][m-1] = true;
// visited[n-1][0] = true;
// visited[n-1][m-1] = true;
//
// Set<String> nextBatch = new HashSet<>();
//
// while(!toVisit.isEmpty()) {
// String key = toVisit.pop();
// String[] split = key.split(",");
// int x = Integer.parseInt(split[0]);
// int y = Integer.parseInt(split[1]);
// res.add(maxDist);
// addNeighbours(nextBatch, x, y, visited, n, m);
//
// if(toVisit.isEmpty()) {
// maxDist--;
// toVisit = new LinkedList<>(nextBatch);
// nextBatch = new HashSet<>();
// }
// }
// return res;
// }
//
//// private static void addNeighbours(List<String> nextBatch, String key, Set<String> visited) {
// private static void addNeighbours(Set<String> nextBatch, int x, int y, boolean[][] visited, int n, int m) {
// if(x-1 >= 0 && !visited[x-1][y]) {
// nextBatch.add(getKey(x - 1, y));
// visited[x-1][y] = true;
// }
// if(x+1 < n && !visited[x+1][y]) {
// nextBatch.add(getKey(x + 1, y));
// visited[x+1][y] = true;
// }
// if(y-1 >= 0 && !visited[x][y-1]) {
// nextBatch.add(getKey(x, y - 1));
// visited[x][y-1] = true;
// }
// if(y+1 < m && !visited[x][y+1]) {
// nextBatch.add(getKey(x, y + 1));
// visited[x][y+1] = true;
// }
// visited[x][y] = true;
// }
//
// private static String getKey(int n, int m) {
// return 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 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
|
571b0a71ad4a49b1f598ed5330ad35a7
|
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.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(rd.readLine());
while(T -- > 0)
{
String s1 = rd.readLine();
String[] s = s1.split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int[] cnt = new int[n + m];
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
int x = i >= n / 2 ? n - 1 - i: i;
int y = j >= m / 2 ? m - 1 - j: j;
cnt[((n - 1) / 2 - x) + ((m - 1) / 2 - y)] ++;
}
}
for(int i = 0; i <cnt.length; i ++)
while(cnt[i] -- > 0){
log.write(String.valueOf(i + (n - 1) / 2 + (m - 1) / 2 + 2 - n % 2 - m % 2));
log.write(" ");
// log.flush();
}
log.write("\n");
log.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 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
|
f148f0fbc7bcb8abd6a6cc4fa3f18ec1
|
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.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
public final class CFPS {
static FastReader fr = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static final int gigamod = 1000000007;
static final int mod = 998244353;
static int t = 1;
static double epsilon = 0.0000000001;
static boolean[] isPrime;
static int[] smallestFactorOf;
static final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
static long cmp;
@SuppressWarnings({"unused"})
public static void main(String[] args) throws Exception {
t = fr.nextInt();
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt(), m = fr.nextInt();
ArrayList<Integer> maxDists = new ArrayList<>();
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
// distances to corners
int maxDist = -1;
// to (1, 1)
maxDist = Math.max(maxDist, (i) + (j));
// to (1, m)
maxDist = Math.max(maxDist, (i) + (m - 1 - j));
// to (n, 1)
maxDist = Math.max(maxDist, (n - 1 - i) + (j));
// to (n, m)
maxDist = Math.max(maxDist, (n - 1 - i) + (m - 1 - j));
maxDists.add(maxDist);
}
Collections.sort(maxDists);
for (int i = 0; i < n * m; i++)
out.print(maxDists.get(i) + " ");
out.println();
}
out.close();
}
// (range add - segment min) segTree
static int nn;
static int[] arr;
static int[] tree;
static int[] lazy;
static void build(int node, int leftt, int rightt) {
if (leftt == rightt) {
tree[node] = arr[leftt];
return;
}
int mid = (leftt + rightt) >> 1;
build(node << 1, leftt, mid);
build(node << 1 | 1, mid + 1, rightt);
tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]);
}
static void segAdd(int node, int leftt, int rightt, int segL, int segR, int val) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (leftt != rightt) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
}
lazy[node] = 0;
}
if (segL > rightt || segR < leftt) return;
if (segL <= leftt && rightt <= segR) {
tree[node] += val;
if (leftt != rightt) {
lazy[node << 1] += val;
lazy[node << 1 | 1] += val;
}
lazy[node] = 0;
return;
}
int mid = (leftt + rightt) >> 1;
segAdd(node << 1, leftt, mid, segL, segR, val);
segAdd(node << 1 | 1, mid + 1, rightt, segL, segR, val);
tree[node] = Math.min(tree[node << 1], tree[node << 1 | 1]);
}
static int minQuery(int node, int leftt, int rightt, int segL, int segR) {
if (lazy[node] != 0) {
tree[node] += lazy[node];
if (leftt != rightt) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
}
lazy[node] = 0;
}
if (segL > rightt || segR < leftt) return Integer.MAX_VALUE / 10;
if (segL <= leftt && rightt <= segR)
return tree[node];
int mid = (leftt + rightt) >> 1;
return Math.min(minQuery(node << 1, leftt, mid, segL, segR),
minQuery(node << 1 | 1, mid + 1, rightt, segL, segR));
}
static class Segment {
int li, ri, wi, id;
Segment(int ll, int rr, int ww, int ii) {
li = ll;
ri = rr;
wi = ww;
id = ii;
}
}
static void compute_automaton(String s, int[][] aut) {
s += '#';
int n = s.length();
int[] pi = prefix_function(s.toCharArray());
for (int i = 0; i < n; i++) {
for (int c = 0; c < 26; c++) {
int j = i;
while (j > 0 && 'A' + c != s.charAt(j))
j = pi[j-1];
if ('A' + c == s.charAt(j))
j++;
aut[i][c] = j;
}
}
}
static void timeDFS(int current, int from, UGraph ug,
int[] time, int[] tIn, int[] tOut) {
tIn[current] = ++time[0];
for (int adj : ug.adj(current))
if (adj != from)
timeDFS(adj, current, ug, time, tIn, tOut);
tOut[current] = ++time[0];
}
static class Pair implements Comparable<Pair> {
int first, second;
int idx;
Pair() { first = second = 0; }
Pair (int ff, int ss, int ii) {
first = ff;
second = ss;
idx = ii;
}
Pair (int ff, int ss) {
first = ff;
second = ss;
idx = -1;
}
public int compareTo(Pair that) {
cmp = first - that.first;
if (cmp == 0)
cmp = second - that.second;
return (int) cmp;
}
}
static boolean areCollinear(long x1, long y1, long x2, long y2, long x3, long y3) {
// we will check if c3 lies on line through (c1, c2)
long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
return a == 0;
}
static int[] treeDiameter(UGraph ug) {
int n = ug.V();
int farthest = -1;
int[] distTo = new int[n];
diamDFS(0, -1, 0, ug, distTo);
int maxDist = -1;
for (int i = 0; i < n; i++)
if (maxDist < distTo[i]) {
maxDist = distTo[i];
farthest = i;
}
distTo = new int[n + 1];
diamDFS(farthest, -1, 0, ug, distTo);
distTo[n] = farthest;
return distTo;
}
static void diamDFS(int current, int from, int dist, UGraph ug, int[] distTo) {
distTo[current] = dist;
for (int adj : ug.adj(current))
if (adj != from)
diamDFS(adj, current, dist + 1, ug, distTo);
}
static class TreeDistFinder {
UGraph ug;
int n;
int[] depthOf;
LCA lca;
TreeDistFinder(UGraph ug) {
this.ug = ug;
n = ug.V();
depthOf = new int[n];
depthCalc(0, -1, ug, 0, depthOf);
lca = new LCA(ug, 0);
}
TreeDistFinder(UGraph ug, int a) {
this.ug = ug;
n = ug.V();
depthOf = new int[n];
depthCalc(a, -1, ug, 0, depthOf);
lca = new LCA(ug, a);
}
private void depthCalc(int current, int from, UGraph ug, int depth, int[] depthOf) {
depthOf[current] = depth;
for (int adj : ug.adj(current))
if (adj != from)
depthCalc(adj, current, ug, depth + 1, depthOf);
}
public int dist(int a, int b) {
int lc = lca.lca(a, b);
return (depthOf[a] - depthOf[lc] + depthOf[b] - depthOf[lc]);
}
}
public static long[][] GCDSparseTable(long[] a)
{
int n = a.length;
int b = 32-Integer.numberOfLeadingZeros(n);
long[][] ret = new long[b][];
for(int i = 0, l = 1;i < b;i++, l*=2) {
if(i == 0) {
ret[i] = a;
} else {
ret[i] = new long[n-l+1];
for(int j = 0;j < n-l+1;j++) {
ret[i][j] = gcd(ret[i-1][j], ret[i-1][j+l/2]);
}
}
}
return ret;
}
public static long sparseRangeGCDQ(long[][] table, int l, int r)
{
// [a,b)
if(l > r)return 1;
// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
int t = 31-Integer.numberOfLeadingZeros(r-l);
return gcd(table[t][l], table[t][r-(1<<t)]);
}
static class Trie {
TrieNode root;
Trie(char[][] strings) {
root = new TrieNode('A', false);
construct(root, strings);
}
public Stack<String> set(TrieNode root) {
Stack<String> set = new Stack<>();
StringBuilder sb = new StringBuilder();
for (TrieNode next : root.next)
collect(sb, next, set);
return set;
}
private void collect(StringBuilder sb, TrieNode node, Stack<String> set) {
if (node == null) return;
sb.append(node.character);
if (node.isTerminal)
set.add(sb.toString());
for (TrieNode next : node.next)
collect(sb, next, set);
if (sb.length() > 0)
sb.setLength(sb.length() - 1);
}
private void construct(TrieNode root, char[][] strings) {
// we have to construct the Trie
for (char[] string : strings) {
if (string.length == 0) continue;
root.next[string[0] - 'a'] = put(root.next[string[0] - 'a'], string, 0);
if (root.next[string[0] - 'a'] != null)
root.isLeaf = false;
}
}
private TrieNode put(TrieNode node, char[] string, int idx) {
boolean isTerminal = (idx == string.length - 1);
if (node == null) node = new TrieNode(string[idx], isTerminal);
node.character = string[idx];
node.isTerminal |= isTerminal;
if (!isTerminal) {
node.isLeaf = false;
node.next[string[idx + 1] - 'a'] = put(node.next[string[idx + 1] - 'a'], string, idx + 1);
}
return node;
}
class TrieNode {
char character;
TrieNode[] next;
boolean isTerminal, isLeaf;
boolean canWin, canLose;
TrieNode(char c, boolean isTerminallll) {
character = c;
isTerminal = isTerminallll;
next = new TrieNode[26];
isLeaf = true;
}
}
}
static class Edge implements Comparable<Edge> {
int from, to;
long weight;
int id;
// int hash;
Edge(int fro, int t, long wt, int i) {
from = fro;
to = t;
id = i;
weight = wt;
// hash = Objects.hash(from, to, weight);
}
/*public int hashCode() {
return hash;
}*/
public int compareTo(Edge that) {
return Long.compare(this.id, that.id);
}
}
public static long[][] minSparseTable(long[] a)
{
int n = a.length;
int b = 32-Integer.numberOfLeadingZeros(n);
long[][] ret = new long[b][];
for(int i = 0, l = 1;i < b;i++, l*=2) {
if(i == 0) {
ret[i] = a;
}else {
ret[i] = new long[n-l+1];
for(int j = 0;j < n-l+1;j++) {
ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]);
}
}
}
return ret;
}
public static long sparseRangeMinQ(long[][] table, int l, int r)
{
// [a,b)
if(l >= r)return Integer.MAX_VALUE;
// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
int t = 31-Integer.numberOfLeadingZeros(r-l);
return Math.min(table[t][l], table[t][r-(1<<t)]);
}
public static long[][] maxSparseTable(long[] a)
{
int n = a.length;
int b = 32-Integer.numberOfLeadingZeros(n);
long[][] ret = new long[b][];
for(int i = 0, l = 1;i < b;i++, l*=2) {
if(i == 0) {
ret[i] = a;
}else {
ret[i] = new long[n-l+1];
for(int j = 0;j < n-l+1;j++) {
ret[i][j] = Math.max(ret[i-1][j], ret[i-1][j+l/2]);
}
}
}
return ret;
}
public static long sparseRangeMaxQ(long[][] table, int l, int r)
{
// [a,b)
if(l >= r)return Integer.MIN_VALUE;
// 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3
int t = 31-Integer.numberOfLeadingZeros(r-l);
return Math.max(table[t][l], table[t][r-(1<<t)]);
}
static class LCA {
int[] height, first, segtree;
ArrayList<Integer> euler;
boolean[] visited;
int n;
LCA(UGraph ug, int root) {
n = ug.V();
height = new int[n];
first = new int[n];
euler = new ArrayList<>();
visited = new boolean[n];
dfs(ug, root, 0);
int m = euler.size();
segtree = new int[m * 4];
build(1, 0, m - 1);
}
void dfs(UGraph ug, int node, int h) {
visited[node] = true;
height[node] = h;
first[node] = euler.size();
euler.add(node);
for (int adj : ug.adj(node)) {
if (!visited[adj]) {
dfs(ug, adj, h + 1);
euler.add(node);
}
}
}
void build(int node, int b, int e) {
if (b == e) {
segtree[node] = euler.get(b);
} else {
int mid = (b + e) / 2;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
int l = segtree[node << 1], r = segtree[node << 1 | 1];
segtree[node] = (height[l] < height[r]) ? l : r;
}
}
int query(int node, int b, int e, int L, int R) {
if (b > R || e < L)
return -1;
if (b >= L && e <= R)
return segtree[node];
int mid = (b + e) >> 1;
int left = query(node << 1, b, mid, L, R);
int right = query(node << 1 | 1, mid + 1, e, L, R);
if (left == -1) return right;
if (right == -1) return left;
return height[left] < height[right] ? left : right;
}
int lca(int u, int v) {
int left = first[u], right = first[v];
if (left > right) {
int temp = left;
left = right;
right = temp;
}
return query(1, 0, euler.size() - 1, left, right);
}
}
static class FenwickTree {
long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates
public FenwickTree(int size) {
array = new long[size + 1];
}
public long rsq(int ind) {
assert ind > 0;
long sum = 0;
while (ind > 0) {
sum += array[ind];
//Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number
ind -= ind & (-ind);
}
return sum;
}
public long rsq(int a, int b) {
assert b >= a && a > 0 && b > 0;
return rsq(b) - rsq(a - 1);
}
public void update(int ind, long value) {
assert ind > 0;
while (ind < array.length) {
array[ind] += value;
//Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number
ind += ind & (-ind);
}
}
public int size() {
return array.length - 1;
}
}
static class Point implements Comparable<Point> {
long x;
long y;
long z;
long id;
// private int hashCode;
Point() {
x = z = y = 0;
// this.hashCode = Objects.hash(x, y, cost);
}
Point(Point p) {
this.x = p.x;
this.y = p.y;
this.z = p.z;
this.id = p.id;
// this.hashCode = Objects.hash(x, y, cost);
}
Point(long x, long y, long z, long id) {
this.x = x;
this.y = y;
this.z = z;
this.id = id;
// this.hashCode = Objects.hash(x, y, id);
}
Point(long a, long b) {
this.x = a;
this.y = b;
this.z = 0;
// this.hashCode = Objects.hash(a, b);
}
Point(long x, long y, long id) {
this.x = x;
this.y = y;
this.id = id;
}
@Override
public int compareTo(Point o) {
if (this.x < o.x)
return -1;
if (this.x > o.x)
return 1;
if (this.y < o.y)
return -1;
if (this.y > o.y)
return 1;
if (this.z < o.z)
return -1;
if (this.z > o.z)
return 1;
return 0;
}
@Override
public boolean equals(Object that) {
return this.compareTo((Point) that) == 0;
}
}
static class BinaryLift {
// FUNCTIONS: k-th ancestor and LCA in log(n)
int[] parentOf;
int maxJmpPow;
int[][] binAncestorOf;
int n;
int[] lvlOf;
// How this works?
// a. For every node, we store the b-ancestor for b in {1, 2, 4, 8, .. log(n)}.
// b. When we need k-ancestor, we represent 'k' in binary and for each set bit, we
// lift level in the tree.
public BinaryLift(UGraph tree) {
n = tree.V();
maxJmpPow = logk(n, 2) + 1;
parentOf = new int[n];
binAncestorOf = new int[n][maxJmpPow];
lvlOf = new int[n];
for (int i = 0; i < n; i++)
Arrays.fill(binAncestorOf[i], -1);
parentConstruct(0, -1, tree, 0);
binConstruct();
}
// TODO: Implement lvlOf[] initialization
public BinaryLift(int[] parentOf) {
this.parentOf = parentOf;
n = parentOf.length;
maxJmpPow = logk(n, 2) + 1;
binAncestorOf = new int[n][maxJmpPow];
lvlOf = new int[n];
for (int i = 0; i < n; i++)
Arrays.fill(binAncestorOf[i], -1);
UGraph tree = new UGraph(n);
for (int i = 1; i < n; i++)
tree.addEdge(i, parentOf[i]);
binConstruct();
parentConstruct(0, -1, tree, 0);
}
private void parentConstruct(int current, int from, UGraph tree, int depth) {
parentOf[current] = from;
lvlOf[current] = depth;
for (int adj : tree.adj(current))
if (adj != from)
parentConstruct(adj, current, tree, depth + 1);
}
private void binConstruct() {
for (int node = 0; node < n; node++)
for (int lvl = 0; lvl < maxJmpPow; lvl++)
binConstruct(node, lvl);
}
private int binConstruct(int node, int lvl) {
if (node < 0)
return -1;
if (lvl == 0)
return binAncestorOf[node][lvl] = parentOf[node];
if (node == 0)
return binAncestorOf[node][lvl] = -1;
if (binAncestorOf[node][lvl] != -1)
return binAncestorOf[node][lvl];
return binAncestorOf[node][lvl] = binConstruct(binConstruct(node, lvl - 1), lvl - 1);
}
// return ancestor which is 'k' levels above this one
public int ancestor(int node, int k) {
if (node < 0)
return -1;
if (node == 0)
if (k == 0) return node;
else return -1;
if (k > (1 << maxJmpPow) - 1)
return -1;
if (k == 0)
return node;
int ancestor = node;
int highestBit = Integer.highestOneBit(k);
while (k > 0 && ancestor != -1) {
ancestor = binAncestorOf[ancestor][logk(highestBit, 2)];
k -= highestBit;
highestBit = Integer.highestOneBit(k);
}
return ancestor;
}
public int lca(int u, int v) {
if (u == v)
return u;
// The invariant will be that 'u' is below 'v' initially.
if (lvlOf[u] < lvlOf[v]) {
int temp = u;
u = v;
v = temp;
}
// Equalizing the levels.
u = ancestor(u, lvlOf[u] - lvlOf[v]);
if (u == v)
return u;
// We will now raise level by largest fitting power of two until possible.
for (int power = maxJmpPow - 1; power > -1; power--)
if (binAncestorOf[u][power] != binAncestorOf[v][power]) {
u = binAncestorOf[u][power];
v = binAncestorOf[v][power];
}
return ancestor(u, 1);
}
}
static class DFSTree {
// NOTE: The thing is made keeping in mind that the whole
// input graph is connected.
UGraph tree;
UGraph backUG;
int hasBridge;
int n;
Edge backEdge;
DFSTree(UGraph ug) {
this.n = ug.V();
tree = new UGraph(n);
hasBridge = -1;
backUG = new UGraph(n);
treeCalc(0, -1, new boolean[n], ug);
}
private void treeCalc(int current, int from, boolean[] marked, UGraph ug) {
if (marked[current]) {
// This is a backEdge.
backUG.addEdge(from, current);
backEdge = new Edge(from, current, 1, 0);
return;
}
if (from != -1)
tree.addEdge(from, current);
marked[current] = true;
for (int adj : ug.adj(current))
if (adj != from)
treeCalc(adj, current, marked, ug);
}
public boolean hasBridge() {
if (hasBridge != -1)
return (hasBridge == 1);
// We have to determine the bridge.
bridgeFinder();
return (hasBridge == 1);
}
int[] levelOf;
int[] dp;
private void bridgeFinder() {
// Finding the level of each node.
levelOf = new int[n];
levelDFS(0, -1, 0);
// Applying DP solution.
// dp[i] -> Highest level reachable from subtree of 'i' using
// some backEdge.
dp = new int[n];
Arrays.fill(dp, Integer.MAX_VALUE / 100);
dpDFS(0, -1);
// Now, we will check each edge and determine whether its a
// bridge.
for (int i = 0; i < n; i++)
for (int adj : tree.adj(i)) {
// (i -> adj) is the edge.
if (dp[adj] > levelOf[i])
hasBridge = 1;
}
if (hasBridge != 1)
hasBridge = 0;
}
private void levelDFS(int current, int from, int lvl) {
levelOf[current] = lvl;
for (int adj : tree.adj(current))
if (adj != from)
levelDFS(adj, current, lvl + 1);
}
private int dpDFS(int current, int from) {
dp[current] = levelOf[current];
for (int back : backUG.adj(current))
dp[current] = Math.min(dp[current], levelOf[back]);
for (int adj : tree.adj(current))
if (adj != from)
dp[current] = Math.min(dp[current], dpDFS(adj, current));
return dp[current];
}
}
static class UnionFind {
// Uses weighted quick-union with path compression.
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in tree rooted at i
// Note: not necessarily correct if i is not a root node
private int count; // number of components
public UnionFind(int n) {
count = n;
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Number of connected components.
public int count() {
return count;
}
// Find the root of p.
public int find(int p) {
while (p != parent[p])
p = parent[p];
return p;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int numConnectedTo(int node) {
return size[find(node)];
}
// Weighted union.
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
public static int[] connectedComponents(UnionFind uf) {
// We can do this in nlogn.
int n = uf.size.length;
int[] compoColors = new int[n];
for (int i = 0; i < n; i++)
compoColors[i] = uf.find(i);
HashMap<Integer, Integer> oldToNew = new HashMap<>();
int newCtr = 0;
for (int i = 0; i < n; i++) {
int thisOldColor = compoColors[i];
Integer thisNewColor = oldToNew.get(thisOldColor);
if (thisNewColor == null)
thisNewColor = newCtr++;
oldToNew.put(thisOldColor, thisNewColor);
compoColors[i] = thisNewColor;
}
return compoColors;
}
}
static class UGraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public UGraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
adj[to].add(from);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int degree(int v) {
return adj[v].size();
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int current, boolean[] marked, UGraph g) {
if (marked[current]) return;
marked[current] = true;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) {
if (marked[current]) return;
marked[current] = true;
if (from != -1)
distTo[current] = distTo[from] + 1;
HashSet<Integer> adj = g.adj(current);
int alreadyMarkedCtr = 0;
for (int adjc : adj) {
if (marked[adjc]) alreadyMarkedCtr++;
dfsMark(adjc, current, distTo, marked, g, endPoints);
}
if (alreadyMarkedCtr == adj.size())
endPoints.add(current);
}
public static void bfsOrder(int current, UGraph g) {
}
public static void dfsMark(int current, int[] colorIds, int color, UGraph g) {
if (colorIds[current] != -1) return;
colorIds[current] = color;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, colorIds, color, g);
}
public static int[] connectedComponents(UGraph g) {
int n = g.V();
int[] componentId = new int[n];
Arrays.fill(componentId, -1);
int colorCtr = 0;
for (int i = 0; i < n; i++) {
if (componentId[i] != -1) continue;
dfsMark(i, componentId, colorCtr, g);
colorCtr++;
}
return componentId;
}
public static boolean hasCycle(UGraph ug) {
int n = ug.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, ug, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = ug.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, ug, marked, hasCycleFirst, current);
}
}
}
static class Digraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public Digraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public Digraph reversed() {
Digraph dg = new Digraph(V());
for (int i = 0; i < V(); i++)
for (int adjVert : adj(i)) dg.addEdge(adjVert, i);
return dg;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static int[] KosarajuSharirSCC(Digraph dg) {
int[] id = new int[dg.V()];
Digraph reversed = dg.reversed();
// Gotta perform topological sort on this one to get the stack.
Stack<Integer> revStack = Digraph.topologicalSort(reversed);
// Initializing id and idCtr.
id = new int[dg.V()];
int idCtr = -1;
// Creating a 'marked' array.
boolean[] marked = new boolean[dg.V()];
while (!revStack.isEmpty()) {
int vertex = revStack.pop();
if (!marked[vertex])
sccDFS(dg, vertex, marked, ++idCtr, id);
}
return id;
}
private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) {
marked[source] = true;
id[source] = idCtr;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id);
}
public static Stack<Integer> topologicalSort(Digraph dg) {
// dg has to be a directed acyclic graph.
// We'll have to run dfs on the digraph and push the deepest nodes on stack first.
// We'll need a Stack<Integer> and a int[] marked.
Stack<Integer> topologicalStack = new Stack<Integer>();
boolean[] marked = new boolean[dg.V()];
// Calling dfs
for (int i = 0; i < dg.V(); i++)
if (!marked[i])
runDfs(dg, topologicalStack, marked, i);
return topologicalStack;
}
static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) {
marked[source] = true;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex])
runDfs(dg, topologicalStack, marked, adjVertex);
topologicalStack.add(source);
}
}
static class FastReader {
private BufferedReader bfr;
private StringTokenizer st;
public FastReader() {
bfr = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
if (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bfr.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().toCharArray()[0];
}
String nextString() {
return next();
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
double[] nextDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextDouble();
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
int[][] nextIntGrid(int n, int m) {
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
grid[i][j] = fr.nextInt();
}
return grid;
}
}
@SuppressWarnings("serial")
static class CountMap<T> extends TreeMap<T, Integer>{
CountMap() {
}
CountMap(Comparator<T> cmp) {
}
CountMap(T[] arr) {
this.putCM(arr);
}
public Integer putCM(T key) {
return super.put(key, super.getOrDefault(key, 0) + 1);
}
public Integer removeCM(T key) {
int count = super.getOrDefault(key, -1);
if (count == -1) return -1;
if (count == 1)
return super.remove(key);
else
return super.put(key, count - 1);
}
public Integer getCM(T key) {
return super.getOrDefault(key, 0);
}
public void putCM(T[] arr) {
for (T l : arr)
this.putCM(l);
}
}
static long dioGCD(long a, long b, long[] x0, long[] y0) {
if (b == 0) {
x0[0] = 1;
y0[0] = 0;
return a;
}
long[] x1 = new long[1], y1 = new long[1];
long d = dioGCD(b, a % b, x1, y1);
x0[0] = y1[0];
y0[0] = x1[0] - y1[0] * (a / b);
return d;
}
static boolean diophantine(long a, long b, long c, long[] x0, long[] y0, long[] g) {
g[0] = dioGCD(Math.abs(a), Math.abs(b), x0, y0);
if (c % g[0] > 0) {
return false;
}
x0[0] *= c / g[0];
y0[0] *= c / g[0];
if (a < 0) x0[0] = -x0[0];
if (b < 0) y0[0] = -y0[0];
return true;
}
static long[][] prod(long[][] mat1, long[][] mat2) {
int n = mat1.length;
long[][] prod = new long[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
// determining prod[i][j]
// it will be the dot product of mat1[i][] and mat2[][i]
for (int k = 0; k < n; k++)
prod[i][j] += mat1[i][k] * mat2[k][j];
return prod;
}
static long[][] matExpo(long[][] mat, long power) {
int n = mat.length;
long[][] ans = new long[n][n];
if (power == 0)
return null;
if (power == 1)
return mat;
long[][] half = matExpo(mat, power / 2);
ans = prod(half, half);
if (power % 2 == 1) {
ans = prod(ans, mat);
}
return ans;
}
static int KMPNumOcc(char[] text, char[] pat) {
int n = text.length;
int m = pat.length;
char[] patPlusText = new char[n + m + 1];
for (int i = 0; i < m; i++)
patPlusText[i] = pat[i];
patPlusText[m] = '^'; // Seperator
for (int i = 0; i < n; i++)
patPlusText[m + i] = text[i];
int[] fullPi = piCalcKMP(patPlusText);
int answer = 0;
for (int i = 0; i < n + m + 1; i++)
if (fullPi[i] == m)
answer++;
return answer;
}
static int[] piCalcKMP(char[] s) {
int n = s.length;
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static boolean[] prefMatchesSuff(char[] s) {
int n = s.length;
boolean[] res = new boolean[n + 1];
int[] pi = prefix_function(s);
res[0] = true;
for (int p = n; p != 0; p = pi[p])
res[p] = true;
return res;
}
static int[] prefix_function(char[] s) {
int n = s.length;
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static long hash(long key) {
long h = Long.hashCode(key);
h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4);
return h & (gigamod-1);
}
static int mapTo1D(int row, int col, int n, int m) {
// Maps elements in a 2D matrix serially to elements in
// a 1D array.
return row * m + col;
}
static int[] mapTo2D(int idx, int n, int m) {
// Inverse of what the one above does.
int[] rnc = new int[2];
rnc[0] = idx / m;
rnc[1] = idx % m;
return rnc;
}
static boolean[] primeGenerator(int upto) {
// Sieve of Eratosthenes:
isPrime = new boolean[upto + 1];
smallestFactorOf = new int[upto + 1];
Arrays.fill(smallestFactorOf, 1);
Arrays.fill(isPrime, true);
isPrime[1] = isPrime[0] = false;
for (long i = 2; i < upto + 1; i++)
if (isPrime[(int) i]) {
smallestFactorOf[(int) i] = (int) i;
// Mark all the multiples greater than or equal
// to the square of i to be false.
for (long j = i; j * i < upto + 1; j++) {
if (isPrime[(int) j * (int) i]) {
isPrime[(int) j * (int) i] = false;
smallestFactorOf[(int) j * (int) i] = (int) i;
}
}
}
return isPrime;
}
static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) {
if (smallestFactorOf == null)
primeGenerator(num + 1);
HashMap<Integer, Integer> fnps = new HashMap<>();
while (num != 1) {
fnps.put(smallestFactorOf[num], fnps.getOrDefault(smallestFactorOf[num], 0) + 1);
num /= smallestFactorOf[num];
}
return fnps;
}
static HashMap<Long, Integer> primeFactorization(long num) {
// Returns map of factor and its power in the number.
HashMap<Long, Integer> map = new HashMap<>();
while (num % 2 == 0) {
num /= 2;
Integer pwrCnt = map.get(2L);
map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1);
}
for (long i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
num /= i;
Integer pwrCnt = map.get(i);
map.put(i, pwrCnt != null ? pwrCnt + 1 : 1);
}
}
// If the number is prime, we have to add it to the
// map.
if (num != 1)
map.put(num, 1);
return map;
}
static HashSet<Long> divisors(long num) {
HashSet<Long> divisors = new HashSet<Long>();
divisors.add(1L);
divisors.add(num);
for (long i = 2; i * i <= num; i++) {
if (num % i == 0) {
divisors.add(num/i);
divisors.add(i);
}
}
return divisors;
}
static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) {
if (m > limit) return;
if (m <= limit && n <= limit)
coprimes.add(new Point(m, n));
if (coprimes.size() > numCoprimes) return;
coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes);
coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes);
coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes);
}
static long nCr(long n, long r, long[] fac) { long p = gigamod; if (r == 0) return 1; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; }
static long modInverse(long n, long p) { return power(n, p - 2, p); }
static long modDiv(long a, long b){return mod(a * power(b, mod - 2, mod), mod);}
static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }
static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); }
static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; }
static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); }
static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } }
static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } }
static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } }
static boolean isPrime(long n) {if (n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
static String toString(int[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();}
static String toString(boolean[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();}
static String toString(long[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();}
static String toString(char[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+"");return sb.toString();}
static String toString(int[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();}
static String toString(long[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++) {sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();}
static String toString(double[][] dp){StringBuilder sb=new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();}
static String toString(char[][] dp){StringBuilder sb = new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+"");}sb.append('\n');}return sb.toString();}
static long mod(long a, long m){return(a%m+1000000L*m)%m;}
}
// NOTES:
// ASCII VALUE OF 'A': 65
// ASCII VALUE OF 'a': 97
// Range of long: 9 * 10^18
// ASCII VALUE OF '0': 48
// Primes upto 'n' can be given by (n / (logn)).
|
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
|
96ff8f0ef182502a3fe1e7b9c80f047c
|
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 sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int m = sc.nextInt();
PriorityQueue<Integer> q=new PriorityQueue<>();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int d=Math.max(i,n-1-i)+Math.max(j,m-1-j);
q.add(d);
}
}
while(!q.isEmpty())
{
System.out.print(q.peek()+" ");
q.remove();
}
}
}
}
|
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
|
944fee0d2f62d1eb2743c4ce4d788442
|
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 Q1627B {
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int a = scn.nextInt();
int b = scn.nextInt();
// int[][] mat = new int[a][b];
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
int d1 = (i)+(j);
int d2 = (a-i)+(j)-1;
int d3 = (a-i)+(b-j)-2;
int d4 = (i)+(b-j)-1;
int mx = Math.max(Math.max(d1,d2),Math.max(d3,d4));
// mat[i][j] = mx;
pq.add(mx);
}
}
// for(int i=0;i<a;i++) {
// for(int j=0;j<b;j++) {
// System.out.print(mat[i][j]+" ");
// }
// System.out.println();
// }
StringBuilder sb= new StringBuilder("");
while(pq.size()>0){
sb.append(pq.remove()+" ");
}
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 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
|
7cd224912a6f93482c24c8b6f72de049
|
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.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static int n, m;
// if Rahul is at (i, j).. Tina wants to sit at max
// Among all such max.. Rahul wants to take min
static void solve(){
List<Integer> pos=new ArrayList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
// 0,0 0,m-1 n-1,m-1 n-1,0
int d1=i+j;
int d2=i+Math.abs(j-m+1);
int d3=Math.abs(i-n+1)+Math.abs(j-m+1);
int d4=Math.abs(i-n+1)+j;
pos.add(Math.max(d1, Math.max(d2, Math.max(d3, d4))));
}
}
Collections.sort(pos);
for(int i=0;i<n*m;i++) str.append(pos.get(i)).append(" ");
str.append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int q = Integer.parseInt(bf.readLine().trim());
while (q-- > 0) {
String s[]=bf.readLine().trim().split("\\s+");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
solve();
}
pw.print(str);
pw.flush();
// System.out.print(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
|
90b62a20152ef4dc5f3a7926b16d810e
|
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.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static int n, m;
static void solve(){
List<Integer> pos=new ArrayList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
// 0,0 0,m-1 n-1,m-1 n-1,0
int d1=i+j;
int d2=i+Math.abs(j-m+1);
int d3=Math.abs(i-n+1)+Math.abs(j-m+1);
int d4=Math.abs(i-n+1)+j;
pos.add(Math.max(d1, Math.max(d2, Math.max(d3, d4))));
}
}
Collections.sort(pos);
for(int i=0;i<n*m;i++) str.append(pos.get(i)).append(" ");
str.append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
int q = Integer.parseInt(bf.readLine().trim());
while (q-- > 0) {
String s[]=bf.readLine().trim().split("\\s+");
n=Integer.parseInt(s[0]);
m=Integer.parseInt(s[1]);
solve();
}
pw.print(str);
pw.flush();
// System.out.print(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
|
f848112d6103055bf38a3ad20005f879
|
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.lang.Math;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
static PrintWriter pw;
static Scanner sc;
static StringBuilder ans;
static long mod = 1000000000+7;
static void pn(final Object arg) { pw.print(arg); pw.flush(); }
/*-------------- for input in an value ---------------------*/
static int ni() { return sc.nextInt(); }
static long nl() { return sc.nextLong(); }
static double nd() { return sc.nextDouble(); }
static String ns() { return sc.next(); }
static String nLine() { return sc.nextLine(); }
static void ap(int arg) { ans.append(arg); }
static void ap(long arg) { ans.append(arg); }
static void ap(String arg) { ans.append(arg); }
static void ap(StringBuilder arg) { ans.append(arg); }
static void apn() { ans.append("\n"); }
static void apn(int arg) { ans.append(arg+"\n"); }
static void apn(long arg) { ans.append(arg+"\n"); }
static void apn(String arg) { ans.append(arg+"\n"); }
static void apn(StringBuilder arg) { ans.append(arg+"\n"); }
static void yes() { ap("Yes\n"); }
static void no() { ap("No\n"); }
/* for Dubuggin */
static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
/*-------------- for input in an array ---------------------*/
static void readArray(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void readArray(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void readArray(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void readArray(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*-------------- File vs Input ---------------------*/
static void runFile() throws Exception {
sc = new Scanner(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
}
static void runIo() throws Exception {
pw =new PrintWriter(System.out);
sc = new Scanner(System.in);
}
static int log2(int n) { return (int)(Math.log(n) / Math.log(2)); }
static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);}
static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); }
static long nCr(long n, long r) { // Combinations
if (n < r)
return 0;
if (r > n - r) { // because nCr(n, r) == nCr(n, n - r)
r = n - r;
}
long ans = 1L;
for (long i = 0; i < r; i++) {
ans *= (n - i);
ans /= (i + 1);
}
return ans;
}
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
static boolean isPrime(long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean sv[] = new boolean[10002];
static void seive() {
//true -> not prime
// false->prime
sv[0] = sv[1] = true;
sv[2] = false;
for(int i = 0; i< sv.length; i++) {
if( !sv[i] && (long)i*(long)i < sv.length ) {
for ( int j = i*i; j<sv.length ; j += i ) {
sv[j] = true;
}
}
}
}
static long kadensAlgo(long ar[]) {
int n = ar.length;
long pre = ar[0];
long ans = ar[0];
for(int i = 1; i<n; i++) {
pre = Math.max(pre + ar[i], ar[i]);
ans = Math.max(pre, ans);
}
return ans;
}
static long binpow( long a, long b) {
long res = 1;
while (b > 0) {
if ( (b & 1) > 0){
res = (res * a);
}
a = (a * a);
b >>= 1;
}
return res;
}
static long factorial(long n) {
long res = 1, i;
for (i = 2; i <= n; i++){
res = ((res%mod) * (i%mod))%mod;
}
return res;
}
static int getCountPrime(int n) {
int ans = 0;
while (n%2==0) {
ans ++;
n /= 2;
}
for (int i = 3; i *i<= n; i+= 2) {
while (n%i == 0) {
ans ++;
n /= i;
}
}
if(n > 1 )
ans++;
return ans;
}
static void sort(int[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void sort(long[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
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);
}
static int upperBound(long ar[], long k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] > k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static int lowerBound(long ar[], long k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] >= k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static void findDivisor(int n, ArrayList<Integer> al) {
for(int i = 1; i*i <= n; i++){
if( n % i == 0){
if(n/i==i){
al.add(i);
}
else{
al.add(n/i);
al.add(i);
}
}
}
}
static class Pair{
int a;
int b;
Pair( int a, int b ){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
int result = a;
result = 31 * result + b;
return result;
}
}
public static void main(String[] args) throws Exception {
// runFile();
runIo();
int t;
t = 1;
t = sc.nextInt();
ans = new StringBuilder();
while( t-- > 0 ) {
solve();
}
pn(ans+"");
}
public static void solve ( ) {
int n = ni();
int m = ni();
int ar[][] = new int[n][m];
for(int i = 1; i<=n; i++) {
for(int j = 1; j<=m; j++) {
ar[i-1][j-1] = max( max( abs(i-1)+abs(j-1), abs(i-1)+abs(j-m) ),
max(abs(i-n)+abs(j-1), abs(i-n)+abs(j-m)) );
}
}
// int hash[] = new int[2*100000 + 1];
Map<Integer, Integer> map = new TreeMap<>();
for(int i = 0;i<n; i++)
for(int j = 0; j<m; j++){
// hash[ar[i][j]]++;
map.put(ar[i][j], map.getOrDefault(ar[i][j], 0) + 1);
}
// for(int i = 0; i<hash.length; i++) {
// for(int j = 0; j<hash[i]; j++)
// ap(i+" ");
// }
for(int k : map.keySet()) {
int size = map.get(k);
for(int j = 0; j<size; j++)
ap(k+" ");
}
apn();
}
}
|
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
|
8dd394da5f8fcd7e0e814486e8014cf5
|
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 B
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int round = in.nextInt();
for (int z = 0; z < round; z++)
{
int n = in.nextInt(), m = in.nextInt();
int center = 1;
if (n % 2 == 0)
center *= 2;
if (m % 2 == 0)
center *= 2;
int zy = 0, sx = 0;
int step = n + m - (n + 1) / 2 - (m + 1) / 2;
for (int i = 1; i <= n || i <= m; i++)
{
if (zy == 0 && i > (n + 1) / 2)
zy = 2 - (m % 2);
if (sx == 0 && i > (m + 1) / 2)
sx = 2 - (n % 2);
int temp = Math.max(0, center - zy * 2 - sx * 2);
// System.out.println(center - zy * 2 - sx * 2);
// System.out.println(center + " " + (zy * 2) + " " + (sx * 2));
for (int j = 0; j < temp; j++)
System.out.print(step + " ");
step++;
if (zy != 0)
zy += 2;
if (sx != 0)
sx += 2;
if (center != 1)
center += 4;
else
center = 4;
}
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
|
21d26324ca0a5f1a52ed1f2ffdf56c97
|
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 Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0)
{
String s[]=(br.readLine()).split(" ");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=1;i<=n/2;i++)
{
for(int j=1;j<=m/2;j++)
{
int x=n-i+m-j;
for(int k=1;k<=4;k++)
{
al.add(x);
}
}
}
if(n%2==1 && m%2==1)
{
int x=n-(n/2+1)+m-(m/2+1);
al.add(x);
for(int i=1;i<=n/2;i++)
{
x=n-i+m-(m/2+1);
al.add(x);
al.add(x);
}
for(int j=1;j<=m/2;j++)
{
x=m-j+n-(n/2+1);
al.add(x);
al.add(x);
}
}
else if(n%2==1)
{
for(int j=1;j<=m/2;j++)
{
int x=n-(n/2+1)+m-j;
al.add(x);
al.add(x);
}
}
else if(m%2==1)
{
for(int i=1;i<=n/2;i++)
{
int x=n-i+m-(m/2+1);
al.add(x);
al.add(x);
}
}
Collections.sort(al);
for(int i=0;i<al.size();i++)
{
pw.print(al.get(i)+" ");
}
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 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
|
9ef372c9815d0737b06848734648294d
|
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 q2
{
private static boolean isValid(int r, int c, int n, int m) {
if(r<0 || c<0 || r>=n || c>=m) {
return false;
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int testcases = Integer.parseInt(reader.readLine());
while(testcases-- >0) {
String[] strNums = reader.readLine().split(" ");
int n = Integer.parseInt(strNums[0]);
int m = Integer.parseInt(strNums[1]);
boolean visited[][] = new boolean[n][m];
Queue<int[]> queue = new LinkedList<>();
if(n%2!=0 && m%2!=0) {
queue.add(new int[]{n/2, m/2});
visited[n/2][m/2] = true;
} else if(n%2==0 && m%2==0) {
queue.add(new int[]{n/2, m/2});
queue.add(new int[]{n/2-1, m/2-1});
queue.add(new int[]{n/2-1, m/2});
queue.add(new int[]{n/2, m/2-1});
visited[n/2][m/2] = true;
visited[n/2-1][m/2-1] = true;
visited[n/2-1][m/2] = true;
visited[n/2][m/2-1] = true;
} else if(n%2==0 && m%2!=0) {
queue.add(new int[]{n/2,m/2});
queue.add(new int[]{n/2-1, m/2});
visited[n/2][m/2] = true;
visited[n/2-1][m/2] = true;
} else {
queue.add(new int[]{n/2,m/2});
queue.add(new int[]{n/2, m/2-1});
visited[n/2][m/2] = true;
visited[n/2][m/2-1] = true;
}
List<Integer> temp = new ArrayList<Integer>();
while(!queue.isEmpty()) {
int size = queue.size();
temp.add(size);
for(int i=0;i<size;i++) {
int[] p = queue.poll();
if(isValid(p[0]-1, p[1], n, m) && visited[p[0]-1][p[1]]==false) {
queue.add(new int[]{p[0]-1, p[1]});
visited[p[0]-1][p[1]] = true;
}
if(isValid(p[0]+1, p[1], n, m) && visited[p[0]+1][p[1]]==false) {
queue.add(new int[]{p[0]+1, p[1]});
visited[p[0]+1][p[1]] = true;
}
if(isValid(p[0], p[1]-1, n, m) && visited[p[0]][p[1]-1]==false) {
queue.add(new int[]{p[0], p[1]-1});
visited[p[0]][p[1]-1] = true;
}
if(isValid(p[0], p[1]+1, n, m) && visited[p[0]][p[1]+1]==false) {
queue.add(new int[]{p[0], p[1]+1});
visited[p[0]][p[1]+1] = true;
}
}
}
int x = n/2+m/2;
for(int i=0;i<temp.size();i++) {
for(int j=0;j<temp.get(i);j++) {
writer.print(x + " ");
}
x++;
}
writer.println();
}
writer.flush();
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
|
12799896e48b40048a54a4b2c8023d44
|
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 javafx.scene.AmbientLight;
import java.io.PrintWriter;
import java.util.*;
public class NotSitting {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int row = scanner.nextInt();
int col = scanner.nextInt();
int distance = 0;
ArrayList<Integer> list = new ArrayList<>();
for (int j = 0; j < row; j++) {
for (int k = 0; k < col; k++) {
distance = findMaxDistance(j, k, row, col);
list.add(distance);
}
}
Collections.sort(list);
int c=0;
int l = list.toArray().length;
while (c<l) {
out.print(list.get(c)+" ");
c++;
}
out.println();
}
out.close();
}
private static int findMaxDistance(int i, int j, int row, int col) {
int distance = 0;
row--;
col--;
int d1 = i + j;
int d2 = Math.abs(i - row) + j;
int d3 = i + Math.abs( j - col);
int d4 = Math.abs(i - row) + Math.abs(j - col);
if (d1 > distance) distance = d1;
if (d2 > distance) distance = d2;
if (d3 > distance) distance = d3;
if (d4 > distance) distance = d4;
return distance;
}
}
|
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
|
6dc8a2439fd069b45e0c56e0a9359e7e
|
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 int i, j, k, n, m, t, y, x, sum = 0;
static long mod = 998244353;
static FastScanner fs = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static String str;
static long ans;
public static void main(String[] args) {
t = fs.nextInt();
while (t-- > 0) {
n = fs.nextInt();
m = fs.nextInt();
int[][] arr = new int[n][m];
for(i=0;i<n;i++){
for(j=0;j<m;j++){
int a = i + j;
int b = i+m-1-j;
int c = n-1-i + j;
int d = n-1-i + m-1-j;
arr[i][j] = Math.max(a, Math.max(b, Math.max(c,d)));
}
}
List<Integer> ans = new ArrayList<>();
for(i=0;i<n;i++){
for(j=0;j<m;j++){
ans.add(arr[i][j]);
}
}
Collections.sort(ans);
for(i=0;i<ans.size();i++){
out.print(ans.get(i)+" ");
}
out.println();
}
out.close();
}
/*static long nck(int n , int k){
long a = fact[n];
long b = modInv(fact[k]);
b*= modInv(fact[n-k]);
b%=mod;
return (a*b)%mod;
}
static void populateFact(){
fact[0]=1;
fact[1] = 1;
for(i=2;i<300005;i++){
fact[i]=i*fact[i-1];
fact[i]%=mod;
}
}
*/
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long exp(long base, long pow) {
if (pow == 0) return 1;
long half = exp(base, pow / 2);
if (pow % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long mul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modInv(long x) {
return exp(x, mod - 2);
}
static void ruffleSort(int[] a) {
//ruffle
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;
}
//then sort
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
//ruffle
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Pair implements Comparable<Pair> {
public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
if (x == o.x)
return Integer.compare(y, o.y);
return Integer.compare(x, o.x);
}
}
}
|
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
|
e0c4aafd62cf55894cf888f2abe2bb61
|
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 int i, j, k, n, m, t, y, x, sum = 0;
static long mod = 998244353;
static FastScanner fs = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static String str;
public static void main(String[] args) {
t = fs.nextInt();
while (t-- > 0) {
n = fs.nextInt();
m = fs.nextInt();
int [][] arr = new int[n][m];
List<Pair> q = new ArrayList<>();
if(n%2 == 1 && m%2 ==1){
x = n/2;
y = m/2;
arr[x][y]=1;
q.add(new Pair(x,y));
}
else if(n%2==0 && m%2==1){
arr[n/2][m/2]=1;
arr[(n-1)/2][m/2]=1;
x = n/2;
y = m/2;
q.add(new Pair(x,y));
x = (n-1)/2;
y = m/2;
q.add(new Pair(x,y));
}
else if(n%2==1 && m%2==0){
arr[n/2][m/2]=1;
arr[n/2][(m-1)/2]=1;
x = n/2;
y = m/2;
q.add(new Pair(x,y));
x = (n)/2;
y = (m-1)/2;
q.add(new Pair(x,y));
}
else{
arr[n/2][m/2]=1;
arr[(n-1)/2][m/2]=1;
arr[(n-1)/2][(m-1)/2]=1;
arr[n/2][(m-1)/2]=1;
x = n/2;
y = m/2;
q.add(new Pair(x,y));
x = (n-1)/2;
y = m/2;
q.add(new Pair(x,y));
x = (n-1)/2;
y = (m-1)/2;
q.add(new Pair(x,y));
x = (n)/2;
y = (m-1)/2;
q.add(new Pair(x,y));
}
i = 0;
while(i< q.size()){
x = q.get(i).x;
y = q.get(i).y;
if(x-1>=0 && arr[x-1][y]==0) {
arr[x - 1][y] = 1 + arr[x][y];
q.add(new Pair(x-1,y));
}
if(y-1>=0 && arr[x][y-1]==0) {
arr[x][y-1] = 1 + arr[x][y];
q.add(new Pair(x,y-1));
}
if(x+1<n && arr[x+1][y]==0) {
arr[x + 1][y] = 1 + arr[x][y];
q.add(new Pair(x+1,y));
}
if(y+1<m && arr[x][y+1]==0) {
arr[x][y+1] = 1 + arr[x][y];
q.add(new Pair(x,y+1));
}
i++;
}
HashMap<Integer,Integer> score = new HashMap();
for(i=0;i<n;i++){
for(j=0;j<m;j++){
x = arr[i][j];
if(score.containsKey(x))
score.put(x,score.get(x)+1);
else
score.put(x,1);
}
}
i = 1;
int temp = n/2 + m/2;
while(score.containsKey(i)){
for(j=0;j<score.get(i);j++)
out.print(temp+" ");
temp++;
i++;
}
out.println();
}
out.close();
}
/*static long nck(int n , int k){
long a = fact[n];
long b = modInv(fact[k]);
b*= modInv(fact[n-k]);
b%=mod;
return (a*b)%mod;
}
static void populateFact(){
fact[0]=1;
fact[1] = 1;
for(i=2;i<300005;i++){
fact[i]=i*fact[i-1];
fact[i]%=mod;
}
}
*/
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long exp(long base, long pow) {
if (pow == 0) return 1;
long half = exp(base, pow / 2);
if (pow % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long mul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modInv(long x) {
return exp(x, mod - 2);
}
static void ruffleSort(int[] a) {
//ruffle
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;
}
//then sort
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
//ruffle
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class Pair implements Comparable<Pair> {
public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
if (x == o.x)
return Integer.compare(y, o.y);
return Integer.compare(x, o.x);
}
}
}
|
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
|
c11b08256e2109838e15265a590a95ad
|
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 C{
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 min=n-(n+1)/2 + m- (m+1)/2;
int max=n-1+m-1;
int A[][]=new int[n][m];
int Dis=n-1+m-1;
int Ans[]=new int[max-min+1];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int k1=(int)Math.min(n-1-i,i);
int k2=(int)Math.min(m-1-j,j);
Ans[Dis-k1-k2-min]++;
}
}
int itr=min;
for(int i=0;i< max-min+1;i++){
for(int j=0;j<Ans[i];j++){
System.out.print(itr+" ");
}
itr++;
}
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
|
cd0d2fedb78bd2875287912627b1dc39
|
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 {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt(), m = scan.nextInt();
ArrayList<Integer> list = 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;
list.add(Math.max(Math.max(d1, d2), Math.max(d3, d4)));
}
}
Collections.sort(list);
for(int i : list) output.print(i + " ");
output.println();
}
output.flush();
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
public static void printArray(int arr[]) {
for(int i:arr) 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());
}
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 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
|
3b1e0402ab24106b1fed5a8b0ba839a8
|
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.*;
import java.math.*;
/**
* @author Naitik
*
*/
public class Main
{
static FastReader sc=new FastReader();
static int dp[];
static boolean v[];
// static int mod=998244353;;
static int mod=1000000007;
static int max;
static int bit[];
//static long fact[];
// static long A[];
static HashMap<Integer,Integer> map;
//static StringBuffer sb=new StringBuffer("");
//static HashMap<Integer,Integer> map;
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
outer :while (ttt-- > 0)
{
int n=i();
int m=i();
int A[][]=new int[n+1][m+1];
int y=(n+1)/2;
int z=(m+1)/2;
ArrayList<Integer> l=new ArrayList<Integer>();
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
int op1=Math.abs(n-i)+Math.abs(m-j);
int op2=Math.abs(n-i)+Math.abs(1-j);
int op3=Math.abs(1-i)+Math.abs(m-j);
int op4=Math.abs(1-i)+Math.abs(1-j);
A[i][j]=max(op1,op2,op3,op4);
l.add(A[i][j]);
}
}
l.sort(null);
for(int i : l) {
out.print(i+" ");
}
out.println();
// ArrayList<Integer> B[]=new ArrayList[m+10];
// for(int i=0;i<B.length;i++) {
// B[i]=new ArrayList<Integer>();
// }
// int c=1;
// for(int j=z;j<=m;j++) {
// for(int i=1;i<=n;i++) {
// B[c].add(A[i][j]);
// }
// B[c].sort(null);
// c+=2;
// }
// c=2;
// for(int j=z-1;j>0;j--) {
// for(int i=1;i<=n;i++) {
// B[c].add(A[i][j]);
// }
// B[c].sort(null);
// c+=2;
// }
// for(int i=1;i<B.length;i++) {
// for(int j : B[i]) {
// out.print(j+" ");
// }
// }
// out.println();
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return find(A, A[a]);
//}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
static void add(int v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(int v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
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 power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
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 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
|
cf18fdc4581853219fadf07059552df3
|
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;
import java.util.*;
public class B {
static final int INF = (int)1e9+5;
public static void main(String args[]) throws Exception {
FastScanner sc = new FastScanner();
int T = 1;
T = sc.nextInt();
PrintWriter pw = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
while (T-- > 0) {
solve(sc, pw, sb);
}
pw.print(sb);
pw.close();
}
public static void solve(FastScanner sc, PrintWriter pw, StringBuilder sb) throws Exception {
int n=sc.nextInt();
int m=sc.nextInt();
ArrayList<Integer> arr=new ArrayList<>();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
// (1,1)
int value=Math.abs(i-1)+Math.abs(j-1);
// (1,m)
value=Math.max(value, Math.abs(i-1)+Math.abs(j-m));
// (n,1)
value=Math.max(value, Math.abs(i-n)+Math.abs(j-1));
// (n,m)
value=Math.max(value, Math.abs(i-n)+Math.abs(j-m));
arr.add(value);
}
}
Collections.sort(arr);
for(int i:arr){
sb.append(i+" ");
}
sb.append("\n");
}
public class Seat{
int row,col;
public Seat(int row,int col){
this.row=row;
this.col=col;
}
}
public static long[] sort(long[] arr){
ArrayList<Long> temp = new ArrayList<>();
for(long x:arr) temp.add(x);
Collections.sort(temp);
int i=0;
for(long x:temp){
arr[i++]=x;
}
return arr;
}
public static int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int[] readIntArray(int n){
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=nextInt();
}
return arr;
}
public long[] readLongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=nextLong();
}
return arr;
}
}
}
|
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
|
e1abe5ed96e3284267fb3ea6818aba01
|
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 Tes{
static Scanner sc=new Scanner(System.in);
static class Pair{
int x;
int y;
int dist;
public Pair(int x,int y,int dist){
this.x=x;
this.y=y;
this.dist=dist;
}
}
public static void sol(){
int n=sc.nextInt();
int m=sc.nextInt();
int dp[][]=new int[n][m];
Queue<Pair> q=new LinkedList<>();
int min=n/2+m/2;
boolean[][] vis=new boolean[n][m];
if(n%2==0 && m%2==0){
q.add(new Pair(n/2,m/2,min));
vis[n/2][m/2]=true;
q.add(new Pair((n-1)/2,m/2,min));
vis[(n-1)/2][m/2]=true;
q.add(new Pair(n/2,(m-1)/2,min));
vis[n/2][(m-1)/2]=true;
q.add(new Pair((n-1)/2,(m-1)/2,min));
vis[(n-1)/2][(m-1)/2]=true;
}
else if(n%2==0 && m%2!=0){
q.add(new Pair((n-1)/2,m/2,min));
vis[(n-1)/2][m/2]=true;
q.add(new Pair(n/2,m/2,min));
vis[n/2][m/2]=true;
}
else if(n%2!=0 && m%2==0){
q.add(new Pair(n/2,m/2,min));
vis[n/2][m/2]=true;
q.add(new Pair(n/2,(m-1)/2,min));
vis[n/2][(m-1)/2]=true;
}
else{
q.add(new Pair(n/2,m/2,min));
vis[n/2][m/2]=true;
}
while(!q.isEmpty()){
Pair p=q.remove();
System.out.print(p.dist+" ");
if(p.x-1>=0 && !vis[p.x-1][p.y]){
q.add(new Pair(p.x-1,p.y,p.dist+1));
vis[p.x-1][p.y]=true;
}
if(p.y-1>=0 && !vis[p.x][p.y-1]){
q.add(new Pair(p.x,p.y-1,p.dist+1));
vis[p.x][p.y-1]=true;
}
if(p.x+1<n && !vis[p.x+1][p.y]){
q.add(new Pair(p.x+1,p.y,p.dist+1));
vis[p.x+1][p.y]=true;
}
if(p.y+1<m && !vis[p.x][p.y+1]){
q.add(new Pair(p.x,p.y+1,p.dist+1));
vis[p.x][p.y+1]=true;
}
}
System.out.println();
}
public static void main(String args[]){
int t=sc.nextInt();
while(t-->0){
sol();
}
}
}
|
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
|
82ecf0b9d5912612000f8b73d63f4fe7
|
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.StreamTokenizer;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer tokenizer = new StreamTokenizer(
new BufferedReader(new InputStreamReader(System.in)));
int t = 0;
if (tokenizer.nextToken() == StreamTokenizer.TT_NUMBER) {
t = (int)tokenizer.nval;
}
for (int p = 0; p < t; p++) {
int n, m;
n = 0;
m = 0;
if (tokenizer.nextToken() == StreamTokenizer.TT_NUMBER) {
n = (int) tokenizer.nval;
}
if (tokenizer.nextToken() == StreamTokenizer.TT_NUMBER) {
m = (int) tokenizer.nval;
}
int[][] matrix = new int[n][m];
int maxValue = n+m-2;
matrix[0][0] = maxValue;
matrix[0][m-1] = maxValue;
matrix[n-1][0] = maxValue;
matrix[n-1][m-1] = maxValue;
for (int i = 0; i < n; i++) {
if (i != 0) {
if (i <= (n - 1) / 2) {
matrix[i][0] = matrix[i - 1][0] - 1;
} else if (i == n/2 && n % 2 == 0) {
matrix[i][0] = matrix[i - 1][0];
} else if (i == n/2 && n % 2 != 0) {
matrix[i][0] = matrix[i - 1][0] - 1;
} else {
matrix[i][0] = matrix[i - 1][0] + 1;
}
}
for (int j = 1; j < m; j++) {
if (i == 0 && j ==0) {matrix[i][j] = maxValue;}
else if (j <= (m-1)/2) {
matrix[i][j] = matrix[i][j-1]-1;
} else if (j == m/2 && m % 2==0) {
matrix[i][j] = matrix[i][j-1];
} else if (j == m/2 && m % 2!=0) {
matrix[i][j] = matrix[i][j-1]-1;
}
else {
matrix[i][j] = matrix[i][j-1]+1;
}
}
}
int[] arr = new int[n*m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i*(m)+j] = matrix[i][j];
}
}
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 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
|
54eb00797c0936a6dbb8261cc00e736f
|
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.*;
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.sqrt;
import static java.lang.Math.floor;
public class topcoder {
static void rec(int [][]mat, int i, int j, int n, int m, boolean vis[][], int [][]res) {
if(i <= 0 || j <= 0 || i > n || j > m || vis[i][j])return;
int a = Math.abs(i-1);
int b = Math.abs(j-1);
int c = Math.abs(i-1);
int d = Math.abs(j-m);
int e = Math.abs(i-n);
int f = Math.abs(j-m);
int x = Math.abs(i-n);
int y = Math.abs(j-1);
vis[i][j] = true;
int max = Math.max(a+b,c+d);
max = Math.max(max, e+f);
max = Math.max(max, x+y);
res[i][j] = max;
rec(mat,i-1,j-1,n,m,vis,res);
rec(mat,i+1,j-1,n,m,vis,res);
rec(mat,i-1,j+1,n,m,vis,res);
rec(mat,i+1,j+1,n,m,vis,res);
rec(mat,i,j-1,n,m,vis,res);
rec(mat,i,j+1,n,m,vis,res);
rec(mat,i-1,j,n,m,vis,res);
rec(mat,i+1,j,n,m,vis,res);
}
public static void main(String args[])throws IOException {
BufferedReader ob = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(ob.readLine());
while(t --> 0) {
StringTokenizer st = new StringTokenizer(ob.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int [][]res = new int[n+1][m+1];
boolean [][]vis = new boolean[n+1][m+1];
int i = n/2;
i += (n%2);
int j = m/2;
j += m%2;
int [][]mat = new int[n][m];
rec(mat,i,j,n,m,vis,res);
ArrayList<Integer>list = new ArrayList<>();
for( i = 1; i <= n; i++) {
for( j = 1; j <= m; j++) {
list.add(res[i][j]);
}
}
ArrayList<Integer>df = new ArrayList<>();
//kkjasfksdlkfjh
//speedforces
int k = 3;
Collections.sort(list);
for(int num : list) {
System.out.print(num+" ");
}
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
|
badd8df52ae7cd662945eab2a687cf1b
|
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 int find(int i,int j,int n,int m) {
int midh=n/2;
int midl=m/2;
int b1=n,b2=m;
if(i>midh) {
b1=1;
}
if(j>midl) {
b2=1;
}
return Math.abs(i-b1)+Math.abs(j-b2);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
while(k--!=0) {
int n=sc.nextInt(),m=sc.nextInt(),go=0;
int num[]=new int[n*m];
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
num[go++]=find(i,j,n,m);
}
}
Arrays.sort(num);
for(int i=0;i<num.length;i++)System.out.print(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 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
|
1126bc92782ed53fd8550276b25f3d8a
|
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.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class cf1 {
static FastReader x = new FastReader();
static OutputStream outputStream = System.out;
static PrintWriter out = new PrintWriter(outputStream);
/*---------------------------------------CODE STARTS HERE-------------------------*/
public static void main(String[] args) {
int t = x.nextInt();
StringBuilder str = new StringBuilder();
while (t > 0) {
int n = x.nextInt();
int m = x.nextInt();
int a[][] =new int[n][m];
ArrayList<Integer> ar = new ArrayList<>();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int p= i+j;
int q = (n-1-i)+j;
int r = i+(m-1-j);
int s = (n-1-i)+(m-1-j);
int ans = max(p, q);
int ans1 = max(r,s);
ar.add(max(ans,ans1));
}
}
Collections.sort(ar);
for(int i=0;i<ar.size();i++) {
System.out.print(ar.get(i)+" ");
}System.out.println();
t--;
}
out.println(str);
out.flush();
}
/*--------------------------------------------FAST I/O--------------------------------*/
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;
}
char nextchar() {
char ch = ' ';
try {
ch = (char) br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
/*--------------------------------------------BOILER PLATE---------------------------*/
static int[] readarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = x.nextInt();
}
return arr;
}
static int[] sortint(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static long[] sortlong(long a[]) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
return a;
}
static int pow(int x, int y) {
int result = 1;
while (y > 0) {
if (y % 2 == 0) {
x = x * x;
y = y / 2;
} else {
result = result * x;
y = y - 1;
}
}
return result;
}
static int[] revsort(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al, Comparator.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static int[] gcd(int a, int b, int ar[]) {
if (b == 0) {
ar[0] = a;
ar[1] = 1;
ar[2] = 0;
return ar;
}
ar = gcd(b, a % b, ar);
int t = ar[1];
ar[1] = ar[2];
ar[2] = t - (a / b) * ar[2];
return ar;
}
static boolean[] esieve(int n) {
boolean p[] = new boolean[n + 1];
Arrays.fill(p, true);
for (int i = 2; i * i <= n; i++) {
if (p[i] == true) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static ArrayList<Integer> primes(int n) {
boolean p[] = new boolean[n + 1];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(p, true);
int i = 0;
for (i = 2; i * i <= n; i++) {
if (p[i] == true) {
al.add(i);
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
for (i = i; i <= n; i++) {
if (p[i] == true) {
al.add(i);
}
}
return al;
}
static int etf(int n) {
int res = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res /= i;
res *= (i - 1);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
res /= n;
res *= (n - 1);
}
return res;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
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 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
|
41e1f22c7e08b0161a9ef43867a7adac
|
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
|
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
public class Main {
public Main() {
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Main.InputReader in = new Main.InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Main.TaskB solver = new Main.TaskB();
int testCount = Integer.parseInt(in.next());
for(int i = 1; i <= testCount; ++i) {
solver.solve(i, in, out);
}
out.close();
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (this.snumChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.snumChars) {
this.curChar = 0;
try {
this.snumChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.snumChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for(c = this.read(); this.isSpaceChar(c); c = this.read()) {
}
int sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
do {
res *= 10;
res += c - 48;
c = this.read();
} while(!this.isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c;
for(c = this.read(); this.isSpaceChar(c); c = this.read()) {
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = this.read();
} while(!this.isSpaceChar(c));
return res.toString();
}
public String next() {
return this.readString();
}
public boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
static class TaskB {
TaskB() {
}
public void solve(int testNumber, Main.InputReader in, PrintWriter out) {
int n = in.nextInt();
int c = in.nextInt();
ArrayList<Integer> distance = new ArrayList();
int i;
for(i = 1; i <= n; ++i) {
for(int j = 1; j <= c; ++j) {
int max = Math.max(0, n - i + c - j);
max = Math.max(max, Math.abs(1 - i) + Math.abs(1 - j));
max = Math.max(max, Math.abs(1 - i) + Math.abs(c - j));
max = Math.max(max, Math.abs(n - i) + Math.abs(1 - j));
distance.add(max);
}
}
Collections.sort(distance);
for(i = 0; i < distance.size(); ++i) {
out.print(distance.get(i) + " ");
}
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
|
0746b3145cf2370a1707fc04f98bf972
|
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 contestb {
FastScanner scn;
PrintWriter w;
PrintStream fs;
long MOD = 1000000007;
int MAX = 200005;
long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);}
long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
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);}
void reverseSort(int[] arr){List<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);}}
boolean LOCAL;
void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));}
//SUFFICIENT DRY RUN????LOGIC VERIFIED FOR ALL TEST CASES???
void solve(){
int t=scn.nextInt();
while(t-->0)
{
int n=scn.nextInt();
int m = scn.nextInt();
// int[][] ar = new int[n+1][m+1];
int tilln = n/2;
if((n&1)!=0) tilln++;
int tillm = m/2;
if((m&1)!=0) tillm++;
int count =0;
ArrayList<Integer> ans = new ArrayList<>();
for(int i=1;i<=tilln;i++){
for(int j=1;j<=tillm;j++){
// ar[i][j] = Math.abs(n-i) + Math.abs(m-j);
int val = 4;
if(tillm==(m/2+1)&&j==tillm) val=2;
if(tilln==(n/2+1)&&i==tilln) val=2;
if(tillm==(m/2+1)&&j==tillm&&tilln==(n/2+1)&&i==tilln) val = 1;
for(int k=1;k<=val;k++){
ans.add(Math.abs(n-i) + Math.abs(m-j));
}
count++;
}
}
Collections.sort(ans);
for(int e:ans){
w.print(e+" ");
}
w.println();
}
}
void run() {
try {
long ct = System.currentTimeMillis();
scn = new FastScanner(new File("input.txt"));
w = new PrintWriter(new File("output.txt"));
fs=new PrintStream("error.txt");
System.setErr(fs);
LOCAL=true;
solve();
w.close();
System.err.println(System.currentTimeMillis() - ct);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
scn = new FastScanner(System.in);
w = new PrintWriter(System.out);
LOCAL=false;
solve();
w.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(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());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; }
void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}
int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b))
boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;}
public static void main(String[] args) {
new contestb().runIO();
}
}
|
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
|
7849327e5939ca735f758a3e8a26800e
|
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 s1 {
public static FastScanner scan;
public static PrintWriter out;
public static void main(String[] args) throws Exception {
scan=new FastScanner(System.in);
out=new PrintWriter(System.out);
// int T=1;
int T=scan.nextInt();
while(T-->0) {
int n=scan.nextInt(),m=scan.nextInt();
ArrayDeque<Integer> queX=new ArrayDeque<>(),queY=new ArrayDeque<>();
boolean[][] vis=new boolean[n][m];
queX.add(n/2);
queY.add(m/2);
vis[n/2][m/2]=true;
if(n%2==0&&m%2==0) {
queX.add(n/2-1);
queY.add(m/2-1);
vis[n/2-1][m/2-1]=true;
} if(n%2==0) {
queX.add(n/2-1);
queY.add(m/2);
vis[n/2-1][m/2]=true;
} if(m%2==0) {
queX.add(n/2);
queY.add(m/2-1);
vis[n/2][m/2-1]=true;
}
int[] dirX=new int[]{0,0,-1,1},dirY=new int[]{1,-1,0,0};
int dist=n/2+m/2;
while(queX.size()>0) {
int size=queX.size();
while(size-->0) {
int curX=queX.poll(),curY=queY.poll();
out.print(dist+" ");
for(int i=0;i<4;i++) {
int nextX=curX+dirX[i],nextY=curY+dirY[i];
if(nextX<0||nextX>=n||nextY<0||nextY>=m) continue;
if(vis[nextX][nextY]) continue;
vis[nextX][nextY]=true;
queX.add(nextX);
queY.add(nextY);
}
} dist++;
} out.println();
} out.close();
}
}
class Triple {
Triple(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
} int a,b,c;
}
class Pair {
int a,b;
Pair(int a,int b) {
this.a=a;
this.b=b;
}
}
class FastScanner {
private InputStream stream;
private byte[] buf=new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) { this.stream=stream; }
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++];
}
boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; }
boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; }
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String next() {
int c=read();
while(isSpaceChar(c)) c=read();
StringBuilder res=new StringBuilder();
do {
res.appendCodePoint(c);
c=read();
} while(!isSpaceChar(c));
return res.toString();
}
String nextLine() {
int c=read();
while(isEndline(c)) c=read();
StringBuilder res=new StringBuilder();
do {
res.appendCodePoint(c);
c=read();
} while(!isEndline(c));
return res.toString();
}
}
|
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
|
ecbd40959ab1dbf92ae23a554e6cf14d
|
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 Jan15P2 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int m = sc.nextInt();
Integer[][] o = new Integer[n][m];
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
o[j][k] = Math.max(Math.max(Math.abs(n - 1-j) + Math.abs(m - 1 - k), Math.abs(j) + Math.abs(k)), Math.max(Math.abs(n-1-j)+k, Math.abs(m-1-k)+j));
}
}
Integer[] lo = new Integer[n*m];
int counter = 0;
for (int j = 0; j < n; j++) {
for (int k = 0; k < m; k++) {
lo[counter] = o[j][k];
counter++;
}
}
Arrays.sort(lo);
for (int j = 0; j < lo.length; j++) {
out.print(lo[j] + " ");
}
out.println();
}
// Start writing your solution here. -------------------------------------
/*
* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); //
* read input as long double d = sc.nextDouble(); // read input as double String
* str = sc.next(); // read input as String String s = sc.nextLine(); // read
* whole line as String
*
* int result = 3*n; out.println(result); // print via PrintWriter
*/
// 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();
}
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
|
193db8190111ec8b53b97916ed74b3ef
|
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
|
/*
Setting up my ambitions
Check 'em one at a time, yeah, I made it
Now it's time for ignition
I'm the start, heat it up
They follow, burning up a chain reaction, oh-oh-oh
Go fire it up, oh, oh, no
Assemble the crowd now, now
Line 'em up on the ground
No leaving anyone behind
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1627B
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
PriorityQueue<Seat> pq = new PriorityQueue<Seat>((x,y) -> {
int max1 = max(x.r, N-x.r-1)+max(x.c, M-x.c-1);
int max2 = max(y.r, N-y.r-1)+max(y.c, M-y.c-1);
return max1-max2;
});
for(int r=0; r < N; r++)
for(int c=0; c < M; c++)
pq.add(new Seat(r, c));
for(int k=0; k < N*M; k++)
{
Seat x = pq.poll();
//System.out.println(x.r+" "+x.c);
int res = max(x.r, N-x.r-1)+max(x.c, M-x.c-1);
sb.append(res+" ");
}
sb.append("\n");
}
System.out.print(sb);
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
class Seat
{
public int r;
public int c;
public Seat(int a, int b)
{
r = a;
c = 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 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
|
250469e0029f3116b5025eeed1826c6b
|
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 {
public static void bfs(int n,int m){
Queue<int[]> que = new LinkedList<>();
int vis[][] = new int[n][m];
if(n%2==1 && m%2==1){
que.add(new int[]{n/2,m/2});
vis[n/2][m/2]=1;
}
else if(n%2==1 && m%2==0){
que.add(new int[]{n/2,m/2-1});
que.add(new int[]{n/2,m/2});
vis[n/2][m/2-1]=1;
vis[n/2][m/2]=1;
}
else if(n%2==0 && m%2==1){
que.add(new int[]{n/2-1,m/2});
que.add(new int[]{n/2,m/2});
vis[n/2-1][m/2]=1;
vis[n/2][m/2]=1;
}
else if(n%2==0 && m%2==0){
que.add(new int[]{n/2-1,m/2-1});
que.add(new int[]{n/2-1,m/2});
que.add(new int[]{n/2,m/2-1});
que.add(new int[]{n/2,m/2});
vis[n/2-1][m/2-1]=1;
vis[n/2-1][m/2]=1;
vis[n/2][m/2-1]=1;
vis[n/2][m/2]=1;
}
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
List<Integer> ar = new ArrayList<>();
while(que.size()>0){
ar.add(que.size());
int k = que.size();
while(k-->0){
int cur[]=que.remove();
int x = cur[0];
int y = cur[1];
for(int t=0;t<4;t++){
int nx = x+dx[t];
int ny = y+dy[t];
if(nx>=0 && ny>=0 && nx<n && ny<m && vis[nx][ny]==0){
que.add(new int[]{nx,ny});
vis[nx][ny]=1;
}
}
}
}
int x = n/2+m/2;
for(int i=0;i<ar.size();i++){
for(int j=0;j<ar.get(i);j++){
System.out.print(x+" ");
}
x++;
}
System.out.println();
}
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");
}
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
bfs(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 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
|
f18e3cc86544e3dcfd65d0e5b7a82633
|
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.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static FastReader fr;
static int defMod = 1000000007;
static int arrForIndexSort[];
static Integer map1[];
static Integer map2[];
static int globalVal;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair{
int first;
int second;
Pair(int first, int second){
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object b) {
Pair a = (Pair)b;
if(this.first == a.first && this.second==a.second) {
return true;
}
return false;
}
}
class PairSorter implements Comparator<Main.Pair>{
public int compare(Pair a, Pair b) {
if(a.first!=b.first) {
return a.first-b.first;
}
return a.second-b.second;
}
}
static class DoublePair{
double first;
double second;
DoublePair(double first, double second){
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object b) {
Pair a = (Pair)b;
if(this.first == a.first && this.second==a.second) {
return true;
}
return false;
}
}
class DoublePairSorter implements Comparator<Main.DoublePair>{
public int compare(DoublePair a, DoublePair b) {
if(a.second>b.second) {
return 1;
}
else if(a.second<b.second) {
return -1;
}
return 0;
}
}
class IndexSorter implements Comparator<Integer>{
public int compare(Integer a, Integer b) {
//desc
if(arrForIndexSort[b]==arrForIndexSort[a]) {
return b-a;
}
return arrForIndexSort[b]-arrForIndexSort[a];
}
}
class ListSorter implements Comparator<List>{
public int compare(List a, List b) {
return b.size()-a.size();
}
}
static class DisjointSet{
int[] dsu;
public DisjointSet(int n) {
makeSet(n);
}
public void makeSet(int n) {
dsu = new int[n+1];
//*** 1 Based indexing ***
for(int i=1;i<=n;i++) {
dsu[i] = -1;
}
}
public int find(int i) {
while(dsu[i] > 0) {
i = dsu[i];
}
return i;
}
public void union(int i, int j) {
int iRep = find(i);
int jRep = find(j);
if(iRep == jRep) {
return;
}
if(dsu[iRep]>dsu[jRep]) {
dsu[jRep] += dsu[iRep];
dsu[iRep] = jRep;
}
else {
dsu[iRep] += dsu[jRep];
dsu[jRep] = iRep;
}
}
}
static class SegmentTree{
int[] tree;
int[] originalArr;
public SegmentTree(int[] a) {
int n = a.length;
tree = new int[4*n];
build(1, a, 0, n-1);
originalArr = a;
}
public void build(int node, int[] a, int start, int end){
if(start == end) {
tree[node] = a[start];
return;
}
int mid = (start+end)/2;
build(2*node, a, start, mid);
build(2*node+1, a, mid+1, end);
tree[node] = Math.min(tree[2*node], tree[2*node+1]);
}
public void update(int node, int start, int end, int idx, int val) {
if(start == end) {
tree[node] = originalArr[idx] = val;
return;
}
int mid = (start+ end)/2;
if(idx<=mid) {
update(2*node, start, mid, idx, val);
}
else {
update(2*node+1, mid+1, end, idx, val);
}
tree[node] = Math.min(tree[2*node], tree[2*node+1]);
}
public int query(int node, int start, int end, int l, int r) {
if(r<start || l>end) {
return Integer.MAX_VALUE;
}
if(start>=l && end<=r) {
return tree[node];
}
int mid = (start+end)/2;
int p1 = query(2*node, start, mid, l, r);
int p2 = query(2*node+1, mid+1, end, l, r);
return Math.min(p1, p2);
}
}
//1 Based Indexing
static class BIT{
long[] tree;
public BIT(int n) {
tree = new long[n];
}
public BIT(int[] a) {
this(a.length);
for(int i=1;i<tree.length;i++) {
add(i, a[i]);
}
}
public void add(int i, int val) {
for(int j=i;j<tree.length;j += j&(-j)) {
tree[j] += val;
}
}
public long sum(int i) {
long ret = 0;
for(int j=i;j>0;j -= j&(-j)) {
ret += tree[j];
}
return ret;
}
public long query(int l, int r) {
long lSum = 0;
if(l>1) {
lSum = sum(l-1);
}
long rSum = sum(r);
return rSum-lSum;
}
}
static void shuffle(int a[])
{
for (int i = 0; i < a.length; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
int x = a[t];
a[t] = a[i];
a[i] = x;
}
}
public static void main(String[] args) {
fr = new FastReader();
int T = 1;
T = fr.nextInt();
int t1 = T;
while (T-- > 0) {
solve(t1-T);
}
}
/* Things to remember
* Keep it Simple (Golden Rule)
* Think Reverse
* Don't get stuck on one approach
* Check corner case
* On error, check->edge case, implementation and question,
* On error, check constraints, check if long needed,
* On error, which one to choose when two values are equal for greedy
* for two different constraints, check if they share same value. like x=2, y=2;
*/
// 1 1 1 2 3 6 6 6
public static void solve(int testcase) {
int n = fr.nextInt();
int m = fr.nextInt();
int count[] = new int[(n/2)+(m/2)+1];
int minDistance = (n/2)+(m/2);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
int midRow = (n+1)/2;
int midCol = (m+1)/2;
if(n%2==0) {
if(i>midRow) {
midRow++;
}
}
if(m%2==0) {
if(j>midCol) {
midCol++;
}
}
int manhattanDistance = Math.abs(midRow-i)+Math.abs(midCol-j);
count[manhattanDistance]++;
}
}
int k = 0;
StringBuilder sb = new StringBuilder();
for(int i=0;i<=n*m-1;i++) {
if(count[k]==0) {
k++;
}
sb.append(minDistance+k).append(" ");
count[k]--;
}
System.out.println(sb);
//EOF
}
}
|
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
|
b4d7a76159b10f6d3cc365b89850f727
|
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.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class R766B {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
solve(n, m);
}
}
private static void solve(int n, int m) {
int[][] matrix = new int[n][m];
calculate(matrix, 0, 0);
calculate(matrix, 0, m - 1);
calculate(matrix, n - 1, 0);
calculate(matrix, n - 1, m - 1);
List<Integer> list = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
list.add(matrix[i][j]);
}
}
StringBuilder builder = new StringBuilder();
list.stream().sorted().forEach(distance -> builder.append(distance).append(" "));
System.out.println(builder);
}
private static void calculate(int[][] matrix, int row, int col) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = Math.max(matrix[i][j], Math.abs(i - row) + Math.abs(j - col));
}
}
}
}
/*
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 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 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
|
4da9eca405d2c5fed9c5545215770e75
|
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 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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args){
FastReader read = new FastReader();
int testCases = read.nextInt();
while(testCases > 0){
int n = read.nextInt();
int m = read.nextInt();
ArrayList<Integer> dist = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
int dist1 = Math.max(Math.abs(i - 0) + Math.abs(j - 0), Math.abs(i - 0) + Math.abs(j - m + 1));
int dist2 = Math.max(Math.abs(i - n + 1) + Math.abs(j - 0), Math.abs(i - n + 1) + Math.abs(j - m + 1));
dist.add(Math.max(dist1, dist2));
}
}
Collections.sort(dist);
for(int i = 0; i < n * m; i++) System.out.print(dist.get(i) + " ");
System.out.println();
testCases--;
}
}
}
|
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
|
92e663cba72f0ee7d1156ba551098e5a
|
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 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t= sc.nextInt();
while(t-->0){
int n= sc.nextInt();
int m=sc.nextInt();
List<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a.add(Math.max(i, n-1-i ) + Math.max(j, m-1-j ));
}
}
Collections.sort(a);
for (int x: a
) {
pw.print(x + " ");
}
pw.println();
}
pw.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 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
|
255e8649092a9f8fedb4fd7220f98312
|
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.List;
import java.util.Scanner;
public class B1627 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
StringBuilder output = new StringBuilder();
for (int t=0; t<T; t++) {
int R = in.nextInt();
int C = in.nextInt();
int[] stat = new int[Math.max(R,C)];
int rCenter1 = R/2;
int rCenter2 = R-1-rCenter1;
int cCenter1 = C/2;
int cCenter2 = C-1-cCenter1;
for (int r=0; r<R; r++) {
int distR = Math.min(Math.abs(r-rCenter1), Math.abs(r-rCenter2));
for (int c=0; c<C; c++) {
int distC = Math.min(Math.abs(c-cCenter1), Math.abs(c-cCenter2));
int dist = distR + distC;
stat[dist]++;
}
}
int idx = 0;
int baseDist = C/2 + R/2;
for (int k=0; k<R*C; k++) {
int dist = baseDist + idx;
output.append(dist).append(' ');
stat[idx]--;
if (stat[idx] == 0) idx++;
}
output.append('\n');
}
System.out.print(output);
}
}
|
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
|
170a87d6ed8f6ca8cd118ccca7cbf5a7
|
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 static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.System.out;
public class pre97 {
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 obj = new FastReader();
int tc = obj.nextInt();
while(tc--!=0){
int n = obj.nextInt(),m = obj.nextInt();
ArrayList<Integer> set = new ArrayList<>();
int d = n/2+m/2;
if(n%2==0 && m%2==0){
int x1 = n/2,x2 = n/2-1;
int y1 = m/2,y2 = m/2-1;
// int ans = min(abs(x1-i)+abs())
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int ans = min(abs(x1-i)+abs(y1-j),(abs(x2-i)+abs(y2-j)));
ans = min(ans,abs(x1-i)+abs(y2-j));
ans = min(ans,abs(x2-i)+abs(y1-j));
set.add(ans+d);
}
}
}else if(n%2==1 && m%2==1){
int x = n/2,y = m/2;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
set.add(abs(x-i)+abs(y-j)+d);
}
}
}else if(n%2==0){
int x1 = n/2,x2 = n/2-1,y = m/2;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
set.add(min(abs(x1-i)+abs(y-j),abs(x2-i)+abs(y-j))+d);
}
}
}else if(m%2==0){
int y1 = m/2,y2 = m/2-1,x = n/2;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
set.add(min(abs(x-i)+abs(y1-j),abs(x-i)+abs(y2-j))+d);
}
}
}
Collections.sort(set);
for(int i:set) out.print(i+" ");
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
|
d71c2189858f0ef433d2250d3a229888
|
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();
while(T-->0){
int n = sc.nextInt();
int m = sc.nextInt();
List<Integer> list = new ArrayList<>();
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
list.add(calDis(i,j,n-1,m-1));
}
}
Collections.sort(list);
for(int i:list){
System.out.print(i+" ");
}
System.out.println();
}
}
public static int calDis(int i,int j,int n,int m){
int disa = i+j;
int disb = m-j + i;
int disc = j + n-i;
int disd = n-i+m-j;
return Math.max(Math.max(disa, disb), Math.max(disc, disd));
}
}
|
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
|
0065679f99dea7c6116858b6d6cbccaf
|
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 Solution {
static BufferedReader bf;
static PrintWriter out;
public static void main (String[] args)throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int t = Integer.parseInt(bf.readLine());
while(t-->0){
solve();
}
}
public static void solve()throws IOException{
String []temp = bf.readLine().split(" ");
int n = toInt(temp[0]);
int m = toInt(temp[1]);
List<Integer>list = new ArrayList<>();
for(int i =1;i<=n;i++){
for(int j = 1;j<=m;j++){
int max = -1;
max = Math.max(max,dist(i,j,1,1));
max = Math.max(max,dist(i,j,1,m));
max = Math.max(max,dist(i,j,n,1));
max = Math.max(max,dist(i,j,n,m));
list.add(max);
}
}
Collections.sort(list);
for(int i =0;i<list.size();i++){
out.print(list.get(i)+ " ");
}
out.println();
out.flush();
}
public static int dist(int a,int b,int x,int y){
return Math.abs(a - x) + Math.abs(b - y);
}
public static int count(long num,String s){
StringBuilder sb = new StringBuilder(s);
StringBuilder st = new StringBuilder(s);
int count = 0;
int j = st.length()-2;
for(int i = sb.length()-1;i>=0;i--){
sb.append(sb.charAt(i));
if(j >= 0){
st.append(st.charAt(j));
j--;
}
}
if(num%(Long.parseLong(sb.toString())) == 0){
count++;
}
if(num%(Long.parseLong(st.toString())) == 0){
count++;
}
return count;
}
// code for input
public static void print(String s ){
System.out.print(s);
}
public static void print(int num ){
System.out.print(num);
}
public static void print(long num ){
System.out.print(num);
}
public static void println(String s){
System.out.println(s);
}
public static void println(int num){
System.out.println(num);
}
public static void println(long num){
System.out.println(num);
}
public static void println(){
System.out.println();
}
public static int toInt(String s){
return Integer.parseInt(s);
}
public static long toLong(String s){
return Long.parseLong(s);
}
public static String[] nextStringArray()throws IOException{
return bf.readLine().split(" ");
}
public static int nextInt()throws IOException{
return Integer.parseInt(bf.readLine());
}
public static long nextLong()throws IOException{
return Long.parseLong(bf.readLine());
}
public static String nextString()throws IOException{
return bf.readLine();
}
public static int[] nextIntArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
int[]arr = new int[n];
for(int i =0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
return arr;
}
public static long[] nextLongArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
long[]arr = new long[n];
for(int i =0;i<n;i++){
arr[i] = Long.parseLong(str[i]);
}
return arr;
}
public static int[][] newIntMatrix(int r,int c)throws IOException{
int[][]arr = new int[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Integer.parseInt(str[j]);
}
}
return arr;
}
public static long[][] newLongMatrix(int r,int c)throws IOException{
long[][]arr = new long[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Long.parseLong(str[j]);
}
}
return arr;
}
static class pair{
int one;
int two;
pair(int one,int two){
this.one = one ;
this.two =two;
}
}
public static long gcd(long a,long b){
if(b == 0)return a;
return gcd(b,a%b);
}
public static long lcm(long a,long b){
return (a*b)/(gcd(a,b));
}
public static boolean isPalindrome(String s){
int i = 0;
int j = s.length()-1;
while(i<=j){
if(s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
}
// use some math tricks it might help
// sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently
// always use long number to do 10^9+7 modulo
// if a problem is related to binary string it could also be related to parenthesis
// *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work******
// try sorting
// try to think in opposite direction of question it might work in your way
// if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general
// if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work.
// in range query sums try to do binary search it could work
// analyse the time complexity of program thoroughly
// anylyse the test cases properly
// if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required
// try to do the opposite operation of what is given in the problem
|
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
|
687e08107eb4c6f8fa3493f41279da91
|
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.RoadMap.Diff1300;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
* @author Syed Ali.
* @createdAt 02/02/2022, Wednesday, 00:33
*/
public class NotSitting {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt(), m = sc.nextInt();
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
int dist = Math.max(i, n - 1 - i) + Math.max(j, m - 1 - j);
minheap.add(dist);
}
while(minheap.size() > 0)
System.out.print(minheap.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
|
3c97a85b825fd168cd0f0a893f94100d
|
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 _1627B {
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();
int []dis=new int[n*m];
int k=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int rt=Math.abs(i-1)+Math.abs(m-j);
int rb=Math.abs(n-i)+Math.abs(m-j);
int lt=i+j-2;
int lb=j-1+n-i;
dis[k++]=(Math.max(Math.max(rt,rb),Math.max(lt,lb)));
}
}
Arrays.sort(dis);
for(int i=0;i<dis.length;i++)
System.out.print(dis[i]+" ");
System.out.println();
}
}
static class FastIO {
InputStream dis;
byte[] buffer = new byte[1 << 17];
int pointer = 0;
public FastIO(String fileName) throws Exception {
dis = new FileInputStream(fileName);
}
public FastIO(InputStream is) throws Exception {
dis = is;
}
int nextInt() throws Exception {
int ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
long nextLong() throws Exception {
long ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
byte nextByte() throws Exception {
if (pointer == buffer.length) {
dis.read(buffer, 0, buffer.length);
pointer = 0;
}
return buffer[pointer++];
}
String next() throws Exception {
StringBuffer ret = new StringBuffer();
byte b;
do {
b = nextByte();
} while (b <= ' ');
while (b > ' ') {
ret.appendCodePoint(b);
b = nextByte();
}
return ret.toString();
}
}
}
|
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
|
95297ee46e2ea304f5a9f617d62951da
|
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
|
// Pratham Gupta
// template
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import java.math.*;
import static java.lang.System.*;
public final class Main {
static int mod = 1000000007;
static int min_value = Integer.MIN_VALUE;
static int max_value = Integer.MAX_VALUE;
static FastReader f;
static FastWriter w;
static String d2b(int n) {
return Integer.toBinaryString(n);
}
static int parseInt(String s) {
return Integer.parseInt(s);
}
static long parseLong(String s) {
return Long.parseLong(s);
}
static int b2d(String s) {
return Integer.parseInt(s, 2);
}
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 int nextPrime(int n) {
// TODO Auto-generated method stub
n++;
while (true) {
boolean prime = true;
for (int i = 2; i * i <= n; i++) {
if (n % i != 0)
continue;
prime = false;
break;
}
if (prime)
return n;
n++;
}
}
static long power(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 modAdd(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modSub(long a, long b) {
return ((a % mod) - (b % mod) + mod) % mod;
}
static long modMult(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static int maxArray(int[] a) {
int max = min_value;
for (int n : a)
max = max(max, n);
return max;
}
static int minArray(int[] a) {
int min = max_value;
for (int n : a)
min = min(min, n);
return min;
}
static HashMap<Integer, Integer> freq(int[] x) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i : x) {
if (!map.containsKey(i))
map.put(i, 1);
else
map.put(i, map.get(i) + 1);
}
return map;
}
public static void main(String[] args) throws IOException {
f = new FastReader();
w = new FastWriter();
int t= f.Int();
while(t-->0) {
int r = f.Int();
int c = f.Int();
int[] dis = new int[r*c];
for(int i = 0; i< r*c; i++) {
int row= i/c;
int col = i%c;
dis[i] = maxD(row, col, r-1, c-1);
}
sort(dis);
StringBuilder sb = new StringBuilder();
for(int i : dis) sb.append(i + " ");
w.string(sb.toString().trim());
}
}
public static int maxD(int x1, int y1, int r, int c) {
int d1 = abs(x1) + abs(y1);
int d2 = abs(x1-r) + abs(y1);
int d3 = abs(x1) + abs(y1-c);
int d4 = abs(x1-r) + abs(y1-c);
return max(d1, max(d2, max(d3, d4)));
}
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int v) {
val = v;
left = null;
right = null;
}
}
static class Pair<A, B> {
A f;
B s;
public Pair(A first, B second) {
this.f = first;
this.s = second;
}
public String toString() {
return f + " " + s;
}
}
static class DSU {
int[] parent;
int[] rank;
int groups;
public DSU(int n) {
rank = new int[n];
parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
groups = n;
}
public int findParent(int i) {
if (parent[i] == i)
return i;
return parent[i] = findParent(parent[i]);
}
public void union(int a, int b) {
int u = findParent(a);
int v = findParent(b);
if (u != v) {
if (rank[u] < rank[v]) {
parent[u] = v;
} else if (rank[u] > rank[v]) {
parent[v] = u;
} else {
parent[u] = v;
rank[v]++;
}
groups--;
}
}
}
static class FastWriter {
BufferedWriter writer;
public FastWriter() {
writer = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void integer(int i) throws IOException {
writer.write(Integer.toString(i));
writer.newLine();
writer.flush();
}
public void decimal(double i) throws IOException {
writer.write(Double.toString(i));
writer.newLine();
writer.flush();
}
public void Long(long i) throws IOException {
writer.write(Long.toString(i));
writer.newLine();
writer.flush();
}
public void arrayWithSpaces(int[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
public void arrayWithSpaces(char[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
public void arrayWithSpaces(long[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
public void arrayWithSpaces(double[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
public void arrayWithoutSpaces(int[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + "");
}
writer.newLine();
writer.flush();
}
public void arrayWithoutSpaces(char[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + "");
}
writer.newLine();
writer.flush();
}
public void arrayWithoutSpaces(double[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + "");
}
writer.newLine();
writer.flush();
}
public void arrayWithoutSpaces(long[] nums) throws IOException {
for (int i = 0; i < nums.length; i++) {
writer.write(nums[i] + "");
}
writer.newLine();
writer.flush();
}
public void string(String s) throws IOException {
writer.write(s);
writer.newLine();
writer.flush();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public int[] array(int n, FastReader f) {
// TODO Auto-generated method stub
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = f.Int();
return arr;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
double Double() {
return Double.parseDouble(next());
}
String String() {
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
|
695b9fec1007ff65d9ed39a9135648f4
|
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 class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int d, int i) {
this.d = d;
this.i = i;
}
public int compareTo(Pair o) {
return this.d - o.d;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int n=sc.nextInt();
int m=sc.nextInt();
int[][] dp=new int[n][m];
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
dp[i][j]=Math.max(Math.max((Math.abs(i-0)+Math.abs(j-0)),(Math.abs(m-1-j)+Math.abs(i-0))),
Math.max(Math.abs(n-1-i)+Math.abs(j-0),Math.abs(n-1-i)+Math.abs(m-1-j)));
list.add(dp[i][j]);
}
}
Collections.sort(list);
for(int i=0;i<(n*m);i++)
sb.append(list.get(i)+" ");
sb.append("\n");
}
System.out.println(sb);
}
public static String Util(String s) {
for (int i = s.length() - 1; i >= 1; i--) {
int l = s.charAt(i - 1) - '0';
int r = s.charAt(i) - '0';
if (l + r >= 10) {
return s.substring(0, i - 1) + (l + r) + s.substring(i + 1);
}
}
int l = s.charAt(0) - '0';
int r = s.charAt(1) - '0';
return (l + r) + s.substring(2);
}
public static boolean isPos(int idx, long[] arr, long[] diff) {
if (idx == 0) {
for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) {
diff[idx] = i;
arr[0] -= i;
arr[1] -= i;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[0] += i;
arr[1] += i;
}
} else if (idx == 1) {
if (arr[2] - arr[1] >= 0) {
long k = arr[1];
diff[idx] = k;
arr[1] = 0;
arr[2] -= k;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[1] = k;
arr[2] += k;
} else
return false;
} else {
if (arr[2] == arr[0] && arr[1] == 0) {
diff[2] = arr[2];
return true;
} else {
return false;
}
}
return false;
}
public static boolean isPal(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array.get(mid)) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
|
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
|
55157951ef599eeffb0a2443bfe43ffc
|
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.*;
import java.math.*;
public class temp {
// Let's Go!! ------------->
static FastScanner sc;
static PrintWriter out;
public static void main(String[] args) {
sc = new FastScanner();
out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
PriorityQueue<Integer> q = new PriorityQueue<>();
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
int d = Math.max(i,n-1-i) + Math.max(j,m-1-j);
q.add(d);
}
}
while(!q.isEmpty()){
p(q.peek() + " ");
q.remove();
}
pn("");
}
// -------END-------
out.close();
}
// <-----------------------Template --------------------->
// Static Initializations ------>
static final long MOD = (long)1e9+7;
static final int MAX = (int)1e5+1, INF = (int)1e9;
static final boolean FLAG = true;
// GCD -------->
static long gcdl(long a, long b){return (b==0)?a:gcdl(b,a%b);}
static int gcdi(int a, int b){return (b==0)?a:gcdi(b,a%b);}
// Pair Class && Sort by First Value(Asc)----------->
static class pair<T> implements Comparable<pair>{
T first, second;
pair(T first, T second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(pair o) {
return (Integer) this.first - (Integer) o.first;
}
}
// Ruffle Sort
static void ruffleSort(int[] a) {
//Shuffle
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;
}
//then sort
Arrays.sort(a);
}
// Fast I/O
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
}
//Quick Print Statements ---->
static void p(Object o){out.print(o);}
static void pn(Object o){out.println(o);}
static void pni(Object o){out.println(o);out.flush();}
// Quick Input Statements ----->
static String n(){return sc.next();}
static String nln(){return sc.nextLine();}
static int ni(){return Integer.parseInt(sc.next());}
static long nl(){return Long.parseLong(sc.next());}
static double nd(){return Double.parseDouble(sc.next());}
//Integer Array Input ---->
static int[] readIntArr(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]= sc.nextInt();
return a;
}
//Long Array Input ----->
static long[] readLongArr(int N){
long[] a = new long[N];
for(int i = 0; i<N; i++)a[i] = sc.nextLong();
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 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
|
da376a06b4ef5708854a900849e898b3
|
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.PriorityQueue;
import java.util.StringTokenizer;
public class NotSitting {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int m = sc.nextInt();
PriorityQueue<Integer>pq = new PriorityQueue<>();
for(int i =0;i<n;i++){
for(int j =0;j<m;j++){
pq.add(Math.max(i,n-i-1)+Math.max(j,m-j-1)); // we are getting the maximum distance from any corner on the grid
}
}
while(!pq.isEmpty()){
pw.print(pq.poll()+" ");
}
pw.println();
}
pw.close();
}
// -------------------------------------------------------Basics----------------------------------------------------
// binary search
public static long gcd(long a , long b ){
if(b==0)return a;
return gcd(b,a%b);
}
static class Pair {
long x ;
long y ;
Pair(long x , long y ){
this.x=x;
this.y=y;
}
}
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();
}
}
}
|
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
|
418465eb6053fc2b461768ef4ca430cd
|
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.RoadMap.Diff1300;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
* @author Syed Ali.
* @createdAt 02/02/2022, Wednesday, 00:33
*/
public class NotSitting {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]){
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt(), m = sc.nextInt();
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
int dist = Math.max(i, n - 1 - i) + Math.max(j, m - 1 - j);
minheap.add(dist);
}
while(minheap.size() > 0)
System.out.print(minheap.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
|
b45917fbcd9f36f2ed9e63004777a714
|
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.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static String ab,b;
static class Node
{
int val;
Node left;
Node right;
public Node(int x) {
// TODO Auto-generated constructor stub
this.val=x;
this.left=null;
this.right=null;
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
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[] nextArray(int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=nextInt();
return arr;
}
}
static String string;
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
return gcd(b, a % b);
}
static long gcd(long a, long b)
{
// Everything divides 0
for(long i=2;i<=b;i++)
{
if(a%i==0&&b%i==0)
return i;
}
return 1;
}
static int fac(int n)
{
int c=1;
for(int i=2;i<n;i++)
if(n%i==0)
c=i;
return c;
}
static int lcm(int a,int b)
{
for(int i=Math.min(a, b);i<=a*b;i++)
if(i%a==0&&i%b==0)
return i;
return 0;
}
static int maxHeight(char[][] ch,int i,int j,String[] arr)
{
int h=1;
if(i==ch.length-1||j==0||j==ch[0].length-1)
return 1;
while(i+h<ch.length&&j-h>=0&&j+h<ch[0].length&&ch[i+h][j-h]=='*'&&ch[i+h][j+h]=='*')
{
String whole=arr[i+h];
//System.out.println(whole.substring(j-h,j+h+1));
if(whole.substring(j-h,j+h+1).replace("*","").length()>0)
return h;
h++;
}
return h;
}
static boolean all(BigInteger n)
{
BigInteger c=n;
HashSet<Character> hs=new HashSet<>();
while((c+"").compareTo("0")>0)
{
String d=""+c;
char ch=d.charAt(d.length()-1);
if(d.length()==1)
{
c=new BigInteger("0");
}
else
c=new BigInteger(d.substring(0,d.length()-1));
if(hs.contains(ch))
continue;
if(d.charAt(d.length()-1)=='0')
continue;
if(!(n.mod(new BigInteger(""+ch)).equals(new BigInteger("0"))))
return false;
hs.add(ch);
}
return true;
}
static int cal(long n,long k)
{
System.out.println(n+","+k);
if(n==k)
return 2;
if(n<k)
return 1;
if(k==1)
return 1+cal(n, k+1);
if(k>=32)
return 1+cal(n/k, k);
return 1+Math.min(cal(n/k, k),cal(n, k+1));
}
static Node buildTree(int i,int j,int[] arr)
{
if(i==j)
{
//System.out.print(arr[i]);
return new Node(arr[i]);
}
int max=i;
for(int k=i+1;k<=j;k++)
{
if(arr[max]<arr[k])
max=k;
}
Node root=new Node(arr[max]);
//System.out.print(arr[max]);
if(max>i)
root.left=buildTree(i, max-1, arr);
else {
root.left=null;
}
if(max<j)
root.right=buildTree(max+1, j, arr);
else {
root.right=null;
}
return root;
}
static int height(Node root,int val)
{
if(root==null)
return Integer.MAX_VALUE-32;
if(root.val==val)
return 0;
if((root.left==null&&root.right==null))
return Integer.MAX_VALUE-32;
return Math.min(height(root.left, val), height(root.right, val))+1;
}
static void shuffle(int a[], int n)
{
for (int i = 0; i < n; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
int x = a[t];
a[t] = a[i];
a[i] = x;
}
}
static void sort(int[] arr )
{
shuffle(arr, arr.length);
Arrays.sort(arr);
}
static boolean arraySortedInc(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
static boolean arraySortedDec(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
static int largestPower(int n, int p) {
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
x += n;
}
return x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
static int power(int x, int y, int p) {
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to 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! % p
static int modFact(int n, int p) {
if (n >= p) {
return 0;
}
int res = 1;
// Use Sieve of Eratosthenes to find all primes
// smaller than n
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
// Consider all primes found by Sieve
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of prime 'i' that divides n
int k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
static boolean[] seiveOfErathnos(int n2)
{
boolean isPrime[] = new boolean[n2 + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n2; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n2; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
static boolean[] seiveOfErathnos2(int n2,int[] ans)
{
boolean isPrime[] = new boolean[n2 + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n2; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n2; j += i) {
if(isPrime[j])
ans[i]++;
isPrime[j] = false;
}
}
}
return isPrime;
}
static long helper(int[] arr,int i,int used,long[][] dp)
{
if(i==arr.length)
return 3-used;
if(dp[i][used]!=-1)
return dp[i][used];
long sum=0,ans=Integer.MAX_VALUE;
for(int j=i;j<arr.length;j++)
{
sum+=arr[j];
long n=(long) Math.ceil(Math.log10(sum)/Math.log10(2));
// System.out.println(sum+","+(1L<<n));
if(used<2||j==arr.length-1)
ans=Math.min(ans,(1L<<n)-sum+helper(arr, j+1, used+1, dp));
}
return dp[i][used]=ans;
}
static boolean canDefeat(long hc,long dc,long hm,long dm)
{
// System.out.println(hc+","+dc);
long steps1=(long) Math.ceil(Double.valueOf(hm)/dc);
long steps2=(long) Math.ceil(Double.valueOf(hc)/dm);
return steps1<=steps2;
}
public static void main(String[] args)throws IOException
{
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
FastReader fs=new FastReader();
// int[] ans=new int[1000001];
int T=fs.nextInt();
// seiveOfErathnos2(1000000, ans);
StringBuilder sb=new StringBuilder();
while(T-->0)
{
int n=fs.nextInt(),m=fs.nextInt();
boolean[][] visited=new boolean[n][m];
Queue<int[]> q=new LinkedList<>();
if(n%2==1&&m%2==1)
{
q.add(new int[] {n/2,m/2,n/2+m/2});
visited[n/2][m/2]=true;
// k=1;
}
else if(n%2==0&&m%2==0)
{
q.add(new int[] {n/2,m/2,n/2+m/2});
visited[n/2][m/2]=true;
q.add(new int[] {n/2-1,m/2,n/2+m/2});
visited[n/2-1][m/2]=true;
q.add(new int[] {n/2,m/2-1,n/2+m/2});
visited[n/2][m/2-1]=true;
q.add(new int[] {n/2-1,m/2-1,n/2+m/2});
visited[n/2-1][m/2-1]=true;
// k=4;
}
else
{
if(n%2==1)
{
q.add(new int[] {n/2,m/2,n/2+m/2});
visited[n/2][m/2]=true;
q.add(new int[] {n/2,m/2-1,n/2+m/2});
visited[n/2][m/2-1]=true;
}
else {
q.add(new int[] {n/2,m/2,n/2+m/2});
visited[n/2][m/2]=true;
q.add(new int[] {n/2-1,m/2,n/2+m/2});
visited[n/2-1][m/2]=true;
}
// k=2;
}
int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
for(int k=1;k<=n*m;k++)
{
int[] temp=q.poll();
sb.append(temp[2]).append(" ");
for(int[] i:dir)
{
int x=temp[0]+i[0];
int y=temp[1]+i[1];
if(x>=0&&y>=0&&x<n&&y<m&&!visited[x][y])
{
q.add(new int[] {x,y,temp[2]+1});
visited[x][y]=true;
}
}
}
// sb.append(ans?"YES":"NO");
sb.append("\n");
// System.out.println(ans);
}
System.out.println(sb);
}
private static void qprint(int sx) {
// TODO Auto-generated method stub
System.out.println(sx);
}
private static void qprint(String sx) {
// TODO Auto-generated method stub
System.out.println(sx);
}
private static void qprint(int sx, int ex) {
// TODO Auto-generated method stub
System.out.println(sx+","+ex);
}
}
|
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
|
3e7413c914bc13d18f111c3b37e7d32a
|
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 cf1627b {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] ans = new int[m * n];
int index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int a = Math.max(n - i - 1, i) + Math.max(m - j - 1, j);
ans[index] = a;
index++;
}
}
Arrays.sort(ans);
for (int i = 0; i < m * n; i++) {
pw.print(ans[i] + " ");
}
pw.println();
}
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 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
|
4a0bb8cd4327e5b249ae4e16ec468389
|
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.awt.Container;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work:
while (tc-- > 0) {
int n = input.nextInt();
int m = input.nextInt();
int leftx = 1,lefty= 1;
int rightx = 1,righty = m;
int leftdownx = n,leftdowny = 1;
int rightdownx= n,rightdowny = m;
PriorityQueue<Integer> ans = new PriorityQueue<>();
for (int i = 1; i <=n; i++) {
for (int j = 1; j <=m; j++) {
int max = 0;
max= Math.max(max, Math.abs(i-leftx)+Math.abs(j-lefty));
max= Math.max(max, Math.abs(i-rightx)+Math.abs(j-righty));
max= Math.max(max, Math.abs(i-leftdownx)+Math.abs(j-leftdowny));
max= Math.max(max, Math.abs(i-rightdownx)+Math.abs(j-rightdowny));
ans.add(max);
}
}
StringBuilder result = new StringBuilder();
while(!ans.isEmpty())
{
result.append(ans.poll()+" ");
}
System.out.println(result);
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
|
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
|
4252216204ff8ae39d98f4c6b0862fd0
|
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 B1627 {
static StringBuilder sb;
static long mod = (long) (1e9 + 7);
static class Pair{
int x,y,d;
Pair(int a,int b,int c){
x = a;
y = b;
d = c;
}
}
static void solve(int m,int n) {
PriorityQueue<Pair> pq = new PriorityQueue<>((a,b)->{
return a.d - b.d;
});
for(int i = 1;i <=n;i++){
for(int j = 1;j <= m;j++){
int d1 = i - 1 + j - 1, d2 = i - 1 + m - j, d3 = n - i + j - 1, d4 = n - i + m - j;
pq.add(new Pair(i,j,Math.max(d1,Math.max(d2,Math.max(d3,d4)))));
}
}
ArrayList<Integer> ans = new ArrayList<>();
int cnt = m * n;
while(cnt-- > 0){
Pair p = pq.remove();
ans.add(p.d);
}
for(int ele : ans){
sb.append(ele + " ");
}
sb.append("\n");
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve(i(),i());
}
out.printLine(sb);
out.flush();
out.close();
}
private static double log(long a,long b){
double ans = Math.log10(a) / Math.log10(b);
return ans;
}
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[] sortlong(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 int[] sortint(int[] a2) {
int n = a2.length;
ArrayList<Integer> l = new ArrayList<>();
for (int 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 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[] readArray(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readLongArray(int 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 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
|
6af9ca037981b0f327a67186a99ba9a7
|
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.rajan.codeforces.leve_1300;
import java.io.*;
import java.util.Arrays;
public class NotSitting {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int tt = Integer.parseInt(reader.readLine());
while (tt-- > 0) {
String[] temp = reader.readLine().split("\\s+");
int r = Integer.parseInt(temp[0]), c = Integer.parseInt(temp[1]);
int[][] corners = new int[][]{{1, 1}, {1, c}, {r, 1}, {r, c}};
int[] distances = new int[r * c];
int index = 0;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
int max = -1;
for (int[] corner : corners) {
max = Math.max(Math.abs(corner[0] - i) + Math.abs(corner[1] - j), max);
}
distances[index++] = max;
}
}
Arrays.sort(distances);
for (int k = 0; k < r * c; k++) {
writer.write((k == 0 ? "" : " ") + distances[k]);
}
writer.write("\n");
}
writer.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 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
|
8ca40ca6f0cad9619087b8d6ba54b57b
|
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 NotSitting{
public static void main(String[] args) {
FastReader fr = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Scanner sc= new Scanner (System.in);
//Code From Here----
int t =fr.nextInt();
while (t-->0) {
int n=fr.nextInt();
int m=fr.nextInt();
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 );
}
}
radixSort2(arr);
for (int i : arr) {
out.print(i+" ");
}
out.println();
}
out.flush();
sc.close();
}
//This RadixSort() is for long method
public static long[] radixSort(long[] f){ return radixSort(f, f.length); }
public static long[] radixSort(long[] f, int n)
{
long[] to = new long[n];
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i];
long[] d = f; f = to; to = d;
}
return f;
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int lowerBound(long a[], long 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(long a[], long x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1L;
if (a[m] <= x) l = m;
else r = m;
}
return l + 1;
}
static int upperBound(long[] arr, int start, int end, long k) {
int N = end;
while (start < end) {
int mid = start + (end - start) / 2;
if (k >= arr[mid]) {
start = mid + 1;
} else {
end = mid;
}
}
if (start < N && arr[start] <= k) {
start++;
}
return start;
}
static long lowerBound(long[] arr, int start, int end, long k) {
int N = end;
while (start < end) {
int mid = start + (end - start) / 2;
if (k <= arr[mid]) {
end = mid;
} else {
start = mid + 1;
}
}
if (start < N && arr[start] < k) {
start++;
}
return start;
}
// For Fast Input ----
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 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
|
7b14f0b3048ab8c18de356b8bdd74202
|
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.util.Map.Entry;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
/**
* Main , Solution , Remove Public
*/
public class B {
public static void process() throws IOException {
int n = sc.nextInt(), m = sc.nextInt();
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
for(int i = 0; i<n; i++) {
for(int j = 0; j<m; j++) {
int max = 0;
// 0,0
int dis = abs(i-0) + abs(j-0);
max = max(max, dis);
// 0,m-1
dis = abs(i-0) + abs(j-m+1);
max = max(max, dis);
// n-1,m-1
dis = abs(i-n+1) + abs(j-m+1);
max = max(max, dis);
// n-1,0
dis = abs(i-n+1) + abs(j-0);
max = max(max, dis);
map.put(max, map.getOrDefault(max, 0)+1);
}
}
while(!map.isEmpty()) {
Entry<Integer, Integer> e = map.pollFirstEntry();
for(int i =0; i<e.getValue(); i++)System.out.print(e.getKey()+" ");
}
System.out.println();
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
private static void google(int tt) {
System.out.print("Case #" + (tt) + ": ");
}
static FastScanner sc;
static FastWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new FastWriter(System.out);
} else {
sc = new FastScanner("input.txt");
out = new FastWriter("output.txt");
}
long s = System.currentTimeMillis();
int t = 1;
t = sc.nextInt();
int TTT = 1;
while (t-- > 0) {
// google(TTT++);
process();
}
out.flush();
// tr(System.currentTimeMillis()-s+"ms");
}
private static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private static void tr(Object... o) {
if (!oj)
System.err.println(Arrays.deepToString(o));
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.x, o.x);
}
/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
*/
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long sqrt(long z) {
long sqz = (long) Math.sqrt(z);
while (sqz * 1L * sqz < z) {
sqz++;
}
while (sqz * 1L * sqz > z) {
sqz--;
}
return sqz;
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lower_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid - 1;
} else {
ans = mid;
low = mid + 1;
}
}
return ans;
}
public static int upper_bound(int[] arr, int x) {
int low = 0, high = arr.length - 1, mid = -1;
int ans = arr.length;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void reverseArray(int[] a) {
int n = a.length;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
// compress Big value to Time Limit
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
// Fast Writer
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first)
write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map)
write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
// Fast Inputs
static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readArrayLong(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public int[][] readArrayMatrix(int N, int M, int Index) {
if (Index == 0) {
int[][] res = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
int[][] res = new int[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = (int) nextLong();
}
return res;
}
public long[][] readArrayMatrixLong(int N, int M, int Index) {
if (Index == 0) {
long[][] res = new long[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
res[i][j] = nextLong();
}
return res;
}
long[][] res = new long[N][M];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++)
res[i][j] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readArrayDouble(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
}
|
Java
|
["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
|
8f6d23a052e854ccb2800d011ff9d9a0
|
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.lang.Math;
public final class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
while(t > 0){
int a, b;
a = sc.nextInt();
b = sc.nextInt();
ArrayList<Integer> ans = new ArrayList<Integer>();
int[][] corners = {{0, 0}, {a - 1, 0}, {0, b - 1}, {a - 1, b - 1}};
for(int i = 0; i < a; i++){
for(int j = 0; j < b; j++){
int res = -1;
for(int k = 0; k < 4; k++){
res = Math.max(res, Math.abs(corners[k][0] - i) + Math.abs(corners[k][1] - j));
}
ans.add(res);
}
}
Collections.sort(ans);
for(int i = 0; i < a * b; i++){
System.out.print(ans.get(i) + " ");
}
System.out.println();
t -= 1;
}
}
}
|
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
|
b4123bc39b1e2ee7f23960acb4226ee3
|
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 TestClass {
public static void main(String args[]) throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int t=Integer.parseInt(br.readLine());
StringTokenizer st;
for(int i=0;i<t;i++)
{
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
int start1=n%2,start2=m%2;
if(start1==0)
start1=2;
if(start2==0)
start2=2;
int start=start1*start2;
if(n>m)
{
n=n+m;
m=n-m;
n=n-m;
}
int types_length=(n+m)/2;
if(n%2==0 && m%2==0)
types_length-=1;
int arr[]=new int[types_length+1];
arr[1]=start;
if(start==1)
start=4;
else
start+=4;
for(int j=2;j<=(n+1)/2;j++)
{
arr[j]=start;
start+=4;
}
int itr=(n+1)/2;
itr+=1;
if(n!=m)
{
if(m%2==0)
start-=4;
else
start-=2;
}
for(int j=itr;j<=(m+1)/2;j++)
{
arr[j]=start;
itr=j;
}
itr=(m+1)/2;
itr+=1;
if(n!=m)
{
if(n%2==0)
start-=4;
else
start-=2;
}
else
{
if(n%2==0)
start-=8;
else
start-=4;
}
for(int j=itr;j<=types_length;j++)
{
arr[j]=start;
start-=4;
}
int print_start=(n-(n+1)/2)+(m-(m+1)/2);
for(int j=1;j<=types_length;j++)
{
for(int k=1;k<=arr[j];k++)
{
bw.write(print_start+" ");
}
print_start+=1;
}
bw.write("\n");
// bw.write("End"+"\n");
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 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
|
52b7a8de41e1409c8ea107c093d8c4ba
|
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.*;
//import static com.sun.tools.javac.jvm.ByteCodes.swap;
// ?)(?
public class fastTemp {
static class Node
{
int data;
Node left, right;
Node(int data)
{
this.data=data;
left=right=null;
}
};
static FastScanner fs = null;
static ArrayList<Integer> graph[] ;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
int n= fs.nextInt();
int m = fs.nextInt();
ArrayList<Integer> al = new ArrayList<>();
int tn = n-1;
int tm = m-1;
for(int i=0;i<n;i++) {
for (int j = 0; j < m; j++) {
int dis = i + j;
dis = Math.max(dis, ((tn - i) + (tm - j)));
dis = Math.max(dis, ((tn - i) + j));
dis = Math.max(dis, (i + (tm - j)));
al.add(dis);
}
}
Collections.sort(al);
for(int v: al){
System.out.print(v+" ");
}
System.out.println();
}
out.close();
}
// JAVA program to compute factorial
// of big numbers
// This function finds factorial of
// large numbers and prints them
static void factorial(int n)
{
int res[] = new int[500];
// Initialize result
res[0] = 1;
int res_size = 1;
// Apply simple factorial formula
// n! = 1 * 2 * 3 * 4...*n
for (int x = 2; x <= n; x++)
res_size = multiply(x, res, res_size);
System.out.println("Factorial of given number is ");
for (int i = res_size - 1; i >= 0; i--)
System.out.print(res[i]);
}
// This function multiplies x with the number
// represented by res[]. res_size is size of res[] or
// number of digits in the number represented by res[].
// This function uses simple school mathematics for
// multiplication. This function may value of res_size
// and returns the new value of res_size
static int multiply(int x, int res[], int res_size)
{
int carry = 0; // Initialize carry
// One by one multiply n with individual
// digits of res[]
for (int i = 0; i < res_size; i++)
{
int prod = res[i] * x + carry;
res[i] = prod % 10; // Store last digit of
// 'prod' in res[]
carry = prod/10; // Put rest in carry
}
// Put carry in res and increase result size
while (carry!=0)
{
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
static int m = 998244353;
static long fact[] = new long[200001];
public static void fact(){
fact[1] = 1;
fact[0] = 1;
for(int i=2;i<=200000;i++){
fact[i] = (fact[i-1]*i)%m;
}
}
public static int help(int val){
System.out.println("+ "+val);
System.out.flush();
int x = fs.nextInt();
return x;
}
static int lower(int array[], int key,int i,int k)
{
int lowerBound = i;
// Traversing the array using length function
while (lowerBound < k) {
// If key is lesser than current value
if (key > array[lowerBound])
lowerBound++;
// This is either the first occurrence of key
// or value just greater than key
else
return lowerBound;
}
return lowerBound;
}
static int upper(int array[], int key ,int i , int k)
{
int upperBound = k;
// Traversing the array using length function
while (upperBound>=i) {
// If key is lesser than current value
if (key > array[upperBound])
upperBound--;
// This is either the first occurrence of key
// or value just greater than key
else
return upperBound;
}
return upperBound;
}
static class Pair {
int x;
int y;
Pair(int x,int y){
this.x = x;
this.y = y;
}
}
public static boolean contains(String s,String s1){
boolean found = false;
for(int i=0;i<s.length();i++){
for(int j = i+1;j<s.length();j++){
String ss = s.substring(i,j);
if(ss.equals(s1)){
found = true;
}
}
}
return found;
}
// Math.ceil(k/n) == (k+n-1)/n
//----------------------graph--------------------------------------------------
/* int vtces = fs.nextInt();
ArrayList<Integer> graph[] = new ArrayList[vtces];
for (int v = 0; v < vtces; v++) {
graph[v] = new ArrayList<>();
}
int edge = fs.nextInt();
for (int i = 0; i < edge; i++) {
int u = fs.nextInt();
int v = fs.nextInt();
graph[u].add(v);
graph[v].add(u);
}
*/
//----------------------graph--------------------------------------------------
public static class String1 implements Comparable<String1>{
String str;
int id;
String1(String str , int id){
this.str = str;
this.id = id;
}
public int compareTo(String1 o){
int i=0;
while(i<str.length() && str.charAt(i)==o.str.charAt(i)){
i++;
}
if(i<str.length()){
if(i%2==1){
return o.str.compareTo(str); // descending order
}else{
return str.compareTo(o.str); // ascending order
}
}
return str.compareTo(o.str);
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class 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());
}
}
// ------------------------------------------swap----------------------------------------------------------------------
static void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
//-------------------------------------------seiveOfEratosthenes----------------------------------------------------
static boolean prime[];
static void sieveOfEratosthenes(int n)
{
// 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.
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)
// System.out.print(i + " ");
// }
}
//------------------------------------------- power------------------------------------------------------------------
public static long power(int a , int b) {
if (b == 0) {
return 1;
} else if (b == 1) {
return a;
} else {
long R = power(a, b / 2);
if (b % 2 != 0) {
return (((power(a, b / 2))) * a * ((power(a, b / 2))));
} else {
return ((power(a, b / 2))) * ((power(a, b / 2)));
}
}
}
//--------------------------------------lower bound----------------------------------------------------------
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;
}
//--------------------------------------------------------------------------------------------------------------
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
//---------------------------------------EXTENDED EUCLID ALGO--------------------------------------------------------
/* public static class Pair{
int x;
int y;
public Pair(int x,int y){
this.x = x;
this.y = y ;
}
}
*/
public static Pair Euclid(int a,int b){
if(b==0){
return new Pair(1,0); // answer of x and y
}
Pair dash = Euclid(b,a%b);
return new Pair(dash.y , dash.x - (a/b)*dash.y);
}
//--------------------------------GCD------------------GCD-----------GCD--------------------------------------------
public static long gcd(long a,long b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
public static void BFS(ArrayList<Integer>[] graph) {
}
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(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
|
832d2a91387c5720ef87f2b8e50d7876
|
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 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t=0;t<T;t++){
solve(sc);
}
}
public static void solve(Scanner sc){
int ROWS = sc.nextInt();
int COLS = sc.nextInt();
int[] dist = new int[ROWS*COLS];
//we're gonna choose 0,0 as location of tina
//calculate distance from all squares to tina
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
dist[COLS*i +j] = Math.max(i,ROWS-i-1) + Math.max(j, COLS-j-1);
}
}
Arrays.sort(dist);
//ascending order
for(int k=0;k<ROWS*COLS;k++){
System.out.print(dist[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 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
|
7bf3ef89b5f4c6ca946cd46102f63033
|
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.lang.*;
public class NotSitting {
static int maxDistance(int r, int c, int n, int m)
{
int[] ans=new int[4];
ans[0]=r+c;
ans[1]=r+Math.abs(c-(m-1));
ans[2]=Math.abs(r-(n-1))+c;
ans[3]=Math.abs(r-(n-1))+Math.abs(c-(m-1));
int max=0;
for(int i=0; i<4; i++)
{
if(ans[i]>max) max=ans[i];
}
return max;
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int t=Integer.parseInt(s);
int n;
int m;
StringBuilder ans = new StringBuilder();
for(int i=1; i<=t; i++)
{
s=in.nextLine();
StringTokenizer st=new StringTokenizer(s);
n=Integer.parseInt(st.nextToken());
m=Integer.parseInt(st.nextToken());
int[][] d=new int[n][m];
for(int r=0; r<n; r++)
{
for(int c=0; c<m; c++)
{
d[r][c]=maxDistance(r,c,n,m);
}
}
int[] distances=new int[n*m];
int index=0;
for(int r=0; r<n; r++)
{
for(int c=0; c<m; c++)
{
distances[index]=d[r][c];
index++;
}
}
Arrays.sort(distances);
for(int j=0; j<distances.length; j++) ans.append(distances[j]+" ");
ans.append("\n");
}
System.out.println(ans.toString());
}
}
|
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
|
f4232a5417ceeb08eac090f011ed1977
|
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 MyCpClass{
public static void main(String []args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine().trim());
while(T-- > 0){
String []ip = br.readLine().trim().split(" ");
int n = Integer.parseInt(ip[0]);
int m = Integer.parseInt(ip[1]);
int N = n*m, i=0;
int []a = new int[N];
for(int r=0; r<n; r++){
for(int c=0; c<m; c++)
a[i++] = Math.max(r,n-r-1) + Math.max(c,m-c-1);
}
Arrays.sort(a);
for(int x : a)
sb.append(x + " ");
sb.append("\n");
}
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 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
|
ebdbcc675cf2889d4d51714269e636f8
|
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.*;
import java.math.BigInteger;
//code by tishrah_
public class _practise {
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());
}
int[] ia(int n)
{
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=nextInt();
return a;
}
int[][] ia(int n , int m)
{
int a[][]=new int[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextInt();
return a;
}
long[][] la(int n , int m)
{
long a[][]=new long[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong();
return a;
}
char[][] ca(int n , int m)
{
char a[][]=new char[n][m];
for(int i=0;i<n;i++)
{
String x =next();
for(int j=0;j<m ;j++) a[i][j]=x.charAt(j);
}
return a;
}
long[] la(int n)
{
long a[]=new long[n];
for(int i=0;i<n;i++)a[i]=nextLong();
return a;
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static void sort(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 sort(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);}
public static long sum(long a[])
{long sum=0; for(long i : a) sum+=i; return(sum);}
public static long count(long a[] , long x)
{long c=0; for(long i : a) if(i==x) c++; return(c);}
public static int sum(int a[])
{ int sum=0; for(int i : a) sum+=i; return(sum);}
public static int count(int a[] ,int x)
{int c=0; for(int i : a) if(i==x) c++; return(c);}
public static int count(String s ,char ch)
{int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);}
public static boolean prime(int n)
{for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static boolean prime(long n)
{for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static int gcd(int n1, int n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static long gcd(long n1, long n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static int[] freq(int a[], int n)
{ int f[]=new int[n+1]; for(int i:a) f[i]++; return f;}
public static int[] pos(int a[], int n)
{ int f[]=new int[n+1]; for(int i=0; i<n ;i++) f[a[i]]=i; return f;}
public static int[] rev(int a[])
{
for(int i=0 ; i<(a.length+1)/2;i++)
{
int temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
return a;
}
public static boolean palin(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
String str=String.valueOf(sb.reverse());
if(s.equals(str))
return true;
else return false;
}
public static String rev(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
return String.valueOf(sb.reverse());
}
public static void main(String args[])
{
FastReader in=new FastReader();
PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
_practise ob = new _practise();
int T = in.nextInt();
// int T = 1;
tc : while(T-->0)
{
int n = in.nextInt();
int m = in.nextInt();
ArrayList<Integer> al = new ArrayList<>();
for(int i=1 ; i<=n ; i++)
{
for(int j=1 ; j<=m ; j++)
{
int x = Math.max(Math.abs(1-i)+Math.abs(1-j), Math.max(Math.abs(1-i)+Math.abs(m-j), Math.max(Math.abs(n-i)+Math.abs(1-j), Math.abs(n-i)+Math.abs(m-j))));
al.add(x);
}
}
Collections.sort(al);
for(int i : al )so.print(i+" "); so.println();
}
so.flush();
/*String s = in.next();
* Arrays.stream(f).min().getAsInt()
* BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap
* initial value banan chahte
int a[] = new int[n];
Stack<Integer> stack = new Stack<Integer>();
Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>();
PriorityQueue<Long> pq = new PriorityQueue<Long>();
ArrayList<Integer> al = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder();
HashSet<Integer> st = new LinkedHashSet<Integer>();
Set<Integer> s = new HashSet<Integer>();
Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value>
for(Map.Entry<Integer, Integer> i :hm.entrySet())
for(int i : hm.values())
for(int i : hm.keySet())
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
so.println("HELLO");
Arrays.sort(a,Comparator.comparingDouble(o->o[0]))
Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1]));
Set<String> ts = new TreeSet<>();*/
}
}
|
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
|
b812e82dbe482a25f020d1909e749850
|
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) {
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for(int ttt = 1; ttt <= T; ttt++) {
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n*m];
int cur = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int m1 = i + j;
int m2 = i + Math.abs(m-1-j);
int m3 = Math.abs(n-1-i) + j;
int m4 = Math.abs(n-1-i) + Math.abs(m-1-j);
a[cur++] = Math.max(Math.max(m1, m2), Math.max(m3, m4));
}
}
Arrays.sort(a);
for(int i = 0; i < a.length; i++) {
out.print(a[i] + " ");
}
out.println();
}
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch(IOException e) { e.printStackTrace(); }
return str;
}
int[] readInt(int size) {
int[] arr = new int[size];
for(int i = 0; i < size; i++)
arr[i] = Integer.parseInt(next());
return arr;
}
long[] readLong(int size) {
long[] arr = new long[size];
for(int i = 0; i < size; i++)
arr[i] = Long.parseLong(next());
return arr;
}
int[][] read2dArray(int rows, int cols) {
int[][] arr = new int[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
arr[i][j] = Integer.parseInt(next());
}
return arr;
}
}
}
|
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
|
0ca872053d962132250866af86e66e91
|
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_464_div2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class A {
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();
List<Integer> ans = new ArrayList<>();
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
int a = Math.abs(i-1) + Math.abs(j-1);
int b = Math.abs(i-1) + Math.abs(j-m);
int c = Math.abs(i-n) + Math.abs(j-1);
int d = Math.abs(i-n) + Math.abs(j-m);
int x = Math.max(a, Math.max(b, Math.max(c, d)));
ans.add(x);
}
}
Collections.sort(ans);
for(int num: ans) System.out.print(num + " ");
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
|
6f73a1324faf3de1f3374cb48cb002c7
|
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 javax.swing.text.Segment;
import java.math.*;
import java.math.BigInteger;
public final class A
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<Integer> g[];
static long mod=(long) 998244353,INF=Long.MAX_VALUE;
static boolean set[];
static int par[],col[],D[];
static long A[];
static int seg[];
static int dp[][];
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
int T=i();
while(T-->0)
{
int N=i(),M=i();
int A[]=new int[N*M];
int max=N+M-2;
for(int i=0; i<N*M; i++)
{
int x=i/M,y=i%M;
A[i]=max-f(N,M,x,y);
}
sort(A);
//print(A);
for(int a:A)ans.append(a+" ");
ans.append("\n");
}
out.println(ans);
out.close();
}
static int f(int N,int M,int x,int y)
{
N-=1;
M-=1;
N=Math.abs(N-x);
M=Math.abs(M-y);
int a=x+y;
a=Math.min(a, x+M);
a=Math.min(a, y+N);
a=Math.min(a, N+M);
return a;
}
static TreeNode start;
public static void f(TreeNode root,TreeNode p,int r)
{
if(root==null)return;
if(p!=null)
{
root.par=p;
}
if(root.val==r)start=root;
f(root.left,root,r);
f(root.right,root,r);
}
public static HashSet<Integer> st;
public static int i=0;
public static HashMap<Integer,Integer> mp=new HashMap<>();
public static TreeNode buildTree(int[] preorder, int[] inorder) {
for(int i=0; i<preorder.length; i++)mp.put(inorder[i],i);
TreeNode head=new TreeNode();
f(preorder,inorder,0,inorder.length-1,head);
return head;
}
public static void f(int pre[],int in[],int l,int r,TreeNode root)
{
// System.out.println(pre[i]);
root.val=pre[i];
int it=mp.get(pre[i++]);//
// System.out.println(it);
if(it-1>=l)
{
TreeNode left=new TreeNode();
root.left=left;
f(pre,in,l,it-1,left);
}
if(it+1<=r)
{
TreeNode right=new TreeNode();
root.right=right;
f(pre,in,it+1,r,right);
}
}
static int right(int A[],int Limit,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<Limit)l=m;
else r=m;
}
return l;
}
static int left(int A[],int a,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<a)l=m;
else r=m;
}
return l;
}
static int f(int k,int x,int N)
{
int l=x-1,r=N;
while(r-l>1)
{
int m=(l+r)/2;
int a=ask(1,0,N-1,x,m);
if(a<k)
{
l=m;
}
else r=m;
}
//System.out.println("-------");
if(r==N)return -1;
return r;
}
static void build(int v,int tl,int tr,int A[])
{
if(tl==tr)
{
seg[v]=A[tl];
return;
}
int tm=(tl+tr)/2;
build(v*2,tl,tm,A);
build(v*2+1,tm+1,tr,A);
seg[v]=Math.max(seg[v*2],seg[v*2+1]);
}
static void update(int v,int tl,int tr,int index,int a)
{
if(index==tl && tl==tr)
seg[v]=a;
else
{
int tm=(tl+tr)/2;
if(index<=tm)update(2*v,tl,tm,index,a);
else update(2*v+1,tm+1,tr,index,a);
seg[v]=Math.max(seg[v*2],seg[v*2+1]);
}
}
static int ask(int v,int tl,int tr,int l,int r)
{
if(l>r)return -1;
if(l==tl && tr==r)return seg[v];
int tm=(tl+tr)/2;
return Math.max(ask(v*2,tl,tm,l,Math.min(tm, r)),ask(2*v+1,tm+1,tr,Math.max(tm+1, l),r));
}
// static int query(long a,TreeNode root)
// {
// long p=1L<<30;
// TreeNode temp=root;
// while(p!=0)
// {
// System.out.println(a+" "+p);
// if((a&p)!=0)
// {
// temp=temp.right;
//
// }
// else temp=temp.left;
// p>>=1;
// }
// return temp.index;
// }
// static void delete(long a,TreeNode root)
// {
// long p=1L<<30;
// TreeNode temp=root;
// while(p!=0)
// {
// if((a&p)!=0)
// {
// temp.right.cnt--;
// temp=temp.right;
// }
// else
// {
// temp.left.cnt--;
// temp=temp.left;
// }
// p>>=1;
// }
// }
// static void insert(long a,TreeNode root,int i)
// {
// long p=1L<<30;
// TreeNode temp=root;
// while(p!=0)
// {
// if((a&p)!=0)
// {
// temp.right.cnt++;
// temp=temp.right;
// }
// else
// {
// temp.left.cnt++;
// temp=temp.left;
// }
// p>>=1;
// }
// temp.index=i;
// }
// static TreeNode create(int p)
// {
//
// TreeNode root=new TreeNode(0);
// if(p!=0)
// {
// root.left=create(p-1);
// root.right=create(p-1);
// }
// return root;
// }
static boolean f(long A[],long m,int N)
{
long B[]=new long[N];
for(int i=0; i<N; i++)
{
B[i]=A[i];
}
for(int i=N-1; i>=0; i--)
{
if(B[i]<m)return false;
if(i>=2)
{
long extra=Math.min(B[i]-m, A[i]);
long x=extra/3L;
B[i-2]+=2L*x;
B[i-1]+=x;
}
}
return true;
}
static int f(int l,int r,long A[],long x)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]>=x)l=m;
else r=m;
}
return r;
}
static boolean f(long m,long H,long A[],int N)
{
long s=m;
for(int i=0; i<N-1;i++)
{
s+=Math.min(m, A[i+1]-A[i]);
}
return s>=H;
}
static boolean f(int i,int j,long last,boolean win[])
{
if(i>j)return false;
if(A[i]<=last && A[j]<=last)return false;
long a=A[i],b=A[j];
//System.out.println(a+" "+b);
if(a==b)
{
return win[i] | win[j];
}
else if(a>b)
{
boolean f=false;
if(b>last)f=!f(i,j-1,b,win);
return win[i] | f;
}
else
{
boolean f=false;
if(a>last)f=!f(i+1,j,a,win);
return win[j] | f;
}
}
static long ask(long l,long r)
{
System.out.println("? "+l+" "+r);
return l();
}
static long f(long N,long M)
{
long s=0;
if(N%3==0)
{
N/=3;
s=N*M;
}
else
{
long b=N%3;
N/=3;
N++;
s=N*M;
N--;
long a=N*M;
if(M%3==0)
{
M/=3;
a+=(b*M);
}
else
{
M/=3;
M++;
a+=(b*M);
}
s=Math.min(s, a);
}
return s;
}
static int ask(StringBuilder sb,int a)
{
System.out.println(sb+""+a);
return i();
}
static void swap(char X[],int i,int j)
{
char x=X[i];
X[i]=X[j];
X[j]=x;
}
static int min(int a,int b,int c)
{
return Math.min(Math.min(a, b), c);
}
static long and(int i,int j)
{
System.out.println("and "+i+" "+j);
return l();
}
static long or(int i,int j)
{
System.out.println("or "+i+" "+j);
return l();
}
static int len=0,number=0;
static void f(char X[],int i,int num,int l)
{
if(i==X.length)
{
if(num==0)return;
//update our num
if(isPrime(num))return;
if(l<len)
{
len=l;
number=num;
}
return;
}
int a=X[i]-'0';
f(X,i+1,num*10+a,l+1);
f(X,i+1,num,l);
}
static boolean is_Sorted(int A[])
{
int N=A.length;
for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
return true;
}
static boolean f(StringBuilder sb,String Y,String order)
{
StringBuilder res=new StringBuilder(sb.toString());
HashSet<Character> set=new HashSet<>();
for(char ch:order.toCharArray())
{
set.add(ch);
for(int i=0; i<sb.length(); i++)
{
char x=sb.charAt(i);
if(set.contains(x))continue;
res.append(x);
}
}
String str=res.toString();
return str.equals(Y);
}
static boolean all_Zero(int f[])
{
for(int a:f)if(a!=0)return false;
return true;
}
static long form(int a,int l)
{
long x=0;
while(l-->0)
{
x*=10;
x+=a;
}
return x;
}
static int count(String X)
{
HashSet<Integer> set=new HashSet<>();
for(char x:X.toCharArray())set.add(x-'0');
return set.size();
}
static int f(long K)
{
long l=0,r=K;
while(r-l>1)
{
long m=(l+r)/2;
if(m*m<K)l=m;
else r=m;
}
return (int)l;
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=A[tl];
// }
// else
// {
// int tm=(tl+tr)/2;
// build(v*2,tl,tm,A);
// build(v*2+1,tm+1,tr,A);
// seg[v]=Math.min(seg[v*2], seg[v*2+1]);
// }
// }
static int [] sub(int A[],int B[])
{
int N=A.length;
int f[]=new int[N];
for(int i=N-1; i>=0; i--)
{
if(B[i]<A[i])
{
B[i]+=26;
B[i-1]-=1;
}
f[i]=B[i]-A[i];
}
for(int i=0; i<N; i++)
{
if(f[i]%2!=0)f[i+1]+=26;
f[i]/=2;
}
return f;
}
static int[] f(int N)
{
char X[]=in.next().toCharArray();
int A[]=new int[N];
for(int i=0; i<N; i++)A[i]=X[i]-'a';
return A;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
if(par[a]>par[b]) //this means size of a is less than that of b
{
int t=b;
b=a;
a=t;
}
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
static void setGraph(int N)
{
par=new int[N+1];
D=new int[N+1];
g=new ArrayList[N+1];
set=new boolean[N+1];
col=new int[N+1];
for(int i=0; i<=N; i++)
{
col[i]=-1;
g[i]=new ArrayList<>();
D[i]=Integer.MAX_VALUE;
}
}
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static int kadane(int A[])
{
int lsum=A[0],gsum=A[0];
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
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 isPrime(long 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 void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(boolean A[][])
{
for(boolean a[]:A)print(a);
}
static void print(long A[][])
{
for(long a[]:A)print(a);
}
static void print(int A[][])
{
for(int a[]:A)print(a);
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class segNode
{
long pref,suff,sum,max;
segNode(long a,long b,long c,long d)
{
pref=a;
suff=b;
sum=c;
max=d;
}
}
//class TreeNode
//{
// int cnt,index;
// TreeNode left,right;
// TreeNode(int c)
// {
// cnt=c;
// index=-1;
// }
// TreeNode(int c,int index)
// {
// cnt=c;
// this.index=index;
// }
//}
class post implements Comparable<post>
{
long x,y,d,t;
post(long a,long b,long c)
{
x=a;
y=b;
d=c;
}
public int compareTo(post X)
{
if(X.t==this.t)
{
return 0;
}
else
{
long xt=this.t-X.t;
if(xt>0)return 1;
return -1;
}
}
}
class TreeNode
{
int val;
TreeNode left, right,par;
TreeNode() {}
TreeNode(int item)
{
val = item;
left =null;
right = null;
par=null;
}
}
class edge
{
int a,wt;
edge(int a,int w)
{
this.a=a;
wt=w;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
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 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
|
1c970a87d46b53d794a96f51eb3fe977
|
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.IOException;
import java.io.*;
import java.util.*;
public class NotSiting {
static InputReader inputReader=new InputReader(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static void solve() throws IOException
{
int n=inputReader.nextInt();
int m=inputReader.nextInt();
int r=n-1;
int c=m-1;
int matrix[][]=new int[n][m];
List<Integer>list=new ArrayList<>();
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
list.add(helper(i,j,r,c));
}
}
Collections.sort(list);
for(int ele:list)
{
out.print(ele+" ");
}
out.println();
}
static int helper(int i,int j,int r,int c)
{
int left=i+j;
int right=i+c-j;
int down=(r-i)+j;
int bottomright=r-i+c-j;
return Math.max(left,Math.max(right,Math.max(down,bottomright)));
}
static PrintWriter out=new PrintWriter((System.out));
public static void main(String args[])throws IOException
{
int t=inputReader.nextInt();
while (t-->0) {
solve();
}
long s = System.currentTimeMillis();
// out.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["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
|
73c20df33902b059a5810dfda0f769ea
|
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.lang.*;
public class StringInput {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static ArrayList<Long> a = new ArrayList<>();
long cnt=0,rmg=0;
static long val =1000000;
// static int dx[]={0,-1,0,1};
// static int dy[]={-1,0,1,0};
public static void main(String[] args) {
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0) {
// ArrayList<pair> pr = new ArrayList<>();
ArrayList<Integer> a = new ArrayList<>();
HashMap<Integer, ArrayList<Integer>> mp = new HashMap<>();
HashSet<Integer> hs = new HashSet<>();
// boolean flag = false, flag2 = false;
int n = sc.nextInt();
int m=sc.nextInt();
//int a[] = new int[n];
int dx[]= {1,n,1,n};
int dy[]= {1,1,m,m};
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
int ans=0;
for(int k=0; k<4; k++){
int sm= Math.abs(i-dx[k]) + Math.abs(j-dy[k]);
ans= Math.max(ans,sm);
}
a.add(ans);
}
}
Collections.sort(a);
for(int i=0; i<n*m; i++){
System.out.print(a.get(i)+" ");
}
System.out.println();
}
}
static long gcd(long a,long b){
if(b==0){
return a;
}
return gcd(b,a%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 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
|
4fbb8d261ebcd13e69dd0f45b5712817
|
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 PrintWriter out;
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
out=new PrintWriter(System.out);
int t=Integer.parseInt(st.nextToken());
while(t-->0) {
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
pq.add(dist(i, j, n, m));
}
}
while(!pq.isEmpty()) {
System.out.print(pq.poll()+" ");
}
System.out.println();
}
}
public static int dist(int i, int j, int n, int m) {
int dist1=Math.abs(i-n)+Math.abs(j-m);
int dist2=Math.abs(i-n)+Math.abs(j-1);
int dist3=Math.abs(i-1)+Math.abs(j-1);
int dist4=Math.abs(i-1)+Math.abs(j-m);
return Math.max(dist1, Math.max(dist2, Math.max(dist3, dist4)));
}
}
|
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
|
fe36fbd772191541be0b9284967fca58
|
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 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
|
b01e7f0dd49d0896fb5b71811db3fc80
|
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.*;
import java.lang.*;
public class Solution{
static class Graph{
public static class Vertex{
HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex
}
public static HashMap<Integer,Vertex> vt; // for vertices(all)
public Graph(){
vt= new HashMap<>();
}
public static int numVer(){
return vt.size();
}
public static boolean contVer(int ver){
return vt.containsKey(ver);
}
public static void addVer(int ver){
Vertex v= new Vertex();
vt.put(ver,v);
}
public static void addEdge(int ver1, int ver2, int weight){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
Vertex v1= vt.get(ver1);
Vertex v2= vt.get(ver2);
v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge
v2.nb.put(ver1,weight);
}
public static void delEdge(int ver1, int ver2){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
vt.get(ver1).nb.remove(ver2);
vt.get(ver2).nb.remove(ver1);
}
public static void delVer(int ver){
if(!vt.containsKey(ver)){
return;
}
Vertex v1= vt.get(ver);
ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
vt.get(s).nb.remove(ver);
}
vt.remove(ver);
}
static boolean done[];
static int parent[];
static ArrayList<Integer>vals= new ArrayList<>();
public static boolean isCycle(int i){
Stack<Integer>stk= new Stack<>();
stk.push(i);
while(!stk.isEmpty()){
int x= stk.pop();
vals.add(x);
// System.out.print("current="+x+" stackinit="+stk);
if(!done[x]){
done[x]=true;
}
else if(done[x] ){
return true;
}
ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet());
for (int j = 0; j <ar.size() ; j++) {
if(parent[x]!=ar.get(j)){
parent[ar.get(j)]=x;
stk.push(ar.get(j));
}
}
// System.out.println(" stackfin="+stk);
}
return false;
}
static int[]level;
static int[] curr;
static int[] fin;
public static void dfs(int src){
done[src]=true;
level[src]=0;
Queue<Integer>q= new LinkedList<>();
q.add(src);
while(!q.isEmpty()){
int x= q.poll();
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
level[v]=level[x]+1;
done[v]=true;
q.offer(v);
}
}
}
}
static int oc[];
static int ec[];
public static void dfs1(int src){
Queue<Integer>q= new LinkedList<>();
q.add(src);
done[src]= true;
// int count=0;
while(!q.isEmpty()){
int x= q.poll();
// System.out.println("x="+x);
int even= ec[x];
int odd= oc[x];
if(level[x]%2==0){
int val= (curr[x]+even)%2;
if(val!=fin[x]){
// System.out.println("bc");
even++;
vals.add(x);
}
}
else{
int val= (curr[x]+odd)%2;
if(val!=fin[x]){
// System.out.println("bc");
odd++;
vals.add(x);
}
}
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
// System.out.println(arr);
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
done[v]=true;
oc[v]=odd;
ec[v]=even;
q.add(v);
}
}
}
}
static long popu[];
static long happy[];
static long count[]; // total people crossing that pos
static long sum[]; // total sum of happy people including that.
public static void bfs(int x){
done[x]=true;
long total= popu[x];
// long smile= happy[x];
ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <nbrs.size() ; i++) {
int r= nbrs.get(i);
if(!done[r]){
bfs(r);
total+=count[r];
// smile+=sum[r];
}
}
count[x]=total;
// sum[x]=smile;
}
public static void bfs1(int x){
done[x]=true;
// long total= popu[x];
long smile= 0;
ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <nbrs.size() ; i++) {
int r= nbrs.get(i);
if(!done[r]){
bfs1(r);
// total+=count[r];
smile+=happy[r];
}
}
// count[x]=total;
sum[x]=smile;
}
}
static 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()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
static class disjointSet{
HashMap<Integer, Node> mp= new HashMap<>();
public static class Node{
int data;
Node parent;
int rank;
}
public void create(int val){
Node nn= new Node();
nn.data=val;
nn.parent=nn;
nn.rank=0;
mp.put(val,nn);
}
public int findparent(int val){
return findparentn(mp.get(val)).data;
}
public Node findparentn(Node n){
if(n==n.parent){
return n;
}
Node rr= findparentn(n.parent);
n.parent=rr;
return rr;
}
public void union(int val1, int val2){ // can also be used to check cycles
Node n1= findparentn(mp.get(val1));
Node n2= findparentn(mp.get(val2));
if(n1.data==n2.data) {
return;
}
if(n1.rank<n2.rank){
n1.parent=n2;
}
else if(n2.rank<n1.rank){
n2.parent=n1;
}
else {
n2.parent=n1;
n1.rank++;
}
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
// smallest form only
public Pair(int x, int y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair x){
if(this.x>x.x){
return 1;
}
else if(this.x==x.x){
if(this.y>x.y){
return 1;
}
else if(this.y==x.y){
return 0;
}
else{
return -1;
}
}
return -1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return x == pair.x && y == pair.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
public String toString(){
return x+" "+y;
}
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t= Reader.nextInt();
for (int tt = 0; tt <t ; tt++) {
int n = Reader.nextInt();
int m = Reader.nextInt();
int[] dist = new int[n * m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dist[i * m + j] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1);
}
}
ArrayList<Integer>arr= new ArrayList<>();
for (int i = 0; i <dist.length ; i++) {
arr.add(dist[i]);
}
Collections.sort(arr);
for (int k = 0; k < n * m; k++) {
System.out.print(arr.get(k)+" ");
}
System.out.println();
}
out.flush();
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 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
|
944bb61fdea565e79c5be59adeced379
|
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
|
// Created By Jefferson Samuel on 23/08/21 at 2:11 am
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.exit;
public class D {
static void solve() throws Exception {
int T = scanInt();
while(T-->0)
{
int r = scanInt();
int c = scanInt();
PriorityQueue<Integer> que = new PriorityQueue<>();
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int firc = i+j;
firc = Math.max(firc,i+(c-j-1));
firc = Math.max(firc,(r-i-1)+(c-j-1));
firc = Math.max(firc,(r-i-1 + j));
que.add(firc);
}
}
while (!que.isEmpty())
System.out.print(que.remove()+" ");
System.out.println();
}
}
// Don't touch
static int scanInt() throws IOException {
return parseInt(scanString());
}
static int[] scInAr(int N) throws IOException {
int[] a = new int[N];
for(int i = 0;i<N;i++)a[i] = scanInt();
return a;
}
static long[] scLoAr(int N) throws IOException {
long[] a = new long[N];
for(int i = 0;i<N;i++)a[i] = scanLong();
return a;
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
}
|
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
|
e9eacc3bb90a3694c1e1a8705cb1d3cb
|
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
|
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
public class CFB {
static CFB.Reader sc = new CFB.Reader();
static BufferedWriter bw;
static int team1;
static int team2;
static int minkicks;
public CFB() {
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
process:
for (int p = 0; p < t; p++) {
int n = sc.nextInt();
int m = sc.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int d1 = getDis(i, j, 1, 1);
int d2 = getDis(i, j, 1, m);
int d3 = getDis(i, j, n, 1);
int d4 = getDis(i, j, n, m);
int max = Math.max(d1, Math.max(d4, Math.max(d2, d3)));
list.add(max);
}
}
Collections.sort(list);
for (long k = 0; k <= (long) n * m - 1; k++) {
System.out.println(list.get((int) k));
}
}
bw.flush();
bw.close();
}
public static int getDis(int a, int b, int c, int d) {
return Math.abs(a-c)+Math.abs(b-d);
}
public static class Dir {
int l;
int r;
public Dir(int l, int r) {
this.l = l;
this.r = r;
}
}
public static int getAns(int i, int j, int[] ar, int c, int[][] dp) {
if (c != 0 && i <= j) {
if (dp[i][c] != -1) return dp[i][c];
int min = Integer.MAX_VALUE;
for (int p = i; p <= j; p++) {
min = Math.min(min, ar[p] - ar[i] + getAns(p + 1, j, ar, c - 1, dp));
}
dp[i][c] = min;
return min;
} else if (i <= j) {
return ar[j] - ar[i];
} else {
return 10000000;
}
}
public static class Coffee {
int st, en;
public Coffee(int st, int en) {
this.st = st;
this.en = en;
}
}
public static int checker(int a) {
if (a % 2 == 0) {
return 1;
} else {
byte b;
do {
b = 2;
} while (a + b != (a ^ b));
return b;
}
}
public static boolean checkPrime(int n) {
if (n != 0 && n != 1) {
for (int j = 2; j * j <= n; ++j) {
if (n % j == 0) {
return false;
}
}
return true;
} else {
return false;
}
}
public static void dfs1(List<List<Integer>> g, boolean[] visited, Stack<Integer> stack, int num) {
visited[num] = true;
Iterator var4 = ((List) g.get(num)).iterator();
while (var4.hasNext()) {
Integer integer = (Integer) var4.next();
if (!visited[integer]) {
dfs1(g, visited, stack, integer);
}
}
stack.push(num);
}
public static void dfs2(List<List<Integer>> g, boolean[] visited, List<Integer> list, int num, int c, int[] raj) {
visited[num] = true;
Iterator var6 = ((List) g.get(num)).iterator();
while (var6.hasNext()) {
Integer integer = (Integer) var6.next();
if (!visited[integer]) {
dfs2(g, visited, list, integer, c, raj);
}
}
raj[num] = c;
list.add(num);
}
public static int inputInt() throws IOException {
return sc.nextInt();
}
public static long inputLong() throws IOException {
return sc.nextLong();
}
public static double inputDouble() throws IOException {
return sc.nextDouble();
}
public static String inputString() throws IOException {
return sc.readLine();
}
public static void print(String a) throws IOException {
bw.write(a);
}
public static void printSp(String a) throws IOException {
bw.write(a + " ");
}
public static void println(String a) throws IOException {
bw.write(a + "\n");
}
public static long getAns(int[] ar, int c, long[][] dp, int i, int sign) {
if (i < 0) {
return 1L;
} else if (c <= 0) {
return 1L;
} else {
dp[i][c] = Math.max(dp[i][c], Math.max((long) ar[i] * getAns(ar, c - 1, dp, i - 1, sign), getAns(ar, c, dp, i - 1, 1)));
return dp[i][c];
}
}
public static long power(long a, long b, long c) {
long ans;
for (ans = 1L; b != 0L; b /= 2L) {
if (b % 2L == 1L) {
ans *= a;
ans %= c;
}
a *= a;
a %= c;
}
return ans;
}
public static long power1(long a, long b, long c) {
long ans;
for (ans = 1L; b != 0L; b /= 2L) {
if (b % 2L == 1L) {
ans = multiply(ans, a, c);
}
a = multiply(a, a, c);
}
return ans;
}
public static long multiply(long a, long b, long c) {
long res = 0L;
for (a %= c; b > 0L; b /= 2L) {
if (b % 2L == 1L) {
res = (res + a) % c;
}
a = (a + a) % c;
}
return res % c;
}
public static long totient(long n) {
long result = n;
for (long i = 2L; i * i <= n; ++i) {
if (n % i == 0L) {
while (n % i == 0L) {
n /= i;
}
result -= result / i;
}
}
if (n > 1L) {
result -= result / n;
}
return result;
}
public static long gcd(long a, long b) {
return b == 0L ? a : gcd(b, a % b);
}
public static boolean[] primes(int n) {
boolean[] p = new boolean[n + 1];
p[0] = false;
p[1] = false;
int i;
for (i = 2; i <= n; ++i) {
p[i] = true;
}
for (i = 2; i * i <= n; ++i) {
if (p[i]) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
public String LargestEven(String S) {
int[] count = new int[10];
int num;
for (num = 0; num < S.length(); ++num) {
++count[S.charAt(num) - 48];
}
num = -1;
for (int i = 0; i <= 8; i += 2) {
if (count[i] > 0) {
num = i;
break;
}
}
StringBuilder ans = new StringBuilder();
for (int i = 9; i >= 0; --i) {
int j;
if (i != num) {
for (j = 1; j <= count[i]; ++j) {
ans.append(i);
}
} else {
for (j = 1; j <= count[num] - 1; ++j) {
ans.append(num);
}
}
}
if (num != -1 && count[num] > 0) {
ans.append(num);
}
return ans.toString();
}
static {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
team1 = 0;
team2 = 0;
minkicks = 10;
}
public static class Score {
int l;
String s;
public Score(int l, String s) {
this.l = l;
this.s = s;
}
}
public static class Ath {
int r1;
int r2;
int r3;
int r4;
int r5;
int index;
public Ath(int r1, int r2, int r3, int r4, int r5, int index) {
this.r1 = r1;
this.r2 = r2;
this.r3 = r3;
this.r4 = r4;
this.r5 = r5;
this.index = index;
}
}
static class Reader {
private final int BUFFER_SIZE = 65536;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer;
private int bytesRead;
public Reader() {
this.din = new DataInputStream(System.in);
this.buffer = new byte[65536];
this.bufferPointer = this.bytesRead = 0;
}
public Reader(String file_name) throws IOException {
this.din = new DataInputStream(new FileInputStream(file_name));
this.buffer = new byte[65536];
this.bufferPointer = this.bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64];
int cnt;
byte c;
for (cnt = 0; (c = this.read()) != -1 && c != 10; buf[cnt++] = (byte) c) {
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c;
for (c = this.read(); c <= 32; c = this.read()) {
}
boolean neg = c == 45;
if (neg) {
c = this.read();
}
do {
ret = ret * 10 + c - 48;
} while ((c = this.read()) >= 48 && c <= 57);
return neg ? -ret : ret;
}
public long nextLong() throws IOException {
long ret = 0L;
byte c;
for (c = this.read(); c <= 32; c = this.read()) {
}
boolean neg = c == 45;
if (neg) {
c = this.read();
}
do {
ret = ret * 10L + (long) c - 48L;
} while ((c = this.read()) >= 48 && c <= 57);
return neg ? -ret : ret;
}
public double nextDouble() throws IOException {
double ret = 0.0D;
double div = 1.0D;
byte c;
for (c = this.read(); c <= 32; c = this.read()) {
}
boolean neg = c == 45;
if (neg) {
c = this.read();
}
do {
ret = ret * 10.0D + (double) c - 48.0D;
} while ((c = this.read()) >= 48 && c <= 57);
if (c == 46) {
while ((c = this.read()) >= 48 && c <= 57) {
ret += (double) (c - 48) / (div *= 10.0D);
}
}
return neg ? -ret : ret;
}
private void fillBuffer() throws IOException {
this.bytesRead = this.din.read(this.buffer, this.bufferPointer = 0, 65536);
if (this.bytesRead == -1) {
this.buffer[0] = -1;
}
}
private byte read() throws IOException {
if (this.bufferPointer == this.bytesRead) {
this.fillBuffer();
}
return this.buffer[this.bufferPointer++];
}
public void close() throws IOException {
if (this.din != null) {
this.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 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
|
997cbd695d78fc774a71bb1ab5499feb
|
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.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
private static LinkedList[] adj;
static class Pair {
int a;
int b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
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 main(String[] args) throws Exception {
// your code goes here
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt();
int m= sc.nextInt();
ArrayList<Integer> s= new ArrayList<>();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int a= i-1+j-1;
int b=n-i +m-j;
int c=n-i +j-1;
int d= i-1 + m-j;
a= Math.max(a,b);
c=Math.max(c,d);
s.add(Math.max(a,c));
}
}
Collections.sort(s);
for(int i=0;i<s.size();i++){
System.out.print(s.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 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
|
97da8ebb87747d858fa106a37a388462
|
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.*;
import java.lang.*;
public class ques1 {
//HashSet<Integer> hs=new HashSet<>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//HashMap<Integer,Integer> hm=new HashMap<>();
//ArrayList<Integer> lt=new ArrayList<>();
static long mod=1000_000_007;
public static String removeLeadZero(String str){
boolean fg=true;
StringBuilder sb1=new StringBuilder();
for(int i1=0;i1<str.length();i1++){
if(str.charAt(i1)=='0' && fg){
}else{
fg=false;
sb1.append(str.charAt(i1));
}
}
return sb1.toString();
}
public static String subSolve(String sum,String aStr){
StringBuilder sb=new StringBuilder();
boolean flag=false;
String curr="";
int i=0,j=0;
boolean imp=false;
int nn=sum.length();
while(aStr.length()<nn){
aStr='0'+aStr;
}
for(;i<sum.length() && j<aStr.length();){
if(sum.charAt(i)>=aStr.charAt(j) && !flag && (i==(nn-1) || sum.charAt(i+1)!='0')){
long diff=Long.valueOf(sum.charAt(i)+"")-Long.valueOf(aStr.charAt(j)+"");
i++; j++;
sb.append(diff+"");
// System.out.println("sb=="+sb+" "+diff);
}else{
if(!flag){
curr=sum.charAt(i)+""; i++; flag=true;
}else{
curr+=sum.charAt(i); i++;
long diff=Long.valueOf(curr)-Long.valueOf(aStr.charAt(j)+"");
j++;
if((diff+"").length()>1){
imp=true; break;
}
sb.append(diff+"");
// System.out.println("sb22=="+sb+" "+diff+" sum=="+sum);
flag=false;
}
}
}
if(i<sum.length() || j<aStr.length() || imp){
return "-1";
}else{
return removeLeadZero(sb.toString());
}
}
public static boolean isValid(int x,int n,int k,int ocnt){
int pl=x;
boolean odd=(x%2==0?false:true);
if(odd)pl--;
int even=n-ocnt;
int diff=(even-(pl*k));
int todd=diff+ocnt;
if(diff>=0 && (odd?(todd>=k?true:false):true)){
return true;
}
return false;
}
public static void solve1() throws Exception{
InputStreamReader ip=new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ip);
int t = Integer.parseInt(br.readLine());
StringBuilder asb=new StringBuilder();
while(t-->0){
// int n = Integer.parseInt(br.readLine());
String[] strs=(br.readLine()).trim().split(" ");
int n=Integer.parseInt(strs[0]),m=Integer.parseInt(strs[1]);//,r=Integer.parseInt(strs[2]),c=Integer.parseInt(strs[3]);
char[][] grid=new char[n][m];
int[] dist = new int[n * m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dist[i * m + j] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1);
}
}
sort(dist);
for(int i=0;i<(n*m);i++){
asb.append(dist[i]+" ");
}
asb.append("\n");
}
System.out.println(asb);
}
//******************************************************************************************************************** */
public static void main(String[] args) throws Exception{
solve1();
}
public static void solve2() throws Exception{
InputStreamReader ip=new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ip);
int t = Integer.parseInt(br.readLine());
StringBuilder asb=new StringBuilder();
while(t-->0){
// int n =Integer.parseInt(br.readLine());
// String[] strs=(br.readLine()).trim().split(" ");
// int n=Integer.parseInt(strs[0]),k=Integer.parseInt(strs[1]),r=Integer.parseInt(strs[2]),s=Integer.parseInt(strs[3]);
// String str=(br.readLine()).trim();
// int[] arr=new int[n];
// for(int i=0;i<n;i++){arr[i]=Integer.parseInt(strs[i]);}
}
}
//******************************************************************************************************************** */
public static long modInverse1(long a, long m){
// int g = gcd(a, m);
// if(g!=1) {System.out.println("Inverse Doesnot Exist");}
return binexp(a, m - 2, m);
}
public static long binexp(long a, long b, long m){
if (b == 0)return 1;
long res = binexp(a, b / 2, m);
if (b % 2 == 1) return (( (res*res)%m )*a) % m;
else return (res*res)%m;
}
//binexp
public static long binexp(long a,long b){
if(b==0)return 1;
long res=binexp(a, b/2);
if(b%2==1){
return (((res*res))*a);
}else return (res*res);
}
//Comparator Interface
public static class comp implements Comparator<int[]>{
public int compare(int[] a,int[] b){
return a[0]-b[0];
}
}
//gcd using Euclid's Division Algorithm
public static long gcd(long a,long b){
if(b==0)return a;
else return gcd(b, a%b);
}
//check prime in root(n)
public static int isprime(int n){
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0)return i;
}
return -1; //means n is prime
}
//SOE
static int[] sieve;
public static void SOE(int n){
sieve=new int[n+1];
// System.out.println("All prime number from 1 to n:-");
for(int x=2;x<=n;x++){
if(sieve[x]!=0){ //Not prime number
continue;
}
// System.out.print(x+" ");
for(int u=2*x;u<=n;u+=x){
sieve[u]=x;
}
}
//If sieve[i]=0 means 'i' is primr else 'i' is not prime
}
//sort
public static void sort(long[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
long temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; //swap
}
Arrays.sort(arr);
}
public static void sort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
int temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; //swap
}
Arrays.sort(arr);
}
//1d print
public static void print(int[] dp) {
for (int val : dp) {
System.out.print(val + " ");
}
System.out.println();
}
public static void print(long[] dp) {//Method Overloading
for (long val : dp) {
System.out.print(val + " ");
}
System.out.println();
}
//2d print
public static void print(long[][] dp) {//Method Overloading
for (long[] a : dp) {
for (long val : a) {
System.out.print(val + " ");
}
System.out.println();
}
}
public static void print(int[][] dp) {
for (int[] a : dp) {
for (int val : a) {
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 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
|
8e3528e6bb229e6ff672b28a1b3b61a9
|
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 scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-- > 0)
{
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] distance = new int[n*m];
int[][] corners = {{0,0},{0,m-1},{n-1,0},{n-1,m-1}};
int distanceIndex = 0;
for(int row = 0; row < n; row++)
{
for(int col = 0; col < m; col++)
{
int minDistance = Integer.MIN_VALUE;
for(int r = 0; r < corners.length; r++)
{
int currDistance = Math.abs(row-corners[r][0]) + Math.abs(col-corners[r][1]);
minDistance = Math.max(minDistance,currDistance);
}
distance[distanceIndex++] = minDistance;
}
}
Arrays.sort(distance);
for(int i = 0; i < distance.length; i++)
{
System.out.print(distance[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 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
|
ef804fdf1650e1a94de0495ea279d7d1
|
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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class CodeForces {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public FastScanner(File f){
try {
br=new BufferedReader(new FileReader(f));
st=new StringTokenizer("");
} catch(FileNotFoundException e){
br=new BufferedReader(new InputStreamReader(System.in));
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[] 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());
}
}
public static long factorial(int n){
if(n==0)return 1;
return (long)n*factorial(n-1);
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void sort (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sortReversed (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sort (long[]a){
ArrayList<Long> b = new ArrayList<>();
for(long i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
ans.add(i);
}
return ans;
}
static int binarySearchSmallerOrEqual(int arr[], int key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
public static int binarySearchStrictlySmaller(int[] arr, int target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static int binarySearch(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void init(int[]arr,int val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static void init(int[][]arr,int val){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=val;
}
}
}
static void init(long[]arr,long val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static<T> void init(ArrayList<ArrayList<T>>arr,int n){
for(int i=0;i<n;i++){
arr.add(new ArrayList());
}
}
public static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target)
{
int start = 0, end = arr.size()-1;
if(end == 0) return -1;
if (target > arr.get(end).y) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid).y >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static long sum (int []a, int[][] vals){
long ans =0;
int m=0;
while(m<5){
for (int i=m+1;i<5;i+=2){
ans+=(long)vals[a[i]-1][a[i-1]-1];
ans+=(long)vals[a[i-1]-1][a[i]-1];
}
m++;
}
return ans;
}
public static void main(String[] args) {
// StringBuilder sbd = new StringBuilder();
StringBuffer sbf = new StringBuffer();
// PrintWriter p = new PrintWriter("output.txt");
// File input = new File("popcorn.in");
// FastScanner fs = new FastScanner(input);
//
FastScanner fs = new FastScanner();
// Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int testNumber =fs.nextInt();
// ArrayList<Integer> arr = new ArrayList<>();
for (int T =0;T<testNumber;T++){
int n =fs.nextInt();
int m =fs.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
int max =-1;
max=Math.max(Math.abs(i-1)+Math.abs(j-1),max);
max=Math.max(Math.abs(i-n)+Math.abs(j-m),max);
max=Math.max(Math.abs(i-1)+Math.abs(j-m),max);
max=Math.max(Math.abs(i-n)+Math.abs(j-1),max);
ans.add(max);
}
}
Collections.sort(ans);
for (int i=0;i<n*m;i++){
out.print(ans.get(i)+" ");
}
out.print("\n");
}
out.flush();
}
static class Pair {
int x;//l
int y;//r
public Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public boolean equals(Object o) {
if(o instanceof Pair){
if(o.hashCode()!=hashCode()){
return false;
} else {
return x==((Pair)o).x&&y==((Pair)o).y;
}
}
return false;
}
@Override
public int hashCode() {
return x+2*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
|
cd5026f1939f8564632120c58c284a74
|
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
|
//---#ON_MY_WAY---
import static java.lang.Math.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class B {
static class pair {
int x, y;
public pair(int a, int b) {
x = a;
y = b;
}
}
static FastReader x = new FastReader();
static OutputStream outputStream = System.out;
static PrintWriter out = new PrintWriter(outputStream);
/*---------------------------------------CODE STARTS HERE-------------------------*/
public static void main(String[] args) throws NumberFormatException, IOException {
long startTime = System.nanoTime();
int mod = 1000000007;
int t = x.nextInt();
TreeSet<Integer> ts = new TreeSet<>();
for(int i = 0; i < 31; i++) {
ts.add((int) pow(2, i));
}
StringBuilder str = new StringBuilder();
while (t > 0) {
int n = x.nextInt(), m = x.nextInt();
PriorityQueue<Integer> p = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int d = i+j;
d = max(d, n-i+j-1);
d = max(d, i+m-j-1);
d = max(d, n-i+m-j-2);
p.add(d);
}
}
while(!p.isEmpty()) {
str.append(p.remove()+" ");
}
str.append("\n");
t--;
}
out.println(str);
out.flush();
long endTime = System.nanoTime();
//System.out.println((endTime-startTime)/1000000000.0);
}
/*--------------------------------------------FAST I/O--------------------------------*/
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;
}
char nextchar() {
char ch = ' ';
try {
ch = (char) br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
/*--------------------------------------------BOILER PLATE---------------------------*/
static int[] readarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = x.nextInt();
}
return arr;
}
static int[] sortint(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static long[] sortlong(long a[]) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
return a;
}
static long pow(long x, long y) {
long result = 1;
while (y > 0) {
if (y % 2 == 0) {
x = x * x;
y = y / 2;
} else {
result = result * x;
y = y - 1;
}
}
return result;
}
static long pow(long x, long y, long mod) {
long result = 1;
x %= mod;
while (y > 0) {
if (y % 2 == 0) {
x = (x % mod * x % mod) % mod;
y /= 2;
} else {
result = (result % mod * x % mod) % mod;
y--;
}
}
return result;
}
static int[] revsort(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al, Comparator.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static int[] gcd(int a, int b, int ar[]) {
if (b == 0) {
ar[0] = a;
ar[1] = 1;
ar[2] = 0;
return ar;
}
ar = gcd(b, a % b, ar);
int t = ar[1];
ar[1] = ar[2];
ar[2] = t - (a / b) * ar[2];
return ar;
}
static boolean[] esieve(int n) {
boolean p[] = new boolean[n + 1];
Arrays.fill(p, true);
for (int i = 2; i * i <= n; i++) {
if (p[i] == true) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static ArrayList<Integer> primes(int n) {
boolean p[] = new boolean[n + 1];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(p, true);
int i = 0;
for (i = 2; i * i <= n; i++) {
if (p[i] == true) {
al.add(i);
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
for (i = i; i <= n; i++) {
if (p[i] == true) {
al.add(i);
}
}
return al;
}
static int etf(int n) {
int res = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res /= i;
res *= (i - 1);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
res /= n;
res *= (n - 1);
}
return res;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
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 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
|
c5c32fd0a303c3753ded6a28896c52ad
|
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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class problems {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n=sc.nextInt(),m=sc.nextInt();
ArrayList<Integer>a=new ArrayList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int d1=i+j;
int d2=(n-1-i)+j;
int d3=(n-i-1)+(m-j-1);
int d4=i+(m-j-1);
a.add(Math.max(Math.max(d1,d2),Math.max(d3,d4)));
}
}
Collections.sort(a);
for(int i=0;i<a.size();i++) System.out.print(a.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 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
|
9ce4f86ec9723f44ee35efd6b9bf018e
|
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.*;
import java.util.*;
public class practice {
static FastScanner fs = new FastScanner();
public static void main(String[] args) {
int t = 1;
t = fs.nextInt();
for(int i=1;i<=t;i++) {
solve(t);
}
}
public static void solve(int tt) {
int n = fs.nextInt();
int m = fs.nextInt();
ArrayList<Integer> ans = new ArrayList<Integer>();
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
int x = Math.max(i, n-i-1) + Math.max(j,m-j-1);
ans.add(x);
}
}
Collections.sort(ans);
for (Iterator iterator = ans.iterator(); iterator.hasNext();) {
Integer integer = (Integer) iterator.next();
System.out.print(integer+" ");
}
System.out.println();
return;
}
public static int [] sortarray(int a[]) {
ArrayList<Integer> L = new ArrayList<Integer>();
for(int i:a) {
L.add(i);
}
Collections.sort(L);
for(int i=0;i<L.size();i++)a[i]=L.get(i);
return a;
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
ArrayList<Integer> readList(int n) {
ArrayList<Integer> a = new ArrayList<Integer>();
int x;
for(int i=0;i<n;i++) {
x=fs.nextInt();
a.add(x);
}
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 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
|
f8715d1277fa2fe690b28d204c9b3486
|
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.*;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
FastInput sc = new FastInput();
int T = sc.nextInt();
while(T-->0)
{
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=m;j++)
{
int ans = max(abs(n-i),i-1) + max(j-1,abs(m-j));
a.add(ans);
}
}
Collections.sort(a);
for(int i = 0;i<a.size();i++)
System.out.print(a.get(i)+" ");
System.out.println();
}
}
public static void sout(Object s)
{
System.out.println(s);
}
}
class FastInput {
BufferedReader br;
StringTokenizer st;
public FastInput()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while(st == null || !st.hasMoreElements())
{
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e)
{
System.out.println(e.toString());
}
}
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)
{
System.out.println(e.toString());
}
return str;
}
public static void sort(int[] arr)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public int [] readArr(int n)
{
int [] a = new int[n];
for(int i = 0; i<n; i++)
a[i] = nextInt();
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 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
|
e3caeac5e83084a44d625c2282a8b152
|
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 Solution {
public static class DSU {
private int[] parent;
private int totalGroup;
public DSU(int n) {
parent = new int[n];
totalGroup = n;
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public boolean union(int a, int b) {
int parentA = findParent(a);
int parentB = findParent(b);
if (parentA == parentB) {
return false;
}
totalGroup--;
if (parentA < parentB) {
this.parent[parentB] = parentA;
} else {
this.parent[parentA] = parentB;
}
return true;
}
public int findParent(int a) {
if (parent[a] != a) {
parent[a] = findParent(parent[a]);
}
return parent[a];
}
public int getTotalGroup() {
return totalGroup;
}
}
public static void main(String[] args) throws Exception {
int tc = io.nextInt();
for (int i = 0; i < tc; i++) {
solve();
}
io.close();
}
private static void solve() throws Exception {
int n = io.nextInt();
int m = io.nextInt();
int[][] tinas = new int[][]{{0, 0}, {n - 1, 0}, {0, m - 1}, {n - 1, m - 1}};
Queue<Integer> pq = new PriorityQueue<>(Integer::compare);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int minDist1 = 0;
for (int[] tina : tinas) {
minDist1 = Math.max(minDist1, Math.abs(tina[0] - i) + Math.abs(tina[1] - j));
}
pq.add(minDist1);
}
}
while (!pq.isEmpty()) {
io.print(pq.poll());
io.print(" ");
}
io.println();
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>(a.length);
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
//-----------PrintWriter for faster output---------------------------------
public static FastIO io = new FastIO();
//-----------MyScanner class for faster input----------
static class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
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; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
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 String nextLine() {
int c;
do {
c = nextByte();
} while (c < '\n');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > '\n');
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 long nextLong() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long 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());
}
}
//--------------------------------------------------------
}
|
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
|
4f34770229ab1c92d8b3d2c6ea228113
|
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 Codeforces766{
static MyScanner sc = new MyScanner();
static void third(){
int n = sc.nextInt();
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 1;i<n;i++){
int a = sc.nextInt();
int b = sc.nextInt();
map.put(a,map.getOrDefault(a,0)+1);
map.put(b,map.getOrDefault(b,0)+1);
if(map.get(a)>2 || map.get(b)>2){
out.println(-1);
return;
}
}
for(int i = 1;i<n;i++){
if(i%2!=0){
out.print(2+" ");
}else{
out.print(3+" ");
}
}
out.println();
}
static void first(){
int n = sc.nextInt();
int m = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
boolean flag = false;
String mat[] = new String[n];
for(int i = 0;i<n;i++){
mat[i] = sc.nextLine();
if(mat[i].contains("B")){
flag = true;
}
}
if(!flag){
out.println(-1);
return;
}
if(mat[r-1].charAt(c-1)=='B'){
out.println(0);
return;
}
if(mat[r-1].contains("B")){
out.println(1);
return;
}
for(int i = 0;i<n;i++){
if(mat[i].charAt(c-1)=='B'){
out.println(1);
return;
}
}
out.println(2);
}
static void second(){
int n = sc.nextInt();
int m = sc.nextInt();
int dp[] = new int[n*m];
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
dp[i*m+j] = Math.max(i,n-i-1) + Math.max(j,m-j-1);
}
}
Arrays.sort(dp);
for(int i = 0;i<n*m;i++){
out.print(dp[i]+" ");
}
out.println();
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-- >0){
// first();
second();
// third();
}
// 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();
}
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
|
273f93b64405416f31b66d80a8bfbd4f
|
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.PriorityQueue;
public class b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
for (int t = 0; t < tc; t++) {
int n = sc.nextInt(), m = sc.nextInt();
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dist = i + j;
dist = Math.max(dist, n - i + j - 1);
dist = Math.max(dist, m + i - j - 1);
dist = Math.max(dist, m + n - i - j - 2);
pq.add(dist);
}
}
while (!pq.isEmpty()) {
System.out.print(pq.remove() + " ");
}
System.out.println();
}
sc.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
|
c7fc0fcfcfd1ca6e2a3b54b5f7e3e778
|
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 B1627 {
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();
int[][] arr=new int[n][m];
int max=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j]=((i>=n/2)?n-i-1:i) +((j>=m/2)?m-j-1:j);
max=Math.max(max,arr[i][j]);
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}*/
//System.out.println(max);
int[] hash=new int[max+1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
hash[arr[i][j]]++;
}
}
int maxVal=n+m-2;
for (int i = hash.length-1; i >=0 ; i--) {
for (int j = 0; j < hash[i]; j++) {
System.out.print(maxVal-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 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
|
d928b470fae8038eeb9c57371f055db2
|
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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.*;
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);
solve(in, out);
out.close();
}
static String reverse(String s) {
return (new StringBuilder(s)).reverse().toString();
}
static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar)
{
factors[1]=1;
int p;
for(p = 2; p*p <=n; p++)
{
if(factors[p] == 0)
{
ar.add(p);
factors[p]=p;
for(int i = p*p; i <= n; i += p)
if(factors[i]==0)
factors[i] = p;
}
}
for(;p<=n;p++){
if(factors[p] == 0)
{
factors[p] = p;
ar.add(p);
}
}
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort1(long ar[]) {
int n = ar.length;
ArrayList<Long> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
static int findMax(int a[], int n, int vis[], int i, int d){
if(i>=n)
return 0;
if(vis[i]==1)
return findMax(a, n, vis, i+1, d);
int max = 0;
for(int j=i+1;j<n;j++){
if(Math.abs(a[i]-a[j])>d||vis[j]==1)
continue;
vis[j] = 1;
max = Math.max(max, 1 + findMax(a, n, vis, i+1, d));
vis[j] = 0;
}
return max;
}
static void findSub(ArrayList<ArrayList<Integer>> ar, int n, ArrayList<Integer> a, int i){
if(i==n){
ArrayList<Integer> b = new ArrayList<Integer>();
for(int y:a){
b.add(y);
}
ar.add(b);
return;
}
for(int j=0;j<n;j++){
if(j==i)
continue;
a.set(i,j);
findSub(ar, n, a, i+1);
}
}
// *-------------------code starts here--------------------------------------------------*
public static void solve(InputReader sc, PrintWriter pw){
long mod=(long)1e9+7;
int t=sc.nextInt();
// int t=1;
L : 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 ans=Math.max(i,(n-i-1))+Math.max(j,(m-j-1));
list.add(ans);
}
}
Collections.sort(list);
for(int i:list){
pw.print(i+" ");
}
pw.println();
}
}
// *-------------------code ends here-----------------------------------------------------*
static void assignAnc(ArrayList<Integer> ar[],int sz[], int pa[] ,int curr, int par){
sz[curr] = 1;
pa[curr] = par;
for(int v:ar[curr]){
if(par==v)
continue;
assignAnc(ar, sz, pa, v, curr);
sz[curr] += sz[v];
}
}
static int findLCA(int a, int b, int par[][], int depth[]){
if(depth[a]>depth[b]){
a = a^b;
b = a^b;
a = a^b;
}
int diff = depth[b] - depth[a];
for(int i=19;i>=0;i--){
if((diff&(1<<i))>0){
b = par[b][i];
}
}
if(a==b)
return a;
for(int i=19;i>=0;i--){
if(par[b][i]!=par[a][i]){
b = par[b][i];
a = par[a][i];
}
}
return par[a][0];
}
static void formArrayForBinaryLifting(int n, int par[][]){
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1)
continue;
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
static long lcm(int a, int b){
return a*b/gcd(a,b);
}
static class Pair1 {
long a;
long b;
Pair1(long a, long b) {
this.a = a;
this.b = b;
}
}
static class Pair implements Comparable<Pair> {
int a;
int b;
// int c;
Pair(int a, int b) {
this.a = a;
this.b = b;
// this.c = c;
}
public int compareTo(Pair p) {
return Integer.compare(a,p.a);
}
}
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 long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 9992768);
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[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
public long[] readarray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
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 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
|
f64b621143efa71c32e6950f5a3cf7e4
|
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.*;
import static java.lang.Math.*;
public class Practice {
static Scanner scn = new Scanner(System.in);
static StringBuilder sb = new StringBuilder();
public static void main(String[] ScoobyDoobyDo) {
p = new HashSet<>();
int t = scn.nextInt();
for(int tests = 0; tests < t; tests++) solveB();
System.out.println(sb);
}
public static void solveB() {
int n = scn.nextInt(), m = scn.nextInt();
Queue<int[]> q = new LinkedList<>();
q.add(new int[] {(n + 1) / 2, (m + 1) / 2});
int[][] dir = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int d = dir.length;
boolean[][] bool = new boolean[n + 1][m + 1];
bool[q.peek()[0]][q.peek()[1]] = true;
List<Integer> res = new ArrayList<>();
while(!q.isEmpty()) {
int sz = q.size();
for(int i = 0; i < n; i++) {
int[] cur = q.poll();
int x = cur[0], y = cur[1];
int max = 0;
for(int[] k : new int[][] {{1, 1}, {1, m}, {n, 1}, {n, m}})
max = max(max, (abs(k[0] - x) + abs(k[1] - y)));
res.add(max);
for(int j = 0; j < d; j++) {
int nx = x + dir[j][0], ny = y + dir[j][1];
if(!check(nx, ny, n, m) || bool[nx][ny]) continue;
bool[nx][ny] = true;
q.add(new int[] {nx, ny});
}
}
}
Collections.sort(res);
for(int i : res) sb.append(i + " ");
sb.append("\n");
}
public static boolean check(int x, int y, int n, int m) {
return x <= n && x >= 1 && y <= m && y >= 1;
}
public static void solve() {
int n = scn.nextInt();
List<List<Integer>> graph = new ArrayList<>();
for(int i = 0; i <= n; i++) graph.add(new ArrayList<>());
for(int i = 0; i < n; i++) {
int x = scn.nextInt(), y = scn.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
for(int i = 1; i <= n; i++) {
if(graph.get(i).size() >= 3) {
sb.append(-1 + "\n");
return;
}
}
}
static HashSet<Integer> p;
public static void sieve(int n) {
boolean[] prime = new boolean[n + 1];
for(int i = 2; i * i <= n; i++) {
for(int j = 2 * i; j <= n; j += i) {
prime[j] = true;
}
}
for(int i = 2; i <= n; i++) if(!prime[i]) p.add(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 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
|
06776ffe49df06389e7dec20c535f0dc
|
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.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int inputs = Integer.parseInt(in.readLine());
while(inputs-->0) {
StringTokenizer st = new StringTokenizer(in.readLine());
int rows = Integer.parseInt(st.nextToken());
int cols = Integer.parseInt(st.nextToken());
int[][] min = new int[rows][cols];
int[][] starts = {{0, 0}, {rows-1, 0}, {0, cols-1}, {rows-1, cols-1}};
for(int[] s : starts) {
boolean[][] vis = new boolean[rows][cols];
Queue<int[]> list = new LinkedList<>();
list.add(new int[] {s[0], s[1], 0});
while(!list.isEmpty()) {
int[] curr = list.poll();
if(vis[curr[0]][curr[1]])
continue;
vis[curr[0]][curr[1]] = true;
min[curr[0]][curr[1]] = Math.max(min[curr[0]][curr[1]], curr[2]);
if(curr[0] > 0) {
list.add(new int[] {curr[0]-1, curr[1], curr[2]+1});
}
if(curr[0] < rows-1) {
list.add(new int[] {curr[0]+1, curr[1], curr[2]+1});
}
if(curr[1] > 0) {
list.add(new int[] {curr[0], curr[1]-1, curr[2]+1});
}
if(curr[1] < cols-1) {
list.add(new int[] {curr[0], curr[1]+1, curr[2]+1});
}
}
}
ArrayList<Integer> dists = new ArrayList<>();
for(int[] m : min) {
for(int i : m)
dists.add(i);
}
Collections.sort(dists);
StringBuilder ans = new StringBuilder();
for(int i : dists) {
ans.append(i + " ");
}
System.out.println(ans.toString().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 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
|
7bc25581a0553082a8ff3af940cb32af
|
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 PrintWriter out;
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
out=new PrintWriter(System.out);
int t=Integer.parseInt(st.nextToken());
while(t-->0) {
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
pq.add(dist(i, j, n, m));
}
}
while(!pq.isEmpty()) {
System.out.print(pq.poll()+" ");
}
System.out.println();
}
}
public static int dist(int i, int j, int n, int m) {
int dist1=Math.abs(i-n)+Math.abs(j-m);
int dist2=Math.abs(i-n)+Math.abs(j-1);
int dist3=Math.abs(i-1)+Math.abs(j-1);
int dist4=Math.abs(i-1)+Math.abs(j-m);
return Math.max(dist1, Math.max(dist2, Math.max(dist3, dist4)));
}
}
|
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
|
2a2a451302fa2c2dc68afcd1f24566be
|
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.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sparsh Sanchorawala
*/
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);
BNotSitting solver = new BNotSitting();
solver.solve(1, in, out);
out.close();
}
static class BNotSitting {
public void solve(int testNumber, InputReader s, PrintWriter w) {
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt(), m = s.nextInt();
int[] f = new int[n + m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int v1 = Math.max(i, n - 1 - i);
int v2 = Math.max(j, m - 1 - j);
f[v1 + v2]++;
}
}
for (int k = 0; k < n + m; k++) {
while (f[k] > 0) {
w.print(k + " ");
f[k]--;
}
}
w.println();
}
}
}
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);
}
}
}
|
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
|
efd2697b850349ac5a043ceb63fe5b13
|
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 a {
public static void main(String[] args){
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> numbers = 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);
int maxd = Math.max(d1, Math.max(d2, Math.max(d3, d4)));
numbers.add(maxd);
}
}
Collections.sort(numbers);
for(int i=0; i<n*m; i++){
System.out.print(numbers.get(i) + " ");
}
System.out.println();
}
}
}
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
|
Java
|
["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
|
d7e60ad9dd06abc27aa061d696d2a442
|
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 final class B {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T = fs.nextInt();
while(T-- > 0){
int n = fs.nextInt();
int m= fs.nextInt();
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
int dist = i + j;
dist = Math.max(dist, i + Math.abs(j - (m - 1)));
dist = Math.max(dist, Math.abs(i - (n- 1)) + j);
dist = Math.max(dist, Math.abs(i - (n - 1)) + Math.abs(j - (m - 1)));
pq.add(dist);
}
}
while(!pq.isEmpty()) out.print(pq.poll() + " ");
out.println();
out.flush();
}
}
static final Random random = new Random();
static final int mod = 1_000_000_007;
static long add(long a, long b) {
return (a + b) % mod;
}
static long sub(long a, long b) {
return ((a - b) % mod + mod) % mod;
}
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 long mul(long a, long b) {
return (a * b) % mod;
}
static long exp(long base, long exp) {
if (exp == 0) return 1;
long half = exp(base, exp / 2);
if (exp % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials = new long[2_000_001];
static long[] invFactorials = new long[2_000_001];
static void precompFacts() {
factorials[0] = invFactorials[0] = 1;
for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i);
invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2);
for (int i = invFactorials.length - 2; i >= 0; i--)
invFactorials[i] = mul(invFactorials[i + 1], i + 1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k]));
}
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 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
|
0a377c67976e7eb54ef7841ca7226a76
|
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.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class CF1627B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int m = scanner.nextInt();
System.out.println(solve(n, m));
}
}
private static String solve2(int n, int m) {
int x1 = 0;
int y1 = 0;
int x2 = n - 1;
int y2 = m - 1;
int[][] nums = new int[n][m];
List<String> resList = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dis1 = (i - x1) + (j - y1);
int dis2 = (i - x1) + (y2 - j);
int dis3 = (x2 - i) + (j - y1);
int dis4 = (x2 - i) + (y2 - j);
nums[i][j] = Math.max(dis1, Math.max(dis2, Math.max(dis3, dis4)));
resList.add(String.valueOf(nums[i][j]));
}
}
Collections.sort(resList);
return String.join(" ", resList);
}
private static String solve(int n, int m) {
int x1 = 0;
int y1 = 0;
int x2 = n - 1;
int y2 = m - 1;
int[][] nums = new int[n][m];
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dis1 = (i - x1) + (j - y1);
int dis2 = (i - x1) + (y2 - j);
int dis3 = (x2 - i) + (j - y1);
int dis4 = (x2 - i) + (y2 - j);
nums[i][j] = Math.max(dis1, Math.max(dis2, Math.max(dis3, dis4)));
list.add(nums[i][j]);
}
}
// List<Integer> => String
Collections.sort(list);
return list.stream().map(String::valueOf).collect(Collectors.joining(" "));
}
}
|
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 17
|
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
|
2d650e5387b31f1a5720e62ac566f656
|
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.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class CF1627B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int m = scanner.nextInt();
System.out.println(solve(n, m));
}
}
private static String solve(int n, int m) {
int x1 = 0;
int y1 = 0;
int x2 = n - 1;
int y2 = m - 1;
int[][] nums = new int[n][m];
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dis1 = (i - x1) + (j - y1);
int dis2 = (i - x1) + (y2 - j);
int dis3 = (x2 - i) + (j - y1);
int dis4 = (x2 - i) + (y2 - j);
nums[i][j] = Math.max(dis1, Math.max(dis2, Math.max(dis3, dis4)));
list.add(nums[i][j]);
}
}
// List<Integer> => String
Collections.sort(list);
StringBuilder stringBuilder = new StringBuilder();
for (int num : list) {
stringBuilder.append(num).append(" ");
}
return stringBuilder.toString().stripTrailing();
}
}
|
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 17
|
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
|
6ebb51c56d06a07457bb6b0e32cd140f
|
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 practice {
public static void solve() {
Reader sc = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] dist = new int[n * m];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j)
dist[i * m + j] = Math.max(i, n - i - 1) + Math.max(j, m - j - 1);
}
sort(dist, n * m);
for(int i : dist)
out.print(i + " ");
out.println();
}
out.flush();
}
public static int[] sort(int[] arr,int n) {
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);
return arr;
}
public static void main(String[] args) throws IOException {
solve();
}
public static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if(st.hasMoreTokens())
str = st.nextToken("\n");
else
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 17
|
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
|
086d254cf7446f3c75a7f1d4779aaba0
|
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
|
/**
* Surly
* 21.09.2022
**/
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter pr = new PrintWriter(System.out);
public static void main (String[] args) throws IOException{
double starttime = System.currentTimeMillis();
int t = sc.nextInt();
while(t-->0)
solve();
pr.close();
double endtime=System.currentTimeMillis();
System.err.println("Elapsed time : "+(endtime-starttime)+" ms.");
}
static void solve() {
int n = sc.nextInt() , m = sc.nextInt();
Vector<Integer> v = new Vector<Integer>();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
int a = Math.abs(1-i)+ Math.abs(1-j);
int b = Math.abs(1-i) + Math.abs(m-j);
int c = Math.abs(n-i)+ Math.abs(1-j);
int d = Math.abs(n-i)+ Math.abs(m-j);
v.add(Math.max(a,Math.max(b,Math.max(c,d))));
}
Collections.sort(v);
for(int i : v)
pr.print(i+" ");
pr.println();
}
static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); }
}
|
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 17
|
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
|
c2043eeb15c8a7e786109097e9a6d83d
|
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.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round766B {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Round766B sol = new Round766B();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n) or matrix(n, 2) for graph edges, pairs, ...
int n, m;
void getInput() {
n = in.nextInt();
m = in.nextInt();
}
void printOutput() {
out.printlnAns(ans);
}
int[] ans;
void solve(){
ans = new int[n*m];
// Rahul chooses a non-pink of smallest radius (distance to farthest seat)
// hence Tina paints so that smallest radius is maximized
// for each seat, find the radius --
// sort the radius
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
int radius = dist(i, j, 0, 0);
radius = Math.max(radius, dist(i, j, n-1, 0));
radius = Math.max(radius, dist(i, j, 0, m-1));
radius = Math.max(radius, dist(i, j, n-1, m-1));
ans[i*m+j] = radius;
}
}
Arrays.sort(ans);
}
private int dist(int i, int j, int n, int m) {
return Math.abs(i-n) + Math.abs(j-m);
}
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
|
Java
|
["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 17
|
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
|
6984eff9e66f0767ca7241d916a7ea22
|
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 first {
public static void main(String[] args) {
// System.out.println("hey");
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<>(n*m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
ans.add(Math.max(i, n-i-1) + Math.max(j, m-j-1));
}
}
// sort(all(ans));
Collections.sort((ans));
for(int x : ans) {
System.out.print(x + " ");
}
System.out.println();
t -= 1;
}
}
}
|
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 17
|
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
|
a5f753d3036e852af7dd6c293996d4b4
|
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.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
public static FastReader cin;
public static PrintWriter out;
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
cin = new FastReader();
long [] T = new long [20];
T[0] = 1;
for(int i = 1 ; i < 20 ; i++){
T[i] = T[i-1]*i;
// out.printf("T[%d]=%d\n",i,T[i]);
}
int qq = cin.nextInt();
// int qq = 1;
label:while (qq-- > 0) {
int n = cin.nextInt();
int m = cin.nextInt();
int [] dist = new int [n * m];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j ++){
dist[i * m + j] = Math.max(i,n - 1 - i) + Math.max(j,m - 1 - j);
}
}
Arrays.sort(dist);
for(int k = 0 ; k < n * m ;k++)out.printf("%d ",dist[k]);
out.println();
}
out.close();
}
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 lastMonthOfVacation) {
lastMonthOfVacation.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 lastMonthOfVacation) {
lastMonthOfVacation.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 17
|
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
|
8aba655faa0e3f784ff250f18095c3e2
|
train_109.jsonl
|
1642257300
|
You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with edges numbered from $$$1$$$ to $$$n-1$$$. A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path.Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: $$$2 \to 1 \to 3$$$ has a weight of $$$11 + 2 = 13$$$, which is prime. Similarly, the path of one edge: $$$4 \to 3$$$ has a weight of $$$5$$$, which is also prime. Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print $$$-1$$$. It can be proven that if a valid assignment exists, one exists with weights between $$$1$$$ and $$$10^5$$$ as well.
|
256 megabytes
|
//created by Toufique on 09/08/2022
import java.io.*;
import java.util.*;
public class NotAssigning {
static ArrayList<Integer>[] adj;
static ArrayList<Integer> ans;
static ArrayList<String> input;
static boolean[] vis;
static int[] parent;
static HashMap<String, Integer> m;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
m = new HashMap<>();
adj = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) adj[i] = new ArrayList<>();
vis = new boolean[n + 1];
input = new ArrayList<>();
parent = new int[n + 1];
for (int i = 0; i < n - 1; i++) {
int u = in.nextInt(), v = in.nextInt();
adj[u].add(v);
adj[v].add(u);
if (u < v)input.add(u + "&" + v);
else input.add(v + "&" + u);
}
solve(n);
for (int x: ans) pw.print(x + " ");
pw.println();
}
pw.close();
}
static void solve(int n) {
ans = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (adj[i].size() > 2) {
ans.add(-1);
return;
}
}
int root = -1;
for(int i = 1; i <= n; i++) if (adj[i].size() == 1) {
root = i;
break;
}
parent[root] = 2;
dfs(root);
for (int i = 0; i < input.size(); i++) ans.add(m.get(input.get(i)));
}
static void dfs(int u) {
vis[u] = true;
for (int v: adj[u]) {
if (!vis[v]) {
int p = parent[u];
// debug(u, v, parent[u]);
if (p == 2) {
parent[v] = 5;
String val = Math.min(u, v) + "&" + Math.max(u, v);
m.put(val, 5);
} else {
parent[v] = 2;
String val = Math.min(u, v) + "&" + Math.max(u, v);
m.put(val, 2);
}
dfs(v);
}
}
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
|
Java
|
["3\n2\n1 2\n4\n1 3\n4 3\n2 1\n7\n1 2\n1 3\n3 4\n3 5\n6 2\n7 2"]
|
1.5 seconds
|
["17\n2 5 11\n-1"]
|
NoteFor the first test case, there are only two paths having one edge each: $$$1 \to 2$$$ and $$$2 \to 1$$$, both having a weight of $$$17$$$, which is prime. The second test case is described in the statement.It can be proven that no such assignment exists for the third test case.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"dfs and similar",
"number theory",
"trees"
] |
0639fbeb3a5be67a4c0beeffe8f5d43b
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the number of vertices in the tree. Then, $$$n-1$$$ lines follow. The $$$i$$$-th line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$) denoting that edge number $$$i$$$ is between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
| 1,400
|
For each test case, if a valid assignment exists, then print a single line containing $$$n-1$$$ integers $$$a_1, a_2, \dots, a_{n-1}$$$ ($$$1 \leq a_i \le 10^5$$$), where $$$a_i$$$ denotes the weight assigned to the edge numbered $$$i$$$. Otherwise, print $$$-1$$$. If there are multiple solutions, you may print any.
|
standard output
| |
PASSED
|
238a3c71e0ffed93ad0f75f37898938e
|
train_109.jsonl
|
1642257300
|
You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with edges numbered from $$$1$$$ to $$$n-1$$$. A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path.Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: $$$2 \to 1 \to 3$$$ has a weight of $$$11 + 2 = 13$$$, which is prime. Similarly, the path of one edge: $$$4 \to 3$$$ has a weight of $$$5$$$, which is also prime. Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print $$$-1$$$. It can be proven that if a valid assignment exists, one exists with weights between $$$1$$$ and $$$10^5$$$ as well.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.sqrt;
import static java.lang.Math.floor;
public class topcoder {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
static int x = -1;
static int y = -1;
public static int first_search(TreeNode root, TreeNode main_root) {
if(root == null) return 0;
int a = first_search(root.left,main_root);
int b = first_search(root.right,main_root);
if(a > main_root.val)
x = a;
if(b < main_root.val) y = b;
return root.val;
}
public static void fix(TreeNode root, TreeNode main_root) {
if(root == null) return;
fix(root.left,main_root);
if(root.val > main_root.val) {
root.val = y;
}
fix(root.right,main_root);
if(root.val < main_root.val);
root.val = x;
}
public static int max(int []nums, int s, int e) {
int max = Integer.MIN_VALUE;
for(int i = s; i <= e; i++) {
max = Math.max(max, nums[i]);
}
return max;
}
public static TreeNode new_node(int []nums, int s, int e) {
int max = max(nums,s,e);
TreeNode node = new TreeNode(max);
return node;
}
public static TreeNode root;
public static void res(int []nums, int s, int e) {
if(s > e)return;
if(root == null) {
root = new_node(nums,s,e);
}
root.left = new_node(nums,s,e);
root.right = new_node(nums,s,e);
return;
}
static int depth(TreeNode root) {
if(root == null)return 0;
int a = 1+ depth(root.left);
int b = 1+ depth(root.right);
return Math.max(a,b);
}
static HashSet<Integer>set = new HashSet<>();
static void deepestLeaves(TreeNode root, int cur_depth, int depth) {
if(root == null)return;
if(cur_depth == depth)set.add(root.val);
deepestLeaves(root.left,cur_depth+1,depth);
deepestLeaves(root.right,cur_depth+1,depth);
}
public static void print(TreeNode root) {
if(root == null)return;
System.out.print(root.val+" ");
System.out.println("er");
print(root.left);
print(root.right);
}
public static HashSet<Integer>original(TreeNode root){
int d = depth(root);
deepestLeaves(root,0,d);
return set;
}
static HashSet<Integer>set1 = new HashSet<>();
static void leaves(TreeNode root) {
if(root == null)return;
if(root.left == null && root.right == null)set1.add(root.val);
leaves(root.left);
leaves(root.right);
}
public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) {
if(s.size() != s1.size())return false;
for(int a : s) {
if(!s1.contains(a))return false;
}
return true;
}
static TreeNode subTree;
public static void smallest_subTree(TreeNode root) {
if(root == null)return;
smallest_subTree(root.left);
smallest_subTree(root.right);
set1 = new HashSet<>();
leaves(root);
boolean smallest = check(set,set1);
if(smallest) {
subTree = root;
return;
}
}
public static TreeNode answer(TreeNode root) {
smallest_subTree(root);
return subTree;
}
}
static class Key<K1, K2>
{
public K1 key1;
public K2 key2;
public Key(K1 key1, K2 key2)
{
this.key1 = key1;
this.key2 = key2;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Key key = (Key) o;
if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) {
return false;
}
if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) {
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = key1 != null ? key1.hashCode() : 0;
result = 31 * result + (key2 != null ? key2.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[" + key1 + ", " + key2 + "]";
}
}
public static int sumOfDigits (long n) {
int sum = 0;
while(n > 0) {
sum += n%10;
n /= 10;
}
return sum;
}
public static void swap(int []ar, int i, int j) {
for(int k= j; k >= i; k--) {
int temp = ar[k];
ar[k] = ar[k+1];
ar[k+1] = temp;
}
}
public static int findOr(int[]bits){
int or=0;
for(int i=0;i<32;i++){
or=or<<1;
if(bits[i]>0)
or=or+1;
}
return or;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
// method to return LCM of two numbers
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static boolean isPrime(long 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+= 6) {
if(n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static long nextPrime(long n) {
boolean found = false;
long prime = n;
while(!found) {
prime++;
if(isPrime(prime))
found = true;
}
return prime;
}
static int countGreater(int arr[], int n, int k){
int l = 0;
int r = n - 1;
// Stores the index of the left most element
// from the array which is greater than k
int leftGreater = n;
// Finds number of elements greater than k
while (l <= r) {
int m = l + (r - l) / 2;
// If mid element is greater than
// k update leftGreater and r
if (arr[m] > k) {
leftGreater = m;
r = m - 1;
}
// If mid element is less than
// or equal to k update l
else
l = m + 1;
}
// Return the count of elements greater than k
return (n - leftGreater);
}
static ArrayList<Integer>printDivisors(int n){
ArrayList<Integer>list = new ArrayList<>();
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
list.add(i);
else // Otherwise print both
list.add(i);
list.add(n/i);
}
}
return list;
}
static void leftRotate(int l, int r,int arr[], int d)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(l,r,arr);
}
static void leftRotatebyOne(int l, int r,int arr[])
{
int i, temp;
temp = arr[l];
for (i = l; i < r; i++)
arr[i] = arr[i + 1];
arr[r] = temp;
}
static long modInverse(long a, long m)
{
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
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;
}
static TreeNode buildTree(TreeNode root,int []ar, int l, int r){
if(l > r)return null;
int len = l+r;
if(len%2 != 0)len++;
int mid = (len)/2;
int v = ar[mid];
TreeNode temp = new TreeNode(v);
root = temp;
root.left = buildTree(root.left,ar,l,mid-1);
root.right = buildTree(root.right,ar,mid+1,r);
return root;
}
public static int getClosest(int val1, int val2, int target) {
if (target - val1 >= val2 - target)
return val2;
else
return val1;
}
public static int findClosest(int start, int end, int arr[], int target)
{
int n = arr.length;
// Corner cases
if (target <= arr[start])
return arr[start];
if (target >= arr[end])
return arr[end];
// Doing binary search
int i = start, j = end+1, mid = 0;
while (i < j) {
mid = (i + j) / 2;
if (arr[mid] == target)
return arr[mid];
/* If target is less than array element,
then search in left */
if (target < arr[mid]) {
// If target is greater than previous
// to mid, return closest of two
if (mid > 0 && target > arr[mid - 1])
return getClosest(arr[mid - 1],
arr[mid], target);
/* Repeat for left half */
j = mid;
}
// If target is greater than mid
else {
if (mid < n-1 && target < arr[mid + 1])
return getClosest(arr[mid],
arr[mid + 1], target);
i = mid + 1; // update i
}
}
// Only single element left after search
return arr[mid];
}
static int LIS(int arr[], int n)
{
int lis[] = new int[n];
int i, j, max = 0;
/* Initialize LIS values for all indexes */
for (i = 0; i < n; i++)
lis[i] = 1;
/* Compute optimized LIS values in
bottom up manner */
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] >= arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
/* Pick maximum of all LIS values */
for (i = 0; i < n; i++)
if (max < lis[i])
max = lis[i];
return max;
}
static void permuteString(String s , String answer)
{
if (s.length() == 0)
{
System.out.print(answer + " ");
return;
}
for(int i = 0 ;i < s.length(); i++)
{
char ch = s.charAt(i);
String left_substr = s.substring(0, i);
String right_substr = s.substring(i + 1);
String rest = left_substr + right_substr;
permuteString(rest, answer + ch);
}
}
static boolean isPowerOfTwo(long n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static class Pair {
int x;
int y;
// Constructor
Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
static class Compare1{
static void compare(Pair arr[], int n)
{
// 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.x - p2.x;
}
});
}
}
static int min;
static int max;
static LinkedList<Integer>[]adj;
static int n;
static void insertlist(int n) {
for(int i = 0; i <= n; i++) {
adj[i] = new LinkedList<>();
}
}
static boolean []vis;
static int []ar;
static HashMap<Key,Integer>map;
static void dfs(int parent, int child, int prev,LinkedList<Integer>[]adj) {
if(child != -1 && prev != -1) {
int v = map.get(new Key(prev,child));
if(v == 5) {
map.put(new Key(child,parent), 2);
}else {
map.put(new Key(child,parent), 5);
}
}else if(child != -1){
map.put(new Key(child,parent),5);
}
for(int a : adj[parent]) {
if(a != child) {
dfs(a,parent,child,adj);
}
}
}
static StringTokenizer st;
static BufferedReader ob;
static int [] readarrayInt( int n)throws IOException {
st = new StringTokenizer(ob.readLine());
int []ar = new int[n];
for(int i = 0; i < n; i++) {
ar[i] = Integer.parseInt(st.nextToken());
}
return ar;
}
static long [] readarrayLong( int n)throws IOException {
st = new StringTokenizer(ob.readLine());
long []ar = new long[n];
for(int i = 0; i < n; i++) {
ar[i] = Long.parseLong(st.nextToken());
}
return ar;
}
static int readInt()throws IOException {
return Integer.parseInt(ob.readLine());
}
static long readLong()throws IOException {
return Long.parseLong(ob.readLine());
}
static int nextTokenInt() {
return Integer.parseInt(st.nextToken());
}
static long nextTokenLong() {
return Long.parseLong(st.nextToken());
}
static Pair []pairar;
static void addEdges(int m)throws IOException {
for(int i = 0; i < m; i++) {
st = new StringTokenizer(ob.readLine());
int u = nextTokenInt();
int v = nextTokenInt();
pairar[i] = new Pair(u,v);
adj[u].add(v);
adj[v].add(u);
}
}
public static void main(String args[])throws IOException {
ob = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = readInt();
while(t --> 0) {
int n = readInt();
adj = new LinkedList[n+1];
insertlist(n);
pairar = new Pair[n];
map = new HashMap<>();
addEdges(n-1);
boolean pos = true;
int start = 1;
for(int i = 1; i <= n; i++) {
if(adj[i].size() >= 3) {
pos = false;
break;
}else if(adj[i].size() == 1) {
start = i;
}
}
dfs(start,-1,-1,adj);
if(!pos) {
System.out.println(-1);
}else {
for(int i = 0; i < n-1; i++) {
if(map.containsKey(new Key(pairar[i].x,pairar[i].y))) {
System.out.print(map.get(new Key(pairar[i].x,pairar[i].y))+" ");
}else if(map.containsKey(new Key(pairar[i].y,pairar[i].x))) {
System.out.print(map.get(new Key(pairar[i].y,pairar[i].x))+" ");
}
}
System.out.println();
}
}
}
}
|
Java
|
["3\n2\n1 2\n4\n1 3\n4 3\n2 1\n7\n1 2\n1 3\n3 4\n3 5\n6 2\n7 2"]
|
1.5 seconds
|
["17\n2 5 11\n-1"]
|
NoteFor the first test case, there are only two paths having one edge each: $$$1 \to 2$$$ and $$$2 \to 1$$$, both having a weight of $$$17$$$, which is prime. The second test case is described in the statement.It can be proven that no such assignment exists for the third test case.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"dfs and similar",
"number theory",
"trees"
] |
0639fbeb3a5be67a4c0beeffe8f5d43b
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the number of vertices in the tree. Then, $$$n-1$$$ lines follow. The $$$i$$$-th line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$) denoting that edge number $$$i$$$ is between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
| 1,400
|
For each test case, if a valid assignment exists, then print a single line containing $$$n-1$$$ integers $$$a_1, a_2, \dots, a_{n-1}$$$ ($$$1 \leq a_i \le 10^5$$$), where $$$a_i$$$ denotes the weight assigned to the edge numbered $$$i$$$. Otherwise, print $$$-1$$$. If there are multiple solutions, you may print any.
|
standard output
| |
PASSED
|
f523b17c5fdf1f8bee64ab4db9406643
|
train_109.jsonl
|
1642257300
|
You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with edges numbered from $$$1$$$ to $$$n-1$$$. A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path.Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: $$$2 \to 1 \to 3$$$ has a weight of $$$11 + 2 = 13$$$, which is prime. Similarly, the path of one edge: $$$4 \to 3$$$ has a weight of $$$5$$$, which is also prime. Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print $$$-1$$$. It can be proven that if a valid assignment exists, one exists with weights between $$$1$$$ and $$$10^5$$$ as well.
|
256 megabytes
|
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.atomic.LongAccumulator;
import javax.management.openmbean.ArrayType;
public class Main {
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O usaing short named function ---------*/
public static String ns() {
return scan.next();
}
public static int ni() {
return scan.nextInt();
}
public static long nl() {
return scan.nextLong();
}
public static double nd() {
return scan.nextDouble();
}
public static String nln() {
return scan.nextLine();
}
public static void p(Object o) {
out.print(o);
}
public static void ps(Object o) {
out.print(o + " ");
}
public static void pn(Object o) {
out.println(o);
}
/*-------- for output of an array ---------------------*/
static void iPA(int arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
static void lPA(long arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
static void sPA(String arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
static void dPA(double arr[]) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < arr.length; i++) output.append(arr[i] + " ");
out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = ni();
}
static void lIA(long arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = nl();
}
static void sIA(String arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = ns();
}
static void dIA(double arr[]) {
for (int i = 0; i < arr.length; i++) arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
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;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo(int x) {
return x != 0 && ((x & (x - 1)) == 0);
}
//Method to return lcm of two numbers
static int gcd(int a, int b) {
return a == 0 ? b : gcd(b % a, a);
}
//Method to count digit of a number
static int countDigit(long n) {
return (int) Math.floor(Math.log10(n) + 1);
}
//Method for sorting
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);
}
//Method for checking if a number is prime or not
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6) if (
n % i == 0 || n % (i + 2) == 0
) return false;
return true;
}
public static void reverse(int a[], int n) {
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
// printing the reversed array
System.out.println("Reversed array is: \n");
for (k = 0; k < n; k++) {
System.out.println(a[k]);
}
}
public static int binarysearch(int arr[], int left, int right, int num) {
int idx = 0;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] >= num) {
idx = mid;
// if(arr[mid]==num)break;
right = mid - 1;
} else {
left = mid + 1;
}
}
return idx;
}
public static int mod = 1000000007;
public static int[] rank;
public static void main(String[] args) throws java.lang.Exception {
OutputStream outputStream = System.out;
out = new PrintWriter(outputStream);
scan = new FastReader();
int T = ni();
while (T-- > 0) {
int n = ni();
ArrayList<Integer>[] al = new ArrayList[n];
for (int i = 0; i < n; i++) {
al[i] = new ArrayList<Integer>();
}
int arr1[]=new int[n];
int arr2[]=new int[n];
HashMap<Integer,ArrayList> map=new HashMap<>();
HashMap<String,Integer> set=new HashMap<>();
for(int i=0;i<n-1;i++){
int a=ni()-1;
int b=ni()-1;
al[a].add(b);
al[b].add(a);
arr1[i]=a;
arr2[i]=b;
}
int chk=0;
for(int i=0;i<n;i++){
if(al[i].size()>2){
System.out.println("-1");
chk=-1;
break;
}
}
if(chk==0){
int j=0;
for(int i=0;i<n;i++){
if(al[i].size()==1){
j=i;
break;
}
}
int p=2;
// System.out.print(2+" ");
set.put(j+"@"+al[j].get(0),3);
set.put(al[j].get(0)+"@"+j,3);
int last=j;
j=al[j].get(0);
for(int i=0;i<n-1;i++){
for(int k=0;k<al[j].size();k++){
if(al[j].get(k)!=last){
set.put(j+"@"+al[j].get(k),p);
set.put(al[j].get(k)+"@"+j,p);
if(p==3)p=2;
else p=3;
// System.out.println(last+" "+j);
last=j;
j=al[j].get(k);
// System.out.println(last+" aftee "+j+" "+p);
}
}
}
// System.out.println(set);
// set.put(j+"@"+last,p);
// set.put(last+"@"+j,p);
for(int i=0;i<n-1;i++){
System.out.print(set.get(arr1[i]+"@"+arr2[i])+" ");
}
System.out.println();
}
}
out.flush();
out.close();
}
public static long power(int a, long b) {
long ans = 1;
while (b > 0) {
if (b % 2 == 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return ans;
}
public static class pair implements Comparable<pair> {
int x;
int y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
if (this.x == o.x) {
return this.y - o.y;
} else {
return this.x - o.x;
}
}
}
}
|
Java
|
["3\n2\n1 2\n4\n1 3\n4 3\n2 1\n7\n1 2\n1 3\n3 4\n3 5\n6 2\n7 2"]
|
1.5 seconds
|
["17\n2 5 11\n-1"]
|
NoteFor the first test case, there are only two paths having one edge each: $$$1 \to 2$$$ and $$$2 \to 1$$$, both having a weight of $$$17$$$, which is prime. The second test case is described in the statement.It can be proven that no such assignment exists for the third test case.
|
Java 8
|
standard input
|
[
"constructive algorithms",
"dfs and similar",
"number theory",
"trees"
] |
0639fbeb3a5be67a4c0beeffe8f5d43b
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the number of vertices in the tree. Then, $$$n-1$$$ lines follow. The $$$i$$$-th line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$) denoting that edge number $$$i$$$ is between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
| 1,400
|
For each test case, if a valid assignment exists, then print a single line containing $$$n-1$$$ integers $$$a_1, a_2, \dots, a_{n-1}$$$ ($$$1 \leq a_i \le 10^5$$$), where $$$a_i$$$ denotes the weight assigned to the edge numbered $$$i$$$. Otherwise, print $$$-1$$$. If there are multiple solutions, you may print any.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.