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
|
b56243ec71c1742472e05981dc5b1b75
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Apple {
public static void main(String args[]) {
try {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
System.out.println();
while (T > 0) {
int n = sc.nextInt();
long x[] = new long[n];
long y[] = new long[n];
boolean flag = true;
for (int i = 0; i < n; i++) {
x[i] = sc.nextLong();
y[i] = sc.nextLong();
}
Arrays.sort(x);
Arrays.sort(y);
if (n % 2 == 1) {
System.out.println(1);
} else {
System.out.println((x[n / 2] - x[n / 2 - 1] + 1) * (y[n / 2] - y[n / 2 - 1] + 1));
}
T--;
}
} catch (
Exception e) {
return;
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
c32d1d1258d5db322af13e637f6a467a
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Codeforces {
public void solve() {
FastScanner fs = new FastScanner();
StringBuilder print = new StringBuilder();
int t = fs.nextInt();
while (t-- > 0 ) {
int n = fs.nextInt();
int[]x = new int[n];
int[]y = new int[n];
for(int i=0;i<n;i++){
x[i] = fs.nextInt();
y[i] = fs.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2 == 1){
print.append("1\n");
}else{
int mid = n/2;
long ans = (x[mid]-x[mid-1]+1)*(long)(y[mid]-y[mid-1]+1);
print.append(ans).append("\n");
}
}
System.out.println(print);
}
public static void main(String[]args){
try{
new Codeforces().solve();
}catch (Exception e){
e.printStackTrace();
}
}
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();
}
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());
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
edae821808effd68d66d86f6256a6736
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
public class p1486B {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(int t=sc.nextInt();t-->0;) {
int n=sc.nextInt(),x[]=new int[n],y[]=new int[n];
for(int i=0;i<n;i++) {x[i]=sc.nextInt();y[i]=sc.nextInt();}
Arrays.sort(x);
Arrays.sort(y);
System.out.println(n%2==1?1:(x[n/2]-x[n/2-1]+1L)*(y[n/2]-y[n/2-1]+1));
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
6eacf05931e22bbaeef6638979e7f569
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
public class p1486B {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(int t=sc.nextInt();t-->0;) {
int n=sc.nextInt(),x[]=new int[n],y[]=new int[n];
for(int i=0;i<n;i++) {x[i]=sc.nextInt();y[i]=sc.nextInt();}
Arrays.sort(x);
Arrays.sort(y);
System.out.println(n%2==1?1:(x[n/2]-x[n/2-1]+1)*(long)(y[n/2]-y[n/2-1]+1));
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
88a936d13a274090f7097463c266c43b
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
public class temp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q-->0){
int n = sc.nextInt();
long[] x = new long[n];
long[] y = new long[n];
for(int i= 0;i<n;i++){
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2!=0){
System.out.println("1");
}
else{
long x1, x2;
long y1, y2;
x1 = x[(n/2)-1];
x2 = x[n/2];
y1 = y[(n/2)-1];
y2 = y[n/2];
System.out.println((x2-x1+1)*(y2-y1+1));
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
40be46ed1752b140503ce5552084e363
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class EasternExhibition implements Closeable {
private InputReader in = new InputReader(System.in);
private PrintWriter out = new PrintWriter(System.out);
public void solve() {
int t = in.ni();
while (t-- > 0) {
int n = in.ni();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.ni();
y[i] = in.ni();
}
Arrays.sort(x);
Arrays.sort(y);
long h, v;
if (n == 1) {
h = 1;
v = 1;
} else if (n == 2) {
h = x[1] - x[0] + 1;
v = y[1] - y[0] + 1;
} else {
if (n % 2 == 0) {
h = x[n / 2] - x[n / 2 - 1] + 1;
v = y[n / 2] - y[n / 2 - 1] + 1;
} else {
h = v = 1;
}
}
out.println(h * v);
}
}
@Override
public void close() throws IOException {
in.close();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public void close() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws IOException {
try (EasternExhibition instance = new EasternExhibition()) {
instance.solve();
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
2ae91bb642f5cb7a39c8c08f14be674d
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class EasternExhibition {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t!=0){
solve(br,pr);
t--;
}
pr.flush();
pr.close();
}
public static void solve(BufferedReader br,PrintWriter pr) throws IOException{
int n=Integer.parseInt(br.readLine());
int[] xS=new int[n];
int[] yS=new int[n];
for(int i=0;i<n;i++){
String[] temp=br.readLine().split(" ");
int x=Integer.parseInt(temp[0]);
int y=Integer.parseInt(temp[1]);
xS[i]=x;
yS[i]=y;
}
shuffleArray(xS);
shuffleArray(yS);
Arrays.sort(xS);
Arrays.sort(yS);
if(n%2==0){
int mid=n/2;
int countX=xS[mid]-xS[mid-1]+1;
int countY=yS[mid]-yS[mid-1]+1;
pr.println((long)countX*(long)countY);
return;
}
else{
pr.println(1);
}
}
public static void shuffleArray(int[] arr)
{
Random rnd = ThreadLocalRandom.current();
for (int i = arr.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
if(index!=i){
arr[index]^=arr[i];
arr[i]^=arr[index];
arr[index]^=arr[i];
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
11355f2be3142776427f3b8504bf265f
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
public class code {
// static class pair implements Comparable<pair>{
// int x;int y;
//
// public pair(int x,int y){
// this.x=x;
// this.y=y;
// }
//
//
// @Override
// public int compareTo(pair o) {
// return this.x-o.x;
// }
// }
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() {
int n=sc.nextInt();
ArrayList<Integer> ax=new ArrayList<>();
ArrayList<Integer> ay=new ArrayList<>();
for (int i = 0; i < n; i++) {
ax.add(sc.nextInt());
ay.add(sc.nextInt());
}
Collections.sort(ax);
Collections.sort(ay);
long x=0;
long y=0;
x=(ax.get(ax.size()/2)-ax.get((ax.size()-1)/2))+1;
y=(ay.get(ay.size()/2)-ay.get((ay.size()-1)/2))+1;
System.out.println(x*y);
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
9faf6a4c18fae4e3f1b25003b43c9dc1
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0){
int n=sc.nextInt();
int[] a=new int[n],b=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
b[i]=sc.nextInt();
}
if(n%2!=0){
System.out.println(1);continue;
}
Arrays.sort(a);
Arrays.sort(b);
System.out.println((long)(a[n/2]-a[n/2-1]+1)*(b[n/2]-b[n/2-1]+1));
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
2feeec9d242ee884a3cdf31439bc3691
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while (t-->0){
int n=sc.nextInt();
int[] x=new int[n+1];
int[] y=new int[n+1];
for (int i = 1; i <=n; i++) {
x[i]=sc.nextInt();
y[i]=sc.nextInt();
}
if (n%2!=0){
System.out.println(1);
continue;
}
Arrays.sort(x,1,n+1);
Arrays.sort(y,1,n+1);
long a=x[n/2+1]-x[n/2]+1;
long b=y[n/2+1]-y[n/2]+1;
System.out.println(a*b);
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
deeca06416ba09d1c43d525c36e1a0e6
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Exibition{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static long sol(ArrayList<Integer> ar){
int n = ar.size();
Collections.sort(ar);
return ar.get(n/2)-ar.get((n-1)/2)+1;
}
static void solve() {
int n = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
for(int i = 0;i<n;i++){
a.add(sc.nextInt());
b.add(sc.nextInt());
}
out.println(sol(a)*sol(b));
}
static class Pair implements Comparable<Pair>{
int x;
int y;
Pair(int n,int f){
x = n;
y = f;
}
public int compareTo(Pair p){
return this.x - p.y;
}
}
static void reverse(int arr[]){
int i = 0;int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
// solve2();
// solve3();
}
// 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[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
52b775551354fd6e1a946e35b272c313
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class GFG {
public static void main (String[] args) {
Scanner a= new Scanner(System.in);
int t=a.nextInt();
for(int k=0;k<t;k++){
int n=a.nextInt();
int x[]=new int[n];
int y[]=new int[n];
for(int i=0;i<n;i++){
x[i]=a.nextInt();
y[i]=a.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2!=0){
System.out.println(1);
continue;
}
long p=x[n/2]-x[n/2-1]+1;
long q=y[n/2]-y[n/2-1]+1;
long ans=p*q;
System.out.println(ans);
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
7df114b93aaed0c89d60221ce0eec0d7
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f;
public static int mod = 1000000007;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
FastScanner sc = new FastScanner();
boolean multiTest = true;
if(multiTest) {
int t = sc.nextInt(), loop = 0;
while (loop < t) {
loop++;
solve(o, sc);
}
} else solve(o, sc);
o.close();
} catch (Exception e){}
}
static void solve(PrintWriter o, FastScanner sc){
try {
int n = sc.nextInt();
int[] x_li = new int[n];
int[] y_li = new int[n];
for(int i=0;i<n;i++){
x_li[i] = sc.nextInt();
y_li[i] = sc.nextInt();
}
Arrays.sort(x_li);
Arrays.sort(y_li);
if(n%2 == 1){
o.println(1);
}
else{
o.println(1l * (x_li[n/2] - x_li[n/2-1] + 1) * (y_li[n/2] - y_li[n/2-1] + 1));
}
} catch (Exception e){
e.printStackTrace();
}
}
public static int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static boolean doubleEqual(double d1, double d2){
if(Math.abs(d1-d2) < 1e-6){
return true;
}
return false;
}
public static long fac(int n){
long ret = 1;
while(n > 0){
ret = ret * n % mod;
n--;
}
return ret;
}
public static long qpow(int n, int m){
long n_ = n, ret = 1;
while(m > 0){
if((m&1) == 1){
ret = ret * n_ % mod;
}
m >>= 1;
n_ = n_ * n_ % mod;
}
return ret;
}
static class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() { return Double.parseDouble(next());}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException{
return Integer.parseInt(next());
}
public static Long nextLong() throws IOException{
return Long.parseLong(next());
}
public static double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public static String nextLine() throws IOException{
return reader.readLine();
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
a4843aab6422a4e9a8050bc00320527e
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class CFp7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
main:
while (t-- > 0) {
int n = in.nextInt();
long[] xs = new long[n];
long[] ys = new long[n];
for (int i = 0; i < n; i++) {
xs[i] = in.nextInt();
ys[i] = in.nextInt();
}
Arrays.sort(xs);
Arrays.sort(ys);
//int out = 1;
//for (int i = 0; i < 2; i++) {
// out *= positions[i][n / 2] - positions[i][(n - 1) / 2] + 1;
//}
System.out.println((xs[n / 2] - xs[(n - 1) / 2] + 1) * (ys[n / 2] - ys[(n - 1) / 2] + 1));
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
9c7d469ce1a358d39615baca1bd7e095
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
// package PracticeSessionNTSCC;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class EasternExhibition {
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();
ArrayList<Integer>x = new ArrayList<>();
ArrayList<Integer>y = new ArrayList<>();
for(int i =0;i<n;i++){
x.add(sc.nextInt());
y.add(sc.nextInt());
}
Collections.sort(x);
Collections.sort(y);
if(n%2==0){
int tmp = n/2 -1;
int tmp2 = n/2;
pw.println((x.get(tmp)-(x.get(tmp2)+1))*1l*((y.get(tmp)-(y.get(tmp2)+1))));
}else{
pw.println(1);
}
}
pw.close();
}
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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
66feaa26a4789fb1b55266e4b4659ca9
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class _1486b {
FastScanner scn;
PrintWriter w;
PrintStream fs;
long MOD = 1000000007;
int N=100000+10;
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(long[] a) {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);}
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();
long[] x=new long[n];
long[] y=new long[n];
for(int i=0;i<n;i++){
x[i]=scn.nextLong();
y[i]=scn.nextLong();
}
if((n&1)!=0){
w.println(1);
}else{
ruffleSort(x);
ruffleSort(y);
long ans=(x[n/2]-x[n/2-1]+1)*(y[n/2]-y[n/2-1]+1);
w.println(ans);
}
}
}
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());
}
}
public static void main(String[] args) {
new _1486b().runIO();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
1d816c9016018e2a5bd8389a3bf1e730
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
// import java.lang.*;
// import java.math.*;
public class Codeforces {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=1000000007;
static long mod1=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int t=I();
while(t-->0)
{
int n=I();
ArrayList<Integer> arr1=new ArrayList<>();
ArrayList<Integer> arr2=new ArrayList<>();
for(int i=0;i<n;i++){
int x=I(),y=I();
arr1.add(x);
arr2.add(y);
}
Collections.sort(arr1);
Collections.sort(arr2);
if(n%2==1){
out.println("1");
}else{
long x=(arr1.get(arr1.size()/2))-(arr1.get(arr1.size()/2-1))+1;
long y=(arr2.get(arr2.size()/2))-(arr2.get(arr2.size()/2-1))+1;
out.println(x*y);
}
}
out.close();
}
public static class pair
{
int a;
int b;
public pair(int val,int index)
{
a=val;
b=index;
}
}
public static class myComp implements Comparator<pair>
{
//sort in ascending order.
public int compare(pair p1,pair p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
//sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static long fact(long n)
{
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n)
{
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long kadane(long a[],int n)
{
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static void DFS(ArrayList<Integer> arr[],int s,boolean visited[])
{
visited[s]=true;
for(int i:arr[s]){
if(!visited[i]){
DFS(arr,i,visited);
}
}
}
public static int BS(int a[],int x,int ii,int jj)
{
// int n=a.length;
int mid=0;
int i=ii,j=jj,in=0;
while(i<=j)
{
mid=(i+j)/2;
if(a[mid]<x){
in=mid+1;
i=mid+1;
}
else
j=mid-1;
}
return in;
}
public static int lower_bound(int arr[], int N, int X)
{
int mid;
int low = 0;
int high = N;
while(low<high) {
mid=low+(high-low)/2;
if(X<=arr[mid]){
high=mid;
}
else{
low=mid+1;
}
}
if(low<N && arr[low]<X){
low++;
}
out.println(arr[low]);
return low;
}
public static ArrayList<Integer> primeSieve(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
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;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree
{
int farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new int[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x,int p)
{
for(;x<=n;x+=x&(-x))
{
farr[x]+=p;
}
}
public long get(int x)
{
long ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
public static class DSU
{
static int par[],rank[];
public DSU(int c)
{
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public static int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public static void union(int a,int b)
{
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
//SEGMENT TREE CODE
// public static void segmentUpdate(int si,int ss,int se,int qs,int qe,long x)
// {
// if(ss>qe || se<qs)return;
// if(qs<=ss && qe>=se)
// {
// seg[si][0]+=1L;
// seg[si][1]+=x*x;
// seg[si][2]+=2*x;
// return;
// }
// int mid=(ss+se)/2;
// segmentUpdate(2*si+1,ss,mid,qs,qe,x);
// segmentUpdate(2*si+2,mid+1,se,qs,qe,x);
// }
// public static long segmentGet(int si,int ss,int se,int x,long f,long s,long t,long a[])
// {
// if(ss==se && ss==x)
// {
// f+=seg[si][0];
// s+=seg[si][1];
// t+=seg[si][2];
// long ans=a[x]+(f*((long)x+1L)*((long)x+1L))+s+(t*((long)x+1L));
// return ans;
// }
// int mid=(ss+se)/2;
// if(x>mid){
// return segmentGet(2*si+2,mid+1,se,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a);
// }else{
// return segmentGet(2*si+1,ss,mid,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a);
// }
// }
public static class myComp1 implements Comparator<pair1>
{
//sort in ascending order.
public int compare(pair1 p1,pair1 p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
//sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static class pair1
{
long a;
long b;
public pair1(long val,long index)
{
a=val;
b=index;
}
}
public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr)
{
//****************use this in main function-Collections.sort(arr,new myComp1());
ArrayList<pair1> a1=new ArrayList<>();
if(arr.size()<=1)
return arr;
a1.add(arr.get(0));
int i=1,j=0;
while(i<arr.size())
{
if(a1.get(j).b<arr.get(i).a)
{
a1.add(arr.get(i));
i++;
j++;
}
else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b)
{
i++;
}
else if(a1.get(j).b>=arr.get(i).a)
{
long a=a1.get(j).a;
long b=arr.get(i).b;
a1.remove(j);
a1.add(new pair1(a,b));
i++;
}
}
return a1;
}
public static boolean isPalindrome(String s,int n)
{
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static boolean isPrime(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;
}
public static boolean isPrime(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;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static void printArray(long a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(long a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArrayL(ArrayList<Long> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printArrayI(ArrayList<Integer> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapInt(HashMap<Integer,Integer> hm){
for(Map.Entry<Integer,Integer> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMapLong(HashMap<Long,Long> hm){
for(Map.Entry<Long,Long> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static long pwr(long m,long n)
{
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public 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);
}
public 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);
}
public static int I(){return sc.I();}
public static long L(){return sc.L();}
public static String S(){return sc.S();}
public static double D(){return sc.D();}
}
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 I(){
return Integer.parseInt(next());
}
long L(){
return Long.parseLong(next());
}
double D(){
return Double.parseDouble(next());
}
String S(){
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
545355a3939de842d9385d00380aa6ae
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static String readLine() throws IOException {
return br.readLine();
}
static PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readChar() throws IOException {
return next().charAt(0);
}
static class Pair implements Comparable<Pair> {
int f, s;
Pair(int f, int s) {
this.f = f; this.s = s;
}
public int compareTo(Pair other) {
if (this.f != other.f) return this.f - other.f;
return this.s - other.s;
}
}
static long get(int n, int x[]) {
Arrays.sort(x);
if ((n&1) == 0) return x[(n>>1) + 1] - x[n>>1] + 1;
return 1;
}
static void solve() throws IOException {
int n = readInt(), x[] = new int[n + 1], y[] = new int[n + 1];
for (int i = 1; i <= n; ++i) {
x[i] = readInt();
y[i] = readInt();
}
pr.println(get(n, x)*get(n, y));
}
public static void main(String[] args) throws IOException {
for (int t = readInt(); t > 0; --t) solve();
pr.close();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
2e53aa33e578e89a6232550bde6038b7
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
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 void ln(String s){
System.out.println(s);
}
static int abs(int a,int b){
if(b>a) return b-a;
return a-b;
}
public static char nextChar(char curr) {
if(curr == 'z') return 'a';
else return (char)((int)curr+1);
}
public static int factorial(int i) {
int res = 1;
while(i>0) res*=i--;
return res;
}
static HashSet<Long> cache = new HashSet<>();
public static void main (String[] args) throws java.lang.Exception
{
FastReader fs = new FastReader();
int t = fs.nextInt();
while(t-->0) {
int n = fs.nextInt();
long[] x = new long[n];
long[] y = new long[n];
for (int i = 0; i < y.length; i++) {
x[i] = fs.nextInt();
y[i] = fs.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
long xt = x[(n)/2] - x[(n-1)/2] +1;
long yt = y[(n)/2] - y[(n-1)/2] +1;
System.out.println(xt*yt);
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
69b17429dc5ce84d83b015ecfc3715cd
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split(" ");
int numTestCases = Integer.parseInt(input[0]);
while(numTestCases-- > 0){
input = br.readLine().trim().split(" ");
int n = Integer.parseInt(input[0]);
ArrayList<Integer> xCoordinates = new ArrayList<>();
ArrayList<Integer> yCoordinates = new ArrayList<>();
for(int i = 0; i < n; i++){
input = br.readLine().trim().split(" ");
xCoordinates.add(Integer.parseInt(input[0]));
yCoordinates.add(Integer.parseInt(input[1]));
}
out.println(numPoints(xCoordinates, yCoordinates));
}
out.flush();
out.close();
}
public static long numPoints(ArrayList<Integer> xCoordinates, ArrayList<Integer> yCoordinates)
{
int n = xCoordinates.size();
if(n % 2 != 0){
return 1;
}
Collections.sort(xCoordinates);
Collections.sort(yCoordinates);
int xMedianDiff = xCoordinates.get((n / 2)) - xCoordinates.get((n / 2) - 1) + 1;
int yMedianDiff = yCoordinates.get((n / 2)) - yCoordinates.get((n / 2) - 1) + 1;
long ans = 1L * xMedianDiff * yMedianDiff;
return ans;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
e0ee962fe5b98b51adb16d8a4854dbfe
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.IOException;
import java.util.Scanner;
public class solve {
static Scanner sc=new Scanner(System.in);
public static void solve() {
int n=sc.nextInt(),x[]=new int[n],y[]=new int[n];
for(int i=0;i<n;i++) {x[i]=sc.nextInt(); y[i]=sc.nextInt();}
Arrays.sort(x);
Arrays.sort(y);
System.out.println(n%2==1 ? 1: (x[n/2]-x[n/2-1]+1L)*(y[n/2]-y[n/2-1]+1L));
}
public static void main(String[] args) throws Exception{
int t=sc.nextInt();
while(t-->0) {
solve();
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
faff837ced017f4a780bfbcc721f02e5
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static PrintWriter out;
public static void main(String[] args) {
MyScanner scan = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int testCases = scan.nextInt();
for(int i = 1; i <= testCases; i++){
int numberHouses = scan.nextInt();
int[] xPos = new int[numberHouses];
int[] yPos = new int[numberHouses];
for(int k = 0; k < numberHouses; k++){
xPos[k] = scan.nextInt();
yPos[k] = scan.nextInt();
}
Arrays.sort(xPos);
Arrays.sort(yPos);
int leftBound = xPos[(numberHouses - 1)/2];
int rightBound = xPos[numberHouses/2];
int lowerBound = yPos[(numberHouses - 1)/2];
int upperBound = yPos[numberHouses/2];
out.println((long)(rightBound-leftBound + 1) * (long)(upperBound-lowerBound + 1));
}
out.close();
}
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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
7476ddb59fb619622f0cbf65e74abc11
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.math.BigInteger;
import static java.lang.Math.max;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.TreeMap;
import java.util.Vector;
import java.util.Scanner;
public class ahh {
//trihund
static Scanner scn = new Scanner(System.in);
public static int a;
int k = 10;
public static void main(String[] args) {
// TODO Auto-generated method stub
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
long sum = 0, ans = 0;
long arr[] = new long[n],brr[]=new long[n];
for (int i = 0; i < n; i++) {
arr[i] = scn.nextLong();
brr[i]=scn.nextLong();
}
Arrays.parallelSort(arr);
Arrays.parallelSort(brr);
if(n%2==0)
{
int m=n/2,o=n/2-1;
//System.out.println(m+" "+o);
ans=(arr[m]-arr[o]+1)*(brr[m]-brr[o]+1);
System.out.println(ans);
}
else
System.out.println("1");
}
}
public static void fac(int n) {
BigInteger b = new BigInteger("1");
for (int i = 1; i <= n; i++) {
b = b.multiply(BigInteger.valueOf(i));
}
System.out.println(b);
}
public static void q2() {
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt(), k = scn.nextInt();
}
}
static void ruffleSort(long[] a) {
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);
}
public static long gcd(long a, long b) {
while (b > 0) {
long c = a % b;
a = b;
b = c;
}
return a;
}
}
class p {
long x, y;
p(long a, long b) {
this.x = a;
this.y = b;
}
}
class pair {
int x, y;
pair(int a, int b) {
this.x = a;
this.y = b;
}
public int hashCode() {
return x * 31 + y * 31;
}
public boolean equals(Object other) {
if (this == other)
return true;
if (other instanceof pair) {
pair pt = (pair) other;
return pt.x == this.x && pt.y == this.y;
} else
return false;
}
}
class sort implements Comparator<p> {
@Override
public int compare(p o1, p o2) {
// TODO Auto-generated method stub
long a = o1.x - o2.x, b = o1.y - o2.y;
if (b < 0)
return -1;
else if (a == 0) {
if (a < 0)
return -1;
else
return 1;
} else
return 1;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
8cd5fcfa352a75f92ce53118e67f7ffd
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
private static FastScanner sc = new FastScanner();
private static long mod = 1000000000;
private static HashMap<Long, Long> h = new HashMap<>();
public static void main(String[] args) {
int t = 0;
if (sc.hasNext())
t = sc.nextInt();
for (int i = 1; i <= t; i++) {
solve();
}
}
public static void solve() {
// start solving
int n = sc.nextInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
if (n % 2 == 1) {
System.out.println(1);
} else {
Arrays.sort(x);
Arrays.sort(y);
int xx = x[n / 2] - x[n / 2 - 1] + 1;
int yy = y[n / 2] - y[n / 2 - 1] + 1;
System.out.println((long)xx*yy);
}
}
static long lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static boolean isPrime(int n) {
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
return true;
}
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static long modular_add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
private static long modular_sub(long a, long b) {
return ((a % mod) - (b % mod) + mod) % mod;
}
private static long modular_mult(long a, long b) {
return ((a % mod) * (b % 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 class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
boolean hasNext() {
if (st != null && st.hasMoreTokens()) {
return true;
}
String tmp;
try {
br.mark(1000);
tmp = br.readLine();
if (tmp == null) {
return false;
}
br.reset();
} catch (IOException e) {
return false;
}
return true;
}
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[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
2b006f09037fc391a982b7557027db6b
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.StringTokenizer;
public class EasternExhibition {
static final int MAXN = 1000_006;
static final long MOD = (long) 1e9 + 7;
public static void main(String[] args) throws IOException {
MyScanner s = new MyScanner();
Print p = new Print();
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
long[] x = new long[n];
long[] y = new long[n];
long sumx = 0;
long sumy = 0;
for (int i = 0; i < n; i++) {
x[i] = s.nextInt();
sumx += x[i];
y[i] = s.nextInt();
sumy += y[i];
}
Arrays.sort(x);
Arrays.sort(y);
if (n % 2 == 1)
p.println(1);
else {
long sol = (x[n / 2] - x[n / 2 - 1] + 1) * (y[n / 2] - y[n / 2 - 1] + 1);
p.println(sol);
}
}
p.close();
}
public static long calc(long xi, long yi, long[] x, long[] y) {
long ans = 0;
for (int i = 0; i < x.length; i++) {
ans += Math.abs(x[i] - xi) + Math.abs(y[i] - yi);
}
return ans;
}
public static class Pair {
long first;
long second;
public Pair(long a, long b) {
this.first = a;
this.second = b;
}
}
public static class Helper {
long MOD = (long) 1e9 + 7;
int MAXN = 1000_006;;
Random rnd;
public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}
public Helper() {
}
public static int[] sieve;
public static ArrayList<Integer> primes;
public void setSieve() {
primes = new ArrayList<>();
sieve = new int[MAXN];
int i, j;
for (i = 2; i < MAXN; ++i)
if (sieve[i] == 0) {
primes.add(i);
for (j = i; j < MAXN; j += i) {
sieve[j] = i;
}
}
}
public static long[] factorial;
public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i)
factorial[i] = factorial[i - 1] * i % MOD;
}
public long getFactorial(int n) {
if (factorial == null)
setFactorial();
return factorial[n];
}
public long ncr(int n, int r) {
if (r > n)
return 0;
if (factorial == null)
setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}
public long[] getLongArray(int size, MyScanner s) throws Exception {
long[] ar = new long[size];
for (int i = 0; i < size; ++i)
ar[i] = s.nextLong();
return ar;
}
public int[] getIntArray(int size, MyScanner s) throws Exception {
int[] ar = new int[size];
for (int i = 0; i < size; ++i)
ar[i] = s.nextInt();
return ar;
}
public int[] getIntArray(String s) throws Exception {
s = s.trim().replaceAll("\\s+", " ");
String[] strs = s.split(" ");
int[] arr = new int[strs.length];
for (int i = 0; i < strs.length; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
return arr;
}
public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public long max(long[] ar) {
long ret = ar[0];
for (long itr : ar)
ret = Math.max(ret, itr);
return ret;
}
public int max(int[] ar) {
int ret = ar[0];
for (int itr : ar)
ret = Math.max(ret, itr);
return ret;
}
public long min(long[] ar) {
long ret = ar[0];
for (long itr : ar)
ret = Math.min(ret, itr);
return ret;
}
public int min(int[] ar) {
int ret = ar[0];
for (int itr : ar)
ret = Math.min(ret, itr);
return ret;
}
public long sum(long[] ar) {
long sum = 0;
for (long itr : ar)
sum += itr;
return sum;
}
public long sum(int[] ar) {
long sum = 0;
for (int itr : ar)
sum += itr;
return sum;
}
public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1)
ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}
}
static class Print {
private BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
61c2606916d2a82d2ca449bc237cb3f1
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int i=0;i<t;i++)
{
int n=s.nextInt();
int[] x=new int[n];
int[] y=new int[n];
for(int j=0;j<n;j++)
{
x[j]=s.nextInt();
y[j]=s.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2!=0)
{
System.out.println(1);
}
else
{
long a=x[(n/2)]-x[(n/2)-1]+1;
long b=y[(n/2)]-y[(n/2)-1]+1;
System.out.println(a*b);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
d0fafb6a6890f719f83deb70f0c666fd
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
public class EasternExhibition {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
long a[]=new long[n];
long b[]=new long[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextLong();
b[i]=sc.nextLong();
}
if(n%2==1)
System.out.println("1");
else {
Arrays.sort(a);
Arrays.sort(b);
System.out.println((a[n/2]-a[n/2-1]+1L)*(b[n/2]-b[n/2-1]+1L));
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
def2aba3e9cb9a07e79977e35e08aca4
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static long myceil(long a, long b){
return (a+b-1)/b;
}
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
tr.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
static void calc(int n){
int i,j;
dp[n-1]=0;
if(n>1)
dp[n-2]=1;
for(i=n-3;i>=0;i--){
long ind=n-i-1;
dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD;
}
}
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[];
static int visited[];
static ArrayList<Pair<Integer,String>> adj[];
//static int dp[][][];
static int R,G,B,MOD=1000000007;
static int[] par,size,memo;
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static char ch[][], ch1[][];
// static void lengen(int arr[],int ind){
// memo[ind]=Math.max(lengen(arr,ind+1),lis(arr,))
// }
static int lis(int arr[],int n,int ind,int cur){
if(ind>=n){
return memo[ind]=0;
}
else if(ind>=0&&memo[ind]>-1){
return memo[ind];
}
else if(cur<arr[ind+1]){
return memo[ind]=Math.max(lis(arr,n,ind+1,arr[ind+1])+1,lis(arr,n,ind+1,cur));
}else{
return memo[ind]=lis(arr,n,ind+1,cur);
}
}
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,tno=0,tte;
t=in.nextInt();
//t=1;
//tte=t;
while(t-->0){
int n=in.nextInt();
long x[]=new long[n];
long y[]=new long[n];
long xa=0,ya=0;
for(i=0;i<n;i++){
//long x,y;
x[i]=in.nextLong();
y[i]=in.nextLong();
//p[i]=new Pair<>(x,y);
//xa+=x;
//ya+=y;
}
Arrays.sort(x);
Arrays.sort(y);
if(n==1){
w.println(1);
continue;
}
long xmul=(long)x[(n+2)/2-1]-x[(n+1)/2-1]+1;
long ymul=(long)y[(n+2)/2-1]-y[(n+1)/2-1]+1;
w.println(xmul*ymul);
}
w.close();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
afc6163f14575039c19bf84ea70bceac
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
static Scanner in = new Scanner(System.in);
// static Scanner in = new Scanner( new File("javain.txt"));
public static void main(String[] args) throws FileNotFoundException {
if(args.length > 0){
in = new Scanner( new File("javain.txt"));
}
int t = in.nextInt();
for(int i = 0; i < t; i++){
solve();
}
}
public static void solve(){
int n = in.nextInt();
long[] A = new long[n];
long[] B = new long[n];
for(int i = 0; i < n; i++){
A[i] = in.nextLong();
B[i] = in.nextLong();
}
Arrays.sort(A);
Arrays.sort(B);
System.out.println((A[(n + 0) / 2] - A[(n - 1) / 2] + 1) * (B[(n + 0)/ 2] - B[(n - 1) / 2] + 1));
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
bea030941b6731c762987f2704a6b347
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(System.out);
int T=Int();
for(int t=0;t<T;t++){
int n=Int();
int A[][]=new int[n][2];
for(int i=0;i<n;i++){
A[i][0]=Int();
A[i][1]=Int();
}
Solution sol=new Solution();
sol.solution(out,A);
}
out.flush();
}
public static long Long(){ return in.nextLong();}
public static int Int(){
return in.nextInt();
}
public static String Str(){
return in.next();
}
}
class Solution{
public void solution(PrintWriter out,int A[][]){
int x[]=new int[A.length];
int y[]=new int[A.length];
for(int i=0;i<A.length;i++){
x[i]=A[i][0];
y[i]=A[i][1];
}
Arrays.sort(x);
Arrays.sort(y);
long res=solve(x)*solve(y);
out.println(res);
}
public long solve(int A[]){
int n=A.length;
if(n%2==1)return 1;
return A[n/2]-A[n/2-1]+1;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
0d832013c304e881d6b381f4cc5f09d8
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class B {
public long calc(long [] a) {
return Math.abs(a[(a.length + 1) / 2 -1 ] - a[(a.length + 2) / 2 - 1]) + 1;
}
public void solve() {
int t = in.nextInt();
while (t > 0) {
int n = in.nextInt();
long x[] = new long[n];
long y[] = new long[n];
for (int i = 0; i<n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
out.println(calc(x) * calc(y));
t--;
}
}
String input = "";
String output = "";
FastScanner in;
PrintWriter out;
void run() throws Exception {
if (input.length() == 0) {
in = new FastScanner(System.in);
} else {
in = new FastScanner(new File(input));
}
if (output.length() == 0) {
out = new PrintWriter(System.out);
} else {
out = new PrintWriter(new File(output));
}
solve();
out.close();
}
public static void main(String[] args) throws Exception {
new B().run();
}
class FastScanner {
BufferedReader bf;
StringTokenizer st;
public FastScanner(InputStream is) {
bf = new BufferedReader(new InputStreamReader(is));
}
public FastScanner(File fr) throws FileNotFoundException {
bf = new BufferedReader(new FileReader(fr));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
} catch (IOException ex) {
ex.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] readIntArray(int length) {
int arr[] = new int[length];
for (int i = 0; i<length; i++)
arr[i] = nextInt();
return arr;
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
5ed5d5dac6781e5c3cef7b365965b3b5
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.lang.*;
import java.util.*;
public class Arpit
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
long[] x = new long[n];
long[] y = new long[n];
for(int i=0;i<n;i++)
{
x[i] = sc.nextLong();
y[i] = sc.nextLong();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2==1)
System.out.println("1");
else
{
long c = (x[n/2]-x[n/2-1]+1)*(y[n/2]-y[n/2-1]+1);
System.out.println(c);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
86b96877b1d12c3f1c6b28a818643134
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
long a[] = new long[n];
long b[] = new long[n];
for(int i = 0; i < n; i++)
{
a[i] = sc.nextLong();
b[i] = sc.nextLong();
}
Arrays.sort(a);
Arrays.sort(b);
if(n % 2 == 1)
{
w.println(1);
}
else
{
w.println((a[n/2] - a[n/2 - 1] + 1) * (b[n/2] - b[n/2 - 1] + 1));
}
}
w.close();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
68730afceb74707bb24bde2f9dc31192
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class Pair
{
int f,s;
Pair(int f,int s)
{
this.f=f;
this.s=s;
}
}
public static void main(String args[])
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int tc=fs.nextInt();
while(tc-->0)
{
int n=fs.nextInt();
long a[][]=new long[2][n];
for(int i=0;i<n;i++)
{
a[0][i]=fs.nextLong();
a[1][i]=fs.nextLong();
}
Arrays.sort(a[0]);
Arrays.sort(a[1]);
if(n%2==0)
{
TreeSet<String> ts=new TreeSet<>();
long x1=a[0][n/2],x2=a[0][n/2-1],y1=a[1][n/2],y2=a[1][n/2-1];
//pw.println(x1+" "+x2+" "+y1+" "+y2);
pw.println(((long)Math.abs(x2-x1)+1)*((long)Math.abs(y2-y1)+1));
}
else
pw.println(1);
}
pw.flush();
pw.close();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
60833b1c45fd15279f4aa0e5a49b00b7
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class B {
static InputReader ir;
static OutputPrinter op;
static int n;
static Long[] x;
static Long[] y;
public static void main(String[] args) throws IOException {
ir = new InputReader();
op = new OutputPrinter();
int t = ir.nextInt();
for (int i = 0; i < t; i++) {
n = ir.nextInt();
x = new Long[n];
y = new Long[n];
for (int j =0 ;j < n; j++) {
x[j] = (long) ir.nextInt();
y[j] = (long) ir.nextInt();
}
f();
}
}
public static void f() throws IOException {
Arrays.sort(x);
Arrays.sort(y);
long result = x[(n)/2]-x[(n-1)/2]+1;
result *= y[(n)/2]-y[(n-1)/2]+1;
op.writeln(""+result);
}
public static int getMin(long a, long b) {
int num = 0;
while (a != 0) {
a/=b;
num++;
}
return num;
}
}
class InputReader {
public BufferedReader br;
public StringTokenizer st;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt() throws IOException {
if (st == null) {
st = new StringTokenizer(br.readLine());
}
if (st.hasMoreElements()) {
return Integer.parseInt(st.nextToken());
} else {
st = new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
}
public double nextDouble() throws IOException {
if (st == null) {
st = new StringTokenizer(br.readLine());
}
if (st.hasMoreElements()) {
return Double.parseDouble(st.nextToken());
} else {
st = new StringTokenizer(br.readLine());
return Double.parseDouble(st.nextToken());
}
}
public String nextToken() throws IOException {
if (st == null) {
st = new StringTokenizer(br.readLine());
}
if (st.hasMoreElements()) {
return st.nextToken();
} else {
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
}
public String nextLine() throws IOException {
return br.readLine();
}
}
class OutputPrinter {
public PrintWriter pw;
public OutputPrinter() {
pw = new PrintWriter(new OutputStreamWriter(System.out), true);
}
public void write(char[] buf) throws IOException {
pw.write(buf);
pw.flush();
}
public void writeln(char[] buf) throws IOException {
write(buf);
pw.write("\n");
pw.flush();
}
public void write(String s) throws IOException {
pw.write(s);
pw.flush();
}
public void writeln(String s) throws IOException {
write(s);
pw.write("\n");
pw.flush();
}
public void write(int t) throws IOException {
pw.write(Integer.toString(t));
pw.flush();
}
public void writeln(int t) throws IOException {
write(t);
pw.write("\n");
pw.flush();
}
public void write(double d) throws IOException {
pw.write(Double.toString(d));
pw.flush();
}
public void writeln(double d) throws IOException {
write(d);
pw.write("\n");
pw.flush();
}
public void writeln() {
pw.write("\n");
pw.flush();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
e9fe2793a42f71bd5e553e60b08b81fd
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Codeforces2 {
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int []x = new int[n];
int []y = new int[n];
for( int i = 0; i < n; i++){
String []line= br.readLine().split(" ");
x[i] = Integer.parseInt(line[0]);
y[i] = Integer.parseInt(line[1]);
}
Arrays.sort(x);
Arrays.sort(y);
if( n % 2 == 1)
System.out.println(1);
else{
long xmedian = (x[n/2] - x[n/2 - 1]) + 1;
long ymedian = (y[n/2] - y[n/2 - 1]) + 1;
System.out.println(xmedian * ymedian);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
cf44941982219d6fa192187821f50fdb
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.lang.*;
public class exhibition
{
public static void main(String args[]) throws IOException
{
Scanner sc=new Scanner (System.in);
int t;
t=sc.nextInt();
sc.nextLine();
int i;
long r[]=new long[t];
for(i=0;i<t;i++)
{
int n=sc.nextInt();
sc.nextLine();
long x[]=new long[n];
long y[]=new long[n];
for(int j=0;j<n;j++)
{
x[j]=sc.nextInt();
y[j]=sc.nextInt();
sc.nextLine();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2==0)
{
long x_value=(x[n/2]-x[(n/2)-1])+1;
long y_value=(y[n/2]-y[(n/2)-1])+1;
r[i]=x_value*y_value;
}
else
{
r[i]=1;;
}
}
for(i=0;i<t;i++)
{
System.out.println(r[i]);
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
1de715edbdc8f142944c63c1234f70a0
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class Main
{
static void sort(long a[])
{
Random ran = new Random();
for (int i = 0; i < a.length; i++) {
int r = ran.nextInt(a.length);
long temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
public static void main(String[] args)
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work: while (tc-- > 0) {
int n = input.nextInt();
long x[] = new long[n];
long y[] = new long[n];
for (int i = 0; i < n; i++) {
x[i] = input.nextLong();
y[i] = input.nextLong();
}
sort(x);
sort(y);
if (n % 2 == 1) {
System.out.println("1");
continue work;
}
long ans = (x[n / 2] - x[n / 2 - 1] + 1) * (y[n / 2] - y[n / 2 - 1] + 1);
System.out.println(ans);
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
3e60f7e0123c4fa6b50d3ad7f2175351
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution {
private static boolean TESTS = true;
private final Input in;
private final PrintStream out;
public Solution(final Input in, final PrintStream out) {
this.in = in;
this.out = out;
}
public void solve() {
for (int test = 1, tests = TESTS ? in.nextInt() : 1; test <= tests; test++) {
final int n = in.nextInt();
final int[] x = new int[n];
final int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
long xPart = (x[n / 2] - x[(n - 1) / 2]) + 1;
long yPart = (y[n / 2] - y[(n - 1) / 2]) + 1;
out.println(xPart * yPart);
}
}
public static void main(final String[] args) {
final Input in = new Input();
final PrintStream out = new PrintStream(new BufferedOutputStream(System.out));
try {
new Solution(in, out).solve();
} finally {
out.flush();
}
}
private static final class Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
public String nextString() {
try {
while (!tokenizer.hasMoreTokens()) {
final String line = reader.readLine();
tokenizer = new StringTokenizer(line, " ");
}
return tokenizer.nextToken();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public long nextLong() {
return Long.parseLong(nextString());
}
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextInts(final int size) {
final int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongs(final int size) {
final long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubles(final int size) {
final double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
406ae324e7731dc9de447b9b5d6d6c7b
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
long x[] = new long[n], y[] = new long[n];
for (int i = 0; i < n; i++) {
x[i] = fs.nextLong();
y[i] = fs.nextLong();
}
if (n % 2 == 1) {
System.out.println( 1 );
continue;
}
Arrays.sort( x );
Arrays.sort( y );
int mid = n / 2;
System.out.println( (x[mid] - x[mid - 1] + 1) * (y[mid] - y[mid - 1] + 1 ));
}
}
private static class FastScanner {
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
StringTokenizer st = new StringTokenizer( "" );
public String next() {
while (!st.hasMoreElements())
try {
st = new StringTokenizer( br.readLine() );
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt( next() );
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong( next() );
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
eb9e4f97012dbb08e83a8c0c9b2aaba1
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
/*
⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀⠀⢠⠞⠉⠙⠲⡀⠀
⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀⡏⠀⠀Y⠀⠀⢷
⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀⢸⠀⠀A⠀⠀ ⡇
⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀⣸⠀⠀S⠀ ⡇
⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀⢹⠀⠀H⠀⠀ ⡇
⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀⠀⡇⠀⠀⠀⠀⡼
⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀⠀⠘⠤⣄⣠⠞⠀
⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀
⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀
⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀
⣿⣿⣧⣀⣿………⣀⣰⣏⣘⣆⣀⠀⠀
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; // System.out is a PrintWriter
import java.util.Arrays;
// import java.util.ArrayDeque;
// import java.util.ArrayList;
// import java.util.Collections; // for ArrayList sorting mainly
// import java.util.HashMap;
// import java.util.HashSet;
// import java.util.Random;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
FastScanner scn = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
for (int tc = scn.nextInt(); tc > 0; tc--) {
int N = scn.nextInt();
int[][] xy = new int[2][N];
for (int i = 0; i < N; i++) {
xy[0][i] = scn.nextInt();
xy[1][i] = scn.nextInt();
}
if ((N & 1) != 0) {
out.println("1");
continue;
}
Arrays.sort(xy[0]);
Arrays.sort(xy[1]);
int M = N / 2;
out.println(1L * (xy[0][M] - xy[0][M - 1] + 1) * (xy[1][M] - xy[1][M - 1] + 1));
}
out.close();
}
private static int gcd(int num1, int num2) {
int temp = 0;
while (num2 != 0) {
temp = num1;
num1 = num2;
num2 = temp % num2;
}
return num1;
}
private static int lcm(int num1, int num2) {
return (int)((1L * num1 * num2) / gcd(num1, num2));
}
private static void ruffleSort(int[] arr) {
// int N = arr.length;
// Random rand = new Random();
// for (int i = 0; i < N; i++) {
// int oi = rand.nextInt(N), temp = arr[i];
// arr[i] = arr[oi];
// arr[oi] = temp;
// }
// Arrays.sort(arr);
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() {
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
fa9b31fe99b72477c67893ae04d63b69
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class easternexhibition {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = scn.nextInt();
long[] x = new long[n];
long[] y = new long[n];
for (int i = 0; i < n; i++) {
x[i] = scn.nextLong();
y[i] = scn.nextLong();
}
if (n % 2 == 1) {
System.out.println("1");
} else {
Arrays.sort(x);
Arrays.sort(y);
long xx = x[n / 2] - x[n / 2 - 1] + 1;
long yy = y[n / 2] - y[n / 2 - 1] + 1;
System.out.println(xx * yy);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
6570e46ea00c8df9a16e68416473260e
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
public class B{
public static void main(String args[]) throws Exception {
FastScanner fs=new FastScanner();
int t = fs.nextInt();
while(t-- > 0){
int n = fs.nextInt();
long x[] = new long[n];
long y[] = new long[n];
for(int i=0;i<n;i++){
x[i] = fs.nextLong();
y[i] = fs.nextLong();
}
if(n%2==1){
System.out.println("1");
}
else{
Arrays.sort(x);
Arrays.sort(y);
System.out.println((x[n/2]-x[n/2-1]+1)*(y[n/2]-y[n/2-1]+1));
}
}
fs.close();
}
static class FastScanner {
InputStreamReader is;
BufferedReader br;
StringTokenizer st;
public FastScanner() {
is = new InputStreamReader(System.in);
br = new BufferedReader(is);
}
String next() throws Exception {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
int[] readArray(int num) throws Exception {
int arr[]=new int[num];
for(int i=0;i<num;i++)
arr[i]=nextInt();
return arr;
}
String nextLine() throws Exception {
return br.readLine();
}
void close() throws Exception {
is.close();
br.close();
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
9d95871b303ba3b1b06aa2592f9b2c71
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.math.*;
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
while(T-->0)
{
int n = input.nextInt();
long x[] = new long[n];
long y[] = new long[n];
for (int i = 0; i < n; i++) {
x[i] = input.nextInt();
y[i] = input.nextInt();
}
if(n==1)
System.out.println(1);
else
{
if(n%2==1)
System.out.println(1);
else
{
Arrays.sort(x);
Arrays.sort(y);
System.out.println((x[n/2]-x[n/2-1]+1)*(y[n/2]-y[n/2-1]+1));
}
}
}
}
}
class Node{
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return x == node.x && y == node.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
deb25c50577b4e76381aa246e94382b8
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class B {
static FastReader reader = new FastReader();
static OutputWriter out = new OutputWriter(System.out);
//1 2 3
static int[] calc(List<Integer> arr) {
if(arr.size() == 1) {
return new int[] {arr.get(0), arr.get(0)};
}
Collections.sort(arr);
int n = arr.size();
if(n % 2 == 0) {
return new int[] {arr.get(n/2-1), arr.get(n/2)};
} else {
return new int[] {arr.get(n/2), arr.get(n/2)};
}
}
public static void main(String[] args) {
int tests = reader.nextInt();
outtter : for(int test = 1; test <= tests; test++) {
int n = reader.nextInt();
List<Integer> xs = new ArrayList<>();
List<Integer> ys = new ArrayList<>();
int[][] points = new int[n][2];
for(int i = 0; i < n; i++) {
points[i][0] = reader.nextInt(); xs.add(points[i][0]);
points[i][1] = reader.nextInt(); ys.add(points[i][1]);
}
int[] x = calc(xs);
int[] y= calc(ys);
out.println((long)((long)x[1]-x[0] + 1) * ((long)y[1]-y[0] + 1));
}
out.flush();
}
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 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(char[] array) {
writer.print(array);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void print(double[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(int[] array) {
print(array);
writer.println();
}
public void println(double[] array) {
print(array);
writer.println();
}
public void println(long[] array) {
print(array);
writer.println();
}
public void println() {
writer.println();
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void print(char i) {
writer.print(i);
}
public void println(char i) {
writer.println(i);
}
public void println(char[] array) {
writer.println(array);
}
public void printf(String format, Object... objects) {
writer.printf(format, objects);
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
public void print(long i) {
writer.print(i);
}
public void println(long i) {
writer.println(i);
}
public void print(int i) {
writer.print(i);
}
public void println(int i) {
writer.println(i);
}
public void separateLines(int[] array) {
for (int i : array) {
println(i);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
e5742c99b40f306235de80c7350a8931
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main2 {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static FastReader sc = new FastReader();
static long n;
public static void main (String[] args) {
PrintWriter out = new PrintWriter(System.out);
int t = 1;
t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long x[] = new long[n+1];
long y[] = new long[n+1];
for(int i=1;i<=n;i++) {
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
Arrays.parallelSort(x);
Arrays.parallelSort(y);
if(n%2!=0) {
out.write("1\n");
}
else {
long ans = (x[n/2+1] - x[n/2] + 1)*(y[n/2+1] - y[n/2] + 1);
out.write(ans+"\n");
}
}
out.close();
}
private static long ask(long idx, long h) {
if(idx>h) return -1;
if(idx == h) return h;
System.out.println("? "+idx+" "+h);
System.out.flush();
long ret = sc.nextLong();
return ret;
}
private static long pow(int i, int x) {
long ans = 1;
while(x>0) {
if(x%2==0) {
i*=i;
x/=2;
}
else {
ans*=i;
x--;
}
}
// System.out.println(ans+" ans");
return ans;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
56f74651139120567e8dc973fe5f1c00
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
// Main Code at the Bottom
import java.util.*;
import java.io.*;
public class Main{
//Fast IO class
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
boolean env=System.getProperty("ONLINE_JUDGE") != null;
//env=true;
if(!env) {
try {
br=new BufferedReader(new FileReader("src\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long MOD=(long)1e9+7;
//debug
static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
//Global variables and functions
//Main function(The main code starts from here)
public static void main (String[] args) throws java.lang.Exception {
int test=1,t=0;
test=sc.nextInt();
while(test-->0) {
int n=sc.nextInt();
long x[]=new long[n],y[]=new long[n];
for(int i=0;i<n;i++) {
x[i]=sc.nextInt();
y[i]=sc.nextInt();
}
Arrays.parallelSort(x);
Arrays.parallelSort(y);
out.println(n%2==1?1:(x[n/2]-x[n/2-1]+1L)*(y[n/2]-y[n/2-1]+1L));
}
out.flush();
out.close();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
66f6c9f6e56edcc06ea6248f5135483e
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class A {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<Integer,Integer>map=new HashMap<>();
public static void main(String args[]) throws IOException {
int t=in.nextInt();
StringBuilder res=new StringBuilder();
loop:
while(t-->0)
{
int n=in.nextInt();
int a[][]=new int[n][2];
long maxx=0;
long maxy=0;
long minx=Long.MAX_VALUE;
long miny=Long.MAX_VALUE;
int xarr[]=new int[n];
int yarr[]=new int[n];
for(int i=0;i<n;i++)
{
xarr[i]=in.nextInt();
yarr[i]=in.nextInt();
}
ruffleSort(xarr);
ruffleSort(yarr);
for(int i=0;i<n;i++)
{
a[i][0]=xarr[i];
a[i][1]=yarr[i];
/*if(a[i][0]>maxx)
{
maxx=a[i][0];
}
if(a[i][1]>maxy)
{
maxy=a[i][1];
}
if(a[i][0]<minx)
{
minx=a[i][0];
}
if(a[i][1]<miny)
{
miny=a[i][1];
}*/
}
if(n%2==1)
{
res.append(1+"\n");
}
else
{
maxx=xarr[n/2]-xarr[n/2-1]+1;
maxy=yarr[n/2]-yarr[n/2-1]+1;
res.append(maxx*maxy+"\n");
}
}
print(res);
}
static long min(long a, long 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 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 [] 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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
d6549bacc8dd1a3f3ad0242fd7028978
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.*;
import java.io.*;
import java.util.LinkedList;
public class Main {
static long startTime = System.currentTimeMillis();
static void run(){
boolean tc = true;
AdityaFastIO r = new AdityaFastIO();
//FastReader r = new FastReader();
try (OutputStream out = new BufferedOutputStream(System.out)) {
//long startTime = System.currentTimeMillis();
int testcases = tc ? r.ni() : 1;
// Hold Here Sparky------------------->>>
// Solution Starts Here
while (testcases --> 0){
int n = r.ni();
List<Long> x = new ArrayList<>();
List<Long> y = new ArrayList<>();
for (int i=0;i<n;i++){
x.add(r.nl());
y.add(r.nl());
}
int count = 0;
if ((n&1)==1) out.write((1 + " ").getBytes());
else{
Collections.sort(x);
Collections.sort(y);
out.write(((x.get(n / 2) - x.get((n / 2) - 1) + 1) * (y.get(n / 2) - y.get((n / 2) - 1) + 1) + " ").getBytes());
}
out.write(("\n").getBytes());
}
// Solution Ends Here
} catch (IOException e) {
e.printStackTrace();
}
}
static class AdityaFastIO{
final private int BUFFER_SIZE = 1<<16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public BufferedReader br;
public StringTokenizer st;
public AdityaFastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public AdityaFastIO(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String word() {
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
public String line() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String readLine() throws IOException {
byte[] buf = new byte[100000001]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int ni() 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 nl() 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 nd() 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 java.lang.Exception {run();}
static long mod = 998244353;
static long modInv(long base, long e) {
long result = 1;
base %= mod;
while (e>0) {
if ((e & 1)>0) result = result * base % mod;
base = base * base % mod;
e >>= 1;
}
return result;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String word() {
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
String line() {
String str = "";
try { str = br.readLine(); }
catch (IOException e) { e.printStackTrace(); }
return str;
}
int ni() { return Integer.parseInt(word()); }
long nl() { return Long.parseLong(word()); }
double nd() { return Double.parseDouble(word()); }
}
static int MOD = (int) (1e9 + 7);
static long powerLL(long x, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 1) result = result * x % MOD;
n = n / 2;x = x * x % MOD;
}
return result;
}
static long powerStrings(int i1, int i2) {
String sa = String.valueOf(i1);
String sb = String.valueOf(i2);
long a = 0, b = 0;
for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD;
for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1);
return powerLL(a, b);
}
static long gcd(long a, long b) { if (a == 0) return b;else return gcd(b % a, a); }
static long lcm(long a, long b) { return (a * b) / gcd(a, b); }
static long lower_bound(List<Long> list, long k) {
int s = 0;
int e = list.size();
while (s != e) {
int mid = (s + e) >> 1;
if (list.get(mid) < k) s = mid + 1;
else e = mid;
}
if (s == list.size()) return -1;
return s;
}
static int upper_bound(List<Long> list, long k) {
int s = 0;
int e = list.size();
while (s != e) {
int mid = (s + e) >> 1;
if (list.get(mid) <= k) s = mid + 1;
else e = mid;
}
if (s == list.size()) return -1; return s;
}
static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) {
graph.get(edge1).add(edge2);graph.get(edge2).add(edge1);
}
public static class Pair implements Comparable<Pair> {
int first;int second;
public Pair(int first, int second) { this.first = first;this.second = second; }
public String toString() { return "(" + first + "," + second + ")"; }
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.first!=o.first)
return (int) (this.first - o.first);
else return(int)(this.second-o.second);
}
}
public static class PairC<X,Y> implements Comparable<PairC> {
X first;Y second;
public PairC(X first, Y second) { this.first = first;this.second = second; }
public String toString() { return "(" + first + "," + second + ")"; }
public int compareTo(PairC o) {
// TODO Auto-generated method stub
return o.compareTo((PairC) first);
}
}
static boolean isCollectionsSorted(List<Long> list) {
if (list.size() == 0 || list.size() == 1) return true;
for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false;
return true;
}
static boolean isCollectionsSortedReverseOrder(List<Long> list) {
if (list.size() == 0 || list.size() == 1) return true;
for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false;
return true;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
d5732495a931e915d9af5b44069a8463
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Scanner;
public class T1486B {
static int homeXs[];
static long distanceToHomeByX[];
static int homeYs[];
static long distanceToHomeByY[];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
homeXs = new int[n];
distanceToHomeByX = new long[n];
homeYs = new int[n];
distanceToHomeByY = new long[n];
for (int j = 0; j < n; j++) {
homeXs[j] = in.nextInt();
homeYs[j] = in.nextInt();
}
calculateDistanceX();
calculateDistanceY();
long answer = (long)getSizeMinimumX() * (long)getSizeMinimumY();
System.out.println(answer);
}
}
static void calculateDistanceX() {
calculateDistance(homeXs, distanceToHomeByX);
}
static void calculateDistanceY() {
calculateDistance(homeYs, distanceToHomeByY);
}
static void calculateDistance(int coordinates[], long distance[]) {
for (int i = 0; i < coordinates.length; i++) {
for (int j = 0; j < i; j++) {
distance[i] += Math.abs(coordinates[i] - coordinates[j]);
}
for (int j = i + 1; j < coordinates.length; j++) {
distance[i] += Math.abs(coordinates[i] - coordinates[j]);
}
}
}
static int getSizeMinimumX() {
return searchLengthSegmentMinimumDistance(homeXs, distanceToHomeByX);
}
static int getSizeMinimumY() {
return searchLengthSegmentMinimumDistance(homeYs, distanceToHomeByY);
}
static int searchLengthSegmentMinimumDistance(int coordinates[], long distance[]) {
int indexMinimum = 0;
int leftCoordinateSegment = coordinates[0];
int rightCoordinateSegment = coordinates[0];
for (int i = 1; i < coordinates.length; i++) {
if (distance[i] < distance[indexMinimum]) {
indexMinimum = i;
leftCoordinateSegment = rightCoordinateSegment = coordinates[i];
} else if (distance[i] == distance[indexMinimum]) {
if (coordinates[i] < leftCoordinateSegment) {
leftCoordinateSegment = coordinates[i];
} else if (coordinates[i] > rightCoordinateSegment) {
rightCoordinateSegment = coordinates[i];
}
}
}
return (rightCoordinateSegment - leftCoordinateSegment + 1);
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
ecdf6156838be44938243887c4ff41ac
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.Comparator;
public class B {
public long cnt(int[] a, int b) {
long ans = 0;
for (int p : a) {
ans += Math.abs(p - b);
}
return ans;
}
public long solvefor(Integer[] b) {
int[] a = new int[b.length];
int n = a.length;
for (int i = 0; i < n; ++i) {
a[i] = b[i];
}
long[] su = new long[n];
long mi = (long)1e18;
for (int i = 0; i < n; ++i) {
su[i] = cnt(a, a[i]);
mi = Math.min(mi, su[i]);
}
int minn = (int)1e9;
int maxx = (int)-1e9;
for (int i = 0; i < n; ++i) {
if (su[i] == mi) {
minn = Math.min(minn, a[i]);
maxx = Math.max(maxx, a[i]);
}
}
return maxx - minn + 1L;
}
public long solve(IO io) throws IOException {
int n = io.nextInt();
Integer[] x = new Integer[n];
Integer[] y = new Integer[n];
for (int i = 0; i < n; ++i) {
x[i] = io.nextInt();
y[i] = io.nextInt();
}
Arrays.sort(x, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return a - b;
}
});
return solvefor(x) * solvefor(y);
}
public void run() throws IOException {
IO io = new IO();
int t = io.nextInt();
for (int i = 0; i < t; ++i) {
io.println(solve(io) + "");
}
io.close();
}
public static void main(String[] args) throws IOException {
new B().run();
}
}
class IO {
public IO() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public void print(String a) {
out.print(a);
}
public void println(String a) {
out.println(a);
}
public void close() {
out.close();
}
}
class Point {
public int x;
public int y;
public Point() {}
public Point(IO io) throws IOException {
x = io.nextInt();
y = io.nextInt();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
99cc415c571df2d1bcfa9554d3f675f1
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
// Scanner sc = new Scanner(System.in);
// final int MOD = 998244353;
// int t = sc.nextInt();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0)
{
int n = Integer.parseInt(br.readLine().trim());
long[][] ar = new long[n][2];
// long sumX = 0, sumY=0;
// HashSet<Long> setX = new HashSet<>(), setY = new HashSet<>();
// long maxX=Long.MIN_VALUE, minX=Long.MAX_VALUE, maxY=Long.MIN_VALUE, minY=Long.MAX_VALUE;
for(int i=0; i<n; i++)
{
String[] in = br.readLine().trim().split("\\s+");
ar[i][0] = Long.parseLong(in[0]);
ar[i][1] = Long.parseLong(in[1]);
// sumX += ar[i][0];
// sumY += ar[i][1];
// maxX = Math.max(maxX, ar[i][0]);
// minX = Math.min(minX, ar[i][0]);
// maxY = Math.max(maxY, ar[i][1]);
// minY = Math.min(minY, ar[i][1]);
// setX.add(ar[i][0]);
// setY.add(ar[i][1]);
}
if(n%2==1)
{
bw.write("1\n");
}
else
{
Arrays.sort(ar, (a,b)->(int)(a[0]-b[0]));
long x=0, y=0;
if(ar[n/2][0]==ar[n/2-1][0])
x=1;
else
x=1+ar[n/2][0] - ar[n/2-1][0];
Arrays.sort(ar, (a,b)->(int)(a[1]-b[1]));
if(ar[n/2][1]==ar[n/2-1][1])
y=1;
else
y=1+ar[n/2][1] - ar[n/2-1][1];
bw.write(x*y+"\n");
}
// if(maxX==minX)
// {
// if(n==2)
// {
// bw.write("3\n");
// }
// else
// {
// bw.write("1\n");
// }
// }
// else
// {
// if(maxY==minY)
// {
// if(n==2)
// bw.write("3\n");
// else
// bw.write("1\n");
// }
// else
// {
// double meanX = 1.0*sumX/n, meanY = 1.0*sumY/n;
// long mX = sumX/n, mY = sumY/n;
// int xPos = meanX-mX==0.5 ? 2 : 1;
// int yPos = meanY-mY==0.5 ? 2 : 1;
// bw.write(xPos*yPos+"\n");
// }
// }
}
br.close();
bw.close();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
bf4452a6975386be30af55ac77b4ad40
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.text.*;
public class Main {
static long mod = 1000_000_007;
static long mod1 = 998244353;
static boolean fileIO = true;
static boolean memory = true;
static FastScanner f;
static PrintWriter pw;
static double eps = (double)1e-6;
static int oo = (int)1e7;
public static void solve() throws Exception {
int n = f.ni();
int p[][] = new int[n][2];
int x[] = new int[n];
int y[] = new int[n];
for (int i = 0; i < n; ++i) {
p[i][0] = f.ni();
p[i][1] = f.ni();
x[i] = p[i][0];
y[i] = p[i][1];
}
sort(x);
sort(y);
if (n == 1) {
pn(1);
return;
}
int rx = x[n / 2];
int ry = y[n / 2];
int lx = x[n / 2 - 1];
int ly = y[n / 2 - 1];
long xc = rx - lx + 1;
long yc = ry - ly + 1;
long ans = xc * yc;
pn(n % 2 == 1 ? 1 : ans);
//pn(xx + " " + yy);
}
public static void main(String[] args)throws Exception {
if(memory) new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "", 1 << 28).start();
else new Main().run();
}
/******************************END OF MAIN PROGRAM*******************************************/
void run()throws Exception {
if (System.getProperty("ONLINE_JUDGE") == null) {
f = new FastScanner("");
pw = new PrintWriter(System.out);
}
else {
f = new FastScanner();
pw = new PrintWriter(System.out);
//fw = new FileWriter("!out.txt");
}
//pre();
int t = f.ni();
int tt = 1;
while(t --> 0) {
//fw.write("Case #" + (tt++) + ": ");
//fw.write("\n");
solve();
}
pw.flush();
pw.close();
//fw.close();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String str) throws Exception {
try {
br = new BufferedReader(new FileReader("!a.txt"));
}
catch (Exception e) {
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next()throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int ni() throws IOException {return Integer.parseInt(next());}
public long nl() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nd() throws IOException {return Double.parseDouble(next());}
}
public static void pn(Object... o) {for(int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " ": "\n"));}
public static void p(Object... o) {for(int i = 0; i < o.length; ++i) pw.print(o[i] + (i + 1 < o.length ? " " : ""));}
public static void pni(Object... o) {for(Object obj : o) pw.print(oo + " "); pw.println(); pw.flush();}
public static int gcd(int a,int b){if(b==0)return a;else{return gcd(b,a%b);}}
public static long gcd(long a,long b){if(b==0l)return a;else{return gcd(b,a%b);}}
public static long lcm(long a,long b){return (a*b/gcd(a,b));}
public static long pow(long a,long b){long res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;}
public static int pow(int a,int b){int res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;}
public static long mpow(long a,long b, long m){long res=1;while(b>0){if((b&1)==1)res=((res%m)*(a%m))%m;b>>=1;a=((a%m)*(a%m))%m;}return res;}
public static long mul(long a , long b , long mod){return ((a%mod)*(b%mod)%mod);}
public static long adp(long a , long b){return ((a%mod)+(b%mod)%mod);}
public static int dig(long a){int cnt=0;while(a>0){a/=10;++cnt;}return Math.max(1,cnt);}
public static int dig(int a){int cnt=0;while(a>0){a/=10;++cnt;}return Math.max(1,cnt);}
public 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;}
public 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 HashSet<Long> factors(long n){HashSet<Long> hs=new HashSet<Long>();for(long i=1;i<=(long)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
public static HashSet<Integer> factors(int n){HashSet<Integer> hs=new HashSet<Integer>();for(int i=1;i<=(int)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
public static HashSet<Long> pf(long n){HashSet<Long> ff=factors(n);HashSet<Long> ans=new HashSet<Long>();for(Long i:ff)if(isPrime(i))ans.add(i);return ans;}
public static HashSet<Integer> pf(int n){HashSet<Integer> ff=factors(n);HashSet<Integer> ans=new HashSet<Integer>();for(Integer i:ff)if(isPrime(i))ans.add(i);return ans;}
public static int gnv(char c){return Character.getNumericValue(c);}
public 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);}
public static void sort(long[] a){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);}
public static void sort(ArrayList<Integer> a){Collections.sort(a);}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
81f399c1e45eabb60631dcdae8cf2e86
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while(T-- > 0) {
int n = sc.nextInt();
long[] x = new long[n];
long[] y = new long[n];
for(int i = 0; i < n; i++){
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
if(n % 2 == 0) {
long w = x[n/2] - x[n/2-1] + 1;
long h = y[n/2] - y[n/2-1] + 1;
System.out.println(w * h);
}
else System.out.println(1);
}
}
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);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
9ac619d51ee6343406320a4ed6cb93bf
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class b1486 implements Runnable{
public static void main(String[] args) {
try{
new Thread(null, new b1486(), "process", 1<<26).start();
}
catch(Exception e){
System.out.println(e);
}
}
public void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//PrintWriter out = new PrintWriter("file.out");
Task solver = new Task();
int t = scan.nextInt();
//int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
static final int oo = Integer.MAX_VALUE;
static final long OO = Long.MAX_VALUE;
public void solve(int testNumber, FastReader sc, PrintWriter out) {
int N = sc.nextInt();
int[] X = new int[N];
int[] Y = new int[N];
for(int i = 0; i < N; i++) {
X[i] = sc.nextInt();
Y[i] = sc.nextInt();
}
shuffle(X);
Arrays.sort(X);
shuffle(Y);
Arrays.sort(Y);
long Xd, Yd;
if(N % 2 == 0) {
Xd = X[N/2] - X[N/2-1] + 1;
Yd = Y[N/2] - Y[N/2-1] + 1;
} else {
Xd = 1;
Yd = 1;
}
out.println(Xd * Yd);
}
}
static long modInverse(long N, long MOD) {
return binpow(N, MOD - 2, MOD);
}
static long modDivide(long a, long b, long MOD) {
a %= MOD;
return (binpow(b, MOD-2, MOD) * a) % MOD;
}
static long binpow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
static int[] reverse(int a[])
{
int[] b = new int[a.length];
for (int i = 0, j = a.length; i < a.length; i++, j--) {
b[j - 1] = a[i];
}
return b;
}
static long[] reverse(long a[])
{
long[] b = new long[a.length];
for (int i = 0, j = a.length; i < a.length; i++, j--) {
b[j - 1] = a[i];
}
return b;
}
static void shuffle(Object[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
Object temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class tup implements Comparable<tup>, Comparator<tup>{
int a, b;
tup(int a,int b){
this.a=a;
this.b=b;
}
public tup() {
}
@Override
public int compareTo(tup o){
return Integer.compare(b,o.b);
}
@Override
public int compare(tup o1, tup o2) {
return Integer.compare(o1.b, o2.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
tup other = (tup) obj;
return a==other.a && b==other.b;
}
@Override
public String toString() {
return a + " " + b;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int size) {
int[] a = new int[size];
for(int i = 0; i < size; i++) {
a[i] = nextInt();
}
return a;
}
long[] readLongArray(int size) {
long[] a = new long[size];
for(int i = 0; i < size; i++) {
a[i] = nextLong();
}
return a;
}
}
static void dbg(int[] arr) {
System.out.println(Arrays.toString(arr));
}
static void dbg(long[] arr) {
System.out.println(Arrays.toString(arr));
}
static void dbg(boolean[] arr) {
System.out.println(Arrays.toString(arr));
}
static void dbg(Object... args) {
for (Object arg : args)
System.out.print(arg + " ");
System.out.println();
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
3fd5c79dce01a9bffceaf5404a009017
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
//import javafx.util.*;
public final class B
{
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<ArrayList<Integer>> g;
static long mod=1000000007;
static int D1[],D2[],par[];
static boolean set[];
public static void main(String args[])throws IOException
{
int T=i();
outer: while(T-->0)
{
int N=i();
long X[]=new long[N];
long Y[]=new long[N];
for(int i=0; i<N; i++)
{
X[i]=l();
Y[i]=l();
}
sort(X);
sort(Y);
if(N==1)
{
System.out.println(1);
}
else
{
if(N%2==0)
{
int m=N/2;
if(X[m]==X[m-1] && Y[m]==Y[m-1])System.out.println(1);
else if(X[m]==X[m-1] && Y[m-1]!=Y[m])
{
System.out.println(Y[m]-Y[m-1]+1);
}
else if(X[m]!=X[m-1] && Y[m-1]==Y[m])
{
System.out.println(X[m]-X[m-1]+1);
}
else
{
long x=X[m]-X[m-1]+1;
long y=Y[m]-Y[m-1]+1;
System.out.println(x*y);
}
}
else
{
System.out.println(1);
}
}
}
}
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)
{
par[a]+=par[b]; //transfers the size
par[b]=a; //changes the parent
}
}
static ArrayList<Integer> IND(int B[],HashMap<Integer,Integer> mp)
{
ArrayList<Integer> A=new ArrayList<>();
for(int i:B)
{
if(mp.containsKey(i))
{
A.add(i);
int f=mp.get(i)-1;
if(f==0)
mp.remove(i);
else mp.put(i, f);
}
}
return A;
}
static HashMap<Integer,Integer> hash(int A[],int index)
{
HashMap<Integer,Integer> mp=new HashMap<Integer,Integer>();
for(int i=index; i<A.length; i++)
{
int f=mp.getOrDefault(A[i], 0)+1;
mp.put(A[i], f);
}
return mp;
}
static void swap(char A[],int a,int b)
{
char ch=A[a];
A[a]=A[b];
A[b]=ch;
}
static long lower_Bound(long A[],int low,int high, long 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 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 void setGraph(int N)
{
//set=new boolean[N+1];
//size=new int[N+1];
//par=new int[N+1];
g=new ArrayList<ArrayList<Integer>>();
//tg=new ArrayList<ArrayList<Integer>>();
for(int i=0; i<=N; i++)
{
g.add(new ArrayList<Integer>());
//tg.add(new ArrayList<Integer>());
}
}
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 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 long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
//Debugging Functions Starts
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(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
//Debugging Functions END
//----------------------
//IO FUNCTIONS STARTS
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 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;
}
//IO FUNCTIONS END
}
class paint implements Comparable<paint>
{
int color,index;
paint(int c,int i)
{
color=c;
index=i;
}
public int compareTo(paint X)
{
return this.color-X.color;
}
}
//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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
f29afb2261278cc6e479e2dd8c566ec7
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution1486B {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Solver1486B solver = new Solver1486B();
int n = in.nextInt();
for (int i = 0; i < n; i++) {
solver.solve(i, in, out);
}
out.close();
}
static class Solver1486B {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
long xn = getMid(n, x);
long yn = getMid(n, y);
out.println(xn * yn);
}
private int getMid(int n, int[] x) {
if (n % 2 == 1) {
return 1;
}
return x[n/2] - x[n/2-1] + 1;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
fffa9987ce6303b1fd94e267913c6b33
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
Integer[]x=new Integer[n];
Integer[]y=new Integer[n];
for(int i=0;i<n;i++) {
x[i]=sc.nextInt();
y[i]=sc.nextInt();
}
if(n%2==1) {
pw.println(1);
}else {
Arrays.sort(x);
Arrays.sort(y);
int p=n/2;
long ans=(x[p]-x[p-1]+1)*1l*(y[p]-y[p-1]+1);
pw.println(ans);
}
}
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Double(x).hashCode() * 31 + new Double(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
b6549b8890576c08917c823f605dba83
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
public class A implements Runnable {
public void run() {
long startTime = System.nanoTime();
int ts = nextInt();
while (ts-- > 0) {
int n = nextInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = nextInt();
y[i] = nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
println(get(x) * get(y));
}
if (fileIOMode) {
System.out.println((System.nanoTime() - startTime) / 1e9);
}
out.close();
}
private long get(int[] a) {
int n = a.length;
return a[n / 2] - a[(n - 1) / 2] + 1;
}
//-----------------------------------------------------------------------------------
private static boolean fileIOMode;
private static BufferedReader in;
private static PrintWriter out;
private static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
fileIOMode = args.length > 0 && args[0].equals("!");
if (fileIOMode) {
in = new BufferedReader(new FileReader("a.in"));
out = new PrintWriter("a.out");
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
tokenizer = new StringTokenizer("");
new Thread(new A()).start();
}
private static String nextLine() {
try {
return in.readLine();
} catch (IOException e) {
return null;
}
}
private static String nextToken() {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
private static int nextInt() {
return Integer.parseInt(nextToken());
}
private static long nextLong() {
return Long.parseLong(nextToken());
}
private static double nextDouble() {
return Double.parseDouble(nextToken());
}
private static BigInteger nextBigInteger() {
return new BigInteger(nextToken());
}
private static void print(Object o) {
if (fileIOMode) {
System.out.print(o);
}
out.print(o);
}
private static void println(Object o) {
if (fileIOMode) {
System.out.println(o);
}
out.println(o);
}
private static void printf(String s, Object... o) {
if (fileIOMode) {
System.out.printf(s, o);
}
out.printf(s, o);
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
efbae8afdc35a7a3c5e8ad6fb7a48fca
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A {
static FastScanner fs;
public static void main(String[] args) {
fs=new FastScanner();
int t = fs.nextInt();
while (t-->0)
solve();
}
public static void solve() {
int n = fs.nextInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i=0; i<n; i++) {
x[i] = fs.nextInt();
y[i] = fs.nextInt();
}
ruffleSort(x);
ruffleSort(y);
if (n%2==1) System.out.println(1);
else {
System.out.println(((long)x[n/2]-x[n/2-1]+1)*((long)y[n/2]-y[n/2-1]+1));
}
}
static class pair {
long v, i;
pair(long a, long b) {
v=a; i=b;
}
}
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static final Random random=new Random();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static int[] reverse(int[] a) {
int n=a.length;
int[] res=new int[n];
for (int i=0; i<n; i++) res[i]=a[n-1-i];
return res;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
faa939863e1d09764cbcd4c3d79234f0
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
main:
while (t-- > 0) {
int n = in.nextInt();
long[] xs = new long[n];
long[] ys = new long[n];
for (int i = 0; i < n; i++) {
xs[i] = in.nextInt();
ys[i] = in.nextInt();
}
Arrays.sort(xs);
Arrays.sort(ys);
System.out.println((xs[n / 2] - xs[(n - 1) / 2] + 1) * (ys[n / 2] - ys[(n - 1) / 2] + 1));
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
b7c1628ddbdd9cf46303be65b30b25cf
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
// cd C:\Users\Lenovo\Desktop\New
//ArrayList<Integer> a=new ArrayList<>();
//List<Integer> lis=new ArrayList<>();
//StringBuilder ans = new StringBuilder();
//HashMap<Integer,Integer> map=new HashMap<>();
public class cf {
static FastReader in=new FastReader();
static final Random random=new Random();
static int mod=1000000007;
//static long dp[]=new long[200002];
static int dp[][];
static int d;
static int res;
public static void main(String args[]) throws IOException {
FastReader sc=new FastReader();
//Scanner s=new Scanner(System.in);
int tt=sc.nextInt();
//int tt=1;
while(tt-->0){
int n=sc.nextInt();
int x[]=new int[n+1];
int y[]=new int[n+1];
x[0]=-1;
y[0]=-1;
for(int i=0;i<n;i++){
x[i]=sc.nextInt();
y[i]=sc.nextInt();
}
ruffleSort(x);
ruffleSort(y);
long x1=0;
long y1=0;
int pos1=(n+1)/2;
int pos2=(n+2)/2;
x1=x[pos2]-x[pos1]+1;
y1=y[pos2]-y[pos1]+1;
System.out.println(1l*x1*y1);
}
}
static boolean prime(int n){
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
static void factorial(int n){
long ret = 1;
while(n > 0){
ret = ret * n % mod;
n--;
}
System.out.println(ret);
}
static long find_min(int a,int arr[]){
long ans=Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++){
if(Math.abs(a-arr[i])<ans){
ans=Math.abs(a-arr[i]);
}
}
return ans;
}
static long find(ArrayList<Long> arr,long n){
int l=0;
int r=arr.size();
while(l+1<r){
int mid=(l+r)/2;
if(arr.get(mid)<n){
l=mid;
}
else{
r=mid;
}
}
return arr.get(l);
}
static void factorial(long []fact){
for(int i=2;i<14;i++){
fact[i]=1l*(i+1)*fact[i-1];
}
}
static void rotate(int ans[]){
int last=ans[0];
for(int i=0;i<ans.length-1;i++){
ans[i]=ans[i+1];
}
ans[ans.length-1]=last;
}
static int reduce(int n){
while(n>1){
if(n%2==1){
n--;
n=n/2;
}
else{
if(n%4==0){
n=n/4;
}
else{
break;
}
}
}
return n;
}
static long count(long n,int p,HashSet<Long> set){
if(n<Math.pow(2,p)){
set.add(n);
return count(2*n+1,p,set)+count(4*n,p,set)+1;
}
return 0;
}
static int countprimefactors(int n){
int ans=0;
int z=(int)Math.sqrt(n);
for(int i=2;i<=z;i++){
while(n%i==0){
ans++;
n=n/i;
}
}
if(n>1){
ans++;
}
return ans;
}
/*static int count(int arr[],int idx,long sum,int []dp){
if(idx>=arr.length){
return 0;
}
if(dp[idx]!=-1){
return dp[idx];
}
if(arr[idx]<0){
return dp[idx]=Math.max(1+count(arr,idx+1,sum+arr[idx],dp),count(arr,idx+1,sum,dp));
}
else{
return dp[idx]=1+count(arr,idx+1,sum+arr[idx],dp);
}
}*/
static String reverse(String s){
String ans="";
for(int i=s.length()-1;i>=0;i--){
ans+=s.charAt(i);
}
return ans;
}
static int find_max(int []check,int y){
int max=0;
for(int i=y;i>=0;i--){
if(check[i]!=0){
max=i;
break;
}
}
return max;
}
static int msb(int x){
int ans=0;
while(x!=0){
x=x/2;
ans++;
}
return ans;
}
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 int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
/* static boolean checkprime(int n1)
{
if(n1%2==0||n1%3==0)
return false;
else
{
for(int i=5;i*i<=n1;i+=6)
{
if(n1%i==0||n1%(i+2)==0)
return false;
}
return true;
}
}
*/
/* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, Integer> entry = iterator.next();
int value = entry.getValue();
if(value==1){
iterator.remove();
}
else{
entry.setValue(value-1);
}
}
*/
static class Pair implements Comparable
{
int a,b;
public String toString()
{
return a+" " + b;
}
public Pair(int x , int y)
{
a=x;b=y;
}
@Override
public int compareTo(Object o) {
Pair p = (Pair)o;
if((Math.abs(a)+Math.abs(b))!=(Math.abs(p.a)+Math.abs(p.b))){
return ((Math.abs(a)+Math.abs(b))-(Math.abs(p.a)+Math.abs(p.b)));
}
else{
return a-p.a;
}
/*if(p.a!=a){
return a-p.a;//in
}
else{
return b-p.b;//
}*/
}
}
/* public static boolean checkAP(List<Integer> lis){
for(int i=1;i<lis.size()-1;i++){
if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){
return false;
}
}
return true;
}
public static int minBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]>=val){
r=mid;
}
else{
l=mid;
}
}
return r;
}
public static int maxBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]<=val){
l=mid;
}
else{
r=mid;
}
}
return l;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}*/
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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
0d06ca29da472b013a6d9c91140d5760
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.StringTokenizer;
public class B1486 {
static FastScanner sc = new FastScanner(System.in);
static FastPrintStream out = new FastPrintStream(System.out);
public static void main(String[] args) {
int t = sc.nextInt();
for (int tn = 0; tn < t; tn++) {
int n = sc.nextInt();
int[] xs = new int[n], ys = new int[n];
for (int i = 0; i < n; i++) {
xs[i] = sc.nextInt();
ys[i] = sc.nextInt();
}
out.println(solve(xs, ys));
}
out.flush();
}
private static long solve(int[] xs, int[] ys) {
int n = xs.length;
xs = sorted(xs);
ys = sorted(ys);
if (n % 2 == 0) {
long lx = xs[n / 2 - 1], rx = xs[n / 2];
long ly = ys[n / 2 - 1], ry = ys[n / 2];
return (rx - lx + 1) * (ry - ly + 1);
} else {
return 1;
}
}
static long[] readLongArray(int n, FastScanner sc) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextLong();
}
return a;
}
static int[] readIntArray(int n, FastScanner sc) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
return a;
}
static int[] sorted(int[] array) {
ArrayList<Integer> list = new ArrayList<>(array.length);
for (int val : array) {
list.add(val);
}
list.sort(Comparator.naturalOrder());
return list.stream().mapToInt(val -> val).toArray();
}
static int[] sortedReverse(int[] array) {
ArrayList<Integer> list = new ArrayList<>(array.length);
for (int val : array) {
list.add(val);
}
list.sort(Comparator.reverseOrder());
return list.stream().mapToInt(val -> val).toArray();
}
static long[] sort(long[] array) {
ArrayList<Long> list = new ArrayList<>(array.length);
for (long val : array) {
list.add(val);
}
list.sort(Comparator.naturalOrder());
return list.stream().mapToLong(val -> val).toArray();
}
static long[] sortedReverse(long[] array) {
ArrayList<Long> list = new ArrayList<>(array.length);
for (long val : array) {
list.add(val);
}
list.sort(Comparator.reverseOrder());
return list.stream().mapToLong(val -> val).toArray();
}
private static class FastPrintStream {
private final BufferedWriter writer;
public FastPrintStream(PrintStream out) {
writer = new BufferedWriter(new OutputStreamWriter(out));
}
public void print(char c) {
try {
writer.write(Character.toString(c));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(int val) {
try {
writer.write(Integer.toString(val));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(long val) {
try {
writer.write(Long.toString(val));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(double val) {
try {
writer.write(Double.toString(val));
} catch (IOException e) {
e.printStackTrace();
}
}
public void print(String val) {
try {
writer.write(val);
} catch (IOException e) {
e.printStackTrace();
}
}
public void println(char c) {
print(c + "\n");
}
public void println(int val) {
print(val + "\n");
}
public void println(long val) {
print(val + "\n");
}
public void println(double val) {
print(val + "\n");
}
public void println(String str) {
print(str + "\n");
}
public void println() {
print("\n");
}
public void flush() {
try {
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class FastScanner {
private final BufferedReader br;
private StringTokenizer st;
public FastScanner(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public byte nextByte() {
return Byte.parseByte(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
69b3dd813db59d0b1813a718c3938d23
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.util.*;
import static java.lang.Math.*;
import java.io.*;
public class S {
public static int surv = 0;
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while(test > 0){
test--;
int n = Integer.parseInt(br.readLine());
int x[] = new int[n];
int y[] = new int[n];
for(int i = 0; i < n; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
x[i] = Integer.parseInt(st.nextToken());
y[i] = Integer.parseInt(st.nextToken());
}
solve(x, y, n, sb);
}
System.out.print(sb.toString());
}
final static int MOD = 1000000007;
public static void solve(int x[], int y[], int n, StringBuilder sb){
sort(x);
sort(y);
int xr = x[n/2];
int xl = xr;
if(n % 2 == 0){
xl = x[(n/2)-1];
}
int xd = xr-xl+1;
int yr = y[n/2];
int yl = yr;
if(n % 2 == 0){
yl = y[(n/2)-1];
}
int yd = yr-yl+1;
long ans = (long)yd*(long)xd;
sb.append(ans + "\n");
}
public static boolean isPossible(int sum, int pairs, int len, int k){
if(len % 2 == 0){
return pairs >= ((len/2)*k);
}else{
int ones = sum - (pairs*2);
if(pairs < (len/2)*k){
return false;
}
pairs -= ((len/2)*k);
return ((pairs*2) + ones) >= k;
}
}
public static void balance(int a[], int b[], int i, int amn){
a[i] -= amn;
b[i] += amn;
}
public static long powerr(long base, long exp){
if(exp < 2)return base;
if(exp % 2 == 0){
long ans = powerr(base, exp/2) % MOD;
ans *= ans;
ans %= MOD;
return ans;
}else{
return (((powerr(base, exp-1)) % MOD) * base) % MOD;
}
}
public static long power(long a, long b){
if(b == 0)return 1l;
long ans = power(a, b/2);
ans *= ans;
ans %= MOD;
if(b % 2 != 0){
ans *= a;
}
return ans % MOD;
}
public static int logLong(long a){
int ans = 0;
long b = 1;
while(b < a){
b*=2;
ans++;
}
return ans;
}
public static void rec(int a[], int l, int r, int d, int d_map[]){
if(l > r)return;
if(l == r){
d_map[a[l]] = d;
return;
}
int max = 0;
int max_ind = -1;
for(int i = l; i <= r; i++){
if(a[i] > max){
max_ind = i;
max = a[i];
}
}
d_map[a[max_ind]] = d;
rec(a, l, max_ind - 1, d+1, d_map);
rec(a, max_ind + 1, r, d+1, d_map);
}
public static void sort(int a[]){
List<Integer> l = new ArrayList<Integer>();
for(int val : a){
l.add(val);
}
Collections.sort(l);
int k = 0;
for(int val : l){
a[k++] = val;
}
}
public static void sortLong(long a[]){
List<Long> l = new ArrayList<Long>();
for(long val : a){
l.add(val);
}
Collections.sort(l);
int k = 0;
for(long val : l){
a[k++] = val;
}
}
public static boolean isPal(String s){
int l = 0;
int r = s.length() - 1;
while(l <= r){
if(s.charAt(l) != s.charAt(r)){
return false;
}
l++;
r--;
}
return true;
}
public static int gcd(int a, int b){
if(b > a){
int temp = a;
a = b;
b = temp;
}
if(b == 0)return a;
return gcd(b, a%b);
}
/* public static long gcd(long a, long b){
if(b > a){
long temp = a;
a = b;
b = temp;
}
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 class DJSet{
public int a[];
public DJSet(int n){
this.a = new int[n];
Arrays.fill(a, -1);
}
public int find(int val){
if(a[val] >= 0){
a[val] = find(a[val]);
return a[val];
}else{
return val;
}
}
public boolean union(int val1, int val2){
int p1 = find(val1);
int p2 = find(val2);
if(p1 == p2){
return false;
}
int size1 = Math.abs(a[p1]);
int size2 = Math.abs(a[p2]);
if(size1 >= size2){
a[p2] = p1;
a[p1] = (size1 + size2) * -1;
}else{
a[p1] = p2;
a[p2] = (size1 + size2) * -1;
}
return true;
}*/
/*
public static class DSU{
public int a[];
public DSU(int size){
this.a = new int[size];
Arrays.fill(a, -1);
}
public int find(int u){
if(a[u] < 0)return u;
a[u] = find(a[u]);
return a[u];
}
public boolean union(int u, int v){
int p1 = find(u);
int p2 = find(v);
if(p1 == p2)return false;
int size1 = a[p1] * -1;
int size2 = a[p2] * -1;
if(size1 >= size2){
a[p2] = p1;
a[p1] = (size1 + size2) * -1;
}else{
a[p1] = p2;
a[p2] = (size1 + size2) * -1;
}
return true;
}
}*/
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
7e39e15ca5a43c3c3f9ab02fdec62b34
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
//package practices.level1500.speedtraining;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class B1486 {
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;
6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial.
If getting stuck, skip it and solve other similar level problems.
Wait for 1 week then try to solve again. Only read editorial after you solved a problem.
7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already.
*/
/*
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();
Integer[] x = new Integer[n], y = new Integer[n];
for(int i = 0; i < n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
}
int c1 = compute(x), c2 = compute(y);
out.println(1L * c1 * c2);
}
out.close();
}
static int compute(Integer[] a) {
Arrays.sort(a);
if(a.length % 2 != 0) return 1;
return a[a.length / 2] - a[a.length / 2 - 1] + 1;
}
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
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
4d9d6efda05f9100f55b4e2f0c1ea492
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class easternexhibition {
static long[] ans;
static int ind;
static long[] x;
static long[] y;
static int n;
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
ans = new long[t];
for (int i = 0; i < t; i++) {
st = new StringTokenizer(br.readLine());
ind = i;
n = Integer.parseInt(st.nextToken());
x = new long[n+1];
y = new long[n+1];
for (int j = 1; j <= n; j++) {
st = new StringTokenizer(br.readLine());
x[j] = Long.parseLong(st.nextToken());
y[j] = Long.parseLong(st.nextToken());
}
x[0] = -1;
y[0] = -1;
solve();
}
br.close();
for (long v : ans) {
System.out.println(v);
}
}
static void solve() {
long v = 1;
Arrays.sort(x);
Arrays.sort(y);
if (n % 2 == 1) {
v = 1;
} else {
v = (x[n/2 + 1] - x[n/2] + 1) * (y[n/2 + 1] - y[n/2] + 1);
}
ans[ind] = v;
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 8
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
9af83514585c5b8306f70f8baf30035a
|
train_110.jsonl
|
1613658900
|
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$.
|
256 megabytes
|
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static boolean issorted(int []arr){
for(int i = 1;i<arr.length;i++){
if(arr[i]<arr[i-1]){
return false;
}
}
return true;
}
public static long sum(int []arr){
long sum = 0;
for(int i = 0;i<arr.length;i++){
sum+=arr[i];
}
return sum;
}
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){
return this.x - o.x; // sort increasingly on the basis of x
// return o.x - this.x // sort decreasingly on the basis of x
}
}
public static void swap(int []arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
static int []parent = new int[1000];
static int []size = new int[1000];
public static void make(int v){
parent[v] = v;
size[v] = 1;
}
public static int find(int v){
if(parent[v]==v){
return v;
}
else{
return parent[v] = find(parent[v]);
}
}
public static void union(int a,int b){
a = find(a);
b = find(b);
if(a!=b){
if(size[a]>size[b]){
parent[b] = parent[a];
size[b]+=size[a];
}
else{
parent[a] = parent[b];
size[a]+=size[b];
}
}
}
static boolean []visited = new boolean[1000];
public static void dfs(int vertex,ArrayList<ArrayList<Integer>>graph){
if(visited[vertex] == true){
return;
}
System.out.println(vertex);
for(int child : graph.get(vertex)){
// work to be done before entering the child
dfs(child,graph);
// work to be done after exitting the child
}
}
public static void displayint(int []arr){
for(int i = 0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void displaystr(String str){
StringBuilder sb = new StringBuilder(str);
for(int i = 0;i<sb.length();i++){
System.out.print(sb.charAt(i));
}
System.out.println();
}
public static boolean checkbalanceparenthesis(StringBuilder ans){
Stack<Character>st = new Stack<>();
int i = 0;
while(i<ans.length()){
if(ans.charAt(i) == '('){
st.push('(');
}
else{
if(st.size() == 0 || st.peek()!='('){
return false;
}
else{
st.pop();
}
}
}
return st.size() == 0;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int n = scn.nextInt();
int []x = new int[n];
int []y = new int[n];
for(int i = 0;i<n;i++){
x[i] = scn.nextInt();
y[i] = scn.nextInt();
}
Arrays.sort(x);
Arrays.sort(y);
if(n%2!=0){
System.out.println(1);
}
else{
long midx1 = x[(n/2)-1];
long midx2 = x[(n/2)];
long diff = (midx2-midx1)+1;
long midy1 = y[(n/2)-1];
long midy2 = y[(n/2)];
long diff2 = (midy2-midy1)+1;
System.out.println(diff*diff2);
}
}
}
}
|
Java
|
["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"]
|
1 second
|
["1\n4\n4\n4\n3\n1"]
|
NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
|
Java 17
|
standard input
|
[
"binary search",
"geometry",
"shortest paths",
"sortings"
] |
e0a1dc397838852957d0e15ec98d5efe
|
First line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \leq x_i, y_i \leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.
| 1,500
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
standard output
| |
PASSED
|
3b9004389e2a51c39510c1d789699d57
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static long myceil(long a, long b){
return (a+b-1)/b;
}
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
tr.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
static void calc(int n){
int i,j;
dp[n-1]=0;
if(n>1)
dp[n-2]=1;
for(i=n-3;i>=0;i--){
long ind=n-i-1;
dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD;
}
}
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[];
static int visited[];
static ArrayList<Pair<Integer,String>> adj[];
//static int dp[][][];
static int R,G,B,MOD=1000000007;
static int[] par,size,memo;
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static char ch[][], ch1[][];
// static void lengen(int arr[],int ind){
// memo[ind]=Math.max(lengen(arr,ind+1),lis(arr,))
// }
static int lis(int arr[],int n,int ind,int cur){
if(ind>=n){
return memo[ind]=0;
}
else if(ind>=0&&memo[ind]>-1){
return memo[ind];
}
else if(cur<arr[ind+1]){
return memo[ind]=Math.max(lis(arr,n,ind+1,arr[ind+1])+1,lis(arr,n,ind+1,cur));
}else{
return memo[ind]=lis(arr,n,ind+1,cur);
}
}
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,tno=0,tte;
//t=in.nextInt();
t=1;
//tte=t;
while(t-->0){
int n=in.nextInt();
int k=in.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++){
arr[i]=in.nextInt();
}
int l=1,r=n;
int mid=(l+r)/2,ans=0;
while(l<=r){
int min=0;
mid=(l+r)/2;
//w.println(l+" "+mid+" "+r);
int flag=0;
int temp[]=new int[n];
int pre[]=new int[n];
for(i=0;i<n;i++){
if(arr[i]<mid){
temp[i]=-1;
}else{
temp[i]=1;
}
if(i==0){
pre[i]=temp[i];
}else{
pre[i]=pre[i-1]+temp[i];
}
if(i>=k-1){
if(pre[i]-min>0){
l=mid+1;
ans=mid;
flag=1;
break;
}
min=Math.min(min,pre[i-k+1]);
}
}
if(flag==0){
r=mid-1;
}
// w.println(l+" "+mid+" "+r);
}
w.println(ans);
}
w.close();
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
7e5274845514580765c36cba98191fed
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],int size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static long myceil(long a, long b){
return (a+b-1)/b;
}
static long C(long n,long r){
long count=0,temp=n;
long ans=1;
long num=n-r+1,div=1;
while(num<=n){
ans*=num;
//ans+=MOD;
ans%=MOD;
ans*=mypow(div,MOD-2);
//ans+=MOD;
ans%=MOD;
num++;
div++;
}
ans+=MOD;
return ans%MOD;
}
static long fact(long a){
long i,ans=1;
for(i=1;i<=a;i++){
ans*=i;
ans%=MOD;
}
return ans%=MOD;
}
static void sieve(int n){
is_sieve[0]=1;
is_sieve[1]=1;
int i,j,k;
for(i=2;i<n;i++){
if(is_sieve[i]==0){
sieve[i]=i;
tr.add(i);
for(j=i*i;j<n;j+=i){
if(j>n||j<0){
break;
}
is_sieve[j]=1;
sieve[j]=i;
}
}
}
}
static void calc(int n){
int i,j;
dp[n-1]=0;
if(n>1)
dp[n-2]=1;
for(i=n-3;i>=0;i--){
long ind=n-i-1;
dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD;
}
}
static long mypow(long x,long y){
long temp;
if( y == 0)
return 1;
temp = mypow(x, y/2);
if (y%2 == 0)
return (temp*temp)%MOD;
else
return ((x*temp)%MOD*(temp)%MOD)%MOD;
}
static long dist[],dp[];
static int visited[];
static ArrayList<Pair<Integer,String>> adj[];
//static int dp[][][];
static int R,G,B,MOD=1000000007;
static int[] par,size,memo;
static int[] sieve,is_sieve;
static TreeSet<Integer> tr;
static char ch[][], ch1[][];
// static void lengen(int arr[],int ind){
// memo[ind]=Math.max(lengen(arr,ind+1),lis(arr,))
// }
static int lis(int arr[],int n,int ind,int cur){
if(ind>=n){
return memo[ind]=0;
}
else if(ind>=0&&memo[ind]>-1){
return memo[ind];
}
else if(cur<arr[ind+1]){
return memo[ind]=Math.max(lis(arr,n,ind+1,arr[ind+1])+1,lis(arr,n,ind+1,cur));
}else{
return memo[ind]=lis(arr,n,ind+1,cur);
}
}
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j,tno=0,tte;
//t=in.nextInt();
t=1;
//tte=t;
while(t-->0){
int n=in.nextInt();
int k=in.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++){
arr[i]=in.nextInt();
}
int l=1,r=n;
int mid=(l+r)/2,ans=0;
while(l<=r){
int min=0;
mid=(l+r)/2;
//w.println(l+" "+mid+" "+r);
int flag=0;
int temp[]=new int[n];
int pre[]=new int[n];
for(i=0;i<n;i++){
if(arr[i]<mid){
temp[i]=-1;
}else{
temp[i]=1;
}
if(i==0){
pre[i]=temp[i];
}else{
pre[i]=pre[i-1]+temp[i];
}
if(i>=k-1){
if(i==k-1){
if(pre[i]-min>0){
l=mid+1;
ans=mid;
flag=1;
break;
}
}else{
min=Math.min(min,pre[i-k]);
if(pre[i]-min>0){
l=mid+1;
ans=mid;
flag=1;
break;
}
}
}
}
if(flag==0){
r=mid-1;
}
// w.println(l+" "+mid+" "+r);
}
w.println(ans);
}
w.close();
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
5c29bc5f1f47e5dd87a8789d0532e16a
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import javax.sound.midi.Track;
import java.io.*;
public class tr0 {
static PrintWriter out;
static StringBuilder sb;
static long mod = (long) 1e9 + 7;
static long inf = (long) 1e16;
static int n, m;
static ArrayList<Integer>[] ad;
static int[][] remove, add;
static int[][] memo, memo1[];
static boolean vis[];
static long[] f, inv, ncr[];
static HashMap<Integer, Integer> hm;
static int[] pre, suf, Smax[], Smin[];
static int idmax, idmin;
static ArrayList<Integer> av;
static HashMap<Integer, Integer> mm;
static boolean[] msks;
static int[] lazy[], lazyCount;
static int[] a, dist;
static char[][] g;
static int ave;
static ArrayList<Integer> gl;
static int[][] arr;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
n = sc.nextInt();
int k = sc.nextInt();
int lo = 1;
int hi = n;
int ans = 0;
int[] a = sc.nextArrInt(n);
while (lo <= hi) {
int mid = lo + hi >> 1;
FenwickTree ft = new FenwickTree(n);
int b = 0;
boolean f = false;
for (int i = 0; i < n; i++) {
if (a[i] >= mid)
b++;
else
b--;
ft.point_update(i + 1, b);
if (i >= k - 1) {
if (b > 0)
f = true;
}
if (i >= k) {
int v = ft.rsq(i + 1 - k);
if (b - v > 0)
f = true;
}
}
if (f) {
ans = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
System.out.println(ans);
out.flush();
}
public static class FenwickTree { // one-based DS
int n;
int[] ft;
FenwickTree(int size) {
n = size;
ft = new int[n + 1];
Arrays.fill(ft, n + n + n);
}
int rsq(int b) // O(log n)
{
int sum = n + n + n;
while (b > 0) {
sum = Math.min(sum, ft[b]);
b -= b & -b;
} // min?
return sum;
}
void point_update(int k, int val) // O(log n), update = increment
{
while (k <= n) {
ft[k] = Math.min(val, ft[k]);
k += k & -k;
} // min?
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[1].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[1].longValue();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
d1f3169d5ae881a3a65ec2cccc1a8b55
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import javax.sound.midi.Track;
import java.io.*;
public class tr0 {
static PrintWriter out;
static StringBuilder sb;
static long mod = (long) 1e9 + 7;
static long inf = (long) 1e16;
static int n, m;
static ArrayList<Integer>[] ad;
static int[][] remove, add;
static int[][] memo, memo1[];
static boolean vis[];
static long[] f, inv, ncr[];
static HashMap<Integer, Integer> hm;
static int[] pre, suf, Smax[], Smin[];
static int idmax, idmin;
static ArrayList<Integer> av;
static HashMap<Integer, Integer> mm;
static boolean[] msks;
static int[] lazy[], lazyCount;
static int[] a, dist;
static char[][] g;
static int ave;
static ArrayList<Integer> gl;
static int[][] arr;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
int lo = 1;
int hi = n;
int ans = 0;
int[] a = sc.nextArrInt(n);
while (lo <= hi) {
int mid = lo + hi >> 1;
int[] min = new int[n];
int b = 0;
boolean f = false;
for (int i = 0; i < n; i++) {
if (a[i] >= mid)
b++;
else
b--;
min[i] = b;
if (i > 0)
min[i] = Math.min(min[i], min[i - 1]);
if (i >= k - 1) {
if (b > 0)
f = true;
}
if (i >= k) {
int v = min[i - k];
if (b - v > 0)
f = true;
}
}
if (f) {
ans = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
System.out.println(ans);
out.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[1].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[1].longValue();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
75e6db39688f9e6e1f958036f7a11f5a
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
//stan hu tao
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1486D2
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[] arr = readArr(N, infile, st);
int low = arr[0];
int high = arr[0];
for(int x: arr)
{
low = min(low, x);
high = max(high, x);
}
while(low != high)
{
int mid = (low+high+1)/2;
int[] tags = new int[N];
Arrays.fill(tags, -1);
for(int i=0; i < N; i++)
if(arr[i] >= mid)
tags[i] = 1;
int[] psums = new int[N];
psums[0] = tags[0];
for(int i=1; i < N; i++)
psums[i] = psums[i-1]+tags[i];
boolean works = false;
int min = 0;
for(int i=0; i < N; i++)
{
if(i >= K)
{
min = min(min, psums[i-K]);
if(psums[i] > min)
works = true;
}
}
if(psums[K-1] > 0)
works = true;
if(works)
low = mid;
else
high = mid-1;
}
System.out.println(low);
}
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;
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
58152293fc6b0295305917e0a4844059
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.math.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public class a{
static long[] count,count1,count2;
static boolean[] prime;
static Node[] nodes,nodes1,nodes2;
static long[] arr;
static long[][] cost;
static int[] arrInt,darrInt,farrInt;
static long[][] dp;
static char[] ch,ch1;
static long[] darr,farr;
static long[][] mat,mat1;
static boolean[] vis;
static long x,h;
static long maxl;
static double dec;
static long mx = (long)1e7;
static long inf = (long)1e15;
static String s,s1,s2,s3,s4;
static long minl;
static long mod = ((long)(1e9))+7;
// static int minl = -1;
// static long n;
static int n,n1,n2,q,r1,c1,r2,c2;
static long a;
static long b;
static long c;
static long d;
static long y,z;
static int m;
static int ans;
static long k;
static FastScanner sc;
static String[] str,str1;
static Set<Integer> set,set1,set2;
static SortedSet<Long> ss;
static List<Long> list,list1,list2,list3;
static PriorityQueue<Integer> pq,pq1,max,min;
static LinkedList<Node> ll,ll1,ll2;
static Map<Integer,List<Integer>> map1;
static Map<Long,Integer> map;
static StringBuilder sb,sb1,sb2;
static int index;
static long[] sum;
static int[] dx = {0,-1,0,1,-1,1,-1,1};
static int[] dy = {-1,0,1,0,-1,-1,1,1};
// public static void solve(){
// FastScanner sc = new FastScanner();
// // int t = sc.nextInt();
// int t = 1;
// for(int tt = 0 ; tt < t ; tt++){
// // s = sc.next();
// // s1 = sc.next();
// n = sc.nextInt();
// m = sc.nextInt();
// // int k = sc.nextInt();
// // sb = new StringBuilder();
// map = new HashMap<>();
// map1 = new HashMap<>();
// // q = sc.nextInt();
// // sb = new StringBuilder();
// // long k = sc.nextLong();
// // ch = sc.next().toCharArray();
// // count = new int[200002];
// // m = sc.nextInt();
// for(int o = 0 ; o < m ; o++){
// int l = sc.nextInt()-1;
// int r = sc.nextInt()-1;
// }
// }
// }
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public static void solve(){
int maxl = -1;
for(int i = 0 ; i < n ; i++)
maxl = Math.max(maxl,arrInt[i]);
int l = 1;
int k = m;
int r = maxl+1;
int ans = 1;
while(r - l > 1){
int mid = (l+r)/2;
int sum = 0;
int[] count = new int[n];
for(int i = 0 ; i < n ; i++){
if(arrInt[i] < mid)
sum += -1;
else
sum += 1;
if(i > 0)
count[i] = Math.min(sum,count[i-1]);
else
count[i] = sum;
if(((i >= k-1) && (sum > 0)) || ((i >= k) && (sum - count[i-k] > 0))){
ans = mid;
l = mid;
break;
}
if(i == n-1)
r = mid;
}
}
System.out.println(ans);
}
public static void main(String[] args) {
sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
// int l = 1;
while(t > 0){
// n = sc.nextInt();
// n = sc.nextLong();
// k = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// d = sc.nextLong();
n = sc.nextInt();
// n1 = sc.nextInt();
m = sc.nextInt();
// q = sc.nextInt();
// k = sc.nextLong();
// x = sc.nextLong();
// d = sc.nextLong();
// s = sc.next();
// ch = sc.next().toCharArray();
// ch1 = sc.next().toCharArray();
// n = 6;
// arr = new long[n];
// for(int i = 0 ; i < n ; i++){
// arr[i] = sc.nextLong();
// }
arrInt = new int[n];
for(int i = 0 ; i < n ; i++){
arrInt[i] = sc.nextInt();
}
// x = sc.nextLong();
// y = sc.nextLong();
// ch = sc.next().toCharArray();
// m = n;
// m = sc.nextInt();
// darr = new long[m];
// for(int i = 0 ; i < m ; i++){
// darr[i] = sc.nextLong();
// }
// k = sc.nextLong();
// m = n;
// darrInt = new int[n];
// for(int i = 0 ; i < n ; i++){
// darrInt[i] = sc.nextInt();
// }
// farrInt = new int[m];
// for(int i = 0; i < m ; i++){
// farrInt[i] = sc.nextInt();
// }
// m = n;
// mat = new long[n][m];
// for(int i = 0 ; i < n ; i++){
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = sc.nextLong();
// }
// }
// m = n;
// mat = new char[n][m];
// for(int i = 0 ; i < n ; i++){
// String s = sc.next();
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = s.charAt(j);
// }
// }
// str = new String[n];
// for(int i = 0 ; i < n ; i++)
// str[i] = sc.next();
// nodes = new Node[n];
// for(int i = 0 ; i < n ;i++)
// nodes[i] = new Node(sc.nextLong(),sc.nextLong());
solve();
t -= 1;
}
}
public static int log(long n,long base){
if(n == 0 || n == 1)
return 0;
if(n == base)
return 1;
double num = Math.log(n);
double den = Math.log(base);
if(den == 0)
return 0;
return (int)(num/den);
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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 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 (b/gcd(b, a % b)) * a;
}
public static long mod_inverse(long a,long mod){
long x1=1,x2=0;
long p=mod,q,t;
while(a%p!=0){
q = a/p;
t = x1-q*x2;
x1=x2; x2=t;
t=a%p;
a=p; p=t;
}
return x2<0 ? x2+mod : x2;
}
public static void swap(long[] curr,int i,int j){
long temp = curr[j];
curr[j] = curr[i];
curr[i] = temp;
}
static final Random random=new Random();
static void ruffleSortLong(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortInt(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortChar(char[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
char temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long binomialCoeff(long n, long k){
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res = (res*(n - i));
res = (res/(i + 1));
}
return res;
}
static long[] fact;
public static long inv(long n){
return power(n, mod-2);
}
public static void fact(int n){
fact = new long[n+1];
fact[0] = 1;
for(int j = 1;j<=n;j++)
fact[j] = (fact[j-1]*(long)j)%mod;
}
public static long binom(int n, int k){
fact(n+1);
long prod = fact[n];
prod*=inv(fact[n-k]);
prod%=mod;
prod*=inv(fact[k]);
prod%=mod;
return prod;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
static void sieve(int n){
prime = new boolean[n+1];
for(int i=2;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;
}
}
}
static class Node{
long first;
long second;
Node(long f,long s){
this.first = f;
this.second = s;
}
}
static long sq(long num){
return num*num;
}
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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
5ed451aa7bf0325e792abbf705261cd0
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.math.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public class a{
static long[] count,count1,count2;
static boolean[] prime;
static Node[] nodes,nodes1,nodes2;
static long[] arr;
static long[][] cost;
static int[] arrInt,darrInt,farrInt;
static long[][] dp;
static char[] ch,ch1;
static long[] darr,farr;
static long[][] mat,mat1;
static boolean[] vis;
static long x,h;
static long maxl;
static double dec;
static long mx = (long)1e7;
static long inf = (long)1e15;
static String s,s1,s2,s3,s4;
static long minl;
static long mod = ((long)(1e9))+7;
// static int minl = -1;
// static long n;
static int n,n1,n2,q,r1,c1,r2,c2;
static long a;
static long b;
static long c;
static long d;
static long y,z;
static int m;
static int ans;
static long k;
static FastScanner sc;
static String[] str,str1;
static Set<Integer> set,set1,set2;
static SortedSet<Long> ss;
static List<Long> list,list1,list2,list3;
static PriorityQueue<Integer> pq,pq1,max,min;
static LinkedList<Node> ll,ll1,ll2;
static Map<Integer,List<Integer>> map1;
static Map<Long,Integer> map;
static StringBuilder sb,sb1,sb2;
static int index;
static long[] sum;
static int[] dx = {0,-1,0,1,-1,1,-1,1};
static int[] dy = {-1,0,1,0,-1,-1,1,1};
// public static void solve(){
// FastScanner sc = new FastScanner();
// // int t = sc.nextInt();
// int t = 1;
// for(int tt = 0 ; tt < t ; tt++){
// // s = sc.next();
// // s1 = sc.next();
// n = sc.nextInt();
// m = sc.nextInt();
// // int k = sc.nextInt();
// // sb = new StringBuilder();
// map = new HashMap<>();
// map1 = new HashMap<>();
// // q = sc.nextInt();
// // sb = new StringBuilder();
// // long k = sc.nextLong();
// // ch = sc.next().toCharArray();
// // count = new int[200002];
// // m = sc.nextInt();
// for(int o = 0 ; o < m ; o++){
// int l = sc.nextInt()-1;
// int r = sc.nextInt()-1;
// }
// }
// }
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public static void solve(){
int maxl = -1;
for(int i = 0 ; i < n ; i++)
maxl = Math.max(maxl,arrInt[i]);
int l = 1;
int k = m;
int r = maxl;
int ans = 1;
while(l <= r){
int mid = (l+r)/2;
int sum = 0;
int[] count = new int[n];
for(int i = 0 ; i < n ; i++){
if(arrInt[i] < mid)
sum += -1;
else
sum += 1;
if(i > 0)
count[i] = Math.min(sum,count[i-1]);
else
count[i] = sum;
if(((i >= k-1) && (sum > 0)) || ((i >= k) && (sum - count[i-k] > 0))){
ans = mid;
l = mid+1;
break;
}
if(i == n-1)
r = mid-1;
}
}
System.out.println(ans);
}
public static void main(String[] args) {
sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
// int l = 1;
while(t > 0){
// n = sc.nextInt();
// n = sc.nextLong();
// k = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// d = sc.nextLong();
n = sc.nextInt();
// n1 = sc.nextInt();
m = sc.nextInt();
// q = sc.nextInt();
// k = sc.nextLong();
// x = sc.nextLong();
// d = sc.nextLong();
// s = sc.next();
// ch = sc.next().toCharArray();
// ch1 = sc.next().toCharArray();
// n = 6;
// arr = new long[n];
// for(int i = 0 ; i < n ; i++){
// arr[i] = sc.nextLong();
// }
arrInt = new int[n];
for(int i = 0 ; i < n ; i++){
arrInt[i] = sc.nextInt();
}
// x = sc.nextLong();
// y = sc.nextLong();
// ch = sc.next().toCharArray();
// m = n;
// m = sc.nextInt();
// darr = new long[m];
// for(int i = 0 ; i < m ; i++){
// darr[i] = sc.nextLong();
// }
// k = sc.nextLong();
// m = n;
// darrInt = new int[n];
// for(int i = 0 ; i < n ; i++){
// darrInt[i] = sc.nextInt();
// }
// farrInt = new int[m];
// for(int i = 0; i < m ; i++){
// farrInt[i] = sc.nextInt();
// }
// m = n;
// mat = new long[n][m];
// for(int i = 0 ; i < n ; i++){
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = sc.nextLong();
// }
// }
// m = n;
// mat = new char[n][m];
// for(int i = 0 ; i < n ; i++){
// String s = sc.next();
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = s.charAt(j);
// }
// }
// str = new String[n];
// for(int i = 0 ; i < n ; i++)
// str[i] = sc.next();
// nodes = new Node[n];
// for(int i = 0 ; i < n ;i++)
// nodes[i] = new Node(sc.nextLong(),sc.nextLong());
solve();
t -= 1;
}
}
public static int log(long n,long base){
if(n == 0 || n == 1)
return 0;
if(n == base)
return 1;
double num = Math.log(n);
double den = Math.log(base);
if(den == 0)
return 0;
return (int)(num/den);
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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 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 (b/gcd(b, a % b)) * a;
}
public static long mod_inverse(long a,long mod){
long x1=1,x2=0;
long p=mod,q,t;
while(a%p!=0){
q = a/p;
t = x1-q*x2;
x1=x2; x2=t;
t=a%p;
a=p; p=t;
}
return x2<0 ? x2+mod : x2;
}
public static void swap(long[] curr,int i,int j){
long temp = curr[j];
curr[j] = curr[i];
curr[i] = temp;
}
static final Random random=new Random();
static void ruffleSortLong(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortInt(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortChar(char[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
char temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long binomialCoeff(long n, long k){
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res = (res*(n - i));
res = (res/(i + 1));
}
return res;
}
static long[] fact;
public static long inv(long n){
return power(n, mod-2);
}
public static void fact(int n){
fact = new long[n+1];
fact[0] = 1;
for(int j = 1;j<=n;j++)
fact[j] = (fact[j-1]*(long)j)%mod;
}
public static long binom(int n, int k){
fact(n+1);
long prod = fact[n];
prod*=inv(fact[n-k]);
prod%=mod;
prod*=inv(fact[k]);
prod%=mod;
return prod;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
static void sieve(int n){
prime = new boolean[n+1];
for(int i=2;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;
}
}
}
static class Node{
long first;
long second;
Node(long f,long s){
this.first = f;
this.second = s;
}
}
static long sq(long num){
return num*num;
}
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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
926358b19b0a4974a6b2d95c9a2e8d48
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.math.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public class a{
static long[] count,count1,count2;
static boolean[] prime;
static Node[] nodes,nodes1,nodes2;
static long[] arr;
static long[][] cost;
static int[] arrInt,darrInt,farrInt;
static long[][] dp;
static char[] ch,ch1;
static long[] darr,farr;
static long[][] mat,mat1;
static boolean[] vis;
static long x,h;
static long maxl;
static double dec;
static long mx = (long)1e7;
static long inf = (long)1e15;
static String s,s1,s2,s3,s4;
static long minl;
static long mod = ((long)(1e9))+7;
// static int minl = -1;
// static long n;
static int n,n1,n2,q,r1,c1,r2,c2;
static long a;
static long b;
static long c;
static long d;
static long y,z;
static int m;
static int ans;
static long k;
static FastScanner sc;
static String[] str,str1;
static Set<Integer> set,set1,set2;
static SortedSet<Long> ss;
static List<Long> list,list1,list2,list3;
static PriorityQueue<Integer> pq,pq1,max,min;
static LinkedList<Node> ll,ll1,ll2;
static Map<Integer,List<Integer>> map1;
static Map<Long,Integer> map;
static StringBuilder sb,sb1,sb2;
static int index;
static long[] sum;
static int[] dx = {0,-1,0,1,-1,1,-1,1};
static int[] dy = {-1,0,1,0,-1,-1,1,1};
// public static void solve(){
// FastScanner sc = new FastScanner();
// // int t = sc.nextInt();
// int t = 1;
// for(int tt = 0 ; tt < t ; tt++){
// // s = sc.next();
// // s1 = sc.next();
// n = sc.nextInt();
// m = sc.nextInt();
// // int k = sc.nextInt();
// // sb = new StringBuilder();
// map = new HashMap<>();
// map1 = new HashMap<>();
// // q = sc.nextInt();
// // sb = new StringBuilder();
// // long k = sc.nextLong();
// // ch = sc.next().toCharArray();
// // count = new int[200002];
// // m = sc.nextInt();
// for(int o = 0 ; o < m ; o++){
// int l = sc.nextInt()-1;
// int r = sc.nextInt()-1;
// }
// }
// }
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public static void solve(){
int l = 1;
int k = m;
int r = n;
int ans = 1;
while(l <= r){
int mid = (l+r)/2;
int sum = 0;
int[] count = new int[n];
for(int i = 0 ; i < n ; i++){
if(arrInt[i] < mid)
sum += -1;
else
sum += 1;
if(i > 0)
count[i] = Math.min(sum,count[i-1]);
else
count[i] = sum;
if(((i >= k-1) && (sum > 0)) || ((i >= k) && (sum - count[i-k] > 0))){
ans = mid;
l = mid+1;
break;
}
if(i == n-1)
r = mid-1;
}
}
System.out.println(ans);
}
public static void main(String[] args) {
sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
// int l = 1;
while(t > 0){
// n = sc.nextInt();
// n = sc.nextLong();
// k = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// d = sc.nextLong();
n = sc.nextInt();
// n1 = sc.nextInt();
m = sc.nextInt();
// q = sc.nextInt();
// k = sc.nextLong();
// x = sc.nextLong();
// d = sc.nextLong();
// s = sc.next();
// ch = sc.next().toCharArray();
// ch1 = sc.next().toCharArray();
// n = 6;
// arr = new long[n];
// for(int i = 0 ; i < n ; i++){
// arr[i] = sc.nextLong();
// }
arrInt = new int[n];
for(int i = 0 ; i < n ; i++){
arrInt[i] = sc.nextInt();
}
// x = sc.nextLong();
// y = sc.nextLong();
// ch = sc.next().toCharArray();
// m = n;
// m = sc.nextInt();
// darr = new long[m];
// for(int i = 0 ; i < m ; i++){
// darr[i] = sc.nextLong();
// }
// k = sc.nextLong();
// m = n;
// darrInt = new int[n];
// for(int i = 0 ; i < n ; i++){
// darrInt[i] = sc.nextInt();
// }
// farrInt = new int[m];
// for(int i = 0; i < m ; i++){
// farrInt[i] = sc.nextInt();
// }
// m = n;
// mat = new long[n][m];
// for(int i = 0 ; i < n ; i++){
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = sc.nextLong();
// }
// }
// m = n;
// mat = new char[n][m];
// for(int i = 0 ; i < n ; i++){
// String s = sc.next();
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = s.charAt(j);
// }
// }
// str = new String[n];
// for(int i = 0 ; i < n ; i++)
// str[i] = sc.next();
// nodes = new Node[n];
// for(int i = 0 ; i < n ;i++)
// nodes[i] = new Node(sc.nextLong(),sc.nextLong());
solve();
t -= 1;
}
}
public static int log(long n,long base){
if(n == 0 || n == 1)
return 0;
if(n == base)
return 1;
double num = Math.log(n);
double den = Math.log(base);
if(den == 0)
return 0;
return (int)(num/den);
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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 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 (b/gcd(b, a % b)) * a;
}
public static long mod_inverse(long a,long mod){
long x1=1,x2=0;
long p=mod,q,t;
while(a%p!=0){
q = a/p;
t = x1-q*x2;
x1=x2; x2=t;
t=a%p;
a=p; p=t;
}
return x2<0 ? x2+mod : x2;
}
public static void swap(long[] curr,int i,int j){
long temp = curr[j];
curr[j] = curr[i];
curr[i] = temp;
}
static final Random random=new Random();
static void ruffleSortLong(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortInt(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortChar(char[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
char temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long binomialCoeff(long n, long k){
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res = (res*(n - i));
res = (res/(i + 1));
}
return res;
}
static long[] fact;
public static long inv(long n){
return power(n, mod-2);
}
public static void fact(int n){
fact = new long[n+1];
fact[0] = 1;
for(int j = 1;j<=n;j++)
fact[j] = (fact[j-1]*(long)j)%mod;
}
public static long binom(int n, int k){
fact(n+1);
long prod = fact[n];
prod*=inv(fact[n-k]);
prod%=mod;
prod*=inv(fact[k]);
prod%=mod;
return prod;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
static void sieve(int n){
prime = new boolean[n+1];
for(int i=2;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;
}
}
}
static class Node{
long first;
long second;
Node(long f,long s){
this.first = f;
this.second = s;
}
}
static long sq(long num){
return num*num;
}
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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
ad6815fee9008fb9ecfccf9696739cea
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.math.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public class a{
static long[] count,count1,count2;
static boolean[] prime;
static Node[] nodes,nodes1,nodes2;
static long[] arr;
static long[][] cost;
static int[] arrInt,darrInt,farrInt;
static long[][] dp;
static char[] ch,ch1;
static long[] darr,farr;
static long[][] mat,mat1;
static boolean[] vis;
static long x,h;
static long maxl;
static double dec;
static long mx = (long)1e7;
static long inf = (long)1e15;
static String s,s1,s2,s3,s4;
static long minl;
static long mod = ((long)(1e9))+7;
// static int minl = -1;
// static long n;
static int n,n1,n2,q,r1,c1,r2,c2;
static long a;
static long b;
static long c;
static long d;
static long y,z;
static int m;
static int ans;
static long k;
static FastScanner sc;
static String[] str,str1;
static Set<Integer> set,set1,set2;
static SortedSet<Long> ss;
static List<Long> list,list1,list2,list3;
static PriorityQueue<Integer> pq,pq1,max,min;
static LinkedList<Node> ll,ll1,ll2;
static Map<Integer,List<Integer>> map1;
static Map<Long,Integer> map;
static StringBuilder sb,sb1,sb2;
static int index;
static long[] sum;
static int[] dx = {0,-1,0,1,-1,1,-1,1};
static int[] dy = {-1,0,1,0,-1,-1,1,1};
// public static void solve(){
// FastScanner sc = new FastScanner();
// // int t = sc.nextInt();
// int t = 1;
// for(int tt = 0 ; tt < t ; tt++){
// // s = sc.next();
// // s1 = sc.next();
// n = sc.nextInt();
// m = sc.nextInt();
// // int k = sc.nextInt();
// // sb = new StringBuilder();
// map = new HashMap<>();
// map1 = new HashMap<>();
// // q = sc.nextInt();
// // sb = new StringBuilder();
// // long k = sc.nextLong();
// // ch = sc.next().toCharArray();
// // count = new int[200002];
// // m = sc.nextInt();
// for(int o = 0 ; o < m ; o++){
// int l = sc.nextInt()-1;
// int r = sc.nextInt()-1;
// }
// }
// }
//--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------
public static void solve(){
// int maxl = -1;
// for(int i = 0 ; i < n ; i++)
// maxl = Math.max(maxl,arrInt[i]);
int l = 1;
int k = m;
int r = n;
int ans = 1;
while(l <= r){
int mid = (l+r)/2;
int sum = 0;
int[] count = new int[n];
for(int i = 0 ; i < n ; i++){
if(arrInt[i] < mid)
sum += -1;
else
sum += 1;
if(i > 0)
count[i] = Math.min(sum,count[i-1]);
else
count[i] = sum;
if((i >= k-1) && (sum > 0 || (i >= k && sum - count[i-k] > 0))){
ans = mid;
l = mid+1;
break;
}
if(i == n-1)
r = mid-1;
}
}
System.out.println(ans);
}
public static void main(String[] args) {
sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
// int l = 1;
while(t > 0){
// n = sc.nextInt();
// n = sc.nextLong();
// k = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// d = sc.nextLong();
n = sc.nextInt();
// n1 = sc.nextInt();
m = sc.nextInt();
// q = sc.nextInt();
// k = sc.nextLong();
// x = sc.nextLong();
// d = sc.nextLong();
// s = sc.next();
// ch = sc.next().toCharArray();
// ch1 = sc.next().toCharArray();
// n = 6;
// arr = new long[n];
// for(int i = 0 ; i < n ; i++){
// arr[i] = sc.nextLong();
// }
arrInt = new int[n];
for(int i = 0 ; i < n ; i++){
arrInt[i] = sc.nextInt();
}
// x = sc.nextLong();
// y = sc.nextLong();
// ch = sc.next().toCharArray();
// m = n;
// m = sc.nextInt();
// darr = new long[m];
// for(int i = 0 ; i < m ; i++){
// darr[i] = sc.nextLong();
// }
// k = sc.nextLong();
// m = n;
// darrInt = new int[n];
// for(int i = 0 ; i < n ; i++){
// darrInt[i] = sc.nextInt();
// }
// farrInt = new int[m];
// for(int i = 0; i < m ; i++){
// farrInt[i] = sc.nextInt();
// }
// m = n;
// mat = new long[n][m];
// for(int i = 0 ; i < n ; i++){
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = sc.nextLong();
// }
// }
// m = n;
// mat = new char[n][m];
// for(int i = 0 ; i < n ; i++){
// String s = sc.next();
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = s.charAt(j);
// }
// }
// str = new String[n];
// for(int i = 0 ; i < n ; i++)
// str[i] = sc.next();
// nodes = new Node[n];
// for(int i = 0 ; i < n ;i++)
// nodes[i] = new Node(sc.nextLong(),sc.nextLong());
solve();
t -= 1;
}
}
public static int log(long n,long base){
if(n == 0 || n == 1)
return 0;
if(n == base)
return 1;
double num = Math.log(n);
double den = Math.log(base);
if(den == 0)
return 0;
return (int)(num/den);
}
public static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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 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 (b/gcd(b, a % b)) * a;
}
public static long mod_inverse(long a,long mod){
long x1=1,x2=0;
long p=mod,q,t;
while(a%p!=0){
q = a/p;
t = x1-q*x2;
x1=x2; x2=t;
t=a%p;
a=p; p=t;
}
return x2<0 ? x2+mod : x2;
}
public static void swap(long[] curr,int i,int j){
long temp = curr[j];
curr[j] = curr[i];
curr[i] = temp;
}
static final Random random=new Random();
static void ruffleSortLong(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortInt(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSortChar(char[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
char temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long binomialCoeff(long n, long k){
long res = 1;
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (long i = 0; i < k; ++i) {
res = (res*(n - i));
res = (res/(i + 1));
}
return res;
}
static long[] fact;
public static long inv(long n){
return power(n, mod-2);
}
public static void fact(int n){
fact = new long[n+1];
fact[0] = 1;
for(int j = 1;j<=n;j++)
fact[j] = (fact[j-1]*(long)j)%mod;
}
public static long binom(int n, int k){
fact(n+1);
long prod = fact[n];
prod*=inv(fact[n-k]);
prod%=mod;
prod*=inv(fact[k]);
prod%=mod;
return prod;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
static void sieve(int n){
prime = new boolean[n+1];
for(int i=2;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;
}
}
}
static class Node{
long first;
long second;
Node(long f,long s){
this.first = f;
this.second = s;
}
}
static long sq(long num){
return num*num;
}
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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
5f068e954b6956fdc06d169de51e2dcf
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
// graph, dfs,bfs, get connected components,iscycle, isbipartite, dfs on trees
public class scratch_25 {
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);
}
}
}
}
}
// int count=0;
//static long count=0;
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
static class Pair implements Comparable<Pair> {
long x;
long y;
public Pair(long x, long y) {
this.x = x;
this.y = y;}
@Override
public String toString() {
return x + " " + y;
}
@Override
public int compareTo(Pair o) {
if(this.x >o.x){
return 1;
}
else if(this.x==o.x){
return 0;
}
else{
return -1;
}
}
}
// After writing solution, quick scan for:
// array out of bounds
// special cases e.g. n=1?
//
// Big numbers arithmetic bugs:
// int overflow
// sorting, or taking max, or negative after MOD
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n= Reader.nextInt();
int k= Reader.nextInt();
long arr[]= new long[n];
HashMap<Long, Integer>map= new HashMap<>();
for (int i = 0; i <n ; i++) {
arr[i]= Reader.nextLong();
map.put(arr[i],map.getOrDefault(arr[i],0)+1);
}
ArrayList<Long>pos= new ArrayList<>(map.keySet());
Collections.sort(pos);
int y= pos.size();
int min=0;
int max= y-1;
while(max-min>1){
int mid= min+(max-min)/2;
long x= pos.get(mid);
if(possible(arr,x,n,k)){
min=mid;
}
else{
max= mid-1;
}
}
while(min!=y-1 && possible(arr,pos.get(min+1),n,k)){
min++;
}
System.out.println(pos.get(min));
// System.out.println(possible(arr,3,n,k));
out.flush();
out.close();
}
public static boolean possible(long arr1[], long x,int n,int k){
long arr[]= new long[n];
for (int i = 0; i <n ; i++) {
arr[i]= arr1[i];
}
for (int i = 0; i <n ; i++) {
if(arr[i]<x){
arr[i]=-1;
}
else{
arr[i]=1;
}
}
// System.out.println("arr="+Arrays.toString(arr));
long mins[]= new long[n];
long psums[]= new long[n];
long psum=arr[0];
psums[0]=psum;
mins[0]=psum;
long min=psum;
for (int i = 1; i <n ; i++) {
psum+=arr[i];
psums[i]=psum;
min= Math.min(min,psum);
mins[i]= min;
}
// System.out.println("mins="+Arrays.toString(mins));
// System.out.println("psums="+Arrays.toString(psums));
boolean b=false;
for (int i = k-1; i <n ; i++) {
int a= i-k;
if(a<0){
if(psums[i]>0){
b=true;
break;
}
}
else{
if(psums[i]-Math.min(mins[i-k],0)>0){
b=true;
break;
}
}
}
return b;
}
public static int localMinUtil(int[] arr, int low,
int high, int n)
{
// Find index of middle element
int mid = low + (high - low) / 2;
// Compare middle element with its neighbours
// (if neighbours exist)
if(mid == 0 || arr[mid - 1] > arr[mid] && mid == n - 1 ||
arr[mid] < arr[mid + 1])
return mid;
// If middle element is not minima and its left
// neighbour is smaller than it, then left half
// must have a local minima.
else if(mid > 0 && arr[mid - 1] < arr[mid])
return localMinUtil(arr, low, mid - 1, n);
// If middle element is not minima and its right
// neighbour is smaller than it, then right half
// must have a local minima.
return localMinUtil(arr, mid + 1, high, n);
}
// A wrapper over recursive function localMinUtil()
public static int localMin(int[] arr, int n)
{
return localMinUtil(arr, 0, n - 1, n);
}
public static String convert(String s,int n){
if(s.length()==n){
return s;
}
else{
int x= s.length();
int v=n-x;
String str="";
for (int i = 0; i <v ; i++) {
str+='0';
}
str+=s;
return str;
}
}
public static int lower(int arr[],int key){
int low = 0;
int high = arr.length-1;
while(low < high){
int mid = low + (high - low)/2;
if(arr[mid] >= key){
high = mid;
}
else{
low = mid+1;
}
}
return low;
}
public static int upper(int arr[],int key){
int low = 0;
int high = arr.length-1;
while(low < high){
int mid = low + (high - low+1)/2;
if(arr[mid] <= key){
low = mid;
}
else{
high = mid-1;
}
}
return low;
}
static long modExp(long a, long b, long mod) {
//System.out.println("a is " + a + " and b is " + b);
if (a==1) return 1;
long ans = 1;
while (b!=0) {
if (b%2==1) {
ans = (ans*a)%mod;
}
a = (a*a)%mod;
b/=2;
}
return ans;
}
public static long modmul(long a, long b, long mod) {
return b == 0 ? 0 : ((modmul(a, b >> 1, mod) << 1) % mod + a * (b & 1)) % mod;
}
static long sum(long n){
// System.out.println("lol="+ (n*(n-1))/2);
return (n*(n+1))/2;
}
public static ArrayList<Integer> Sieve(int n) {
boolean arr[]= new boolean [n+1];
Arrays.fill(arr,true);
arr[0]=false;
arr[1]=false;
for (int i = 2; i*i <=n ; i++) {
if(arr[i]){
for (int j = 2; j <=n/i ; j++) {
int u= i*j;
arr[u]=false;
}}
}
ArrayList<Integer> ans= new ArrayList<>();
for (int i = 0; i <n+1 ; i++) {
if(arr[i]){
ans.add(i);
}
}
return ans;
}
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ceil_div(long a, long b){
return (a+b-1)/b;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a*b)/gcd(a, b);
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
8e59ad4b587fc2afbb2edf33b4b1083c
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import org.omg.PortableInterceptor.INACTIVE;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main implements Runnable {
int n, m;
static boolean use_n_tests = false;
List<Integer> ls = generatePrimes(1000000);
void solve(FastScanner in, PrintWriter out, int testNumber) {
n = in.nextInt();
int k = in.nextInt();
Integer[] a = in.nextArray2(n);
int ans = -1;
int l = 1;
int r = n;
while (l <= r) {
int pos = (l + r) / 2;
if (can(pos, a, k)) {
ans = pos;
l = pos + 1;
} else {
r = pos - 1;
}
}
out.println(ans);
}
boolean can(int m, Integer[] a, int limit) {
int[] sum = new int[a.length];
for (int i = 0; i < a.length; i++) {
if (a[i] < m) {
sum[i] = -1;
} else {
sum[i] = 1;
}
if (i > 0) {
sum[i] += sum[i - 1];
}
}
int mx = sum[limit - 1];
int mn = 0;
for (int i = limit; i < n; i++) {
mn = Math.min(mn, sum[i - limit]);
mx = Math.max(mx, sum[i] - mn);
}
return mx > 0;
}
// ****************************** template code ***********
List<Integer> generatePrimes(int n) {
List<Integer> res = new ArrayList<>();
boolean[] sieve = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (!sieve[i]) {
res.add(i);
}
if ((long) i * i <= n) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = true;
}
}
}
return res;
}
int ask(int l, int r) {
if (l >= r) {
return -1;
}
System.out.printf("? %d %d\n", l + 1, r + 1);
System.out.flush();
return in.nextInt() - 1;
}
static int stack_size = 1 << 27;
class Mod {
long mod;
Mod(long mod) {
this.mod = mod;
}
long add(long a, long b) {
a = mod(a);
b = mod(b);
return (a + b) % mod;
}
long sub(long a, long b) {
a = mod(a);
b = mod(b);
return (a - b + mod) % mod;
}
long mul(long a, long b) {
a = mod(a);
b = mod(b);
return a * b % mod;
}
long div(long a, long b) {
a = mod(a);
b = mod(b);
return (a * inv(b)) % mod;
}
public long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
private long mod(long a) {
return a % mod;
}
}
class Coeff {
long mod;
long[][] C;
long[] fact;
boolean cycleWay = false;
Coeff(int n, long mod) {
this.mod = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = i;
fact[i] %= mod;
fact[i] *= fact[i - 1];
fact[i] %= mod;
}
}
Coeff(int n, int m, long mod) {
// n > m
cycleWay = true;
this.mod = mod;
C = new long[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= Math.min(i, m); j++) {
if (j == 0 || j == i) {
C[i][j] = 1;
} else {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
}
}
public long C(int n, int m) {
if (cycleWay) {
return C[n][m];
}
return fC(n, m);
}
private long fC(int n, int m) {
return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
}
private long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
}
class Pair {
int first;
int second;
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
class MultisetTree<T> {
int size = 0;
TreeMap<T, Integer> mp = new TreeMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
boolean contains(T x) {
return mp.containsKey(x);
}
T greatest() {
return mp.lastKey();
}
T smallest() {
return mp.firstKey();
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
class Multiset<T> {
int size = 0;
Map<T, Integer> mp = new HashMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
boolean contains(T x) {
return mp.containsKey(x);
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static List<List<Integer>> intInit2D(int n) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
return res;
}
static boolean isSorted(Integer[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static public long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public long sum(long[] a) {
long sum = 0;
for (long x : a) {
sum += x;
}
return sum;
}
static public long sum(Integer[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public int min(Integer[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int min(int[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int max(Integer[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int max(int[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> graph;
Graph(int n) {
create(n);
}
void create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
this.graph = graph;
}
void readBi(int m, FastScanner in) {
for (int i = 0; i < m; i++) {
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
graph.get(v).add(u);
graph.get(u).add(v);
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String line() {
String result = "";
try {
result = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long[] nextArrayL(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Long[] nextArrayL2(int n) {
Long[] res = new Long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Integer[] nextArray2(int n) {
Integer[] res = new Integer[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
c8db969fe1708dc871304bbb33f10270
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import org.omg.PortableInterceptor.INACTIVE;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main implements Runnable {
int n, m;
static boolean use_n_tests = false;
List<Integer> ls = generatePrimes(1000000);
void solve(FastScanner in, PrintWriter out, int testNumber) {
n = in.nextInt();
int k = in.nextInt();
Integer[] a = in.nextArray2(n);
int ans = -1;
int l = 1;
int r = n + 1;
while (r - l > 1) {
int pos = (l + r) / 2;
if (can(pos, a, k)) {
ans = pos;
l = pos;
} else {
r = pos;
}
}
out.println(l);
}
boolean can(int m, Integer[] a, int limit) {
int[] sum = new int[a.length];
for (int i = 0; i < a.length; i++) {
if (a[i] < m) {
sum[i] = -1;
} else {
sum[i] = 1;
}
if (i > 0) {
sum[i] += sum[i - 1];
}
}
int mx = sum[limit - 1];
int mn = 0;
for (int i = limit; i < n; i++) {
mn = Math.min(mn, sum[i - limit]);
mx = Math.max(mx, sum[i] - mn);
}
return mx > 0;
}
// ****************************** template code ***********
List<Integer> generatePrimes(int n) {
List<Integer> res = new ArrayList<>();
boolean[] sieve = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (!sieve[i]) {
res.add(i);
}
if ((long) i * i <= n) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = true;
}
}
}
return res;
}
int ask(int l, int r) {
if (l >= r) {
return -1;
}
System.out.printf("? %d %d\n", l + 1, r + 1);
System.out.flush();
return in.nextInt() - 1;
}
static int stack_size = 1 << 27;
class Mod {
long mod;
Mod(long mod) {
this.mod = mod;
}
long add(long a, long b) {
a = mod(a);
b = mod(b);
return (a + b) % mod;
}
long sub(long a, long b) {
a = mod(a);
b = mod(b);
return (a - b + mod) % mod;
}
long mul(long a, long b) {
a = mod(a);
b = mod(b);
return a * b % mod;
}
long div(long a, long b) {
a = mod(a);
b = mod(b);
return (a * inv(b)) % mod;
}
public long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
private long mod(long a) {
return a % mod;
}
}
class Coeff {
long mod;
long[][] C;
long[] fact;
boolean cycleWay = false;
Coeff(int n, long mod) {
this.mod = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = i;
fact[i] %= mod;
fact[i] *= fact[i - 1];
fact[i] %= mod;
}
}
Coeff(int n, int m, long mod) {
// n > m
cycleWay = true;
this.mod = mod;
C = new long[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= Math.min(i, m); j++) {
if (j == 0 || j == i) {
C[i][j] = 1;
} else {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
}
}
public long C(int n, int m) {
if (cycleWay) {
return C[n][m];
}
return fC(n, m);
}
private long fC(int n, int m) {
return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
}
private long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
}
class Pair {
int first;
int second;
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
class MultisetTree<T> {
int size = 0;
TreeMap<T, Integer> mp = new TreeMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
boolean contains(T x) {
return mp.containsKey(x);
}
T greatest() {
return mp.lastKey();
}
T smallest() {
return mp.firstKey();
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
class Multiset<T> {
int size = 0;
Map<T, Integer> mp = new HashMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
boolean contains(T x) {
return mp.containsKey(x);
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static List<List<Integer>> intInit2D(int n) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
return res;
}
static boolean isSorted(Integer[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static public long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public long sum(long[] a) {
long sum = 0;
for (long x : a) {
sum += x;
}
return sum;
}
static public long sum(Integer[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public int min(Integer[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int min(int[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int max(Integer[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int max(int[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> graph;
Graph(int n) {
create(n);
}
void create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
this.graph = graph;
}
void readBi(int m, FastScanner in) {
for (int i = 0; i < m; i++) {
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
graph.get(v).add(u);
graph.get(u).add(v);
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String line() {
String result = "";
try {
result = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long[] nextArrayL(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Long[] nextArrayL2(int n) {
Long[] res = new Long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Integer[] nextArray2(int n) {
Integer[] res = new Integer[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
a2f928be34f461712fa63eff9c2cf506
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class TaskA {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
// main solver
static class Task {
Scanner sc = new Scanner(System.in);
public void solve(InputReader in, PrintWriter out) {
int t = 1;
for (int i = 0; i < t; i++){
solveOne(in, out);
}
}
private boolean check(int median, int k, int A[]){
int n = A.length;
int mask[] = new int[n+1];
int cur = 0;
boolean ok = false;
for (int i = 0; i < n; i++){
cur += (A[i]>=median?1:-1);
mask[i+1] = cur;
if (i+1 >= k && mask[i+1] - mask[i+1-k] > 0){
ok = true; break;
}
mask[i+1] = Math.min(mask[i+1], mask[i]);
}
return ok;
}
private void solveOne(InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int A[] = new int[n];
for (int i = 0; i < n; i++) A[i] = in.nextInt();
int l = 1, r = n;
while (l < r-1){
int mid = (l+r)>>1;
if (check(mid, k, A)) l = mid;
else r = mid - 1;
}
if (l!=r && !check(r, k, A)){
r = l;
}
out.println(r);
}
}
static class Point{
public int x,y,z, ID;
public Point(int x, int y, int z, int ID){
this.x = x;
this.y = y;
this.z = z;
this.ID = ID;
}
@Override
public String toString() {
return "[" + x + " " + y + " " + z + " " + ID + "]";
}
}
static class SortByX implements Comparator<Point>{
@Override
public int compare(Point p1, Point p2) {
return Integer.compare(p1.x, p2.x);
}
}
static class SortByY implements Comparator<Point>{
@Override
public int compare(Point p1, Point p2) {
if (p1.y != p2.y) return Integer.compare(p1.y, p2.y);
if (p1.z > p2.z) return -1;
return 1;
}
}
// fast input reader class;
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong(){
return Long.parseLong(nextToken());
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
f4a8082230225e791b3d2fe9484d59ef
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class Pair
{
int f,s;
Pair(int f,int s)
{
this.f=f;
this.s=s;
}
}
public static void main(String args[])
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int n=fs.nextInt();
int k=fs.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=fs.nextInt();
int l=1,r=n+1;
while(r-l>1)
{
int mid=(l+r)/2;
int new_a[]=new int[n];
for(int i=0;i<n;i++)
{
if(a[i]<mid)
new_a[i]=-1;
else
new_a[i]=1;
}
for(int i=1;i<n;i++)
new_a[i]+=new_a[i-1];
int max=new_a[k-1],min=0;
for(int i=k;i<n;i++)
{
min=Math.min(min,new_a[i-k]);
max=Math.max(max,new_a[i]-min);
}
if(max>0)
l=mid;
else
r=mid;
}
pw.println(l);
pw.flush();
pw.close();
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
7db18313605528e3fb0566dd4372efd8
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.Arrays;
//Max Median of subarray of length at least k
//https://codeforces.com/contest/1486/problem/D
import java.util.Scanner;
//import codeforces.CF_1486d.Mode;
public class CF_1486d{
private static final boolean DEBUG= true;
enum Mode{
BIN_SEARCH_SUBARRAY_LEN_AT_LEAST_K,
ORDER_STAT_SET_SUBARRAY_LEN_AT_LEAST_K,
BIN_SEARCH_SUBARRAY_LEN_EQUAL_K,
ORDER_STAT_SET_SUBARRAY_LEN_EQUAL_K,
}
public static int[] parseInts(String[] arr){
if(arr==null) return null;
int n= arr.length;
if(n==0 || n==1 && arr[0]!=null && arr[0].length()==0)
return new int[0];
int[] intArr= new int[n];
for(int i=0; i<n; ++i)
intArr[i]= Integer.parseInt(arr[i]);
return intArr;
}
public static void err(String format, Object... args){
System.err.println(String.format(format, args));
}
public static final void main(String[] args){
Mode mode= Mode.BIN_SEARCH_SUBARRAY_LEN_AT_LEAST_K;
if(args!=null && args.length==1)
mode= Mode.valueOf(args[0]);
Scanner s= new Scanner(System.in);
int n= s.nextInt(), k= s.nextInt();
s.nextLine();
int[] a= parseInts(s.nextLine().split(" "));
s.close();
if(DEBUG) err("n=%d, k=%d, a=%s", n, k, Arrays.toString(a));
int maxMedian= Integer.MIN_VALUE;
switch(mode){
case ORDER_STAT_SET_SUBARRAY_LEN_EQUAL_K:
maxMedian= orderStatisticSet_maxMedianOfSubarrayLenEqualK(a, n, k); break;
case ORDER_STAT_SET_SUBARRAY_LEN_AT_LEAST_K:
maxMedian= orderStatisticSet_maxMedianOfSubarrayLenAtLeastK(a, n, k); break;
case BIN_SEARCH_SUBARRAY_LEN_EQUAL_K:
maxMedian= binarySearch_maxMedianOfSubarrayLenEqualK(a, n, k); break;
case BIN_SEARCH_SUBARRAY_LEN_AT_LEAST_K:
maxMedian= binarySearch_maxMedianOfSubarrayLenAtLeastK(a, n, k); break;
default:
maxMedian= binarySearch_maxMedianOfSubarrayLenEqualK(a, n, k); break;
}
System.out.println(maxMedian);
}
static int binarySearch_maxMedianOfSubarrayLenEqualK(int[] a, int n, int k){
int[] b= new int[n];
int l= 1, r= n+1;
while(r-l>1){
int mid= (l+r)/2;
for(int i=0; i<n; i++) b[i]= a[i]>=mid?1:-1;
if(DEBUG) err("l=%d, r=%d, mid=%d\nb=%s", l, r, mid, Arrays.toString(b));
for(int i=1; i<n; i++) b[i]+= b[i-1];
if(DEBUG) err("ps b=%s", Arrays.toString(b));
boolean ans= false;
if(b[k-1]>0) ans= true;
for(int i=k; i<n; i++)
if(b[i]-b[i-k]>0) ans= true;
if(ans) l= mid;
else r= mid;
}
return l;
}
static int binarySearch_maxMedianOfSubarrayLenAtLeastK(int[] a, int n, int k){
int[] b= new int[n];
int l= 1, r= n+1;
while(r-l>1){
int mid= (l+r)/2;
for(int i=0; i<n; i++) b[i]= a[i]>=mid?1:-1;
if(DEBUG) err("l=%d, r=%d, mid=%d\nb=%s", l, r, mid, Arrays.toString(b));
for(int i=1; i<n; i++) b[i]+= b[i-1];
if(DEBUG) err("ps b=%s", Arrays.toString(b));
boolean ans= false;
if(b[k-1]>0) ans= true;
int min= 0;
for(int i=k; i<n; i++){
min= Math.min(min, b[i-k]);
if(b[i]-min>0) ans= true;
}
if(ans) l= mid;
else r= mid;
}
return l;
}
static int orderStatisticSet_maxMedianOfSubarrayLenAtLeastK(int[] a, int n, int k){
return Integer.MIN_VALUE;
}
static int orderStatisticSet_maxMedianOfSubarrayLenEqualK(int[] a, int n, int k){
return Integer.MIN_VALUE;
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
769007591afdd4ad6052d4694908c254
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
//import static lib.Utils.*;
//import static lib.StringUtils.*;
import java.util.Arrays;
//Max Median of subarray of length at least k
//https://codeforces.com/contest/1486/problem/D
import java.util.Scanner;
public class CF_1486d{
private static final boolean DEBUG= false;
enum Mode{
BIN_SEARCH_SUBARRAY_LEN_AT_LEAST_K,
ORDER_STAT_SET_SUBARRAY_LEN_AT_LEAST_K,
BIN_SEARCH_SUBARRAY_LEN_EQUAL_K,
ORDER_STAT_SET_SUBARRAY_LEN_EQUAL_K,
}
public static int[] parseInts(String[] arr){
if(arr==null) return null;
int n= arr.length;
if(n==0 || n==1 && arr[0]!=null && arr[0].length()==0)
return new int[0];
int[] intArr= new int[n];
for(int i=0; i<n; ++i)
intArr[i]= Integer.parseInt(arr[i]);
return intArr;
}
public static void err(String format, Object... args){
System.err.println(String.format(format, args));
}
public static final void main(String[] args){
Mode mode= Mode.BIN_SEARCH_SUBARRAY_LEN_AT_LEAST_K;
if(args!=null && args.length==1)
mode= Mode.valueOf(args[0]);
Scanner s= new Scanner(System.in);
int n= s.nextInt(), k= s.nextInt();
s.nextLine();
int[] a= parseInts(s.nextLine().split(" "));
s.close();
if(DEBUG) err("n=%d, k=%d, a=%s", n, k, Arrays.toString(a));
int maxMedian= Integer.MIN_VALUE;
switch(mode){
case ORDER_STAT_SET_SUBARRAY_LEN_EQUAL_K:
maxMedian= orderStatisticSet_maxMedianOfSubarrayLenEqualK(a, n, k); break;
case ORDER_STAT_SET_SUBARRAY_LEN_AT_LEAST_K:
maxMedian= orderStatisticSet_maxMedianOfSubarrayLenAtLeastK(a, n, k); break;
case BIN_SEARCH_SUBARRAY_LEN_EQUAL_K:
maxMedian= binarySearch_maxMedianOfSubarrayLenEqualK(a, n, k); break;
case BIN_SEARCH_SUBARRAY_LEN_AT_LEAST_K:
default: maxMedian= binarySearch_maxMedianOfSubarrayLenAtLeastK(a, n, k); break;
}
System.out.println(maxMedian);
}
static int binarySearch_maxMedianOfSubarrayLenEqualK(int[] a, int n, int k){
return Integer.MIN_VALUE;
}
static int binarySearch_maxMedianOfSubarrayLenAtLeastK(int[] a, int n, int k){
int[] b= new int[n];
int l= 1, r= n+1;
while(r-l>1){
int mid= (l+r)/2;
for(int i=0; i<n; i++) b[i]= a[i]>=mid?1:-1;
if(DEBUG) err("l=%d, r=%d, mid=%d\nb=%s", l, r, mid, Arrays.toString(b));
for(int i=1; i<n; i++) b[i]+= b[i-1];
if(DEBUG) err("ps b=%s", Arrays.toString(b));
boolean ans= false;
if(b[k-1]>0) ans= true;
int min= 0;
for(int i=k; i<n; i++){
min= Math.min(min, b[i-k]);
if(b[i]-min>0) ans= true;
}
if(ans) l= mid;
else r= mid;
}
return l;
}
static int orderStatisticSet_maxMedianOfSubarrayLenAtLeastK(int[] a, int n, int k){
return Integer.MIN_VALUE;
}
static int orderStatisticSet_maxMedianOfSubarrayLenEqualK(int[] a, int n, int k){
return Integer.MIN_VALUE;
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
e99a9b685aab7cd7cd4cb99198a32c08
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
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.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
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 {
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt(), k = fr.nextInt();
int[] arr = fr.nextIntArray(n);
// Observations:
// 1. The task is to determine the maximum median we
// can get from a subarray of length at least 'k'.
// 2. At least k => Exactly k ?
// A: Doesn't really matter.
int lo = 0, hi = (int) 2e5;
int max = -1;
while (lo <= hi) {
int med = (lo + hi) >> 1;
boolean can = false;
int[] brr = new int[n];
for (int i = 0; i < n; i++)
if (arr[i] >= med)
brr[i] = 1;
else
brr[i] = -1;
for (int i = 1; i < n; i++)
brr[i] += brr[i - 1];
int[] maxFrom = new int[n];
maxFrom[n - 1] = brr[n - 1];
for (int i = n - 2; i > -1; i--)
maxFrom[i] = Math.max(maxFrom[i + 1], brr[i]);
int mxxSuff = maxFrom[k - 1];
if (mxxSuff >= 1)
can = true;
for (int i = 0; i < n; i++) {
if (i + k > n - 1) break;
// [i + k + 1, n - 1] is the segment
int maxSuff = maxFrom[i + k];
if (maxSuff >= brr[i] + 1)
can = true;
}
if (can) {
max = med;
lo = med + 1;
} else
hi = med - 1;
}
out.println(max);
}
out.close();
}
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 HashMap<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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
ee84a198dcad025c4d9822c8b56e0879
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
/*
*/
public class D{
static FastReader sc=null;
static int nax=(int)1e9;
public static void main(String[] args) {
sc=new FastReader();
int n=sc.nextInt(),k=sc.nextInt();
int a[]=sc.readArray(n);
int l=0,r=nax;
while(l+1<r) {
int mid=(l+r)/2;
if(pos(a,mid,k))l=mid;
else r=mid;
}
System.out.println(l);
}
static boolean pos(int a[],int m,int k) {
int n=a.length;
//if for all sub-arrays of length >= K
//if no of elements less than M is >= (N+1)/2 | sum>=0
//then we have to decrease our value to get the right median
//we can remove the length from the equation if we replace them with +1/-1
//More Convenient Definition:-
//if there exists at-least one sub-array with no of elements less than M<(N+1)/2 | sum<0
//then this can be a possible median and we have to move our L forward
int b[]=new int[n];
for(int i=0;i<n;i++)
b[i]=(a[i]<m?1:-1);
//we have to minimize the value cs[i]-cs[l] i-l+1>=k
//so we have to maximize the value cs[l]
int max=-nax,curr=0;
int cs[]=new int[n+1];
for(int i=0,j=-1;i<n;i++) {
curr+=b[i];
cs[i+1]=curr;
while(i-j+1>k) {
max=Math.max(max, cs[j+1]);
j++;
}
if(curr-max<0) {
return true;
}
}
return false;
}
static int[] ruffleSort(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 void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
StringTokenizer st=new StringTokenizer("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String next() {
while(!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
210072e93cbea774c8c3c3196a4211ef
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
/*
*/
public class D{
static FastReader sc=null;
static int nax=(int)1e9;
public static void main(String[] args) {
sc=new FastReader();
int n=sc.nextInt(),k=sc.nextInt();
int a[]=sc.readArray(n);
int l=0,r=nax;
while(l+1<r) {
int mid=(l+r)/2;
if(pos(a,mid,k))l=mid;
else r=mid;
}
System.out.println(l);
}
//find the maximum median we can get
static boolean pos(int a[],int m,int k) {
int n=a.length;
//if for all sub-arrays of length >= K
//if no of elements less than M is >= (N+1)/2 | sum>=0
//then we have to decrease our value to get the right median
//if there exists at-least one sub-array with no of elements less than M<(N+1)/2 | sum<0
//then this can be a possible median and we have to move our L forward
//alternatively we can remove the length from the equation if we replace them with +1/-1
int b[]=new int[n];
for(int i=0;i<n;i++)
b[i]=(a[i]<m?1:-1);
//we have to minimize the value cs[i]-cs[l] i-l+1>=k
//so we have to maximize the value cs[l]
int max=0,curr=0;
int cs[]=new int[n+1];
for(int i=0,j=0;i<n;i++) {
curr+=b[i];
cs[i+1]=curr;
while(i-j+1>k) {
max=Math.max(max, cs[j+1]);
j++;
}
if(i>=k-1 && curr-max<0) {
return true;
}
}
return false;
}
static int[] ruffleSort(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 void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
StringTokenizer st=new StringTokenizer("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String next() {
while(!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
d9ef2c39a2972c8721f29f7783192193
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
/*
*/
public class D{
static FastReader sc=null;
static int nax=(int)1e9;
public static void main(String[] args) {
sc=new FastReader();
int n=sc.nextInt(),k=sc.nextInt();
int a[]=sc.readArray(n);
int l=0,r=nax;
while(l+1<r) {
int mid=(l+r)/2;
if(pos(a,mid,k))l=mid;
else r=mid;
}
System.out.println(l);
}
//find the maximum median we can get
static boolean pos(int a[],int m,int k) {
int n=a.length;
//if for all sub-arrays of length >= K
//if no of elements less than M is >= (N+1)/2 | sum>=0
//then we have to decrease our value to get the right median
//if there exists at-least one sub-array with no of elements less than M<(N+1)/2 | sum<0
//then this can be a possible median and we have to move our L forward
//alternatively we can remove the length from the equation if we replace them with +1/-1
int b[]=new int[n];
for(int i=0;i<n;i++)
b[i]=(a[i]<m?1:-1);
//we have to maximize the value cs[i]-cs[l] i-l+1>=k
//so we have to minimize the value cs[l]
int max=-nax,curr=0;
int cs[]=new int[n+1];
for(int i=0,j=-1;i<n;i++) {
curr+=b[i];
cs[i+1]=curr;
while(i-j+1>k) {
max=Math.max(max, cs[j+1]);
j++;
}
if(curr-max<0) {
return true;
}
}
return false;
}
static int[] ruffleSort(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 void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
StringTokenizer st=new StringTokenizer("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String next() {
while(!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
5d90f71eef52c07c7cff899b542583a7
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
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)1e9+7,INF=Long.MAX_VALUE;
static boolean set[],col[];
static int par[],tot[],partial[];
static int Days[],P[][];
static int dp[][],sum=0,size[];
static int seg[];
static ArrayList<Long> A;
// static HashSet<Integer> set;
// static node1 seg[];
//static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)};
public static void main(String args[])throws IOException
{
int N=i(),K=i();
int A[]=input(N);
int l=N+1,r=N+1;
for(int a:A)l=Math.min(l, a);
while(r-l>1)
{
int m=(l+r)/2;
if(f(m,A,K))l=m;
else r=m;
}
out.println(l);
out.close();
}
static boolean f(int x,int A[],int K)
{
int c=0;
int N=A.length;
int B[]=new int[N];
int sum[]=new int[N+1];
int min[]=new int[N+1];
for(int i=0; i<N; i++)
{
if(A[i]>=x)
B[i]=1;
else B[i]=-1;
sum[i+1]=sum[i]+B[i];
min[i+1]=Math.min(min[i], sum[i+1]);
}
int l=0,r=K;
while(r<=N)
{
int s=sum[r]-min[l];
if(s>0)
{
c=1;
break;
}
l++;
r++;
}
return c>0;
}
static int f(char X[][],int i,int j,int N,int M)
{
int c1=0,c2=0;
int x=i,y=j;
while(i>=0 && j>=0 && X[i][j]=='*')
{
c1++;
i--;
j--;
}
i=x;
j=y;
while(i>=0 && j<M && X[i][j]=='*')
{
c2++;
i--;
j++;
}
return Math.min(c1,c2)-1;
}
static void update(char X[][],int min[][],int i,int j,int N,int M,int d)
{
int x=i,y=j;
int t=d+1;
while(t-->0 && i>=0 && j>=0 && X[i][j]=='*')
{
min[i][j]=Math.max(min[i][j],d);
i--;
j--;
}
i=x;
j=y;
t=d+1;
while(t-->0 && i>=0 && j<M && X[i][j]=='*')
{
min[i][j]=Math.max(min[i][j],d);
i--;
j++;
}
}
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 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 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 long ask(int v,int tl,int tr,int l,int r)
{
if(l>r)return (long)(1e10);
if(tl==l && tr==r)
{
return seg[v];
}
int tm=(tl+tr)/2;
return Math.min(ask(v*2,tl,tm,l,Math.min(tm, r)), ask(v*2+1,tm+1,tr,Math.max(l,tm+1),r));
}
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 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)
{
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)
{
tot=new int[N+1];
partial=new int[N+1];
Days=new int[N+1];
P=new int[N+1][(int)(Math.log(N)+10)];
set=new boolean[N+1];
g=new ArrayList[N+1];
for(int i=0; i<=N; i++)
{
g[i]=new ArrayList<>();
Days[i]=-1;
//D2[i]=INF;
}
}
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 Pair23 implements Comparable<Pair23>
{
int a;
char ch;
Pair23(int a,char f)
{
this.ch=f;
this.a=a;
}
public int compareTo(Pair23 X)
{
return this.a-X.a;
}
}
//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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
ace2c4454d04721dd81bae0fbe87b109
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class G {
static class MinCostMaxFlow {
// Stores the found edges
boolean found[];
// Stores the number of nodes
int N;
// Stores the capacity
// of each edge
int cap[][];
int flow[][];
// Stores the cost per
// unit flow of each edge
int cost[][];
// Stores the distance from each node
// and picked edges for each node
int dad[], dist[], pi[];
static final int INF = Integer.MAX_VALUE / 2 - 1;
// Function to check if it is possible to
// have a flow from the src to sink
boolean search(int src, int sink) {
// Initialise found[] to false
Arrays.fill(found, false);
// Initialise the dist[] to INF
Arrays.fill(dist, INF);
// Distance from the source node
dist[src] = 0;
// Iterate untill src reaches N
while (src != N) {
int best = N;
found[src] = true;
for (int k = 0; k < N; k++) {
// If already found
if (found[k])
continue;
// Evaluate while flow
// is still in supply
if (flow[k][src] != 0) {
// Obtain the total value
int val = dist[src] + pi[src] - pi[k] - cost[k][src];
// If dist[k] is > minimum value
if (dist[k] > val) {
// Update
dist[k] = val;
dad[k] = src;
}
}
if (flow[src][k] < cap[src][k]) {
int val = dist[src] + pi[src] - pi[k] + cost[src][k];
// If dist[k] is > minimum value
if (dist[k] > val) {
// Update
dist[k] = val;
dad[k] = src;
}
}
if (dist[k] < dist[best])
best = k;
}
// Update src to best for
// next iteration
src = best;
}
for (int k = 0; k < N; k++)
pi[k] = Math.min(pi[k] + dist[k], INF);
// Return the value obtained at sink
return found[sink];
}
// Function to obtain the maximum Flow
int[] getMaxFlow(int cap[][], int cost[][], int src, int sink) {
this.cap = cap;
this.cost = cost;
N = cap.length;
found = new boolean[N];
flow = new int[N][N];
dist = new int[N + 1];
dad = new int[N];
pi = new int[N];
int totflow = 0, totcost = 0;
// If a path exist from src to sink
while (search(src, sink)) {
// Set the default amount
int amt = INF;
for (int x = sink; x != src; x = dad[x])
amt = Math.min(amt, flow[x][dad[x]] != 0 ? flow[x][dad[x]] : cap[dad[x]][x] - flow[dad[x]][x]);
for (int x = sink; x != src; x = dad[x]) {
if (flow[x][dad[x]] != 0) {
flow[x][dad[x]] -= amt;
totcost -= amt * cost[x][dad[x]];
} else {
flow[dad[x]][x] += amt;
totcost += amt * cost[dad[x]][x];
}
}
totflow += amt;
}
// Return pair total cost and sink
return new int[] { totflow, totcost };
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = 1;
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int a[] = sc.intArr(n);
int pre[] = new int[n+1];
int lo = 1;
int hi = n;
int ans = -1;
while (hi >= lo) {
int mid = hi + lo >> 1;
for (int i = 1; i < pre.length; i++) {
pre[i] = pre[i - 1] + (a[i-1] >= mid ? 1 : -1);
}
int h = 0;
boolean f = false;
for (int i = k; i < pre.length; i++) {
h = Math.min(h, pre[i - k]);
f |= (pre[i] - h) > 0;
}
if (f) {
ans = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
pw.println(ans);
}
pw.flush();
}
///////////////////////////////////////////////////////////////////////////////////////////
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
static class pair implements Comparable<pair> {
int x, y;
boolean w, l;
PriorityQueue<Long> pq;
public pair(boolean a, boolean b) {
w = a;
l = b;
}
pair(int s, int d) {
x = s;
y = d;
}
@Override
public int compareTo(pair p) {
return Long.compare(x, p.x);
}
@Override
public String toString() {
return x + " " + y;
}
}
static long mod(long ans, int mod) {
return (ans % mod + mod) % mod;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int log(int n, int base) {
int ans = 0;
while (n + 1 > base) {
ans++;
n /= base;
}
return ans;
}
static long pow(long b, long e) {
long ans = 1;
while (e > 0) {
if ((e & 1) == 1)
ans = ((ans * 1l * b));
e >>= 1;
{
}
b = ((b * 1l * b));
}
return ans;
}
static long powmod(long r, long e, int mod) {
long ans = 1;
r %= mod;
while (e > 0) {
if ((e & 1) == 1)
ans = (int) ((ans * 1l * r) % mod);
e >>= 1;
r = (int) ((r * 1l * r) % mod);
}
return ans;
}
static int ceil(int a, int b) {
int ans = a / b;
return a % b == 0 ? ans : ans + 1;
}
static long ceil(long a, long b) {
long ans = a / b;
return a % b == 0 ? ans : ans + 1;
}
static HashMap<Integer, Integer> compress(int a[]) {
TreeSet<Integer> ts = new TreeSet<>();
HashMap<Integer, Integer> hm = new HashMap<>();
for (int x : a)
ts.add(x);
for (int x : ts) {
hm.put(x, hm.size() + 1);
}
return hm;
}
// Returns nCr % p
static int C[];
static int nCrModp(int n, int r, int p) {
if (r > n - r)
r = n - r;
if (C[r] != 0)
return C[r];
// The array C is going to store last
// row of pascal triangle at the end.
// And last entry of last row is nCr
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++) {
// Fill entries of current row using previous
// row values
for (int j = Math.min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public int[] intArr(int n) throws IOException {
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextInt();
}
return a;
}
public long[] longArr(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextLong();
}
return a;
}
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 boolean ready() throws IOException {
return br.ready();
}
}
public static void shuffle(int[] times2) {
int n = times2.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = times2[i];
times2[i] = times2[r];
times2[r] = tmp;
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
ea5107cd21b9fe5a62abd92eed1a25f5
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(System.out);
int T=1;
for(int t=0;t<T;t++){
int n=Int();int k=Int();
int A[]=new int[n];
for(int i=0;i<n;i++){
A[i]=Int();
}
Solution sol=new Solution();
sol.solution(out,A,k);
}
out.flush();
}
public static long Long(){ return in.nextLong();}
public static int Int(){
return in.nextInt();
}
public static String Str(){
return in.next();
}
}
class Solution{
public void solution(PrintWriter out,int A[],int k){
int l=1;
int r=0;
for(int i:A){
r=Math.max(r,i);
}
int res=-1;
while(l<=r){
int mid=l+(r-l)/2;
if(check(A,mid,k)){
res=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
System.out.println(res);
}
public boolean check(int B[],int mid,int k){
int A[]=new int[B.length];
for(int i=0;i<A.length;i++){
if(B[i]<mid){
A[i]=-1;
}
else{
A[i]=1;
}
}
int pre[]=new int[A.length];
int sum=0;
for(int i=0;i<A.length;i++){
sum+=A[i];
pre[i]=sum;
if(i+1>=k){
int min=0;
if(pre[i]-min>0){
return true;
}
}
//if(mid==6){
//System.out.print(pre[i]+" ");
//}
}
//System.out.println();
PriorityQueue<Integer>pq1=new PriorityQueue<>();
PriorityQueue<Integer>pq2=new PriorityQueue<>();
for(int i=0;i<A.length;i++){
if(i+1<k)continue;
if(pq1.size()!=0&&pre[i]-pq1.peek()>0){
//if(mid==6){
//System.out.print(i+" "+pre[i]+" "+pq1.peek());System.out.println();
//}
return true;
}
pq1.add(pre[i-k+1]);
}
return false;
}
}
// 0 0 1 1 1 1 2
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
c3bbef9d18f7a423768d3c4e9a80ddd2
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
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.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
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 {
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt(), k = fr.nextInt();
int[] arr = fr.nextIntArray(n);
// Observations:
// 1. The task is to determine the maximum median we
// can get from a subarray of length at least 'k'.
// 2. We will use binary search. Then the problem has
// radix {-1, 1}.
// --
// We require positive sum in segment of size >= k
// to get the desired median.
int lo = 0, hi = (int) 2e5;
int max = -1;
while (lo <= hi) {
int med = (lo + hi) >> 1;
boolean can = false;
int[] brr = new int[n];
for (int i = 0; i < n; i++)
if (arr[i] >= med)
brr[i] = 1;
else
brr[i] = -1;
for (int i = 1; i < n; i++)
brr[i] += brr[i - 1];
int[] maxFrom = new int[n];
maxFrom[n - 1] = brr[n - 1];
for (int i = n - 2; i > -1; i--)
maxFrom[i] = Math.max(maxFrom[i + 1], brr[i]);
int mxxSuff = maxFrom[k - 1];
if (mxxSuff >= 1)
can = true;
for (int i = 0; i < n; i++) {
if (i + k > n - 1) break;
// [i + k, n - 1] is the segment
int maxSuff = maxFrom[i + k];
if (maxSuff >= brr[i] + 1)
can = true;
}
if (can) {
max = med;
lo = med + 1;
} else
hi = med - 1;
}
out.println(max);
}
out.close();
}
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 HashMap<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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
5f764db31e5c3b7040dfcafb1cb5ad7f
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Round703_D {
public static boolean check(int val, int[] a, int k){
int[] b = new int[a.length];
int[] sum = new int[a.length + 1];
for(int i = 0; i < a.length; i++){
b[i] = a[i] >= val ? 1 : -1;
}
for(int i = 1; i < sum.length; i++){
sum[i] = sum[i - 1] + b[i - 1];
}
int minVal = 0;
int maxVal = sum[k];
for(int i = k + 1; i < sum.length; i++){
minVal = Math.min(minVal, sum[i - k]);
maxVal = Math.max(maxVal, sum[i] - minVal);
}
return maxVal > 0;
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
in.nextLine();
String[] str = in.nextLine().split(" ");
// for(String s : str){
// System.out.println(s);
// }
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(str[i]);
}
int l = 1, r = n;
while(l < r){
int mid = (l + r + 1) >> 1;
if(check(mid, a, k)) l = mid;
else r = mid - 1;
}
System.out.println(l);
}
// static class FastRead{
//
// private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//
// public int NextInt() throws IOException {
// return Integer.parseInt(in.readLine());
// }
//
// public String[] NextStringArray() throws IOException {
// return in.readLine().split(" ");
// }
//
// public int[] NextIntArray() throws IOException {
// String[] str = this.NextStringArray();
// int[] a = new int[str.length];
// for(int i = 0; i < str.length; i++){
// a[i] = Integer.parseInt(str[i]);
// }
// return a;
// }
//
// public String NextString() throws IOException {
// return this.NextStringArray()[0];
// }
// }
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
f7dd036c714521cc740d7d06a636fbb7
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Round703_D {
public static boolean check(int val, int[] a, int k){
int[] b = new int[a.length];
int[] sum = new int[a.length + 1];
for(int i = 0; i < a.length; i++){
b[i] = a[i] >= val ? 1 : -1;
}
for(int i = 1; i < sum.length; i++){
sum[i] = sum[i - 1] + b[i - 1];
}
int minVal = 0;
int maxVal = sum[k];
for(int i = k + 1; i < sum.length; i++){
minVal = Math.min(minVal, sum[i - k]);
maxVal = Math.max(maxVal, sum[i] - minVal);
}
return maxVal > 0;
}
public static void main(String[] args) throws IOException {
FastRead in = new FastRead();
int[] t = in.NextIntArray();
int n = t[0];
int k = t[1];
int[] a = in.NextIntArray();
int l = 1, r = n;
while(l < r){
int mid = (l + r + 1) >> 1;
if(check(mid, a, k)) l = mid;
else r = mid - 1;
}
System.out.println(l);
}
}
class FastRead{
private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public int NextInt() throws IOException {
return Integer.parseInt(in.readLine());
}
public String[] NextStringArray() throws IOException {
return in.readLine().split(" ");
}
public int[] NextIntArray() throws IOException {
String[] str = this.NextStringArray();
int[] a = new int[str.length];
for(int i = 0; i < str.length; i++){
a[i] = Integer.parseInt(str[i]);
}
return a;
}
public String NextString() throws IOException {
return this.NextStringArray()[0];
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
edb9fe8d76169677a5ec72b5283602c6
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Round703_D {
public static boolean check(int val, int[] a, int k){
int[] b = new int[a.length];
int[] sum = new int[a.length + 1];
for(int i = 0; i < a.length; i++){
b[i] = a[i] >= val ? 1 : -1;
}
for(int i = 1; i < sum.length; i++){
sum[i] = sum[i - 1] + b[i - 1];
}
int minVal = 0;
int maxVal = sum[k];
for(int i = k + 1; i < sum.length; i++){
minVal = Math.min(minVal, sum[i - k]);
maxVal = Math.max(maxVal, sum[i] - minVal);
}
return maxVal > 0;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] str = in.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
str = in.readLine().split(" ");
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(str[i]);
}
int l = 1, r = n;
while(l < r){
int mid = (l + r + 1) >> 1;
if(check(mid, a, k)) l = mid;
else r = mid - 1;
}
System.out.println(l);
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
0e9396f3fe25685d84335b7444be8efd
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class D {
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;
}
}
private static void runWithStd() {
FastReader scanner = new FastReader();
PrintWriter output = new PrintWriter(System.out);
int n = scanner.nextInt(),k = scanner.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
int ret = solve(nums,k);
output.println(ret);
output.close();
}
private static int solve(int[] nums, int k) {
int lo = 1,hi = 200000,ret = 1;
while (lo <= hi){
int mid = (lo + hi) / 2;
if(valid(nums,k,mid)){
ret = mid;
lo = mid + 1;
}else{
hi = mid - 1;
}
}
return ret;
}
private static boolean valid(int[] nums, int k, int mid) {
HashMap<Integer,Integer> diffs = new HashMap<>();
int b = 0;
for (int i = 0; i < nums.length; i++) {
int cur = nums[i] >= mid ? 1 : -1;
b += cur;
if(b > 0){
if(i + 1 >= k) return true;
}else{
int gap = b - 1;
if(diffs.containsKey(gap)){
int prv = diffs.get(gap);
if(i - prv >= k) return true;
}
if(!diffs.containsKey(b)) diffs.put(b,i);
}
}
return false;
}
private static void runWithDebug() {
Random random = new Random();
int t = 100;
while (t-- > 0) {
long ts = System.currentTimeMillis();
long delta = System.currentTimeMillis() - ts;
System.out.println("case t = " + t + " time = " + delta);
}
System.out.println("all passed");
}
public static void main(String[] args) {
runWithStd();
//runWithDebug();
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
805cc5de5a8672420aec2b3ed481ea00
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class MaxMedian_703D {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] nums = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i ++) nums[i] = Integer.parseInt(st.nextToken());
int l = 1, r = 200000;
while(l <= r) {
int m = (l + r) / 2;
if(valid(nums, m, k)) {
l = m + 1;
} else {
r = m - 1;
}
}
System.out.println(r);
}
private static boolean valid(int[] nums, int target, int len) {
int min = 0, curr = 0;
int[] arr = new int[nums.length];
for(int i = 0; i < nums.length; i ++) {
curr += nums[i] < target? -1 : 1;
if(i >= len) min = Math.min(min, arr[i - len]);
if(i >= len - 1 && curr - min > 0) return true;
arr[i] = curr;
}
return false;
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
370271bbd7b92ffbdd25784ec932c0bf
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
public class Test {
static boolean check(int[] nums, int[] dif, int mid, int k) {
int n = nums.length;
for (int i = 0; i < n; i++) dif[i+1] = dif[i] + (nums[i] >= mid ? 1 : -1);
int minPre = 0, ans = dif[k];
for (int i = k + 1; i <= n; i++) {
minPre = Math.min(minPre, dif[i - k]);
ans = Math.max(ans, dif[i] - minPre);
}
return ans > 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt();
int[] nums = new int[n], copy = new int[n], dif = new int[n+1];
for (int i = 0; i < n; i++) copy[i] = nums[i] = sc.nextInt();
Arrays.sort(copy);
int i = 0, j = n - 1;
while (i < j) {
int mid = i + (j - i + 1) / 2;
if (check(nums, dif, copy[mid], k)) i = mid;
else j = mid - 1;
}
System.out.println(copy[i]);
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
b78b30c5c0d4cfe94f018ab414472f0d
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution
{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
/* static int sz;
static pair st[];
static int lazy[];
static void update(int lx ,int rx , int l , int r , int x)
{
if(lazy[x] != 0)
{
st[x].one = rx-lx+1-st[x].one;
st[x].zero = rx-lx+1-st[x].zero;
int temp = st[x].i;
st[x].i = st[x].d;
st[x].d = temp;
if(lx != rx)
{
lazy[2*x+1] = (1+lazy[2*x+1])%2;
lazy[2*x+2] = (1+lazy[2*x+2])%2;
}
lazy[x] = 0;
}
if(lx > r || rx < l)
return;
if(lx >= l && rx <= r)
{
st[x].one = rx-lx+1-st[x].one;
st[x].zero = rx-lx+1-st[x].zero;
int temp = st[x].i;
st[x].i = st[x].d;
st[x].d = temp;
if(lx != rx)
{
lazy[2*x+1] = (1+lazy[2*x+1])%2;
lazy[2*x+2] = (1+lazy[2*x+2])%2;
}
}
int mid = (lx+rx)/2;
update(lx,mid,l,r,2*x+1);
update(mid+1,rx,l,r,2*x+2);
st[x].one = st[2*x+1].one+st[2*x+2].one;
st[x].zero = st[2*x+1].zero+st[2*x+2].zero;
st[x].i = Math.max(st[2*x+1].i+st[2*x+2].one,st[2*x+2].i+st[2*x+1].zero);
st[x].i = Math.max(st[x].i,Math.max(st[x].one,st[x].zero));
st[x].d = Math.max(st[2*x+1].d+st[2*x+2].zero,st[2*x+1].one+st[2*x+2].d);
st[x].d = Math.max(st[x].d,Math.max(st[x].one,st[x].zero));
}
static void update(int l , int r)
{
update(0,sz-1,l,r,0);
}
static pair max(int lx , int rx , int l , int r , int x)
{
if(lazy[x] != 0)
{
st[x].one = rx-lx+1-st[x].one;
st[x].zero = rx-lx+1-st[x].zero;
int temp = st[x].i;
st[x].i = st[x].d;
st[x].d = temp;
if(lx != rx)
{
lazy[2*x+1] = (1+lazy[2*x+1])%2;
lazy[2*x+2] = (1+lazy[2*x+2])%2;
}
lazy[x] = 0;
}
if(lx > r || rx < l)
return new pair();
if(lx >= l && rx <= r)
{
return st[x];
}
int mid = (lx+rx)/2;
pair p1 = max(lx,mid,l,r,2*x+1);
pair p2 = max(mid+1,rx,l,r,2*x+2);
pair ans = new pair();
ans.one = p1.one+p2.one;
ans.zero = p1.zero+p2.zero;
ans.i = Math.max(p1.i+p2.one,p2.i+p1.zero);
ans.i = Math.max(ans.i,Math.max(ans.one,ans.zero));
ans.d = Math.max(p1.d+p2.zero,p1.one+p2.d);
ans.d = Math.max(ans.d,Math.max(ans.one,ans.zero));
return ans;
}
static pair max(int l , int r)
{
return max(0,sz-1,l,r,0);
}
static void build(int lx , int rx , String s , int x)
{
st[x] = new pair();
if(lx == rx)
{
if(s.charAt(lx) == '4')
st[x].zero = 1;
else
st[x].one = 1;
st[x].i = 1;
st[x].d = 1;
return;
}
int mid = (lx+rx)/2;
build(lx,mid,s,2*x+1);
build(mid+1,rx,s,2*x+2);
st[x].one = st[2*x+1].one+st[2*x+2].one;
st[x].zero = st[2*x+1].zero+st[2*x+2].zero;
st[x].i = Math.max(st[2*x+1].i+st[2*x+2].one,st[2*x+2].i+st[2*x+1].zero);
st[x].i = Math.max(st[x].i,Math.max(st[x].one,st[x].zero));
st[x].d = Math.max(st[2*x+1].d+st[2*x+2].zero,st[2*x+1].one+st[2*x+2].d);
st[x].d = Math.max(st[x].d,Math.max(st[x].one,st[x].zero));
}*/
static boolean check(int x , int arr[] , int k)
{
int n = arr.length-1;
int odd[] = new int[n+1];
int even[] = new int[n+1];
odd[0] = Integer.MAX_VALUE;
int sum = 0;
for(int i = 1 ; i <= n ; i++)
{
if(arr[i] >= x)
sum++;
if(i%2 == 1)
{
if(i-k >= 0 && 2*sum-i-2 >= odd[i-k])
{
return true;
}
if(i-k >= 0 && 2*sum-i >= even[i-k])
return true;
even[i] = even[i-1];
odd[i] = Math.min(2*sum-i,odd[i-1]);
}
else
{
if(i-k >= 0 && 2*sum-i >= odd[i-k])
return true;
if(i-k >= 0 && 2*sum-i-2 >= even[i-k])
return true;
odd[i] = odd[i-1];
even[i] = Math.min(2*sum-i,even[i-1]);
}
}
return false;
}
public static void main(String []args) throws IOException
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int l = 1 , r = 300000;
int arr[] = new int[n+1];
for(int i =1 ; i <= n ; i++)
{
arr[i] = sc.nextInt();
}
while(l <= r)
{
int mid = (l+r)/2;
if(check(mid,arr,k))
l = mid+1;
else
r = mid-1;
}
System.out.println(r);
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
ce2138f61d1d833505ee8b543304cf4b
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public final class Solution {
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static long mod = (long) 1e9 + 7;
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)};
public static void main(String[] args) {
int t = 1;
out:
while (t-- > 0) {
int n = i();
int k = i();
int[] arr = input(n);
int l = arr[0];
int r = arr[0];
for (int i = 0; i < n; i++) {
l = Math.min(l, arr[i]);
r = Math.max(r, arr[i]);
}
while (l < r) {
int m = (l + r + 1) / 2;
if (q(m, arr, k)) {
l = m;
} else {
r = m - 1;
}
}
out.println(r);
}
out.flush();
}
private static boolean q(int m, int[] arr, int k) {
int[] pre = new int[arr.length];
int min = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= m) {
pre[i] = (i - 1 >= 0 ? pre[i - 1] : 0) + 1;
} else {
pre[i] = (i - 1 >= 0 ? pre[i - 1] : 0) - 1;
}
if (i - k + 1 == 0) {
if (pre[i] > 0) {
return true;
}
} else if (i - k + 1 > 0) {
min = Math.min(min, pre[i - k]);
if (pre[i] - min > 0) {
return true;
}
}
}
return false;
}
static boolean isPS(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static int sd(long i) {
int d = 0;
while (i > 0) {
d += i % 10;
i = i / 10;
}
return d;
}
static int[] leastPrime;
static void sieveLinear(int N) {
int[] primes = new int[N];
int idx = 0;
leastPrime = new int[N + 1];
for (int i = 2; i <= N; i++) {
if (leastPrime[i] == 0) {
primes[idx++] = i;
leastPrime[i] = i;
}
int curLP = leastPrime[i];
for (int j = 0; j < idx; j++) {
int p = primes[j];
if (p > curLP || (long) p * i > N) {
break;
} else {
leastPrime[p * i] = p;
}
}
}
}
static int lower(long A[], long x) {
int l = -1, r = A.length;
while (r - l > 1) {
int m = (l + r) / 2;
if (A[m] >= x) {
r = m;
} else {
l = m;
}
}
return r;
}
static int upper(long A[], long x) {
int l = -1, r = A.length;
while (r - l > 1) {
int m = (l + r) / 2;
if (A[m] <= x) {
l = m;
} else {
r = m;
}
}
return l;
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static int lowerBound(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 lowerBound(A, low, mid - 1, x);
}
return lowerBound(A, mid + 1, high, x);
}
static long pow(long a, long b) {
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 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) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static void printYes() {
out.println("YES");
}
static void printNo() {
out.println("NO");
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static String s() {
return in.nextLine();
}
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 int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
}
class BIT {
int n;
int[] tree;
public BIT(int n) {
this.n = n;
this.tree = new int[n + 1];
}
public static int lowbit(int x) {
return x & (-x);
}
public void update(int x) {
while (x <= n) {
++tree[x];
x += lowbit(x);
}
}
public int query(int x) {
int ans = 0;
while (x > 0) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
public int query(int x, int y) {
return query(y) - query(x - 1);
}
}
class SegmentTree {
long[] t;
public SegmentTree(int n) {
t = new long[n + n];
Arrays.fill(t, Long.MIN_VALUE);
}
public long get(int i) {
return t[i + t.length / 2];
}
public void add(int i, long value) {
i += t.length / 2;
t[i] = value;
for (; i > 1; i >>= 1) {
t[i >> 1] = Math.max(t[i], t[i ^ 1]);
}
}
// max[a, b]
public long max(int a, int b) {
long res = Long.MIN_VALUE;
for (a += t.length / 2, b += t.length / 2; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
if ((a & 1) != 0) {
res = Math.max(res, t[a]);
}
if ((b & 1) == 0) {
res = Math.max(res, t[b]);
}
}
return res;
}
}
class Pair {
int i, j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
}
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
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
| |
PASSED
|
d044c9594b7c83430c64984a97542c57
|
train_110.jsonl
|
1613658900
|
You are a given an array $$$a$$$ of length $$$n$$$. Find a subarray $$$a[l..r]$$$ with length at least $$$k$$$ with the largest median.A median in an array of length $$$n$$$ is an element which occupies position number $$$\lfloor \frac{n + 1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example: $$$median([1, 2, 3, 4]) = 2$$$, $$$median([3, 2, 1]) = 2$$$, $$$median([2, 1, 2, 1]) = 1$$$.Subarray $$$a[l..r]$$$ is a contiguous part of the array $$$a$$$, i. e. the array $$$a_l,a_{l+1},\ldots,a_r$$$ for some $$$1 \leq l \leq r \leq n$$$, its length is $$$r - l + 1$$$.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
InputReader in = new InputReader(inputStream);
// for(int i=6;i<=6;i++) {
/// InputStream uinputStream = new FileInputStream("2.in");
// String f = "Input"+i+".txt";
// String of = "boards.out";
// InputStream uinputStream = new FileInputStream(f);
/// InputReader in = new InputReader(uinputStream);
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(of)));
// Task t = new Task();
// t.solve(in, out);
// out.close();
// }
Task t = new Task();
t.solve(in, out);
out.close();
}
static class Task{
public void solve(InputReader in, PrintWriter out) throws IOException {
int n = in.nextInt();
int k = in.nextInt();
int arr[] = in.readIntArray(n);
int sz = 200001;
//sz = 101;
BIT bit1 = new BIT(sz);
BIT bit2 = new BIT(sz);
TreeMap<Integer,Integer> tm = new TreeMap<Integer,Integer>();
Queue<Integer> que = new LinkedList<Integer>();
int tot = 0;
int max = 0;
int start = 0;
for(int i=0;i<n;i++) {
if(i==29) {
//Dumper.print("here");
}
bit1.add(arr[i], 1);
tm.put(arr[i], tm.getOrDefault(arr[i], 0)+1);
tot++;
if(tot>=k) {
int p = get_median(bit1,tot,sz-1);
int cur = tm.floorKey(p);
max = Math.max(max, cur);
if(start<i-k+1) {
bit2.add(arr[start], 1);
que.add(arr[start++]);
int less = bit2.sum(max);
int bigger = tot-k-less;
if(bigger<=less) {
while(!que.isEmpty()) {
int x = que.poll();
bit1.add(x, -1);
bit2.add(x, -1);
tm.put(x, tm.get(x)-1);
if(tm.get(x)==0) tm.remove(x);
tot--;
}
p = get_median(bit1,tot,sz-1);
cur = tm.floorKey(p);
max = Math.max(max, cur);
}
}
}
}
out.println(max);
}
int get_median(BIT bit, int num, int sz) {
int l = 1;
int r = sz;
while(l<r-1) {
int mid = l+(r-l)/2;
if(bit.sum(mid)<(num+1)/2) {
l = mid;
}else {
r = mid;
}
}
if(bit.sum(l)>=(num+1)/2) return l;
return r;
}
int dfs(ArrayList<edge>[] g, int cur, boolean vis[], int dp[], int f, int dist[]) {
if(cur==0) return dp[cur] = 1;
if(dp[cur]>=0) return dp[cur];
int v = 0;
for(edge e:g[cur]) {
int nxt = e.t;
if(dist[nxt]>dist[cur]) {
vis[nxt] = true;
v+=dfs(g,nxt,vis,dp,f,dist);
v%=f;
}
}
return dp[cur] = v;
}
class edge implements Comparable<edge>{
int id,f,t; int len;
public edge(int a, int b, int c, int d) {
f=a;t=b;len=c;id=d;
}
@Override
public int compareTo(edge t) {
if(this.len-t.len>0) return 1;
else if(this.len-t.len<0) return -1;
return 0;
}
}
public int minMoves(int[] nums, int k) {
int n = 0;
for(int i:nums) n+=i;
int arr[] = new int[n];
for(int i=0,j=0;i<nums.length;i++) {
if(nums[i]==1) arr[j++] = i;
}
int sum[] = new int[n];
for(int i=0;i<n;i++) {
sum[i] = arr[i];
if(i>0) sum[i]+=sum[i-1];
}
int ret = Integer.MAX_VALUE;
for(int i=0;i+k-1<n;i++) {
int j = i+k-1;
int mid = (i+j)/2;
int lnum = mid-i;
int left = arr[mid]*lnum-(mid-1>=0?sum[mid-1]:0)+(i>0?sum[i-1]:0)-(1+lnum)*lnum/2;
int rnum = j-mid;
int right = sum[j]-sum[mid]-rnum*arr[mid]-(1+rnum)*rnum/2;
ret = Math.min(ret, left+right);
}
return ret;
}
class pair implements Comparable<pair>{
int idx;
int bit_pos;
public pair(int a, int b) {
idx=a;bit_pos=b;
}
@Override
public int compareTo(pair t) {
return t.idx-this.idx;
}
}
public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {
int idx = 0;
int m = edges.length;
ArrayList<edge> all = new ArrayList<edge>();
for(int x[]:edges) {
int f = x[0];
int t = x[1];
int w = x[2];
edge tmp1 = new edge(f,t,w,idx++);
all.add(tmp1);
}
DSU ds = new DSU(n+1);
Collections.sort(all);
Set<Integer> crit = new HashSet<Integer>();
Set<Integer> no_use = new HashSet<Integer>();
int p = 0;
boolean vis[] = new boolean[n];
while(p<m) {
edge t = all.get(p);
ArrayList<edge> tmp = new ArrayList<edge>();
tmp.add(t);
p++;
while(p<m&&all.get(p).len==t.len) {
tmp.add(all.get(p++));
}
int cnt[] = new int[n];
Arrays.fill(cnt, -1);
for(edge v:tmp) {
if(ds.find(v.f)==ds.find(v.t)) {
no_use.add(v.id);
continue;
}
if(!vis[v.f]) {
if(cnt[v.f]==-1) {
cnt[v.f] = v.id;
}else {
cnt[v.f] = -2;
}
}
if(!vis[v.t]) {
if(cnt[v.t]==-1) {
cnt[v.t] = v.id;
}else {
cnt[v.t] = -2;
}
}
}
for(int i=0;i<n;i++) {
if(cnt[i]>=0) crit.add(cnt[i]);
}
for(edge v:tmp) {
ds.union(v.f, v.t);
vis[v.f] = true;
vis[v.t] = true;
}
}
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> r1 = new ArrayList<Integer>();
List<Integer> r2 = new ArrayList<Integer>();
for(int i=0;i<m;i++) {
if(crit.contains(i)) r1.add(i);
else if(!no_use.contains(i)) {
r2.add(i);
}
}
ret.add(r1);ret.add(r2);
return ret;
}
class pair2 implements Comparable<pair2>{
long val; int idx;
public pair2(long a, int b) {
val = a;
idx = b;
}
@Override
public int compareTo(pair2 t) {
if(this.val-t.val>0) return 1;
else if(this.val-t.val<0) return -1;
return 0;
}
}
static class sgt{
sgt lt;
sgt rt;
int l,r;
int sum, max, min, lazy;
int min_idx;
public sgt(int L, int R, int arr[]) {
l=L;r=R;
if(l==r-1) {
sum = max = min = arr[l];
lazy = 0;
min_idx = l;
return;
}
lt = new sgt(l, l+r>>1, arr);
rt = new sgt(l+r>>1, r, arr);
pop_up();
}
void pop_up() {
this.sum = lt.sum + rt.sum;
this.max = Math.max(lt.max, rt.max);
this.min = Math.min(lt.min, rt.min);
if(lt.min<rt.min)
this.min_idx = lt.min_idx;
else if(lt.min>rt.min) this.min_idx = rt.min_idx;
else this.min_idx = Math.min(lt.min_idx, rt.min_idx);
}
void push_down() {
if(this.lazy!=0) {
lt.sum+=lazy;
rt.sum+=lazy;
lt.max+=lazy;
lt.min+=lazy;
rt.max+=lazy;
rt.min+=lazy;
lt.lazy+=this.lazy;
rt.lazy+=this.lazy;
this.lazy = 0;
}
}
void change(int L, int R, int v) {
if(R<=l||r<=L) return;
if(L<=l&&r<=R) {
this.max+=v;
this.min+=v;
this.sum+=v*(r-l);
this.lazy+=v;
return;
}
push_down();
lt.change(L, R, v);
rt.change(L, R, v);
pop_up();
}
int query_max(int L, int R) {
if(L<=l&&r<=R) return this.max;
if(r<=L||R<=l) return Integer.MIN_VALUE;
push_down();
return Math.max(lt.query_max(L, R), rt.query_max(L, R));
}
int query_min(int L, int R) {
if(L<=l&&r<=R) return this.min;
if(r<=L||R<=l) return Integer.MAX_VALUE;
push_down();
return Math.min(lt.query_min(L, R), rt.query_min(L, R));
}
int query_sum(int L, int R) {
if(L<=l&&r<=R) return this.sum;
if(r<=L||R<=l) return 0;
push_down();
return lt.query_sum(L, R) + rt.query_sum(L, R);
}
int query_min_idx(int L, int R) {
if(L<=l&&r<=R) {
return this.min_idx;
}
if(r<=L||R<=l) return Integer.MAX_VALUE;
int a = lt.query_min_idx(L, R);
int b = rt.query_min_idx(L, R);
int aa = lt.query_min(L, R);
int bb = rt.query_min(L, R);
int ret = 0;
if(aa<bb) ret = a;
else if(aa>bb) ret = b;
else ret = Math.min(a,b);
return ret;
}
}
// List<List<String>> convert(String arr[][]){
// int n = arr.length;
// List<List<String>> ret = new ArrayList<>();
// for(int i=0;i<n;i++) {
// ArrayList<String> tmp = new ArrayList<String>();
// for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]);
// ret.add(tmp);
// }
// return ret;
// }
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public long GCD(long a, long b) {
if (b==0) return a;
return GCD(b,a%b);
}
}
static class ArrayUtils {
static final long seed = System.nanoTime();
static final Random rand = new Random(seed);
public static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(int[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
public static void sort(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(long[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
static class BIT{
int arr[];
int n;
public BIT(int a) {
n=a;
arr = new int[n];
}
int sum(int p) {
int s=0;
while(p>0) {
s+=arr[p];
p-=p&(-p);
}
return s;
}
void add(int p, int v) {
while(p<n) {
arr[p]+=v;
p+=p&(-p);
}
}
}
static class DSU{
int[] arr;
int[] sz;
public DSU(int n) {
arr = new int[n];
sz = new int[n];
for(int i=0;i<n;i++) arr[i] = i;
Arrays.fill(sz, 1);
}
public int find(int a) {
if(arr[a]!=a) arr[a] = find(arr[a]);
return arr[a];
}
public void union(int a, int b) {
int x = find(a);
int y = find(b);
if(x==y) return;
arr[y] = x;
sz[x] += sz[y];
}
public int size(int x) {
return sz[find(x)];
}
}
static class MinHeap<Key> implements Iterable<Key> {
private int maxN;
private int n;
private int[] pq;
private int[] qp;
private Key[] keys;
private Comparator<Key> comparator;
public MinHeap(int capacity){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
}
public MinHeap(int capacity, Comparator<Key> c){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
comparator = c;
}
public boolean isEmpty() { return n==0; }
public int size() { return n; }
public boolean contains(int i) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
return qp[i] != -1;
}
public int peekIdx() {
if (n == 0) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
public Key peek(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return keys[pq[1]];
}
public int poll(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
int min = pq[1];
exch(1,n--);
down(1);
assert min==pq[n+1];
qp[min] = -1;
keys[min] = null;
pq[n+1] = -1;
return min;
}
public void update(int i, Key key) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (!contains(i)) {
this.add(i, key);
}else {
keys[i] = key;
up(qp[i]);
down(qp[i]);
}
}
private void add(int i, Key x){
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue");
n++;
qp[i] = n;
pq[n] = i;
keys[i] = x;
up(n);
}
private void up(int k){
while(k>1&&less(k,k/2)){
exch(k,k/2);
k/=2;
}
}
private void down(int k){
while(2*k<=n){
int j=2*k;
if(j<n&&less(j+1,j)) j++;
if(less(k,j)) break;
exch(k,j);
k=j;
}
}
public boolean less(int i, int j){
if (comparator == null) {
return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0;
}
else {
return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0;
}
}
public void exch(int i, int j){
int swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return null;
}
}
private static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int zcurChar;
private int znumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (znumChars == -1)
throw new InputMismatchException();
if (zcurChar >= znumChars)
{
zcurChar = 0;
try
{
znumChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (znumChars <= 0)
return -1;
}
return buf[zcurChar++];
}
public int nextInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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 nextString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class Dumper {
static void print_int_arr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_char_arr(char[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_double_arr(double[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(int[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(boolean[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print(Object o) {
System.out.println(o.toString());
}
static void getc() {
System.out.println("here");
}
}
}
|
Java
|
["5 3\n1 2 3 2 1", "4 2\n1 2 3 4"]
|
2 seconds
|
["2", "3"]
|
NoteIn the first example all the possible subarrays are $$$[1..3]$$$, $$$[1..4]$$$, $$$[1..5]$$$, $$$[2..4]$$$, $$$[2..5]$$$ and $$$[3..5]$$$ and the median for all of them is $$$2$$$, so the maximum possible median is $$$2$$$ too.In the second example $$$median([3..4]) = 3$$$.
|
Java 8
|
standard input
|
[
"binary search",
"data structures",
"dp"
] |
dddeb7663c948515def967374f2b8812
|
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
| 2,100
|
Output one integer $$$m$$$ — the maximum median you can get.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.