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
|
b856d04b53a0d992dced76a2b13c78d1
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import static java.lang.Math.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import java.io.PrintStream;
import java.io.PrintWriter;
public class A {
static int mod = 1000000007;
static long MOD = 1000000007;
static long temp = 998244353L;
static final long M = (int)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int aa, int bb)
{
first = aa; second = bb;
}
public int compareTo(Pair p)
{
// if(a == p.a) return b - p.b;
// return a - p.a;
if(first == p.first) return (second - p.second);
return (first - p.first);
}
}
/*
* IO FOR 2D GRID IN JAVA
* char[][] arr = new char[n][m]; //grid in Q.
for(int i = 0;i<n;i++)
{
char[] nowLine = sc.next().toCharArray();
for(int j = 0;j<m;j++)
{
arr[i][j] = nowLine[i];
}
}
* */
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 Reader {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static boolean isPrime(long n) {
if(n == 1)
{
return false;
}
for(long i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> Sieve(int n)
{
boolean prime[] = new boolean[n+1];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i=p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p=2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static int phi(int n) //euler totient function
{
int result = 1;
for (int i = 2; i < n; i++)
if (gcd(i, n) == 1)
result++;
return result;
}
public static int[] computePrefix(int arr[], int n)
{
int[] prefix = new int[n];
prefix[0] = arr[0];
for(int i = 1;i<n;i++)
{
prefix[i] = prefix[i-1]+arr[i];
}
return prefix;
}
public static long fastPow(long x, long n) //include mod at each step if asked and in args of fn too
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long power(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCr(long n, long r,
long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long[] modInvArr(long n, long mod)
{
long[] ans = new long[(int)(n)+1];
ans[1] = 1;
for(int i = 2;i <= n;i++)
{
ans[i] = (mod - (mod/i) * ans[(int)(mod%i)])%mod;
}
return ans;
}
public static int LowerBound(int a[], int x) {
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(int a[], int x) {
int l=-1;
int r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void Sort(int[] a) {
List<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
//Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//MODULO OPS for addition and multiplication
public static long perfomMod(long x){
return ((x%M + M)%M);
}
public static long addMod(long a, long b){
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long mulMod(long a, long b){
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static void main(String[] args)
{
Reader sc=new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0)
{
String s = sc.next();
System.out.println(s.length());
}
out.close();
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
3847d6a16cbdd2510fe4fe8d86e0620c
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
public class StrangeFunctions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
do {
String s = new String();
if(sc.hasNext()){
s= sc.next();
}
System.out.println(s.length());
t--;
} while (t > 0);
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
898d34998c97b7c684412ad1add25e74
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
public class UVA10550 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int T = s.nextInt();
for(int tt= 0;tt<T;tt++) {
String in = s.next();
int c = in.length();
//int pos = c-1;
//while(in.charAt(pos--)=='0'){
// c--;
// }
System.out.println(c);
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
5d8a7aefd12426473522a667afc94c7c
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
public class A {
static int INF = 1000000007;
public static void main(String[] args) {
int test = fs.nextInt();
// int test = 1;
for (int cases = 0; cases < test; cases++) {
String s = fs.next();
int a = s.length();
if (a == 1) {
System.out.println(1);
} else {
System.out.println(a);
}
}
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
static long nCrModPFermat(long n, long r, long p) {
long ans1 = 1;
long i = n;
long k = r;
while (k > 0) {
ans1 = mul(ans1, i, p);
i--;
k--;
}
long ans2 = 1;
while (r > 0) {
ans2 = mul(ans2, r, p);
r--;
}
r = modInverse(ans2, p);
ans1 = mul(ans1, r, p);
return ans1;
}
static long facCalc(long total) {
long ans = 1;
for (long i = 2; i <= total; i++) {
ans = mul(ans, i, INF);
}
return ans;
}
static long mul(long a, long b, long p) {
return ((a % p) * (b % p)) % p;
}
static void sieve() {
boolean prime[] = new boolean[101];
Arrays.fill(prime, true);
prime[1] = false;
for (int i = 2; i * i <= prime.length - 1; i++) {
for (int j = i * i; j <= prime.length - 1; j += i) {
prime[j] = false;
}
}
}
public static int[] radixSort(int[] f) {
return radixSort(f, f.length);
}
public static int[] radixSort(int[] f, int n) {
int[] to = new int[n];
{
int[] b = new int[65537];
for (int i = 0; i < n; i++)
b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++)
b[i] += b[i - 1];
for (int i = 0; i < n; i++)
to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < n; i++)
b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++)
b[i] += b[i - 1];
for (int i = 0; i < n; i++)
to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public String toString() {
return "[" + first + "," + second + "]";
}
}
static class LongPair {
long first;
long second;
LongPair(long a, long b) {
this.first = a;
this.second = b;
}
public String toString() {
return "[" + first + "," + second + "]";
}
}
static long expmodulo(long a, long b, long c) {
long x = 1;
long y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y) % c;
}
y = (y * y) % c; // squaring the base
b /= 2;
}
return (long) x % c;
}
// static int modInverse(int a, int m) {
// int m0 = m;
// int y = 0, x = 1;
//
// if (m == 1)
// return 0;
//
// while (a > 1) {
// int q = a / m;
// int t = m;
// m = a % m;
// a = t;
// t = y;
// y = x - q * y;
// x = t;
// }
// if (x < 0)
// x += m0;
// return x;
// }
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortMyMapusingValues(HashMap<Integer, Integer> hm) {
List<Map.Entry<Integer, Integer>> capitalList = new LinkedList<>(hm.entrySet());
Collections.sort(capitalList, new Comparator<Map.Entry<Integer, Integer>>() {
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<Integer, Integer> result = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}
}
static void primeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if (n > 2)
System.out.print(n);
}
static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sq = (long) Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static 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 OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
9b757ccf00ba731496f03f40e1e7161e
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) throws IOException
{
FastScanner f = new FastScanner();
int t=f.nextInt();
// int t=1;
while(t-->0){
// int n=f.nextInt();
// int ans=1000000;
String a=f.next();
System.out.println(a.length());
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long[] readLongArray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
efccf0b2b993ed2503873d8d45277143
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class firstProg {
public static void main(String[] args) throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(sc.readLine());
while (--t >= 0) {
System.out.println(sc.readLine().length());
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
b955b33b0fdbe4c8d4fbaa2c9aaac308
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
public class codeforces
{
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 result=0;
String s=br.readLine();
if(s.length()==1)
{
System.out.println("1");
}
else
{
if(s.length()>1)
{
System.out.println(s.length());
}
}
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
63b5f1403bcb46888aca3609c4cec49c
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.lang.*;
import java.util.*;
public class Codeforces
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
s.nextLine();
for(int i=0;i<t;i++)
{
String str=s.nextLine();
System.out.println(str.length());
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
161d5d0d89a15a970291d177bf9a943c
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
public class infOly6 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(r.readLine());
for( int i = 0; i < x; i ++) {
String n = r.readLine();
System.out.println(n.length());
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
f57eb20373c99ab9cde928f56045faad
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import com.sun.source.tree.Tree;
import java.io.*;
import java.util.*;
// ββββββ ββββ ββ ββββββ ββββ β ββββββββ ββββββββ ββββββ ββββββ ββββββ
// β β β β β ββ β β β β ββ β ββ β β β β β β β β β β β β β
// ββ β βββββ β β β βββ ββββββ β βββββ ββ ββ ββ
// ββ β β β ββ ββ β β β β ββ β ββ β ββ β ββ β
// ββββ ββ ββ ββββ ββ β ββββββ β β ββββ ββββ ββββ
// β β β β β β β β β β β β
// β β β β
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = nextInt();
for (int qqq = 0; qqq < t; qqq++) {
pw.println(next().length());
}
pw.close();
}
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
986864bea173ab776824e92f155093c7
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import com.sun.source.tree.Tree;
import java.io.*;
import java.util.*;
// ββββββ ββββ ββ ββββββ ββββ β ββββββββ ββββββββ ββββββ ββββββ ββββββ
// β β β β β ββ β β β β ββ β ββ β β β β β β β β β β β β β
// ββ β βββββ β β β βββ ββββββ β βββββ ββ ββ ββ
// ββ β β β ββ ββ β β β β ββ β ββ β ββ β ββ β
// ββββ ββ ββ ββββ ββ β ββββββ β β ββββ ββββ ββββ
// β β β β β β β β β β β β
// β β β β
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = nextInt();
for (int qqq = 0; qqq < t; qqq++) {
char[] a = next().toCharArray();
pw.println(a.length);
}
pw.close();
}
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
db78e0b2457317e8c814ee1cf1ebacbb
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.Scanner;
public class l {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int i=0;i<t;i++){
String str=s.next();
System.out.println(str.length());
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
7f1f01bb622cbb95d3880d81c5d579de
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1455a {
public static void main(String[] args) throws IOException {
int t = ri();
while (t --> 0) {
prln(rcha().length);
}
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int lcm(int a, int b) {return a * b / gcf(a, b);}
static long lcm(long a, long b) {return a * b / gcf(a, b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__out.flush();}
static void close() {__out.close();}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
c3143afe9f192d483acb8e7e87f193cf
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.HashSet;
import java.util.Scanner;
public class Raund {
public static void main(String[] args) {
int t = Reader.readInt();
while (t --> 0) {
String line = Reader.readLine();
Writer.add(line.length());
Writer.lineBreak();
}
Writer.lineBreak();
Writer.write();
}
public static long gx(long x) {
long fx = Long.parseLong(reverseLine(Long.toString(x)));
long ffx = Long.parseLong(reverseLine(Long.toString(fx)));
return x / ffx;
}
public static String reverseLine(String line) {
String result = "";
for (int i = line.length() - 1; i >= 0; i--) {
result = result + line.charAt(i);
}
return result;
}
public static String massToString(HashSet<Long> mass) {
StringBuilder builder = new StringBuilder();
for (Long m : mass) {
builder.append(m).append(" ");
}
return builder.toString();
}
public static class Reader {
public static Scanner in = new Scanner(System.in);
public static int readInt() {
return in.nextInt();
}
public static String readLine() {
return in.next();
}
public static long readLong() {
return in.nextLong();
}
}
public static class Writer {
public static StringBuilder builder = new StringBuilder();
public static void add(String line) {
builder.append(line);
}
public static void add(int num) {
builder.append(num);
}
public static void add(long num) {
builder.append(num);
}
public static void addPob() {
builder.append(" ");
}
public static void lineBreak() {
builder.append("\n");
}
public static void clear() {
builder.delete(0, builder.length());
}
public static void write() {
System.out.println(builder.toString());
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
1b0b0aa2e1a5eedc8228647171e626ac
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class CodeChef2 {
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 x,y;
public Pair(int x,int y) {
this.x=x;
this.y=y;
}
}
public static void main(String[] args) throws Exception {
FastReader sc=new FastReader();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int t=sc.nextInt();
long M=1000000007l;
outer:while(t-- > 0) {
String n=sc.next();
// System.out.println();
bw.append(n.length()+"\n");
}
bw.close();
}
private static int getNext2(long a) {
int i=0;
while(1<<i < a) {
i++;
}
return 1<<i;
}
// int n=sc.nextInt();
// int m=sc.nextInt();
//
// HashMap<Integer,Pair> vertex=new HashMap<>();
// HashMap<Integer,ArrayList<Integer>> graph=new HashMap<>();
//
// for(int i=0;i<m;i++) {
// vertex.put(i+1,new Pair(sc.nextInt(),sc.nextInt()));
// graph.put(i+1, new ArrayList<>());
// }
//
// for(int i:vertex.keySet()) {
// Pair p = vertex.get(i);
// int x=p.x;
// int y=p.y;
//
// for(int j:vertex.keySet()) {
// if(vertex.get(j).x ==x && vertex.get(j).y==y) {
// continue;
// }
// if(vertex.get(j).x ==x || vertex.get(j).y ==y) {
// graph.get(i).add(j);
// }
// }
// }
//
//// graph.forEach((i,j)->System.out.println(i+":"+j));
//
// boolean[] visited=new boolean[m+1];
//
// LinkedList<Integer> stack=new LinkedList<>();
//
// stack.push(1);
// while(!stack.isEmpty()) {
//
// int cur=stack.pop();
// if(visited[cur]) {
// continue;
// }
// visited[cur]=true;
// bw.append(cur+" ");
//
// for(int child:graph.get(cur)) {
// if(!visited[child])
// stack.push(child);
// }
// }
static ArrayList<Integer> getDivisor(int n){
ArrayList<Integer> div=new ArrayList<>();
for (int i=1; i*i <=n; i++)
{
if (n%i==0)
{
if (n/i == i)
div.add(i);
else {
div.add(i);
div.add(n/i);
}
}
}
return div;
}
static int gcd(int a,int b) {
return b==0?a:gcd(b,a%b);
}
static int MAXN = 1000001;
static int[] spf=new int[MAXN];
static void sieveSmallestFactor()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
private static HashMap<Long,Integer> PrimeFactorizationmap(long n) {
int count=0;
HashMap<Long,Integer> factors=new HashMap<>();
if(n==1) {
factors.put( 1l,1);
return factors;
}else {
for(long i=2; i*i <= n ;i++) {
long z=n;
if(z%i==0) {
count=0;
while(z%i==0) {
count++;
z=z/i;
}
factors.put(i+0l,count);
}
}
if(n>1) {
factors.put(n,1);
}
}
return factors;
}
static HashMap<Integer,Integer> getprimeFactors(int n)
{
HashMap<Integer,Integer> ret = new HashMap<>();
while (n > 1)
{
if(ret.containsKey(spf[n])) {
ret.put(spf[n],ret.get(spf[n])+1);
}else {
ret.put(spf[(int) n],1);
}
n = n / spf[n];
}
return ret;
}
static ArrayList<Integer> getPrimeSieve(){
int primesieve[]=new int[1000001];
Arrays.fill(primesieve,0);
for(int i=2;i*i<primesieve.length;i++) {
if(primesieve[i]==0)
for(int j=i*i;j<primesieve.length;j+=i) {
primesieve[j]=1;
}
}
ArrayList<Integer> prime=new ArrayList<>();
for(int i=2;i<primesieve.length;i++) {
if(primesieve[i]==0) {
prime.add(i);
}
}
return prime;
}
private static boolean checkPrimeRM(long n,int k) {
if(n<=4) {
return n==2||n==3;
}
int s=0;
long d=n-1;
while((d&1) != 1) {
d=d/2;
s++;
}
for(int i=0;i<k;i++) {
long a=2+(int)Math.random()*(n-4);
if(isComposite(a,s,d,n)) {
return false;
}
}
return true;
}
private static boolean isComposite(long a, int s, long d, long n) {
long x=power(a,d,n);
if(x==1 || x==n-1) {
return false;
}
for(int i=0;i<s;i++){
if(x%(n-1)==0) {
return false;
}
x=(x*x)%n;
}
return true;
}
private static long power(long a, long d,long n) {
long res=1;
while(d != 0) {
if((d&1)==1) {
res=(res*a)%n;
}
d>>=1;
a=(a*a)%n;
}
return res;
}
public static HashSet<Long> getPrimeLtoR(int l,int r,List<Integer> prime) {
if(l==1) l++;
int[] arr=new int[r-l+1];
Arrays.fill(arr,0);
for(int i: prime ){
if(i*i<=r) {
int j=(l/i)*i;
if(j<l)
j+=i;
for(;j<=r;j+=i) {
if(j!=i)
arr[j-l]=1;
}
}else {
break;
}
}
HashSet<Long> primeLtoR=new HashSet<>();
for(int i=0;i<arr.length;i++) {
if(arr[i]==0) {
primeLtoR.add((i+l+0l));
}
}
return primeLtoR;
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
4d86f9b9e8aa11d596184508735a9402
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
public class ytt
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();int c=0;
for(int i=1;i<=t;i++)
{
String s=sc.next();
c=s.length();
System.out.println(c); } }}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
ba1048b12814369cdc0f4c9e04b930f6
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Integer testCases= input.nextInt();
while(testCases!=0)
{
BigInteger number = input.nextBigInteger();
int digits = 0;
while(true)
{
number = number.divide(BigInteger.valueOf(10));
digits++;
if(number.equals(BigInteger.valueOf(0)))
break;
}
System.out.println(digits);
testCases--;
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
7a8d7ded6b38ae4f67b3c5f6aee60465
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class A {
public static class FastIO {
public static final int DEFAULT_BUFFER_SIZE = 65536;
public static final int DEFAULT_INTEGER_SIZE = 11;
public static final int DEFAULT_LONG_SIZE = 20;
public static final int DEFAULT_WORD_SIZE = 256;
public static final int DEFAULT_LINE_SIZE = 8192;
public static final int EOF = -1;
private final InputStream in;
private final OutputStream out;
private byte[] inBuffer;
private int nextIn, inLength;
private byte[] outBuffer;
private int nextOut;
private char[] charBuffer;
private byte[] byteBuffer;
public FastIO(InputStream in, OutputStream out, int inBufferSize, int outBufferSize) {
this.in = in;
this.inBuffer = new byte[inBufferSize];
this.nextIn = 0;
this.inLength = 0;
this.out = out;
this.outBuffer = new byte[outBufferSize];
this.nextOut = 0;
this.charBuffer = new char[DEFAULT_LINE_SIZE];
this.byteBuffer = new byte[DEFAULT_LONG_SIZE];
}
public FastIO(InputStream in, OutputStream out) {
this(in, out, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
}
public FastIO(InputStream in, OutputStream out, int bufferSize) {
this(in, out, bufferSize, bufferSize);
}
public char nextChar() {
byte b;
while (isSpace(b = read()))
;
return (char) b;
}
public String next() {
byte b;
while (isSpace(b = read()))
;
int pos = 0;
do {
charBuffer[pos++] = (char) b;
ensureCapacity(pos);
} while (!isSpace(b = read()));
return new String(charBuffer, 0, pos);
}
public String nextLine() {
byte b;
int pos = 0;
while (!isLine(b = read())) {
charBuffer[pos++] = (char) b;
ensureCapacity(pos);
}
return new String(charBuffer, 0, pos);
}
public int nextInt() {
byte b;
while (isSpace(b = read()))
;
boolean negative = false;
int result = b - '0';
if (b == '-') {
negative = true;
result = 0;
}
while (isDigit(b = read()))
result = (result * 10) + (b - '0');
return negative ? -result : result;
}
public long nextLong() {
byte b;
while (isSpace(b = read()))
;
boolean negative = false;
long result = b - '0';
if (b == '-') {
negative = true;
result = 0;
}
while (isDigit(b = read()))
result = (result * 10) + (b - '0');
return negative ? -result : result;
}
public float nextFloat() {
byte b;
while (isSpace(b = read()))
;
int pos = 0;
do {
charBuffer[pos++] = (char) b;
} while (!isSpace(b = read()));
return Float.parseFloat(new String(charBuffer, 0, pos));
}
public float nextFloat2() {
byte b;
while (isSpace(b = read()))
;
boolean negative = false;
float result = b - '0';
if (b == '-') {
negative = true;
result = 0;
}
while (isDigit(b = read()))
result = (result * 10) + (b - '0');
float d = 1;
if (b == '.') {
while (isDigit(b = read()))
result += (b - '0') / (d *= 10);
}
return negative ? -result : result;
}
public double nextDouble() {
byte b;
while (isSpace(b = read()))
;
int pos = 0;
do {
charBuffer[pos++] = (char) b;
} while (!isSpace(b = read()));
return Double.parseDouble(new String(charBuffer, 0, pos));
}
public double nextDouble2() {
byte b;
while (isSpace(b = read()))
;
boolean negative = false;
double result = b - '0';
if (b == '-') {
negative = true;
result = 0;
}
while (isDigit(b = read()))
result = (result * 10) + (b - '0');
double d = 1;
if (b == '.') {
while (isDigit(b = read()))
result += (b - '0') / (d *= 10);
}
return negative ? -result : result;
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
public char[] nextToCharArray() {
byte b;
while (isSpace(b = read()))
;
int pos = 0;
do {
charBuffer[pos++] = (char) b;
ensureCapacity(pos);
} while (!isSpace(b = read()));
char[] array = new char[pos];
System.arraycopy(charBuffer, 0, array, 0, pos);
return array;
}
public int[] nextIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = nextInt();
return array;
}
public long[] nextLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++)
array[i] = nextLong();
return array;
}
public int[][] nextInt2DArray(int Y, int X) {
int[][] array = new int[Y][X];
for (int y = 0; y < Y; y++)
for (int x = 0; x < X; x++)
array[y][x] = nextInt();
return array;
}
public void print(char c) {
write((byte) c);
}
public void print(char[] chars) {
print(chars, 0, chars.length);
}
public void print(char[] chars, int start) {
print(chars, start, chars.length);
}
public void print(char[] chars, int start, int end) {
for (int i = start; i < end; i++)
write((byte) chars[i]);
}
public void print(String s) {
for (int i = 0; i < s.length(); i++)
write((byte) s.charAt(i));
}
public void print(int i) {
if (i == 0) {
write((byte) '0');
return;
}
if (i == Integer.MIN_VALUE) {
write((byte) '-');
write((byte) '2');
write((byte) '1');
write((byte) '4');
write((byte) '7');
write((byte) '4');
write((byte) '8');
write((byte) '3');
write((byte) '6');
write((byte) '4');
write((byte) '8');
return;
}
if (i < 0) {
write((byte) '-');
i = -i;
}
int pos = 0;
while (i > 0) {
byteBuffer[pos++] = (byte) ((i % 10) + '0');
i /= 10;
}
while (pos-- > 0)
write(byteBuffer[pos]);
}
public void print(long l) {
if (l == 0) {
write((byte) '0');
return;
}
if (l == Long.MIN_VALUE) {
write((byte) '-');
write((byte) '9');
write((byte) '2');
write((byte) '2');
write((byte) '3');
write((byte) '3');
write((byte) '7');
write((byte) '2');
write((byte) '0');
write((byte) '3');
write((byte) '6');
write((byte) '8');
write((byte) '5');
write((byte) '4');
write((byte) '7');
write((byte) '7');
write((byte) '5');
write((byte) '8');
write((byte) '0');
write((byte) '8');
return;
}
if (l < 0) {
write((byte) '-');
l = -l;
}
int pos = 0;
while (l > 0) {
byteBuffer[pos++] = (byte) ((l % 10) + '0');
l /= 10;
}
while (pos-- > 0)
write(byteBuffer[pos]);
}
public void print(float f) {
String sf = Float.toString(f);
for (int i = 0; i < sf.length(); i++)
write((byte) sf.charAt(i));
}
public void print(double d) {
String sd = Double.toString(d);
for (int i = 0; i < sd.length(); i++)
write((byte) sd.charAt(i));
}
public void printls(char c) {
print(c);
write((byte) ' ');
}
public void printls(String s) {
print(s);
write((byte) ' ');
}
public void printls(int i) {
print(i);
write((byte) ' ');
}
public void printls(long l) {
print(l);
write((byte) ' ');
}
public void printls(float f) {
print(f);
write((byte) ' ');
}
public void printls(double d) {
print(d);
write((byte) ' ');
}
public void printls() {
write((byte) ' ');
}
public void println(char c) {
print(c);
write((byte) '\n');
}
public void println(char[] chars) {
print(chars, 0, chars.length);
write((byte) '\n');
}
public void println(String s) {
print(s);
write((byte) '\n');
}
public void println(int i) {
print(i);
write((byte) '\n');
}
public void println(long l) {
print(l);
write((byte) '\n');
}
public void println(float f) {
print(f);
write((byte) '\n');
}
public void println(double d) {
print(d);
write((byte) '\n');
}
public void println() {
write((byte) '\n');
}
public void printf(String format, Object... args) {
String s = String.format(format, args);
for (int i = 0; i < s.length(); i++)
write((byte) s.charAt(i));
}
public void fprint(char c) {
print(c);
flushBuffer();
}
public void fprint(String s) {
print(s);
flushBuffer();
}
public void fprint(int i) {
print(i);
flushBuffer();
}
public void fprint(long l) {
print(l);
flushBuffer();
}
public void fprint(float f) {
print(f);
flushBuffer();
}
public void fprint(double d) {
print(d);
flushBuffer();
}
public void fprintf(String format, Object... args) {
printf(format, args);
flushBuffer();
}
private byte read() {
if (nextIn >= inLength) {
if ((inLength = fill()) == EOF)
return EOF;
nextIn = 0;
}
return inBuffer[nextIn++];
}
private void write(byte b) {
if (nextOut >= outBuffer.length)
flushBuffer();
outBuffer[nextOut++] = b;
}
private int fill() {
try {
return in.read(inBuffer, 0, inBuffer.length);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void flush() {
flushBuffer();
}
private void flushBuffer() {
if (nextOut == 0)
return;
try {
out.write(outBuffer, 0, nextOut);
} catch (Exception e) {
throw new RuntimeException(e);
}
nextOut = 0;
}
public void close() {
flush();
}
public void exit(char c) {
fprint(c);
System.exit(0);
}
public void exit(String s) {
fprint(s);
System.exit(0);
}
public void exit(int i) {
fprint(i);
System.exit(0);
}
public void exit(long l) {
fprint(l);
System.exit(0);
}
public void exit(float f) {
fprint(f);
System.exit(0);
}
public void exit(double d) {
fprint(d);
System.exit(0);
}
public void exit(String format, Object... args) {
fprintf(format, args);
System.exit(0);
}
public void exit() {
flushBuffer();
System.exit(0);
}
private void ensureCapacity(int size) {
if (size < charBuffer.length)
return;
char[] array = new char[size * 2];
System.arraycopy(charBuffer, 0, array, 0, size);
charBuffer = array;
}
private boolean isDigit(byte b) {
return b >= '0' && b <= '9';
}
private boolean isLine(byte b) {
return b == '\n' || b == '\r' || b == EOF;
}
private boolean isSpace(byte b) {
return b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\f' || b == EOF;
}
}
public static void main(String[] args) {
FastIO ioio = new FastIO(System.in, System.out);
int tc = ioio.nextInt();
for (int tt = 0; tt < tc; tt++) {
String s = ioio.next();
int a = s.length();
System.out.println(a);
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
f368d4388d77f1028dbd43cc4f8cda21
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class A{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int mod = (int)(1e9+7);
public static long pow(long a,long b)
{
long ans = 1;
while(b> 0)
{
if((b & 1)==1){
ans = (ans*a) % mod;
}
a = (a*a) % mod;
b = b>>1;
}
return ans;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = in.nextInt();
while(t-- >0)
{
String s = in.nextLine();
out.printLine(s.length());
}
out.flush();
out.close();
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
d4eb21c483c439df6f97758d3a66d8e7
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
int count = 0;
while(t > 0) {
BigInteger n = input.nextBigInteger();
BigInteger division = new BigInteger("10");
BigInteger zero = new BigInteger("0");
do {
n = n.divide(division);
count++;
} while(n.compareTo(zero) != 0);
System.out.println(count);
count = 0;
t--;
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
98b4e832026a7103555c658b070f8e63
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.io.*;
import java.util.Scanner;
public class StrangeFunctions {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static Reader sc = new Reader();
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String args[]) throws IOException {
/*
* For integer input: int n=inputInt();
* For long input: long n=inputLong();
* For double input: double n=inputDouble();
* For String input: String s=inputString();
* Logic goes here
* For printing without space: print(a+""); where a is a variable of any datatype
* For printing with space: printSp(a+""); where a is a variable of any datatype
* For printing with new line: println(a+""); where a is a variable of any datatype
*/
Scanner s = new Scanner(System.in);
int t = s.nextInt();
s.nextLine();
while(t-->0){
String str = s.nextLine();
System.out.println(str.length());
}
bw.flush();
bw.close();
}
public static int inputInt() throws IOException {
return sc.nextInt();
}
public static long inputLong() throws IOException {
return sc.nextLong();
}
public static double inputDouble() throws IOException {
return sc.nextDouble();
}
public static String inputString() throws IOException {
return sc.readLine();
}
public static void print(String a) throws IOException {
bw.write(a);
}
public static void printSp(String a) throws IOException {
bw.write(a + " ");
}
public static void println(String a) throws IOException {
bw.write(a + "\n");
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
b2dcf5a49ccb00292e0151d6a415cee8
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int test = scanner.nextInt();
for (int i = 0; i < test; i++) {
BigInteger n = scanner.nextBigInteger();
System.out.println(countDigit(n));
}
}
static long countDigit(BigInteger n)
{
if (n.equals(BigInteger.ZERO))
return 0;
return countDigit(n.divide(BigInteger.TEN))+1 ;
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
c352c3958d6f3855c4f1c8b3bad15783
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.Scanner;
public class Test1 {
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0;i<t;i++) {
BigInteger a = sc.nextBigInteger();
BigInteger c = BigInteger.valueOf(1);
int count = 0;
while(c.compareTo(a)!=1) {
count++;
c = c.multiply(BigInteger.valueOf(10));
}
System.out.println(count);
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
5b454abd8b15990c87d6ec2bb6e46f14
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
import java.math.*;
public class Main{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int t= sc.nextInt();
sc.nextLine();
while(t-- !=0)
{
BigInteger n = new BigInteger(sc.nextLine());
long c=0;
long i=0;
while(BigDecimal.valueOf(Math.pow(10,i)).toBigInteger().compareTo(n)<=0)
{
i++;
c++;
}
System.out.println(c);
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
3584dbcd8397ad2e08443d6d3b448a84
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
public static int total(BigInteger a){
BigInteger temp = a;
int b = 0;
int r = 10;
do{
temp = temp.divide(BigInteger.valueOf(r));
b ++;
}while (temp != BigInteger.ZERO);
return b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
BigInteger in [] = new BigInteger [t];
for (int i = 0; i < t; i ++){
in[i] = sc.nextBigInteger();
}
for(int i = 0; i < t; i ++){
int s = total(in[i]);
System.out.println(s);
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
efc43b570f065343a03dd9bc3221d7f9
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.*;
import java.util.Scanner;
public class StrangeFunctions {
public static int getCount(BigInteger number) {
double factor = Math.log(2) / Math.log(10);
int Count = (int) (factor * number.bitLength() + 1);
if (BigInteger.TEN.pow(Count - 1).compareTo(number) > 0) {
return Count - 1;
}
return Count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
BigInteger n = sc.nextBigInteger();
System.out.println(getCount(n));
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
3f25b2e5c613d7e5f682d370a2c57f1a
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.math.BigInteger;
import java.util.Scanner;
public class Edu9982 {
public static int getDigitCount(BigInteger number) {
double factor = Math.log(2) / Math.log(10);
int digitCount = (int) (factor * number.bitLength() + 1);
if (BigInteger.TEN.pow(digitCount - 1).compareTo(number) > 0) {
return digitCount - 1;
}
return digitCount;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
BigInteger n = sc.nextBigInteger();
System.out.println(getDigitCount(n));
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
a2bf5faad0107dcd68eb44abf3d3f9c8
|
train_000.jsonl
|
1606746900
|
Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.
|
256 megabytes
|
import java.util.Scanner;
public class StrangeFunc {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
String n;
for(int i=t;i>0;i--) {
n=sc.nextLine();
int ans=n.length();
System.out.println(ans);
}
}
}
|
Java
|
["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"]
|
2 seconds
|
["1\n2\n9\n10\n26"]
|
NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.
|
Java 11
|
standard input
|
[
"constructive algorithms",
"number theory",
"math"
] |
ea011f93837fdf985f7eaa7a43e22cc8
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.
| null |
For each test case, print one integer β the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.
|
standard output
| |
PASSED
|
efadc8c2e53854bb636131488b289f4f
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.InputMismatchException;
import java.util.TreeMap;
public class c3po {
static class Solver {
int N, ans[][], AID = 0;
TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> pts;
// all points have same x and y; solve
int solveY(TreeMap<Integer, Integer> ypts) {
int last = -1;
for(int z : ypts.keySet()) {
z = ypts.get(z);
if(last != -1) {
ans[AID][0] = last;
ans[AID++][1] = z;
last = -1;
} else
last = z;
}
return last;
}
// all points have same x; solve
int solveX(TreeMap<Integer, TreeMap<Integer, Integer>> xpts) {
ArrayDeque<Integer> post = new ArrayDeque<>();
for(int y : xpts.keySet()) {
int go = solveY(xpts.get(y));
if(go != -1) post.add(go);
}
while(!post.isEmpty()) {
if(post.size() == 1) return post.pollFirst();
ans[AID][0] = post.pollFirst();
ans[AID++][1] = post.pollFirst();
}
return -1;
}
void add(int x, int y, int z, int id) {
if (!pts.containsKey(x)) pts.put(x, new TreeMap<>());
if (!pts.get(x).containsKey(y)) pts.get(x).put(y, new TreeMap<>());
pts.get(x).get(y).put(z, id);
}
void solve(FastScanner s, PrintWriter out) {
N = s.nextInt(); pts = new TreeMap<>();
for(int x, y, z, i = 0; i < N; i++) {
x = s.nextInt(); y = s.nextInt(); z = s.nextInt();
add(x, y, z, i + 1);
}
ans = new int[N / 2][2];
ArrayDeque<Integer> post = new ArrayDeque<>();
for(int x : pts.keySet()) {
int go = solveX(pts.get(x));
if (go != -1) post.add(go);
}
while(!post.isEmpty()) {
ans[AID][0] = post.pollFirst();
ans[AID++][1] = post.pollFirst();
}
for(int[] a : ans)
out.printf("%d %d%n", a[0], a[1]);
}
}
public static void main(String[] args) {
FastScanner s = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
Solver solver = new Solver();
solver.solve(s, out);
out.close();
s.close();
}
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
public FastScanner(File f) throws FileNotFoundException {
this(new FileInputStream(f));
}
public FastScanner(String s) {
this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
}
void close() {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
// Jacob Garbage
public int[] nextIntArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = this.nextInt();
return ret;
}
public int[][] next2DIntArray(int N, int M) {
int[][] ret = new int[N][];
for (int i = 0; i < N; i++)
ret[i] = this.nextIntArray(M);
return ret;
}
public long[] nextLongArray(int N) {
long[] ret = new long[N];
for (int i = 0; i < N; i++)
ret[i] = this.nextLong();
return ret;
}
public long[][] next2DLongArray(int N, int M) {
long[][] ret = new long[N][];
for (int i = 0; i < N; i++)
ret[i] = nextLongArray(M);
return ret;
}
public double[] nextDoubleArray(int N) {
double[] ret = new double[N];
for (int i = 0; i < N; i++)
ret[i] = this.nextDouble();
return ret;
}
public double[][] next2DDoubleArray(int N, int M) {
double[][] ret = new double[N][];
for (int i = 0; i < N; i++)
ret[i] = this.nextDoubleArray(M);
return ret;
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
5b4b7330105726522034699892de1b08
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class BalancedRemovals
{
public static void main(String[] args)
{
FS scan = new FS();
int n = scan.nextInt();
Pt[] arr = new Pt[n];
int[] next = new int[n];
Arrays.fill(next, -1);
for(int i = 0; i < n; i++){
arr[i] = new Pt(scan.nextInt(), scan.nextInt(), scan.nextInt(), i+1);
if(i > 0) next[i-1] = i;
}
Arrays.sort(arr);
PrintWriter out = new PrintWriter(System.out);
boolean[] used = new boolean[n];
// System.out.println(Arrays.toString(next));
// System.out.println(Arrays.toString(used));
int i = 0;
int last = -1;
while(i < n){
if(used[i]){
i++;
continue;
}
if(next[i] != -1)
if(arr[i].x == arr[next[i]].x && arr[i].y == arr[next[i]].y){
used[i] = true;
used[next[i]] = true;
out.println(arr[i].id+" "+arr[next[i]].id);
i++;
continue;
}
// System.out.println("I: "+i);
// System.out.println(last);
if(last == -1) last = i;
else{
next[i] = -1;
next[last] = i;
last = i;
}
i++;
}
// System.out.println(Arrays.toString(next));
// System.out.println(Arrays.toString(used));
i = 0;
last = -1;
while(i < n){
if(used[i]){
i++;
continue;
}
if(next[i] != -1)
if(arr[i].x == arr[next[i]].x){
used[i] = true;
used[next[i]] = true;
out.println(arr[i].id+" "+arr[next[i]].id);
i++;
continue;
}
if(last == -1) last = i;
else{
next[i] = -1;
next[last] = i;
last = i;
}
i++;
}
// System.out.println("past 2nd sweep");
// System.out.println(Arrays.toString(next));
// System.out.println(Arrays.toString(used));
for(i = 0; i < n; i++){
if(!used[i]){
out.println(arr[i].id+" "+arr[next[i]].id);
used[i] = true;
used[next[i]] = true;
}
}
out.flush();
}
static class Pt implements Comparable<Pt>{
int x;
int y;
int z;
int id;
public Pt(int x, int y, int z, int i){
this.x = x;
this.y = y;
this.z = z;
id = i;
}
@Override
public int compareTo(Pt arg0)
{
if(this.x == arg0.x){
if(this.y == arg0.y){
return this.z-arg0.z;
}
return this.y-arg0.y;
}
return this.x-arg0.x;
}
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
a796d89debd1007a00a334e1b91111a1
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
import java.util.stream.IntStream;
import java.io.*;
public class BalancedRemovalshard {
/************************ SOLUTION STARTS HERE ************************/
static int pairing[][];
static int pts[][];
static int ptr;
static Integer order[];
static int rec(int dim, int l, int r) { // r excluded
if(dim == 0) { // base case
for(int i = l; i + 1 < r; i += 2) {
pairing[ptr][0] = order[i] + 1;
pairing[ptr][1] = order[i + 1] + 1;
ptr++;
}
return (r - l) % 2 == 0 ? -1 : r - 1;
}
else {
ArrayList<Integer> collect = new ArrayList<>();
for(int i = l; i < r; ) {
int j = i + 1;
for(; j < r && pts[j][dim] == pts[i][dim]; j++)
;
int ret = rec(dim - 1, i, j);
i = j;
if(ret >= 0)
collect.add(ret);
}
for(int i = 0; i + 1 < collect.size(); i += 2) {
pairing[ptr][0] = order[collect.get(i)] + 1;
pairing[ptr][1] = order[collect.get(i + 1)] + 1;
ptr++;
}
return collect.size() % 2 == 0 ? -1 : collect.get(collect.size() - 1);
}
}
private static void solve() {
int n = nextInt();
pts = new int[n][];
for(int i = 0; i < n; i++)
pts[i] = nextIntArray(3);
order = new Integer[n];
for(int i = 0; i < n; i++)
order[i] = i;
Arrays.sort(order, (i1, i2) -> pts[i1][2] != pts[i2][2] ? pts[i1][2] - pts[i2][2]
: (pts[i1][1] != pts[i2][1] ? pts[i1][1] - pts[i2][1]
: pts[i1][0] - pts[i2][0]));
Arrays.sort(pts, (p1, p2) -> p1[2] != p2[2] ? p1[2] - p2[2]
: (p1[1] != p2[1] ? p1[1] - p2[1] : p1[0] - p2[0]));
ptr = 0;
pairing = new int[n / 2][2];
rec(2, 0, n);
Arrays.stream(pairing).forEach(p -> println(p[0] + " " + p[1]));
}
/************************ SOLUTION ENDS HERE ************************/
/************************ TEMPLATE STARTS HERE **********************/
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
st = null;
solve();
reader.close();
writer.close();
}
static BufferedReader reader;
static PrintWriter writer;
static StringTokenizer st;
static String next()
{while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}
st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
static String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}
static int nextInt() {return Integer.parseInt(next());}
static long nextLong() {return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static char nextChar() {return next().charAt(0);}
static int[] nextIntArray(int n) {int[] a= new int[n]; int i=0;while(i<n){a[i++]=nextInt();} return a;}
static long[] nextLongArray(int n) {long[]a= new long[n]; int i=0;while(i<n){a[i++]=nextLong();} return a;}
static int[] nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}
static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}
static void print(Object o) { writer.print(o); }
static void println(Object o){ writer.println(o);}
/************************ TEMPLATE ENDS HERE ************************/
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
904760761bbaf88c6369f9d23159748c
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
public class ProblemF {
public static void main (String[] strings){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
TreeMap<Integer, List<Triple>> map = new TreeMap<>();
for (int i = 1; i <= n; i++) {
int x = scanner.nextInt();
Triple triple = new Triple(i, scanner.nextInt(), scanner.nextInt());
if (map.containsKey(x)){
map.get(x).add(triple);
}
else{
ArrayList<Triple> temp = new ArrayList<>();
temp.add(triple);
map.put(x, temp);
}
}
List<Triple>[] arr = new List[map.size()];
map.values().toArray(arr);
Triple old = null;
for (int i = 0; i < arr.length; i++) {
List<Triple> list = arr[i];
Collections.sort(list);
int j = 0;
boolean bool = list.size() %2 != 0;
for (; j < list.size() - 1;) {
Triple curr = list.get(j);
if (curr.y == list.get(j+1).y){
System.out.println(curr.n + " " + list.get(j+1).n);
j+=2;
}
else{
int pos = j+1;
int nextY = list.get(pos).y;
int nextEl = pos;
int delta = Integer.MAX_VALUE;
while (pos < list.size() && list.get(pos).y == nextY){
int currDelta = Math.abs(curr.z - list.get(pos).z);
if (currDelta < delta){
delta = currDelta;
nextEl = pos;
}
pos++;
}
System.out.println(curr.n + " " + list.remove(nextEl).n);
j++;
}
}
if (bool){
if (old == null){
old = list.get(list.size()-1);
}
else{
System.out.println(old.n + " " + list.get(list.size()-1).n);
old = null;
}
}
}
}
}
class Triple implements Comparable<Triple>{
int n, y, z;
public Triple(int n, int y, int z) {
this.n = n;
this.y = y;
this.z = z;
}
@Override
public int compareTo(Triple o) {
if (this.y == o.y)
return this.z - o.z;
return this.y - o.y;
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
ee3fc2a109df847b95b006912365e5ab
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import javax.print.attribute.IntegerSyntax;
import java.io.*;
import java.util.*;
/**
* A simple template for competitive programming problems.
*/
public class Banana {
final InputReader in = new InputReader(System.in);
final PrintWriter out = new PrintWriter(System.out);
final long mod = 1000000009;
final boolean DEBUG = false;
boolean[] taken; Point[] points;
void solve() {
int n = in.nextInt();
points = new Point[n];
for(int i=0; i<n; i++) {
points[i] = new Point(in.nextInt(), in.nextInt(), in.nextInt(), i+1);
}
Arrays.sort(points);
if (DEBUG) {
for(int i=0; i<n; i++) {
out.print(points[i].index + " ");
}
out.println();
}
taken = new boolean[n];
for(int i=0; i<n; i++) {
if(taken[i]) continue;
int lastJ = i+1;
while(taken[lastJ]) {
lastJ++;
}
if(points[i].x==points[lastJ].x) {
if(points[i].y==points[lastJ].y) {
print(i, lastJ);
continue;
}
if(points[i].z <= points[lastJ].z) {
print(i, lastJ);
continue;
}
for(int j=lastJ; j<n; j++) {
if(taken[j]) continue;
if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y && points[j].z<=points[i].z)) {
break;
}
lastJ = j;
}
print(i, lastJ);
} else {
if(points[i].y <= points[lastJ].y) {
for(int j=lastJ; j<n; j++) {
if(taken[j]) continue;
if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y && points[j].z<=points[i].z)) {
break;
}
lastJ = j;
}
print(i, lastJ);
continue;
}
for(int j=lastJ; j<n; j++) {
if(taken[j]) continue;
if(!(points[j].x==points[lastJ].x && points[j].y<=points[i].y)) {
break;
}
lastJ = j;
}
for(int j=lastJ; j>i; j--) {
if(taken[j]) continue;
if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y)) {
break;
}
lastJ = j;
}
for(int j=lastJ; j<n; j++) {
if(taken[j]) continue;
if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y && points[j].z<=points[i].z)) {
break;
}
lastJ = j;
}
print(i, lastJ);
}
}
}
private void print(int i, int j) {
out.println(points[i].index + " " + points[j].index);
taken[i] = true;
taken[j] = true;
}
private boolean inBetween(Point p1, Point p2, Point p3) {
int minX = Math.min(p1.x, p2.x);
int minY = Math.min(p1.y, p2.y);
int minZ = Math.min(p1.z, p2.z);
int maxX = Math.max(p1.x, p2.x);
int maxY = Math.max(p1.y, p2.y);
int maxZ = Math.max(p1.z, p2.z);
return p3.x <= maxX && p3.x >= minX && p3.y >= minY && p3.y <= maxY && p3.z <= maxZ && p3.z >= minZ;
}
class Point implements Comparable<Point> {
int x; int y; int z;
int index;
Point(int x, int y, int z, int index) {
this.x = x; this.y = y; this.z = z; this.index = index;
}
@Override
public int compareTo(Point p) {
int diff = this.x - p.x;
if(diff!=0)
return diff;
diff = this.y - p.y;
if(diff != 0)
return diff;
return this.z - p.z;
}
}
public static void main(String[] args) throws FileNotFoundException { Banana s = new Banana(); s.solve(); s.out.close(); }
public Banana() throws FileNotFoundException { }
private static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
InputReader(InputStream stream) {
this.stream = stream;
}
InputReader(String fileName) {
InputStream stream = null;
try {
stream = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.stream = stream;
}
int[] nextArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
int[][] nextMatrix(int n, int m) {
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
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();
}
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;
}
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;
}
double nextDouble() {
return Double.parseDouble(nextString());
}
private 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++];
}
private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
f0d31aadb3d5ee9aef260ca7ba7a9a85
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC2 solver = new TaskC2();
solver.solve(1, in, out);
out.close();
}
static class TaskC2 {
final int DIMENSIONS = 3;
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
List<Point> ps = new ArrayList<>();
for (int i = 0; i < n; i++) {
Point p = new Point();
p.id = i + 1;
for (int j = 0; j < DIMENSIONS; j++) {
p.coord[j] = in.nextInt();
}
ps.add(p);
}
Collections.sort(ps);
Point last = solve(out, ps, 0);
if (last != null) {
throw new AssertionError();
}
}
private Point solve(PrintWriter out, List<Point> ps, int dim) {
if (dim == DIMENSIONS) {
if (ps.size() != 1) {
throw new AssertionError();
}
return ps.get(0);
}
List<Point> leftovers = new ArrayList<>();
for (List<Point> g : groupBy(ps, dim)) {
Point last = solve(out, g, dim + 1);
if (last != null) {
leftovers.add(last);
}
}
return solveOneDimensional(out, leftovers);
}
private Point solveOneDimensional(PrintWriter out, List<Point> ps) {
for (int i = 0; i + 1 < ps.size(); i += 2) {
out.println(ps.get(i).id + " " + ps.get(i + 1).id);
}
if (ps.size() % 2 == 1) {
return ps.get(ps.size() - 1);
}
return null;
}
private List<List<Point>> groupBy(List<Point> ps, int dim) {
List<List<Point>> groups = new ArrayList<>();
for (int i = 0; i < ps.size(); ) {
int j = i;
while (j < ps.size() && ps.get(i).coord[dim] == ps.get(j).coord[dim]) {
++j;
}
List<Point> cur = new ArrayList<>();
for (int k = i; k < j; k++) {
cur.add(ps.get(k));
}
groups.add(cur);
i = j;
}
return groups;
}
class Point implements Comparable<Point> {
int[] coord = new int[DIMENSIONS];
int id;
public int compareTo(Point o) {
for (int i = 0; i < coord.length; i++) {
if (coord[i] != o.coord[i]) {
return coord[i] < o.coord[i] ? -1 : 1;
}
}
return 0;
}
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
ddee5f1d58a80712542d5efb87fc910d
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC2 solver = new TaskC2();
solver.solve(1, in, out);
out.close();
}
static class TaskC2 {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
Point[] p = new Point[n];
for (int i = 0; i < n; i++) {
p[i] = new Point();
p[i].x = in.nextInt();
p[i].y = in.nextInt();
p[i].z = in.nextInt();
p[i].id = i + 1;
}
Arrays.sort(p);
Point[] leftoversZ = new Point[n];
int numLeftoversZ = 0;
Point[] leftoversY = new Point[n];
int numLeftoversY = 0;
for (int iz = 0; iz < n; ) {
int jz = iz;
while (jz < n && p[iz].z == p[jz].z) {
++jz;
}
numLeftoversY = 0;
for (int iy = iz; iy < jz; ) {
int jy = iy;
while (jy < jz && p[iy].y == p[jy].y) {
++jy;
}
for (int k = iy; k + 1 < jy; k += 2) {
out.println(p[k].id + " " + p[k + 1].id);
}
if ((jy - iy) % 2 != 0) {
leftoversY[numLeftoversY++] = p[jy - 1];
}
iy = jy;
}
for (int i = 0; i + 1 < numLeftoversY; i += 2) {
Point a = leftoversY[i];
Point b = leftoversY[i + 1];
out.println(a.id + " " + b.id);
}
if (numLeftoversY % 2 != 0) {
leftoversZ[numLeftoversZ++] = leftoversY[numLeftoversY - 1];
}
iz = jz;
}
for (int i = 0; i + 1 < numLeftoversZ; i += 2) {
Point a = leftoversZ[i];
Point b = leftoversZ[i + 1];
out.println(a.id + " " + b.id);
}
if (numLeftoversZ % 2 != 0) {
leftoversZ[numLeftoversZ++] = leftoversZ[numLeftoversZ - 1];
}
}
class Point implements Comparable<Point> {
int x;
int y;
int z;
int id;
public int compareTo(Point o) {
if (z != o.z) {
return z < o.z ? -1 : 1;
}
if (y != o.y) {
return y < o.y ? -1 : 1;
}
if (x != o.x) {
return x < o.x ? -1 : 1;
}
return 0;
}
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
076aa00bcb57b9aed6a4124bd9aa2b81
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class c {
static StringBuilder sol;
public static void main(String[] args) {
FS scan = new FS(System.in);
int N = scan.nextInt();
Point[] list = new Point[N];
for(int i=0;i<N;i++)list[i] = new Point(scan.nextInt(),scan.nextInt(),scan.nextInt(),i+1);
Arrays.sort(list, (a,b)->{
if(a.z==b.z&&a.x==b.x)return a.y-b.y;
else if(a.z==b.z)return a.x-b.x;
else return a.z-b.z;
});
sol = new StringBuilder();
ArrayList<Point> zord = new ArrayList<>();
ArrayList<ArrayList<Point>> planes = new ArrayList<>();
ArrayList<Point> tmp = new ArrayList<>();
for(int i=0;i<N;i++) {
if(i==0)tmp.add(list[i]);
else if (list[i].z==list[i-1].z) {
tmp.add(list[i]);
} else {
Point p = solve(tmp);
if(p!=null)zord.add(p);
tmp.clear();
tmp.add(list[i]);
}
}
Point lll = solve(tmp);
if(lll!=null)zord.add(lll);
Point prev = null;
for(Point p : zord) {
if(prev==null) {
prev=p;
continue;
}else {
sol.append(prev.idx+" "+p.idx+"\n");
prev=null;
}
}
System.out.println(sol);
}
private static Point solve(ArrayList<Point> list) {
ArrayList<Point> tmp = new ArrayList<>();
// System.out.println(list);
Point prev = null;
for(Point p : list) {
if(prev==null) {
prev=p;
continue;
}
if(prev.x==p.x) {
sol.append(prev.idx+" "+p.idx+"\n");
prev = null;
}else {
tmp.add(prev);
prev = p;
}
}
if(prev!=null)tmp.add(prev);
prev= null;
for(Point p : tmp) {
if(prev==null) {
prev= p;
continue;
}else {
sol.append(prev.idx+" "+p.idx+"\n");
prev = null;
}
}
return prev;
}
private static class Point {
int x, y, z, idx;
public Point(int x, int y, int z, int idx) {
this.x=x;
this.y=y;
this.z=z;
this.idx=idx;
}
public String toString() {
return "("+x+","+y+","+z+")";
}
}
private static class FS {
BufferedReader br;
StringTokenizer st;
public FS(InputStream in) {
br = new BufferedReader(new InputStreamReader(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());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
bd6a340d3e712819bdba386a8b8be475
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Jeel Vaishnav
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
C2BalancedRemovalsHarder solver = new C2BalancedRemovalsHarder();
solver.solve(1, in, out);
out.close();
}
static class C2BalancedRemovalsHarder {
public void solve(int testNumber, InputReader sc, PrintWriter out) {
int n = sc.nextInt();
Pair p[] = new Pair[n];
for (int i = 0; i < n; ++i) {
p[i] = new Pair(sc.nextInt(), sc.nextInt(), sc.nextInt(), i + 1);
}
Arrays.sort(p, new Comparator<Pair>() {
public int compare(Pair o1, Pair o2) {
if (o1.x < o2.x)
return -1;
if (o1.x > o2.x)
return 1;
if (o1.y < o2.y)
return -1;
if (o1.y > o2.y)
return 1;
if (o1.z < o2.z)
return -1;
if (o1.z > o2.z)
return 1;
return 0;
}
});
ArrayList<Integer> list = new ArrayList<>();
Pair lastP = null;
int sel[] = new int[n + 1];
for (int i = 0; i < n; ++i) {
if (lastP == null)
lastP = p[i];
else {
if (lastP.x == p[i].x && lastP.y == p[i].y) {
list.add(lastP.ind);
list.add(p[i].ind);
sel[lastP.ind] = 1;
sel[p[i].ind] = 1;
lastP = null;
} else
lastP = p[i];
}
}
lastP = null;
for (int i = 0; i < n; ++i) {
if (sel[p[i].ind] == 0) {
if (lastP == null)
lastP = p[i];
else {
if (lastP.x == p[i].x) {
list.add(lastP.ind);
list.add(p[i].ind);
sel[lastP.ind] = 1;
sel[p[i].ind] = 1;
lastP = null;
} else
lastP = p[i];
}
}
}
lastP = null;
for (int i = 0; i < n; ++i) {
if (sel[p[i].ind] == 0) {
if (lastP == null)
lastP = p[i];
else {
list.add(lastP.ind);
list.add(p[i].ind);
sel[lastP.ind] = 1;
sel[p[i].ind] = 1;
lastP = null;
}
}
}
for (int i = 0; i < n; i += 2)
out.println(list.get(i) + " " + list.get(i + 1));
}
class Pair {
int x;
int y;
int z;
int ind;
Pair(int a, int b, int c, int d) {
x = a;
y = b;
z = c;
ind = d;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
d684e2d208d8015e384b1f1474036f6f
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class c {
static int oo = 1_000_000_000;
public static void main(String[] args) {
FS in = new FS(System.in);
PrintWriter out = new PrintWriter(System.out);
TreeSet<Integer> xComp = new TreeSet<Integer>();
TreeSet<Integer> yComp = new TreeSet<Integer>();
int n = in.nextInt();
int[] x = new int[n];
int[] y = new int[n];
int[] z = new int[n];
for(int i=0; i<n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
z[i] = in.nextInt();
xComp.add(x[i]);
yComp.add(y[i]);
}
// HashMap<Integer, Integer> xComp2 = new HashMap<>();
// Iterator iter = xComp.iterator();
// int xInd = 0;
// while(iter.hasNext()) {
// int num = (int)iter.next();
// xComp2.put(num, xInd);
// xInd++;
// }
// HashMap<Integer, Integer> yComp2 = new HashMap<>();
// iter = yComp.iterator();
// int yInd = 0;
// while(iter.hasNext()) {
// int num = (int)iter.next();
// yComp2.put(num, yInd);
// yInd++;
// }
// PriorityQueue<Point> points = new PriorityQueue<Point>();
// for(int i=0; i<n; i++) {
// x[i] = xComp2.get(x[i]);
// y[i] = yComp2.get(y[i]);
// points.add(new Point(x[i], y[i], i));
// }
PriorityQueue<Point> points = new PriorityQueue<>();
for(int i=0; i<n; i++) {
points.add(new Point(x[i], y[i], z[i], i+1));
}
PriorityQueue<Point> skippedX = new PriorityQueue<Point>();
PriorityQueue<Point> skippedY = new PriorityQueue<Point>();
// X sweep
// Gotta go plane by plane. *sigh*
while(!points.isEmpty()) {
// grab a plane
PriorityQueue<Point> curPlane = new PriorityQueue<>();
int curX = points.peek().x;
while(!points.isEmpty()) {
Point a = points.poll();
if(a.x == curX) {
curPlane.add(a);
} else {
points.add(a);
break;
}
}
// Do the sweep on a plane
// Y Sweep
PriorityQueue<Point> skippedCP = new PriorityQueue<>();
while(!curPlane.isEmpty()) {
Point a = curPlane.poll();
if(curPlane.isEmpty()) {
skippedCP.add(a);
break;
}
Point b = curPlane.poll();
if(a.y != b.y) {
curPlane.add(b);
skippedCP.add(a);
} else {
out.println(a.id + " " + b.id);
}
}
// Z leftovers
// we assume they're all on same x plane.
while(!skippedCP.isEmpty()) {
Point a = skippedCP.poll();
if(skippedCP.isEmpty()) {
// Put the leftover into SkippedY
skippedY.add(a);
break;
}
Point b = skippedCP.poll();
out.println(a.id + " " + b.id);
}
}
// y sweep
// PriorityQueue<Point> skippedY = new PriorityQueue<>();
// while(!skippedX.isEmpty()) {
// Point a = skippedX.poll();
// if(skippedX.isEmpty()) {
// skippedY.add(a);
// break;
// }
// Point b = skippedX.poll();
// if(a.x != b.x) {
// skippedX.add(b);
// skippedY.add(a);
// } else {
// System.out.println(a.id + " " + b.id);
// }
// }
while(!skippedY.isEmpty()) {
Point a = skippedY.poll();
Point b = skippedY.poll();
out.println(a.id + " " + b.id);
}
// HashMap<Integer, ArrayList<Point>> xCordPoints = new HashMap<>();
// iter = xComp.iterator();
// while(iter.hasNext()) {
// }
// boolean goingUp = true;
out.flush();
}
static class Point implements Comparable<Point>{
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;
}
public int compareTo(Point o) {
if(x != o.x) return Integer.compare(x, o.x);
if(y != o.y) return Integer.compare(y, o.y);
return Integer.compare(z, o.z);
}
}
static class FS {
BufferedReader in;
StringTokenizer token;
public FS(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
if(token == null || !token.hasMoreElements()) {
try {
token = new StringTokenizer(in.readLine());
} catch (Exception e) {}
return next();
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
/*
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
3 6
5 1
2 4
Me:
6 2
4 5
3 1
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
4 5
1 6
2 7
3 8
mine
2 3
5 8
1 4
7 6
*/
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
7cca45e301ef0ed7f7fd36b2bb2461ae
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.AbstractMap;
import java.util.TreeMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sparsh Sanchorawala
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
C2BalancedRemovalsHarder solver = new C2BalancedRemovalsHarder();
solver.solve(1, in, out);
out.close();
}
static class C2BalancedRemovalsHarder {
public void solve(int testNumber, InputReader s, PrintWriter w) {
int n = s.nextInt();
TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> tm = new TreeMap<>();
for (int i = 0; i < n; i++) {
int x = s.nextInt(), y = s.nextInt(), z = s.nextInt();
if (tm.get(x) == null)
tm.put(x, new TreeMap<>());
if (tm.get(x).get(y) == null)
tm.get(x).put(y, new TreeMap<>());
tm.get(x).get(y).put(z, i);
}
while (!tm.isEmpty()) {
int x = tm.firstKey();
int y = tm.get(x).firstKey();
int z = tm.get(x).get(y).firstKey();
int i = tm.get(x).get(y).get(z);
tm.get(x).get(y).remove(z);
int j = -1;
if (tm.get(x).get(y).size() > 0) {
int z2 = tm.get(x).get(y).firstKey();
j = tm.get(x).get(y).get(z2);
tm.get(x).get(y).remove(z2);
if (tm.get(x).get(y).size() == 0)
tm.get(x).remove(y);
if (tm.get(x).size() == 0)
tm.remove(x);
w.println((i + 1) + " " + (j + 1));
continue;
}
tm.get(x).remove(y);
if (tm.get(x).size() > 0) {
int y2 = 0;
if (tm.get(x).ceilingEntry(y) != null) {
y2 = tm.get(x).ceilingKey(y);
} else {
y2 = tm.get(x).lowerKey(y);
}
int z2 = 0;
if (tm.get(x).get(y2).ceilingEntry(z) != null) {
z2 = tm.get(x).get(y2).ceilingKey(z);
} else {
z2 = tm.get(x).get(y2).lowerKey(z);
}
j = tm.get(x).get(y2).get(z2);
tm.get(x).get(y2).remove(z2);
if (tm.get(x).get(y2).size() == 0)
tm.get(x).remove(y2);
if (tm.get(x).size() == 0)
tm.remove(x);
w.println((i + 1) + " " + (j + 1));
continue;
}
tm.remove(x);
int x2 = 0;
if (tm.ceilingEntry(x) != null) {
x2 = tm.ceilingKey(x);
} else {
x2 = tm.lowerKey(x);
}
int y2 = 0;
if (tm.get(x2).ceilingEntry(y) != null) {
y2 = tm.get(x2).ceilingKey(y);
} else {
y2 = tm.get(x2).lowerKey(y);
}
int z2 = 0;
if (tm.get(x2).get(y2).ceilingEntry(z) != null) {
z2 = tm.get(x2).get(y2).ceilingKey(z);
} else {
z2 = tm.get(x2).get(y2).lowerKey(z);
}
j = tm.get(x2).get(y2).get(z2);
tm.get(x2).get(y2).remove(z2);
if (tm.get(x2).get(y2).size() == 0)
tm.get(x2).remove(y2);
if (tm.get(x2).size() == 0)
tm.remove(x2);
w.println((i + 1) + " " + (j + 1));
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
39a9353626e65b4b36478e20010c65c3
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class TestGenerator {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter wr = new PrintWriter(System.out);
int n = sc.nextInt();
int[][] p = new int[n][4];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 3; ++j) {
p[i][j] = sc.nextInt();
}
p[i][3] = i + 1;
}
Arrays.sort(p, (o1, o2) -> {
for (int i = 0; i < o1.length; ++i) if (o1[i] != o2[i]) return o1[i] - o2[i];
return 0;
});
boolean[] alive = new boolean[n];
Arrays.fill(alive, true);
for (int i = 0; i < n; ++i)
if (alive[i]) {
int j = i + 1;
while (!alive[j]) ++j;
int diff = 0;
while (p[i][diff] == p[j][diff]) ++diff;
int k = j;
while (k < n && p[k][diff] == p[j][diff]) ++k;
int bestScore = Integer.MAX_VALUE;
int bt = -1;
for (int t = j; t < k; ++t)
if (alive[t]) {
int score = 0;
for (int u = 0; u < 3; ++u) score += Math.abs(p[i][u] - p[t][u]);
if (score < bestScore) {
bestScore = score;
bt = t;
}
}
alive[i] = false;
alive[bt] = false;
wr.println(p[i][3] + " " + p[bt][3]);
}
wr.close();
}
public static int[] RandomizeArray(int[] array){
Random rgen = new Random(); // Random number generator
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
}
class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
d8af17ded0e53ecd38e8120df84f4e88
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class q5 {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=Reader.nextInt();
TreeMap<Integer,NoD> xmap=new TreeMap<Integer,NoD>();
TreeMap<Integer,TreeSet<Integer>> count=new TreeMap<Integer,TreeSet<Integer>>();
TreeMap<Integer,Integer> c2=new TreeMap<Integer,Integer>();
for(int i=0;i<n;i++) {
int x=Reader.nextInt(),y=Reader.nextInt(),z=Reader.nextInt();
if(!c2.containsKey(x)) c2.put(x, 0);
int v=c2.get(x);
if(v!=0) {
count.get(v).remove(x);
if(count.get(v).size()==0) count.remove(v);
}
v++;
c2.put(x, v);
if(!count.containsKey(v)) count.put(v, new TreeSet<Integer>());
count.get(v).add(x);
if(!xmap.containsKey(x)) {
xmap.put(x,new NoD());
}
xmap.get(x).insert(y, z, i+1);
}
for(int i=0;i<n/2;i++) {
int a=count.lastKey();
if(a==1) {
int x1=count.get(a).pollFirst();
int x2=count.get(a).pollFirst();
int i1=xmap.get(x1).get2(),i2=xmap.get(x2).get2();
out.println(i1+" "+i2);
}
else {
int x=count.get(a).pollFirst();
if(count.get(a).size()==0) count.remove(a);
N3 ans=xmap.get(x).get();
out.println(ans.i1+" "+ans.i2);
a-=2;
if(a>0) {
if(!count.containsKey(a)) count.put(a, new TreeSet<Integer>());
count.get(a).add(x);
}
}
}
out.flush();
}
}
class N3{
int i1, i2;
N3(int ii1, int ii2){
i1=ii1;i2=ii2;
}
}
class NoD{
TreeMap<Integer,N2> tm;
TreeMap<Integer,TreeSet<Integer>> count;
TreeMap<Integer,Integer> c2;
NoD(){
tm=new TreeMap<Integer,N2>();
count=new TreeMap<Integer,TreeSet<Integer>>();
c2=new TreeMap<Integer,Integer>();
}
void insert(int y, int z, int ind) {
if(!c2.containsKey(y)) {
c2.put(y, 0);
}
int v=c2.get(y);
if(v!=0) {
count.get(v).remove(y);
if(count.get(v).size()==0) count.remove(v);
}
v++;
c2.put(y, v);
if(!count.containsKey(v)) count.put(v, new TreeSet<Integer>());
count.get(v).add(y);
if(!tm.containsKey(y)) tm.put(y, new N2());
tm.get(y).insert(z, ind);
}
N3 get() {
int a=count.lastKey();
if(a==1) {
int y1=count.get(a).pollFirst();
int y2=count.get(a).pollFirst();
int i1=tm.get(y1).get(),i2=tm.get(y2).get();
return new N3(i1,i2);
}
else {
int y=count.get(a).pollFirst();
if(count.get(a).size()==0) count.remove(a);
int i1=tm.get(y).get();
int i2=tm.get(y).get();
a-=2;
if(a>0) {
if(!count.containsKey(a))count.put(a, new TreeSet<Integer>());
count.get(a).add(y);
}
return new N3(i1,i2);
}
}
int get2() {
int y=count.get(1).pollFirst();
int a=tm.get(y).get();
tm.remove(y);
return a;
}
}
class N2
{
TreeMap<Integer,Integer> tm;
N2(){
tm=new TreeMap<Integer,Integer>();
}
void insert(int z, int y) {
tm.put(z, y);
}
int get() {
int z=tm.lastKey();
int c= tm.get(z);
tm.remove(z);
return c;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init() throws IOException {
reader = new BufferedReader(
new FileReader("input.txt"));
tokenizer = new StringTokenizer("");
}
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String nextLine() throws IOException{
return reader.readLine();
}
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 long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
7fdcf3ec5709399b8c77bbbc07bf53e2
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class C implements Runnable {
public static void main (String[] args) {new Thread(null, new C(), "_cf", 1 << 28).start();}
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
System.err.println("");
int n = fs.nextInt();
Point[] a = new Point[n];
for(int i = 0; i < n; i++) a[i] = new Point(fs.nextInt(), fs.nextInt(), fs.nextInt(), i);
Arrays.sort(a);
boolean[] removed = new boolean[n];
int[][] res = new int[n/2][2];
int ptr = 0;
for(int i = 0; i < n; i++) {
int j = i;
while(j < n && a[i].z == a[j].z) j++;
for(int k = i; k < j-1; k++) {
if(a[k].x == a[k+1].x) {
removed[a[k].id] = removed[a[k+1].id] = true;
res[ptr][0] = a[k].id+1; res[ptr][1] = a[k+1].id+1;
ptr++;
k++;
}
}
for(int k = i; k < j; k++) if(!removed[a[k].id]) {
int l = k+1;
while(l < j && removed[a[l].id]) l++;
if(l == j) break;
removed[a[k].id] = removed[a[l].id] = true;
res[ptr][0] = a[k].id+1; res[ptr][1] = a[l].id+1;
ptr++;
k = l;
}
i = --j;
}
for(int i = 0; i < n; i++) if(!removed[a[i].id]) {
int j = i+1;
while(j < n && removed[a[j].id]) j++;
removed[a[i].id] = removed[a[j].id] = true;
res[ptr][0] = a[i].id+1; res[ptr][1] = a[j].id+1;
ptr++;
i = j;
}
for(int i = 0; i < n/2; i++) {
out.println(res[i][0] + " " + res[i][1]);
}
out.close();
}
class Point implements Comparable<Point> {
int x, y, z, id;
Point(int a, int b, int c, int i) {
x = a; y = b; z = c;
id = i;
}
public int compareTo(Point p) {
int c = Integer.compare(z, p.z);
if(c == 0) c = Integer.compare(x, p.x);
if(c == 0) c = Integer.compare(y, p.y);
return c;
}
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
27d3fbecda44cca66e159213d078d518
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static final long MOD = 998244353;
public static void main(String[] args) throws IOException {
FastScanner sc = new FastScanner();
int N = sc.nextInt();
int[][] points = new int[N][4];
for (int i = 0; i < N; i++) {
points[i][0] = i;
for (int j = 1; j <= 3; j++)
points[i][j] = sc.nextInt();
}
int[][] sorted = sort(points);
TreeMap<Integer,TreeMap<Integer,TreeSet<Integer>>> tmNext = new TreeMap<Integer,TreeMap<Integer,TreeSet<Integer>>>();
for (int[] p: points) {
if (tmNext.containsKey(p[1])) {
TreeMap<Integer,TreeSet<Integer>> tm = tmNext.get(p[1]);
if (tm.containsKey(p[2])) {
tmNext.get(p[1]).get(p[2]).add(p[3]);
} else {
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(p[3]);
tm.put(p[2],ts);
tmNext.put(p[1],tm);
}
} else {
TreeMap<Integer,TreeSet<Integer>> tm = new TreeMap<Integer,TreeSet<Integer>>();
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(p[3]);
tm.put(p[2],ts);
tmNext.put(p[1],tm);
}
}
boolean[] vis = new boolean[N];
int[][] ans = new int[N/2][2];
int index = 0;
HashMap<Point,Integer> hm = new HashMap<Point,Integer>();
for (int i = 0; i < N; i++) {
Point p = new Point(sorted[i][1],sorted[i][2],sorted[i][3]);
hm.put(p,sorted[i][0]);
}
//System.out.println(Arrays.deepToString(sorted));
for (int i = 0; i < N; i++) {
if (vis[sorted[i][0]]) continue;
//Remove point
if (tmNext.get(sorted[i][1]).get(sorted[i][2]).size() == 1) {
if (tmNext.get(sorted[i][1]).size() == 1)
tmNext.remove(sorted[i][1]);
else
tmNext.get(sorted[i][1]).remove(sorted[i][2]);
} else {
tmNext.get(sorted[i][1]).get(sorted[i][2]).remove(sorted[i][3]);
}
TreeMap<Integer,TreeSet<Integer>> tm = new TreeMap<Integer,TreeSet<Integer>>();
Integer fx = tmNext.floorKey(sorted[i][1]);
Integer cx = tmNext.ceilingKey(sorted[i][1]);
int x = 0;
if (fx != null) {
tm = tmNext.get(fx);
x = fx;
} else {
tm = tmNext.get(cx);
x = cx;
}
TreeSet<Integer> ts = new TreeSet<Integer>();
Integer fy = tm.floorKey(sorted[i][2]);
Integer cy = tm.ceilingKey(sorted[i][2]);
int y = 0;
if (fy != null) {
ts = tm.get(fy);
y = fy;
} else {
ts = tm.get(cy);
y = cy;
}
int z = 0;
Integer fz = ts.floor(sorted[i][3]);
Integer cz = ts.ceiling(sorted[i][3]);
if (fz != null)
z = fz;
else
z = cz;
//Remove used point
if (tmNext.get(x).get(y).size() == 1) {
if (tmNext.get(x).size() == 1)
tmNext.remove(x);
else
tmNext.get(x).remove(y);
} else {
tmNext.get(x).get(y).remove(z);
}
Point p = new Point(x,y,z);
int match = hm.get(p);
vis[sorted[i][0]] = true;
vis[match] = true;
ans[index] = new int[]{sorted[i][0]+1,match+1};
index++;
}
StringBuilder print = new StringBuilder();
for (int i = 0; i < N/2; i++)
print.append(ans[i][0] + " " + ans[i][1] + "\n");
System.out.print(print);
}
static class Point {
public int x;
public int y;
public int z;
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public int hashCode() {
long hc = (961 * (long)(x)) + (31 * (long)y) + z;
hc += 10000*MOD;
hc %= MOD;
return (int)hc;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x || y != other.y || z != other.z)
return false;
return true;
}
}
public static int[][] sort(int[][] array) {
//Sort an array (immune to quicksort TLE)
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int[] temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] arr1, int[] arr2) {
if (arr1[1] != arr2[1])
return arr1[1]-arr2[1];
else if (arr1[2] != arr2[2])
return arr1[2]-arr2[2];
else
return arr1[3]-arr2[3];
}
});
return array;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
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 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
1551d7142add95297f2f440e3abbcdeb
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author lewin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
C1BalancedRemovalsEasier solver = new C1BalancedRemovalsEasier();
solver.solve(1, in, out);
out.close();
}
static class C1BalancedRemovalsEasier {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
ArrayList<C1BalancedRemovalsEasier.Point> ps = new ArrayList<>();
for (int i = 0; i < n; i++) {
ps.add(new C1BalancedRemovalsEasier.Point(in.nextInt(), in.nextInt(), in.nextInt(), i + 1));
}
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
int r = 3 - i - j;
ps.sort(Comparator.comparingInt(x -> x.cs[r]));
HashMap<Pair<Integer, Integer>, C1BalancedRemovalsEasier.Point> mp = new HashMap<>();
for (C1BalancedRemovalsEasier.Point x : ps) {
Pair<Integer, Integer> k = new Pair<>(x.cs[i], x.cs[j]);
if (mp.containsKey(k)) {
C1BalancedRemovalsEasier.Point v = mp.remove(k);
out.println(x.idx + " " + v.idx);
} else {
mp.put(k, x);
}
}
ps = new ArrayList<>(mp.values());
}
}
for (int i = 0; i < 3; i++) {
if (i == 0) {
final int i1 = 1, i2 = 2;
ps.sort((a, b) -> (a.cs[i1] == b.cs[i1] ? a.cs[i2] - b.cs[i2] : a.cs[i1] - b.cs[i1]));
} else if (i == 1) {
final int i1 = 0, i2 = 2;
ps.sort((a, b) -> (a.cs[i1] == b.cs[i1] ? a.cs[i2] - b.cs[i2] : a.cs[i1] - b.cs[i1]));
} else {
final int i1 = 0, i2 = 1;
ps.sort((a, b) -> (a.cs[i1] == b.cs[i1] ? a.cs[i2] - b.cs[i2] : a.cs[i1] - b.cs[i1]));
}
HashMap<Integer, C1BalancedRemovalsEasier.Point> mp = new HashMap<>();
for (C1BalancedRemovalsEasier.Point x : ps) {
if (mp.containsKey(x.cs[i])) {
C1BalancedRemovalsEasier.Point v = mp.remove(x.cs[i]);
out.println(x.idx + " " + v.idx);
} else {
mp.put(x.cs[i], x);
}
}
ps = new ArrayList<>(mp.values());
}
ps.sort((a, b) -> (a.cs[0] == b.cs[0] ? (a.cs[1] == b.cs[1] ? a.cs[2] - a.cs[2] : a.cs[1] - b.cs[1]) : a.cs[0] - b.cs[0]));
for (int i = 0; i < ps.size(); i += 2) {
out.println(ps.get(i).idx + " " + ps.get(i + 1).idx);
}
}
static class Point {
public int idx;
public int[] cs;
public Point(int x, int y, int z, int idx) {
cs = new int[]{x, y, z};
this.idx = idx;
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
;
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public final U u;
public final V v;
public Pair(U u, V v) {
this.u = u;
this.v = v;
}
public int hashCode() {
return (u == null ? 0 : u.hashCode() * 31) + (v == null ? 0 : v.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 (u == null ? p.u == null : u.equals(p.u)) && (v == null ? p.v == null : v.equals(p.v));
}
public int compareTo(Pair<U, V> b) {
int cmpU = u.compareTo(b.u);
return cmpU != 0 ? cmpU : v.compareTo(b.v);
}
public String toString() {
return String.format("[Pair = (%s, %s)", u.toString(), v.toString());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
73c5c29919f5d0efc59d2d5f5df98ee1
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Rustam Musin (t.me/musin_acm)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
C1SbalansirovannieUdaleniyaPoproshe solver = new C1SbalansirovannieUdaleniyaPoproshe();
solver.solve(1, in, out);
out.close();
}
static class C1SbalansirovannieUdaleniyaPoproshe {
C1SbalansirovannieUdaleniyaPoproshe.Point[] a;
List<IntIntPair> answer;
void save(int i, int j) {
answer.add(IntIntPair.makePair(i, j));
}
List<Integer> dfs(int l, int r, int t) {
List<Integer> results = new ArrayList<>();
if (t == 3) {
for (int i = l; i <= r; i++) results.add(i);
return results;
}
for (int i = l; i <= r; i++) {
int j = i;
while (j + 1 <= r && a[j + 1].a[t] == a[i].a[t]) j++;
results.addAll(dfs(i, j, t + 1));
i = j;
}
while (results.size() > 1) {
int x = results.get(results.size() - 2);
int y = results.get(results.size() - 1);
save(x, y);
results.remove(results.size() - 1);
results.remove(results.size() - 1);
}
return results;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
a = new C1SbalansirovannieUdaleniyaPoproshe.Point[n];
for (int i = 0; i < n; i++)
a[i] = new C1SbalansirovannieUdaleniyaPoproshe.Point(i, in.readInt(), in.readInt(), in.readInt());
Arrays.sort(a);
answer = new ArrayList<>();
dfs(0, n - 1, 0);
for (IntIntPair p : answer) out.printLine(a[p.first].idx + 1, a[p.second].idx + 1);
}
static class Point implements Comparable<C1SbalansirovannieUdaleniyaPoproshe.Point> {
int[] a;
int idx;
public Point(int i, int... a) {
idx = i;
this.a = a;
}
public int compareTo(C1SbalansirovannieUdaleniyaPoproshe.Point o) {
for (int i = 0; i < a.length; i++) if (a[i] != o.a[i]) return Integer.compare(a[i], o.a[i]);
return 0;
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public static IntIntPair makePair(int first, int second) {
return new IntIntPair(first, second);
}
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if (value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
5547fadcff0fdc6d83fc71be23f92c67
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Queue;
import java.util.ArrayDeque;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Rustam Musin (t.me/musin_acm)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
C1SbalansirovannieUdaleniyaPoproshe solver = new C1SbalansirovannieUdaleniyaPoproshe();
solver.solve(1, in, out);
out.close();
}
static class C1SbalansirovannieUdaleniyaPoproshe {
C1SbalansirovannieUdaleniyaPoproshe.Point[] a;
List<IntIntPair> answer;
void save(int i, int j) {
answer.add(IntIntPair.makePair(i, j));
}
Queue<Integer> dfs(int l, int r, int t) {
Queue<Integer> results = new ArrayDeque<>();
if (t == 3) {
for (int i = l; i <= r; i++) results.add(i);
return results;
}
for (int i = l; i <= r; i++) {
int j = i;
while (j + 1 <= r && a[j + 1].a[t] == a[i].a[t]) j++;
results.addAll(dfs(i, j, t + 1));
i = j;
}
while (results.size() > 1) save(results.poll(), results.poll());
return results;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
a = new C1SbalansirovannieUdaleniyaPoproshe.Point[n];
for (int i = 0; i < n; i++)
a[i] = new C1SbalansirovannieUdaleniyaPoproshe.Point(i, in.readInt(), in.readInt(), in.readInt());
Arrays.sort(a);
answer = new ArrayList<>();
dfs(0, n - 1, 0);
for (IntIntPair p : answer) out.printLine(a[p.first].idx + 1, a[p.second].idx + 1);
}
static class Point implements Comparable<C1SbalansirovannieUdaleniyaPoproshe.Point> {
int[] a;
int idx;
public Point(int i, int... a) {
idx = i;
this.a = a;
}
public int compareTo(C1SbalansirovannieUdaleniyaPoproshe.Point o) {
for (int i = 0; i < a.length; i++) if (a[i] != o.a[i]) return Integer.compare(a[i], o.a[i]);
return 0;
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public static IntIntPair makePair(int first, int second) {
return new IntIntPair(first, second);
}
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if (value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
8d67014904150837e3a991e5e0670d39
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int MOD = 1000000007;
// 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, after MOD
void solve() throws IOException {
int n = ri();
int[][] points = new int[n][4];
for (int i = 0; i < n; i++) {
int[] xyz = ril(3);
System.arraycopy(xyz, 0, points[i], 0, 3);
points[i][3] = i+1;
}
Map<Integer, List<int[]>> planes = new HashMap<>();
for (int[] p : points) {
int z = p[2];
if (!planes.containsKey(z)) planes.put(z, new ArrayList<>());
planes.get(z).add(p);
}
List<int[]> newPoints = new ArrayList<>();
for (int z : planes.keySet()) {
int[] ret = solvePlane(planes.get(z));
if (ret != null) newPoints.add(ret);
}
solveLine(newPoints, 2);
}
int[] solvePlane(List<int[]> points) {
Map<Integer, List<int[]>> lines = new HashMap<>();
for (int[] p : points) {
int y = p[1];
if (!lines.containsKey(y)) lines.put(y, new ArrayList<>());
lines.get(y).add(p);
}
List<int[]> newPoints = new ArrayList<>();
for (int y : lines.keySet()) {
int[] ret = solveLine(lines.get(y), 0);
if (ret != null) newPoints.add(ret);
}
return solveLine(newPoints, 1);
}
int[] solveLine(List<int[]> points, int axis) {
Collections.sort(points, (p1, p2) -> Integer.compare(p1[axis], p2[axis]));
for (int i = points.size()-2; i >= 0; i -= 2) {
int i1 = points.get(i+1)[3];
int i2 = points.get(i)[3];
points.remove(i+1);
points.remove(i);
pw.println(i1 + " " + i2);
}
return points.isEmpty() ? null : points.get(0);
}
// Template code below
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solve();
m.close();
}
void close() throws IOException {
pw.flush();
pw.close();
br.close();
}
int ri() throws IOException {
return Integer.parseInt(br.readLine());
}
long rl() throws IOException {
return Long.parseLong(br.readLine());
}
int[] ril(int n) throws IOException {
int[] nums = new int[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
long[] rll(int n) throws IOException {
long[] nums = new long[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
long x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
char[] rs() throws IOException {
return br.readLine().toCharArray();
}
void sort(int[] A) {
Random r = new Random();
for (int i = A.length-1; i > 0; i--) {
int j = r.nextInt(i+1);
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
Arrays.sort(A);
}
int max(int a, int b) {
return a >= b ? a : b;
}
int min(int a, int b) {
return a <= b ? a : b;
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
dc9c2a4273897194ef529c748d1442a4
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.Random;
import java.util.Comparator;
import java.util.ArrayList;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
C1BalancedRemovalsEasier solver = new C1BalancedRemovalsEasier();
solver.solve(1, in, out);
out.close();
}
static class C1BalancedRemovalsEasier {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int[][] ar = new int[n][4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) ar[i][j] = in.scanInt();
ar[i][3] = i + 1;
}
CodeHash.shuffle(ar);
Arrays.sort(ar, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
if (o1[1] == o2[1]) {
return o1[2] - o2[2];
} else return o1[1] - o2[1];
} else return o1[0] - o2[0];
}
});
ArrayList<int[]> ans = new ArrayList<>();
boolean[] done = new boolean[n];
for (int i = 1; i < n; i++) {
boolean check = (!done[i - 1]);
for (int j = 0; j < 2; j++) {
check &= (ar[i][j] == ar[i - 1][j]);
}
if (check) {
done[i - 1] = true;
done[i] = true;
ans.add(new int[]{ar[i - 1][3], ar[i][3]});
}
}
ArrayList<int[]> remaining1 = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (!done[i]) remaining1.add(ar[i]);
}
boolean[] done1 = new boolean[remaining1.size()];
for (int i = 1; i < remaining1.size(); i++) {
boolean check = (!done1[i - 1]);
for (int j = 0; j < 1; j++) {
check &= (remaining1.get(i)[j] == remaining1.get(i - 1)[j]);
}
if (check) {
done1[i - 1] = true;
done1[i] = true;
ans.add(new int[]{remaining1.get(i - 1)[3], remaining1.get(i)[3]});
}
}
ArrayList<int[]> remaining2 = new ArrayList<>();
for (int i = 0; i < remaining1.size(); i++) {
if (!done1[i]) remaining2.add(remaining1.get(i));
}
for (int i = 0; i < remaining2.size(); i += 2) {
ans.add(new int[]{remaining2.get(i)[3], remaining2.get(i + 1)[3]});
}
for (int[] p : ans) out.println(p[0] + " " + p[1]);
}
}
static class CodeHash {
public static void shuffle(int[][] ar) {
Random rd = new Random(new Random().nextInt());
for (int i = 0; i < ar.length; i++) {
int index = rd.nextInt(ar.length);
int[] temp = ar[i];
ar[i] = ar[index];
ar[index] = temp;
}
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
64771866e2d47b5ab7dbbd7ad82dd973
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
void solve() throws IOException {
int n=ni();
int[][]A=new int[n][4];
for (int x=1;x<=n;x++) {
A[x-1][0]=ni();
A[x-1][1]=ni();
A[x-1][2]=ni();
A[x-1][3]=x;
}
Arrays.sort(A, (a,b)-> {
if (a[0]==b[0] && a[1]==b[1]) return a[2]-b[2];
else {
if (a[0]==b[0]) return a[1]-b[1];
else return a[0]-b[0];
}
});
int sv=0;
boolean sf=false;
int p=0;
int q=0;
int ct=0;
while (p<n) {
ct=0;
q=p;
while (q<n && A[q][0]==A[p][0]) { ct++; q++; }
if (ct==1) {
if (sf) {
out.println(sv+" "+A[p][3]);
sf=false;
p=q;
}
else { sf=true; sv=A[p][3]; p=q; }
continue;
}
if (ct==2) {
out.println(A[p][3]+" "+A[p+1][3]);
p=q;
continue;
}
boolean tf=false;
int tv=0;
int a=p;
int b=p+1;
int c=p+2;
while (ct>2) {
if (A[a][1]==A[b][1] && A[b][1]==A[c][1]) {
out.println(A[a][3]+" "+A[b][3]);
ct-=2;
a=c;
b=a+1;
c=b+1;
}
else {
if (A[b][1]!=A[c][1]) {
out.println(A[a][3]+" "+A[b][3]);
ct-=2;
a=c;
b=a+1;
c=b+1;
}
else {
out.println(A[b][3]+" "+A[c][3]);
ct-=2;
if (tf) { out.println(A[tv][3]+" "+A[a][3]); ct--; tf=false; }
else { tf=true; tv=a; ct--; }
a=c+1;
b=a+1;
c=b+1;
}
}
}
if (ct==2) { out.println(A[a][3]+" "+A[b][3]); }
if (ct==1) {
if (tf) { out.println(A[tv][3]+" "+A[a][3]); tf=false; }
else { tf=true; tv=a; }
}
if (tf) {
if (sf) {
out.println(sv+" "+A[tv][3]);
sf=false;
}
else { sf=true; sv=A[tv][3]; }
}
p=q;
continue;
}
out.flush();
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
242212765208e38629c0bd4e2bae59fe
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
class Reader {
BufferedReader in;
Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
}
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
}
class Writer {
PrintWriter out;
Writer() {
out = new PrintWriter(System.out);
}
StringBuilder str = new StringBuilder();
void print(Object o) {
str.append(o);
}
void close() {
out.write(str.toString());
out.close();
}
}
class pair {
int x, y, z, i;
pair(int x1, int y1, int z1, int i1) {
x = x1;
y = y1;
z = z1;
i = i1;
}
}
Writer out;
ArrayList<pair> a;
int n;
boolean used[];
void printz(int ind) {
int x1 = a.get(ind).x;
int y1 = a.get(ind).y;
pair last = null;
for (int i = ind; i < n && a.get(i).y == y1 && a.get(i).x == x1; i++) {
if (last == null) {
last = a.get(i);
} else {
out.print((a.get(i).i + 1) + " " + (last.i + 1) + "\n");
used[a.get(i).i] = true;
used[last.i] = true;
last = null;
}
}
}
void printy(int ind) {
int x1 = a.get(ind).x;
for (int i = ind; i < n && a.get(i).x == x1; i++) {
printz(i);
int y1 = a.get(i).y;
while (i + 1 < n && a.get(i + 1).x == x1 && a.get(i + 1).y == y1)
i++;
}
pair last = null;
for (int i = ind; i < n && a.get(i).x == x1; i++) {
if (!used[a.get(i).i]) {
if (last == null) {
last = a.get(i);
} else {
out.print((a.get(i).i + 1) + " " + (last.i + 1) + "\n");
used[a.get(i).i] = true;
used[last.i] = true;
last = null;
}
}
}
}
void printx() {
for (int i = 0; i < n; i++) {
printy(i);
int x1 = a.get(i).x;
while (i + 1 < n && a.get(i + 1).x == x1)
i++;
}
pair last = null;
for (int i = 0; i < n; i++) {
if (!used[a.get(i).i]) {
if (last == null) {
last = a.get(i);
} else {
out.print((a.get(i).i + 1) + " " + (last.i + 1) + "\n");
used[a.get(i).i] = true;
used[last.i] = true;
last = null;
}
}
}
}
void slave() throws IOException {
Reader in = new Reader();
out = new Writer();
n = in.nextInt();
a = new ArrayList<>(n);
used = new boolean[n];
for (int i = 0; i < n; i++) {
a.add(new pair(in.nextInt(), in.nextInt(), in.nextInt(), i));
}
Collections.sort(a, new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if (o1.x == o2.x) {
if (o1.y == o2.y) {
return o1.z - o2.z;
} else {
return o1.y - o2.y;
}
} else {
return o1.x - o2.x;
}
}
});
printx();
out.close();
}
public static void main(String[] args) throws IOException {
new Main().slave();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
ba0ec8cce13dd6a12c6f10f222465e98
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
class Reader {
StreamTokenizer tok;
Reader() {
tok = new StreamTokenizer(new BufferedInputStream(System.in));
}
String next() throws IOException {
tok.nextToken();
return tok.sval;
}
int nextInt() throws IOException {
tok.nextToken();
return (int) (tok.nval);
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
}
class Writer {
PrintWriter out;
Writer() {
out = new PrintWriter(System.out);
}
StringBuilder str = new StringBuilder();
void print(Object o) {
str.append(o);
}
void close() {
out.write(str.toString());
out.close();
}
}
class pair {
int x, y, z, i;
pair(int x1, int y1, int z1, int i1) {
x = x1;
y = y1;
z = z1;
i = i1;
}
}
Writer out;
ArrayList<pair> a;
int n;
boolean used[];
void printz(int ind) {
int x1 = a.get(ind).x;
int y1 = a.get(ind).y;
pair last = null;
for (int i = ind; i < n && a.get(i).y == y1 && a.get(i).x == x1; i++) {
if (last == null) {
last = a.get(i);
} else {
out.print((a.get(i).i + 1) + " " + (last.i + 1) + "\n");
used[a.get(i).i] = true;
used[last.i] = true;
last = null;
}
}
}
void printy(int ind) {
int x1 = a.get(ind).x;
for (int i = ind; i < n && a.get(i).x == x1; i++) {
printz(i);
int y1 = a.get(i).y;
while (i + 1 < n && a.get(i + 1).x == x1 && a.get(i + 1).y == y1)
i++;
}
pair last = null;
for (int i = ind; i < n && a.get(i).x == x1; i++) {
if (!used[a.get(i).i]) {
if (last == null) {
last = a.get(i);
} else {
out.print((a.get(i).i + 1) + " " + (last.i + 1) + "\n");
used[a.get(i).i] = true;
used[last.i] = true;
last = null;
}
}
}
}
void printx() {
for (int i = 0; i < n; i++) {
printy(i);
int x1 = a.get(i).x;
while (i + 1 < n && a.get(i + 1).x == x1)
i++;
}
pair last = null;
for (int i = 0; i < n; i++) {
if (!used[a.get(i).i]) {
if (last == null) {
last = a.get(i);
} else {
out.print((a.get(i).i + 1) + " " + (last.i + 1) + "\n");
used[a.get(i).i] = true;
used[last.i] = true;
last = null;
}
}
}
}
void slave() throws IOException {
Reader in = new Reader();
out = new Writer();
n = in.nextInt();
a = new ArrayList<>(n);
used = new boolean[n];
for (int i = 0; i < n; i++) {
a.add(new pair(in.nextInt(), in.nextInt(), in.nextInt(), i));
}
Collections.sort(a, new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if (o1.x == o2.x) {
if (o1.y == o2.y) {
return o1.z - o2.z;
} else {
return o1.y - o2.y;
}
} else {
return o1.x - o2.x;
}
}
});
printx();
out.close();
}
public static void main(String[] args) throws IOException {
new Main().slave();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
2472e6f149725e80022031253af1631d
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Remo {
static FastReader sc = new FastReader();
static OutputStream outputstream = System.out;
static PrintWriter out = new PrintWriter(outputstream);
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 Point {
long x,y,z;
int ind;
Point(long x, long y, long z, int ind) {
this.x=x;
this.y=y;
this.z=z;
this.ind=ind;
}
public String toString() {
return this.x + " " + this.y + " " + this.z + " " + (this.ind+1);
}
}
static class comp implements Comparator<Point> {
public int compare(Point p1, Point p2) {
if(p1.x==p2.x) {
if(p1.y==p2.y) {
if(p1.z>p2.z) return 1;
return -1;
} else if(p1.y>p2.y) {
return 1;
}
return -1;
} else if(p1.x>p2.x) {
return 1;
}
return -1;
}
}
static class Node {
int x, y;
Node(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return this.x + " " + this.y;
}
}
public static void main(String[] args) {
int n = sc.nextInt();
ArrayList<Point> al = new ArrayList();
for(int i = 0; i < n; i++) {
al.add(new Point(sc.nextLong(),sc.nextLong(),sc.nextLong(),i));
}
boolean[] bools = new boolean[n];
Collections.sort(al,new comp());
ArrayList<ArrayList<Point>> subs = new ArrayList();
subs.add(new ArrayList());
subs.get(subs.size()-1).add(al.get(0));
for(int i = 1; i < n; i++) {
long last = subs.get(subs.size()-1).get(subs.get(subs.size()-1).size()-1).x;
long now = al.get(i).x;
if(last==now) {
subs.get(subs.size()-1).add(al.get(i));
} else {
subs.add(new ArrayList());
subs.get(subs.size()-1).add(al.get(i));
}
}
int ind = 0;
ArrayList<Node> res = new ArrayList();
for(int i = 0; i < subs.size(); i++) {
ArrayList<Point> als = subs.get(i);
int lastind = -1;
int firstind = ind;
for(int j = 0; j < als.size(); j++) {
if(j==als.size()-1) {
bools[ind]=true;
lastind = ind;
ind++;
break;
}
if(als.get(j).y==als.get(j+1).y) {
Node tmp = new Node(als.get(j).ind+1,als.get(j+1).ind+1);
res.add(tmp);
j++;
ind++;
} else {
bools[ind] = true;
lastind = ind;
}
ind++;
}
for(int j = firstind; j <= lastind; j++) {
if(bools[j]) {
for(int k = j+1; k <= lastind; k++) {
if(bools[k]) {
Node tmp = new Node(al.get(j).ind+1,al.get(k).ind+1);
res.add(tmp);
bools[j] = false;
bools[k] = false;
j=k;
break;
}
}
}
}
}
for(int i = 0; i < n; i++) {
if(bools[i]) {
for(int j = i+1; j < n; j++) {
if(bools[j]) {
Node tmp = new Node(al.get(i).ind+1,al.get(j).ind+1);
res.add(tmp);
i=j;
break;
}
}
}
}
for(Node nd : res) out.println(nd);
out.close();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
c379cf8b2c7187a6f2b9320349e08a01
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Remo {
static FastReader sc = new FastReader();
static OutputStream outputstream = System.out;
static PrintWriter out = new PrintWriter(outputstream);
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 Point {
long x,y,z;
int ind;
Point(long x, long y, long z, int ind) {
this.x=x;
this.y=y;
this.z=z;
this.ind=ind;
}
public String toString() {
return this.x + " " + this.y + " " + this.z + " " + (this.ind+1);
}
}
static class comp implements Comparator<Point> {
public int compare(Point p1, Point p2) {
if(p1.x==p2.x) {
if(p1.y==p2.y) {
if(p1.z>p2.z) return 1;
return -1;
} else if(p1.y>p2.y) {
return 1;
}
return -1;
} else if(p1.x>p2.x) {
return 1;
}
return -1;
}
}
static class Node {
int x, y;
Node(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return this.x + " " + this.y;
}
}
public static void main(String[] args) {
int n = sc.nextInt();
ArrayList<Point> al = new ArrayList();
for(int i = 0; i < n; i++) {
al.add(new Point(sc.nextLong(),sc.nextLong(),sc.nextLong(),i));
}
boolean[] bools = new boolean[n];
Collections.sort(al,new comp());
ArrayList<ArrayList<Point>> subs = new ArrayList();
subs.add(new ArrayList());
subs.get(subs.size()-1).add(al.get(0));
for(int i = 1; i < n; i++) {
long last = subs.get(subs.size()-1).get(subs.get(subs.size()-1).size()-1).x;
long now = al.get(i).x;
if(last==now) {
subs.get(subs.size()-1).add(al.get(i));
} else {
subs.add(new ArrayList());
subs.get(subs.size()-1).add(al.get(i));
}
}
int ind = 0;
// for(ArrayList<Point> als : subs) {
// for(Point p : als) out.println(p);
// out.println();
// }
ArrayList<Node> res = new ArrayList();
for(int i = 0; i < subs.size(); i++) {
ArrayList<Point> als = subs.get(i);
int lastind = -1;
int firstind = ind;
for(int j = 0; j < als.size(); j++) {
if(j==als.size()-1) {
bools[ind]=true;
lastind = ind;
// out.println(Arrays.toString(bools) + " " + ind);
ind++;
break;
}
if(als.get(j).y==als.get(j+1).y) {
Node tmp = new Node(als.get(j).ind+1,als.get(j+1).ind+1);
res.add(tmp);
// out.println((als.get(j).ind+1) + " " + (als.get(j+1).ind+1));
j++;
ind++;
// out.println(Arrays.toString(bools) + " " + ind);
} else {
bools[ind] = true;
lastind = ind;
// out.println(Arrays.toString(bools) + " " + ind);
}
ind++;
}
// if(i==1) out.println(firstind + " " + lastind);
for(int j = firstind; j <= lastind; j++) {
// out.println(j);
if(bools[j]) {
for(int k = j+1; k <= lastind; k++) {
if(bools[k]) {
// out.println(j + " " + k);
Node tmp = new Node(al.get(j).ind+1,al.get(k).ind+1);
res.add(tmp);
// out.println((al.get(j).ind+1) + " " + (al.get(k).ind+1));
bools[j] = false;
bools[k] = false;
j=k;
break;
}
}
}
}
// out.println(Arrays.toString(bools) + " " + "hi");
}
for(int i = 0; i < n; i++) {
if(bools[i]) {
for(int j = i+1; j < n; j++) {
if(bools[j]) {
Node tmp = new Node(al.get(i).ind+1,al.get(j).ind+1);
res.add(tmp);
// out.println((al.get(i).ind+1) + " " + (al.get(j).ind+1));
i=j;
break;
}
}
}
}
// for(boolean b : bools) out.print(b + " ");
if(res.size()!=n/2) out.println(res.size());
else
for(Node nd : res) out.println(nd);
out.close();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
ba640040dc2c54e498eacd8472b468d4
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Remo3D {
static FastReader sc = new FastReader();
static OutputStream outputstream = System.out;
static PrintWriter out = new PrintWriter(outputstream);
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 Point {
long x,y,z;
int ind;
Point(long x, long y, long z, int ind) {
this.x=x;
this.y=y;
this.z=z;
this.ind=ind;
}
public String toString() {
return this.x + " " + this.y + " " + this.z + " " + (this.ind+1);
}
}
static class comp implements Comparator<Point> {
public int compare(Point p1, Point p2) {
if(p1.x==p2.x) {
if(p1.y==p2.y) {
if(p1.z>p2.z) return 1;
return -1;
} else if(p1.y>p2.y) {
return 1;
}
return -1;
} else if(p1.x>p2.x) {
return 1;
}
return -1;
}
}
static class Node {
int x, y;
Node(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return this.x + " " + this.y;
}
}
public static void main(String[] args) {
int n = sc.nextInt();
ArrayList<Point> al = new ArrayList();
for(int i = 0; i < n; i++) {
al.add(new Point(sc.nextLong(),sc.nextLong(),sc.nextLong(),i));
}
Collections.sort(al,new comp());
// for(Point p : al) out.println(p);
ArrayList<ArrayList<ArrayList<Integer>>> first = new ArrayList();
first.add(new ArrayList());
ArrayList<ArrayList<Integer>> second = first.get(0);
second.add(new ArrayList());
ArrayList<Integer> third = second.get(0);
third.add(al.get(0).ind);
for(int i = 1; i < n; i++) {
if(al.get(i-1).x==al.get(i).x) {
if(al.get(i).y==al.get(i-1).y) {
third.add(al.get(i).ind);
} else {
second.add(new ArrayList());
third = second.get(second.size()-1);
third.add(al.get(i).ind);
}
} else {
first.add(new ArrayList());
second = first.get(first.size()-1);
second.add(new ArrayList());
third = second.get(second.size()-1);
third.add(al.get(i).ind);
}
}
// for(ArrayList<ArrayList<Integer>> sec : first) {
// for(ArrayList<Integer> fir : sec) out.println(fir.toString());
// out.println();
// }
boolean[] bools = new boolean[n];
int ind = 0;
for(int i = 0; i < first.size(); i++) {
second = first.get(i);
int firstind = ind;
int lastind = ind;
for(int j = 0; j < second.size(); j++) {
third = second.get(j);
for(int k = 0; k < third.size()-1; k = k+2) {
// out.println((third.get(k)+1) + " " + (third.get(k+1)+1));
out.println((al.get(ind).ind+1) + " " + (al.get(ind+1).ind+1));
ind = ind+2;
}
if(third.size()%2!=0) {
bools[ind] = true;
lastind = ind;
ind++;
}
}
for(int j = firstind; j <= lastind; j++) {
if(bools[j]) {
for(int k = j+1; k <= lastind; k++) {
if(bools[k]) {
out.println((al.get(j).ind+1) + " " + (al.get(k).ind+1));
bools[j] = false;
bools[k] = false;
j = k;
break;
}
}
}
}
}
// out.println(Arrays.toString(bools));
for(int i = 0; i < n; i++) {
if(bools[i]) {
for(int j = i+1; j < n; j++) {
if(bools[j]) {
out.println((al.get(i).ind+1) + " " + (al.get(j).ind+1));
i=j;
break;
}
}
}
}
out.close();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
d956f68c1e862f93f46ac1b53ab92cfa
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Remo3D {
static FastReader sc = new FastReader();
static OutputStream outputstream = System.out;
static PrintWriter out = new PrintWriter(outputstream);
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 Point {
long x,y,z;
int ind;
Point(long x, long y, long z, int ind) {
this.x=x;
this.y=y;
this.z=z;
this.ind=ind;
}
public String toString() {
return this.x + " " + this.y + " " + this.z + " " + (this.ind+1);
}
}
static class comp implements Comparator<Point> {
public int compare(Point p1, Point p2) {
if(p1.x==p2.x) {
if(p1.y==p2.y) {
if(p1.z>p2.z) return 1;
return -1;
} else if(p1.y>p2.y) {
return 1;
}
return -1;
} else if(p1.x>p2.x) {
return 1;
}
return -1;
}
}
static class Node {
int x, y;
Node(int x, int y) {
this.x=x;
this.y=y;
}
public String toString() {
return this.x + " " + this.y;
}
}
public static void main(String[] args) {
int n = sc.nextInt();
ArrayList<Point> al = new ArrayList();
for(int i = 0; i < n; i++) {
al.add(new Point(sc.nextLong(),sc.nextLong(),sc.nextLong(),i));
}
Collections.sort(al,new comp());
ArrayList<ArrayList<ArrayList<Integer>>> first = new ArrayList();
first.add(new ArrayList());
ArrayList<ArrayList<Integer>> second = first.get(0);
second.add(new ArrayList());
ArrayList<Integer> third = second.get(0);
third.add(al.get(0).ind);
for(int i = 1; i < n; i++) {
if(al.get(i-1).x==al.get(i).x) {
if(al.get(i).y==al.get(i-1).y) {
third.add(al.get(i).ind);
} else {
second.add(new ArrayList());
third = second.get(second.size()-1);
third.add(al.get(i).ind);
}
} else {
first.add(new ArrayList());
second = first.get(first.size()-1);
second.add(new ArrayList());
third = second.get(second.size()-1);
third.add(al.get(i).ind);
}
}
boolean[] bools = new boolean[n];
int ind = 0;
for(int i = 0; i < first.size(); i++) {
second = first.get(i);
int firstind = ind;
int lastind = ind;
for(int j = 0; j < second.size(); j++) {
third = second.get(j);
for(int k = 0; k < third.size()-1; k = k+2) {
out.println((al.get(ind).ind+1) + " " + (al.get(ind+1).ind+1));
ind = ind+2;
}
if(third.size()%2!=0) {
bools[ind] = true;
lastind = ind;
ind++;
}
}
for(int j = firstind; j <= lastind; j++) {
if(bools[j]) {
for(int k = j+1; k <= lastind; k++) {
if(bools[k]) {
out.println((al.get(j).ind+1) + " " + (al.get(k).ind+1));
bools[j] = false;
bools[k] = false;
j = k;
break;
}
}
}
}
}
for(int i = 0; i < n; i++) {
if(bools[i]) {
for(int j = i+1; j < n; j++) {
if(bools[j]) {
out.println((al.get(i).ind+1) + " " + (al.get(j).ind+1));
i=j;
break;
}
}
}
}
out.close();
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
4cf0b61641aa7518ec0107e8e3df1068
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
public class C {
static class Point implements Comparable<Point> {
int x, y, z, index;
public Point(int index, String coordinate) {
this.index = index;
String[] temp = coordinate.split(" ");
this.x = Integer.parseInt(temp[0]);
this.y = Integer.parseInt(temp[1]);
this.z = Integer.parseInt(temp[2]);
}
@Override
public int compareTo(Point o) {
if (this.x > o.x) {
return 1;
} else if (this.x < o.x) {
return -1;
} else {
if (this.y > o.y) {
return 1;
} else if (this.y < o.y) {
return -1;
} else {
if (this.z > o.z) {
return 1;
} else if (this.z < o.z) {
return -1;
} else {
return 0;
}
}
}
}
@Override
public String toString() {
return "(" + this.x + ", " + this.y + ", " + this.z + ")";
}
}
@SuppressWarnings("serial")
static class Node<E> extends LinkedList<E> implements Comparable<Node<E>> {
int label;
Node(int label) {
super();
this.label = label;
}
@Override
public int compareTo(Node<E> o) {
if (this.label > o.label) {
return 1;
} else if (this.label < o.label) {
return -1;
} else {
return 0;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
Point[] a = new Point[n];
for (int i = 0; i < n; i++) {
a[i] = new Point(i, input.readLine());
}
input.close();
Arrays.parallelSort(a);
LinkedList<Node<Node<Point>>> searchTree= new LinkedList<>();
searchTree.add(new Node<>(a[0].x));
searchTree.get(0).add(new Node<>(a[0].y));
searchTree.get(0).get(0).add(a[0]);
int currentX = 0, currentY = 0;
for (int i = 1; i < n; i++) {
if (a[i].x == searchTree.get(currentX).label) {
if (a[i].y == searchTree.get(currentX).get(currentY).label) {
searchTree.get(currentX).get(currentY).add(a[i]);
} else {
searchTree.get(currentX).add(new Node<>(a[i].y));
currentY++;
searchTree.get(currentX).get(currentY).add(a[i]);
}
} else {
searchTree.add(new Node<>(a[i].x));
searchTree.getLast().add(new Node<>(a[i].y));
searchTree.getLast().get(0).add(a[i]);
currentX++;
currentY = 0;
}
}
Point[] result = new Point[n];
for (int i = 0; i < n / 2; i++) {
Point current = searchTree.get(0).get(0).get(0);
result[2 * i] = current;
if (searchTree.get(0).get(0).size() > 1) {
result[2 * i + 1] = searchTree.get(0).get(0).get(1);
if (searchTree.get(0).get(0).size() == 2) {
searchTree.get(0).remove(0);
if (searchTree.get(0).isEmpty()) {
searchTree.remove(0);
}
} else {
searchTree.get(0).get(0).remove(0);
searchTree.get(0).get(0).remove(0);
}
} else if (searchTree.get(0).size() > 1){
searchTree.get(0).remove(0);
if (searchTree.get(0).get(0).size() == 1) {
result[2 * i + 1] = searchTree.get(0).get(0).get(0);
searchTree.get(0).remove(0);
if (searchTree.get(0).isEmpty()) {
searchTree.remove(0);
}
} else {
Point min = searchTree.get(0).get(0).get(0);
for (Point p : searchTree.get(0).get(0)) {
if (Math.abs(min.z - current.z) > Math.abs(p.z - current.z)) {
min = p;
}
}
result[2 * i + 1] = min;
searchTree.get(0).get(0).remove(min);
}
} else {
searchTree.remove(0);
Node<Point> minY = searchTree.get(0).get(0);
for (Node<Point> p : searchTree.get(0)) {
if (Math.abs(p.label - current.y) < Math.abs(minY.label - current.y)) {
minY = p;
}
}
if (minY.size() == 1) {
result[2 * i + 1] = minY.get(0);
searchTree.get(0).remove(minY);
if (searchTree.get(0).isEmpty()) {
searchTree.remove(0);
}
} else {
Point min = minY.get(0);
for (Point p : minY) {
if (Math.abs(min.z - current.z) > Math.abs(p.z - current.z)) {
min = p;
}
}
result[2 * i + 1] = min;
minY.remove(min);
}
}
}
StringBuilder output = new StringBuilder();
for (int i = 0; i < n / 2; i++) {
output.append(result[2 * i].index + 1).append(" ").append(result[2 * i + 1].index + 1).append("\n");
}
System.out.println(output.toString());
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
3c290b371456fa9310d3f804f3ca72dc
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
//class Declaration
static class pair implements Comparable < pair > {
long x;
long y;
pair(long i, long j) {
x = i;
y = j;
}
public int compareTo(pair p) {
if (this.x != p.x) {
return Long.compare(this.x,p.x);
} else {
return Long.compare(this.y,p.y);
}
}
public int hashCode() {
return (x + " " + y).hashCode();
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
pair x = (pair) o;
return (x.x == this.x && x.y == this.y);
}
}
// int[] dx = {0,0,1,-1};
// int[] dy = {1,-1,0,0};
// int[] ddx = {0,0,1,-1,1,-1,1,-1};
// int[] ddy = {1,-1,0,0,1,-1,-1,1};
int inf = (int) 1e9 + 9;
long biginf = (long)1e17 + 7 ;
long mod = (long)1e9 + 7;
void solve() throws Exception {
int n=ni();
Long[][] points = new Long[n][3];
TreeMap<Long,TreeMap<Long,TreeMap<Long,Integer>>> coord =new TreeMap<>();
boolean used[] = new boolean[n];
for(int i=0;i<n;++i){
points[i][0] = nl();points[i][1] = nl();points[i][2] = nl();
//final long[] fpoints = {points[i][0],points[i][1],points[i][1]};
final int ind = i ;
coord.compute(points[ind][2],(key,val)->{
if(val==null){
TreeMap<Long,TreeMap<Long,Integer>> ycor = new TreeMap<>();
TreeMap<Long,Integer> xcor = new TreeMap<>();
xcor.put(points[ind][0],ind+1);
ycor.put(points[ind][1],xcor);
return ycor;
}
else{
TreeMap<Long,TreeMap<Long,Integer>> ycor= val ;
ycor.compute(points[ind][1],(kk,xcor)->{
if(xcor==null){
TreeMap<Long,Integer> xx = new TreeMap<>();
xx.put(points[ind][0],ind+1);
return xx;
}
else{
TreeMap<Long,Integer> xx = xcor;
xx.put(points[ind][0],ind+1);
return xx ;
}
});
return ycor;
}
});
}
//pn(coord);
int[] ans = new int[n];
int ians = 0;
ArrayList<Integer> zans= new ArrayList<>();
for(long z : coord.keySet()){
TreeMap<Long,TreeMap<Long,Integer>> ycor = coord.get(z);
ArrayList<Integer> yans= new ArrayList<>();
for(long y: ycor.keySet()){
TreeMap<Long,Integer> xcor = ycor.get(y);
ArrayList<Integer> xans = new ArrayList<>();
for(long x : xcor.keySet()){
//ans[ians++] = xcor.get(x);
xans.add(xcor.get(x));
}
for(int i=0;i<=xans.size()-2;i+=2){
pn(xans.get(i)+" "+xans.get(i+1));
}
if(xans.size()%2==1){
yans.add(xans.get(xans.size()-1));
}
}
for(int i=0;i<=yans.size()-2;i+=2){
pn(yans.get(i)+" "+yans.get(i+1));
}
if(yans.size()%2==1){
zans.add(yans.get(yans.size()-1));
}
}
for(int i=0;i<=zans.size()-2;i+=2){
pn(zans.get(i)+" "+zans.get(i+1));
}
//pn(coord);
//pn(Arrays.deepToString(points));
}
long dist(Long[] a, Long[]b){
long dis = 0;
for(int i=0;i<3;++i){
dis+= (a[i]-b[i])*(a[i]-b[i]);
}
return dis;
}
long pow(long a, long b) {
long result = 1;
while (b > 0) {
if (b % 2 == 1) result = (result * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return result;
}
void print(Object o) {
System.out.println(o);
System.out.flush();
}
long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
//output methods
private void pn(Object o) {
out.println(o);
}
private void p(Object o) {
out.print(o);
}
private ArrayList < ArrayList < Integer >> ng(int n, int e) {
ArrayList < ArrayList < Integer >> g = new ArrayList < > ();
for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ());
for (int i = 0; i < e; ++i) {
int u = ni(), v = ni();
g.get(u).add(v);
g.get(v).add(u);
}
return g;
}
private ArrayList < ArrayList < pair >> nwg(int n, int e) {
ArrayList < ArrayList < pair >> g = new ArrayList < > ();
for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ());
for (int i = 0; i < e; ++i) {
int u = ni(), v = ni(), w = ni();
g.get(u).add(new pair(w, v));
g.get(v).add(new pair(w, u));
}
return g;
}
//input methods
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b));
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private void tr(Object...o) {
if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
}
void watch(Object...a) throws Exception {
int i = 1;
print("watch starts :");
for (Object o: a) {
//print(o);
boolean notfound = true;
if (o.getClass().isArray()) {
String type = o.getClass().getName().toString();
//print("type is "+type);
switch (type) {
case "[I":
{
int[] test = (int[]) o;
print(i + " " + Arrays.toString(test));
break;
}
case "[[I":
{
int[][] obj = (int[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[J":
{
long[] obj = (long[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[J":
{
long[][] obj = (long[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[D":
{
double[] obj = (double[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[D":
{
double[][] obj = (double[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[Ljava.lang.String":
{
String[] obj = (String[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[Ljava.lang.String":
{
String[][] obj = (String[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[C":
{
char[] obj = (char[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[C":
{
char[][] obj = (char[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
default:
{
print(i + " type not identified");
break;
}
}
notfound = false;
}
if (o.getClass() == ArrayList.class) {
print(i + " al: " + o);
notfound = false;
}
if (o.getClass() == HashSet.class) {
print(i + " hs: " + o);
notfound = false;
}
if (o.getClass() == TreeSet.class) {
print(i + " ts: " + o);
notfound = false;
}
if (o.getClass() == TreeMap.class) {
print(i + " tm: " + o);
notfound = false;
}
if (o.getClass() == HashMap.class) {
print(i + " hm: " + o);
notfound = false;
}
if (o.getClass() == LinkedList.class) {
print(i + " ll: " + o);
notfound = false;
}
if (o.getClass() == PriorityQueue.class) {
print(i + " pq : " + o);
notfound = false;
}
if (o.getClass() == pair.class) {
print(i + " pq : " + o);
notfound = false;
}
if (notfound) {
print(i + " unknown: " + o);
}
i++;
}
print("watch ends ");
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
74a9a896e9e95fe4658f85791db40a7f
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
dataC str[]=new dataC[n+1];
str[0]=new dataC();
for(int i=1;i<=n;i++)
{
str[i]=new dataC();
str[i].x=sc.nextInt();
str[i].y=sc.nextInt();
str[i].z=sc.nextInt();
str[i].id=i;
}
Arrays.sort(str,1,n+1,new Comparator<dataC>()
{
public int compare(dataC a,dataC b)
{
if(a.x!=b.x) return a.x<b.x?-1:1;
if(a.y!=b.y) return a.y<b.y?-1:1;
return a.z<b.z?-1:1;
}
});
boolean vis[]=new boolean[n+1];
Arrays.fill(vis, false);
for(int i=1;i<=n-1;i++)
{
if(str[i].x==str[i+1].x&&str[i].y==str[i+1].y)
{
vis[i]=true;vis[i+1]=true;
System.out.println(str[i].id+" "+str[i+1].id);
i++;
}
}
for(int i=1;i<=n-1;i++)
{
if(vis[i]) continue;
int j=i+1;
while(j<=n&&vis[j]) j++;
if(j<=n&&str[i].x==str[j].x)
{
vis[i]=true;vis[j]=true;
System.out.println(str[i].id+" "+str[j].id);
i=j;
}
}
for(int i=1;i<=n-1;i++)
{
if(vis[i])
continue;
int j=i+1;
while(vis[j])
j++;
System.out.println(str[i].id+" "+str[j].id);
vis[i]=true;vis[j]=true;
i=j;
}
}
}
class dataC
{
int x,y,z,id;
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
ed84f7000e12dfaf6f51bc4b0b2a5d63
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
work();
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return b==0?a:gcd(b,a%b);
}
void work() {
int n=in.nextInt();
int[][] A=new int[n][4];
for(int i=0;i<n;i++) {
int x=in.nextInt();
int y=in.nextInt();
int z=in.nextInt();
A[i]=new int[] {x,y,z,i+1};
}
Arrays.sort(A,new Comparator<int[]>() {
public int compare(int[] arr1,int[] arr2) {
if(arr1[0]==arr2[0]) {
if(arr1[1]==arr2[1]) {
return arr1[2]-arr2[2];
}else {
return arr1[1]-arr2[1];
}
}else {
return arr1[0]-arr2[0];
}
}
});
boolean[] vis=new boolean[n];
for(int i=0,j=1;i<n;) {
if(vis[i]) {
i++;
continue;
}
j=i+1;
while(j<n&&vis[j]) {
j++;
}
if(j<n&&A[i][0]==A[j][0]&&A[i][1]==A[j][1]) {//ηΈι»ε
ζΆζ
out.println(A[i][3]+" "+A[j][3]);
vis[i]=true;
vis[j]=true;
}
i=j;
}
for(int i=0,j=1;i<n;) {
if(vis[i]) {
i++;
continue;
}
j=i+1;
while(j<n&&vis[j]) {
j++;
}
if(j<n&&A[i][0]==A[j][0]) {//ηΈι»ε
ζΆζ
out.println(A[i][3]+" "+A[j][3]);
vis[i]=true;
vis[j]=true;
}
i=j;
}
for(int i=0,j=1;i<n;) {
if(vis[i]) {
j=i+1;
i++;
continue;
}
j=i+1;
while(j<n&&vis[j]) {
j++;
}
if(j<n) {
out.println(A[i][3]+" "+A[j][3]);
vis[i]=true;
vis[j]=true;
}
i=j;
}
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next()
{
if(st==null || !st.hasMoreElements())
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
84bf105562aea95b17fc1683d2233e9d
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
static Point[] arr;
static Point solveZ(ArrayList<Point> a) {
Collections.sort(a, (o1, o2) -> Integer.compare(o1.z, o2.z));
for (int i = 0; i < a.size() - 1; i += 2) {
int u = a.get(i).id, v = a.get(i + 1).id;
out.println(u + " " + v);
}
return (a.size() % 2 == 0) ? null : a.get(a.size() - 1);
}
static Point solveY(ArrayList<Point> a) {
TreeMap<Integer, ArrayList<Point>> byy = new TreeMap<>();
for (Point p : a) {
ArrayList<Point> val = byy.getOrDefault(p.y, new ArrayList<>());
val.add(p);
byy.put(p.y, val);
}
ArrayList<Point> rem = new ArrayList<>();
for (Map.Entry<Integer, ArrayList<Point>> e : byy.entrySet()) {
Point p = solveZ(e.getValue());
if (p != null)
rem.add(p);
}
for (int i = 0; i < rem.size() - 1; i += 2) {
int u = rem.get(i).id, v = rem.get(i + 1).id;
out.println(u + " " + v);
}
return (rem.size() % 2 == 0) ? null : rem.get(rem.size() - 1);
}
static void solveX() {
TreeMap<Integer, ArrayList<Point>> byx = new TreeMap<>();
for (int i = 0; i < arr.length; i++) {
ArrayList<Point> val = byx.getOrDefault(arr[i].x, new ArrayList<>());
val.add(arr[i]);
byx.put(arr[i].x, val);
}
ArrayList<Point> rem = new ArrayList<>();
for (Map.Entry<Integer, ArrayList<Point>> e : byx.entrySet()) {
Point p = solveY(e.getValue());
if(p != null) rem.add(p);
}
for (int i = 0; i < rem.size() - 1; i += 2) {
int u = rem.get(i).id, v = rem.get(i + 1).id;
out.println(u + " " + v);
}
}
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
arr = new Point[n];
for (int i = 0; i < n; i++) {
int x = sc.nextInt(), y = sc.nextInt(), z = sc.nextInt();
arr[i] = new Point(x, y, z, i);
}
solveX();
out.close();
}
static class Point {
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 + 1;
}
public String toString() {
return "" + id;
}
}
}
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 Long nextLong() throws IOException {
return Long.parseLong(next());
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
454e2b4951d01ba255e768b275104f40
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.*;
import java.nio.CharBuffer;
import java.util.*;
public class P1237C2 {
public static void main(String[] args) {
SimpleScanner scanner = new SimpleScanner(System.in);
PrintWriter writer = new PrintWriter(System.out);
int n = scanner.nextInt();
TreeMap<Integer, TreeMap<Integer, ArrayList<int[]>>> xMap = new TreeMap<>();
for (int i = 0; i < n; ++i) {
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
xMap.putIfAbsent(x, new TreeMap<>());
TreeMap<Integer, ArrayList<int[]>> yMap = xMap.get(x);
yMap.putIfAbsent(y, new ArrayList<>());
ArrayList<int[]> zList = yMap.get(y);
zList.add(new int[]{z, i + 1});
}
ArrayList<Integer> xList = new ArrayList<>();
for (Map.Entry<Integer, TreeMap<Integer, ArrayList<int[]>>> xEntry : xMap.entrySet()) {
TreeMap<Integer, ArrayList<int[]>> yMap = xEntry.getValue();
ArrayList<Integer> yList = new ArrayList<>();
for (Map.Entry<Integer, ArrayList<int[]>> yEntry : yMap.entrySet()) {
ArrayList<int[]> zList = yEntry.getValue();
zList.sort(Comparator.comparingInt(o -> o[0]));
for (int i = 0; i < zList.size() - 1; i += 2)
writer.println(zList.get(i)[1] + " " + zList.get(i + 1)[1]);
if (zList.size() % 2 == 1)
yList.add(zList.get(zList.size() - 1)[1]);
}
for (int i = 0; i < yList.size() - 1; i += 2)
writer.println(yList.get(i) + " " + yList.get(i + 1));
if (yList.size() % 2 == 1)
xList.add(yList.get(yList.size() - 1));
}
for (int i = 0; i < xList.size() - 1; i += 2)
writer.println(xList.get(i) + " " + xList.get(i + 1));
writer.close();
}
private static long dis(int[] x, int[] y, int[] z, int i, int j) {
long dx = Math.abs(x[i] - x[j]);
long dy = Math.abs(y[i] - y[j]);
long dz = Math.abs(z[i] - z[j]);
return dx * dx + dy * dy + dz * dz;
}
private static class SimpleScanner {
private static final int BUFFER_SIZE = 10240;
private Readable in;
private CharBuffer buffer;
private boolean eof;
private SimpleScanner(InputStream in) {
this.in = new BufferedReader(new InputStreamReader(in));
buffer = CharBuffer.allocate(BUFFER_SIZE);
buffer.limit(0);
eof = false;
}
private char read() {
if (!buffer.hasRemaining()) {
buffer.clear();
int n;
try {
n = in.read(buffer);
} catch (IOException e) {
n = -1;
}
if (n <= 0) {
eof = true;
return '\0';
}
buffer.flip();
}
return buffer.get();
}
private void checkEof() {
if (eof)
throw new NoSuchElementException();
}
private char nextChar() {
checkEof();
char b = read();
checkEof();
return b;
}
private String next() {
char b;
do {
b = read();
checkEof();
} while (Character.isWhitespace(b));
StringBuilder sb = new StringBuilder();
do {
sb.append(b);
b = read();
} while (!eof && !Character.isWhitespace(b));
return sb.toString();
}
private int nextInt() {
return Integer.parseInt(next());
}
private long nextLong() {
return Long.parseLong(next());
}
private double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
200fa4c7044b708cb64e8211f3724d60
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Solution {
public static void main (String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int arr[][] = new int[n][4];
for(int i=0;i<n;i++)
{
String k[] = br.readLine().split(" ");
arr[i][0] = Integer.parseInt(k[0]);
arr[i][1] = Integer.parseInt(k[1]);
arr[i][2] = Integer.parseInt(k[2]);
arr[i][3] = i+1;
}
Arrays.sort(arr,new Comparator<int[]>(){
public int compare(int a[],int b[])
{
if(a[2]>b[2])return 1;
else if(a[2]==b[2])
{
if(a[1]>b[1])return 1;
else if(a[1]==b[1])
{
if(a[0]>b[0])return 1;
}
}
return -1;
}
});
StringBuilder sb = new StringBuilder();
boolean b[] = new boolean[n];
for(int i=0;i<n-1;i++)
{
//System.out.println(arr[i][0]+" "+arr[i][1]+" "+arr[i][2]+" "+arr[i][3]);
if(b[i])continue;
if(arr[i][1]==arr[i+1][1] && arr[i][2]==arr[i+1][2])
{
sb.append(arr[i][3]+" "+arr[i+1][3]+"\n");
b[i]=true;
b[i+1]=true;
}
}
for(int i=0;i<n;i++)
{
if(b[i])continue;
int j = i+1;
while(j<n && b[j])j++;
if(j==n)break;
if(arr[i][2]==arr[j][2]){
sb.append(arr[i][3]+" "+arr[j][3]+"\n");
b[i]=true;
b[j]=true;
i=j;
}
else i=j-1;
}
for(int i=0;i<n;i++)
{
if(b[i])continue;
int j = i+1;
while(b[j])j++;
sb.append(arr[i][3]+" "+arr[j][3]+"\n");
b[i]=true;
b[j]=true;
i=j;
}
System.out.println(sb);
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
f100fb1549e5b5fb1803abe4b8210e3e
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class C {
static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
MyScanner sc = new MyScanner();
int N = sc.nextInt();
List<Point> points = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
points.add(new Point(x, y, z, i + 1));
}
Collections.sort(points, (p1, p2) -> {
int cmp = Integer.compare(p1.x, p2.x);
if (cmp != 0) return cmp;
cmp = Integer.compare(p1.y, p2.y);
if (cmp != 0) return cmp;
return Integer.compare(p1.z, p2.z);
});
List<Point> got = new ArrayList<>();
int from = 0;
for (int i = 1; i < N; i++) {
if (points.get(i).x != points.get(from).x) {
Point tmp = samePlane(points.subList(from, i));
if (tmp != null) got.add(tmp);
from = i;
}
}
Point tmp = samePlane(points.subList(from, N));
if (tmp != null) got.add(tmp);
sameLine(got);
out.flush();
}
static void print(Point a, Point b) {
out.println(a.id + " " + b.id);
}
static Point samePlane(List<Point> points) {
List<Point> got = new ArrayList<>();
int from = 0;
for (int i = 1; i < points.size(); i++) {
if (points.get(i).y != points.get(from).y) {
Point tmp = sameLine(points.subList(from, i));
if (tmp != null) got.add(tmp);
from = i;
}
}
Point tmp = sameLine(points.subList(from, points.size()));
if (tmp != null) got.add(tmp);
return sameLine(got);
}
static Point sameLine(List<Point> points) {
for (int i = 0; i + 1 < points.size(); i+=2) {
print(points.get(i), points.get(i + 1));
}
return points.size() % 2 == 1 ? points.get(points.size() - 1) : null;
}
static class Point {
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;
}
}
static class MyScanner {
private BufferedReader br;
private StringTokenizer tokenizer;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
027f6e18cdbb50f50aee49ced59b5250
|
train_000.jsonl
|
1571236500
|
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC1 solver = new TaskC1();
solver.solve(1, in, out);
out.close();
}
static class TaskC1 {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[][] p = new int[n][4];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 3; ++j) {
p[i][j] = in.nextInt();
}
p[i][3] = i + 1;
}
Arrays.sort(p, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
for (int i = 0; i < o1.length; ++i) if (o1[i] != o2[i]) return o1[i] - o2[i];
return 0;
}
});
boolean[] alive = new boolean[n];
Arrays.fill(alive, true);
for (int i = 0; i < n; ++i)
if (alive[i]) {
int j = i + 1;
while (!alive[j]) ++j;
int diff = 0;
while (p[i][diff] == p[j][diff]) ++diff;
int k = j;
while (k < n && p[k][diff] == p[j][diff]) ++k;
int bestScore = Integer.MAX_VALUE;
int bt = -1;
for (int t = j; t < k; ++t)
if (alive[t]) {
int score = 0;
for (int u = 0; u < 3; ++u) score += Math.abs(p[i][u] - p[t][u]);
if (score < bestScore) {
bestScore = score;
bt = t;
}
}
alive[i] = false;
alive[bt] = false;
out.println(p[i][3] + " " + p[bt][3]);
}
}
}
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 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"]
|
1 second
|
["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"]
|
NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
|
Java 8
|
standard input
|
[
"greedy",
"constructive algorithms",
"implementation",
"divide and conquer",
"sortings",
"binary search"
] |
3d92a54be5c544b313f1e87f7b9cc5c8
|
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.
| 1,900 |
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.
|
standard output
| |
PASSED
|
67ea9c2bc2971dfbd660aad44a051d51
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = 0;
int x = 0;
int b = input.nextInt();
int d = input.nextInt();
int sum= 0;
for (int i = 0; i < n; i++) {
a= input.nextInt();
if(a<=b){
sum+=a;
}
if(sum>d){
x++;
sum = 0;
}
}
System.out.println(x);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
048c2c26ae9ce36fce0d9d2e8c518a55
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.Scanner;
public class Juicer {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int b = in.nextInt();
int d = in.nextInt();
in.nextLine();
int w = 0, o=0, f=0;
for (int i = 0; i < n; i++) {
o=in.nextInt();
if(b>=o) {
w+=o;
}
if(w>d){
f++;
w=0;
}
}
if(w>d) f++;
System.out.println(f);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
cde59c08889fbd5135e64f994d027b05
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.lang.*;
import java.util.Scanner;
import java.util.*;
import java.io.*;
public class TestClass {
public static void main(String args[] ) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr = new int[n];
int b=s.nextInt();
int d = s.nextInt();
int x=0;
int ans=0;
for (int i = 0; i < n; i++) {
arr[i]=s.nextInt();
if(arr[i]<=b ){
x+=arr[i];
if(x>d){
ans++;
x=0;
}
}
}
System.out.println(ans);
s.close();
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
2b8181b1f1fa646a77734526d343f52b
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args)throws IOException, URISyntaxException {
Reader.init(System.in);
StringBuilder s=new StringBuilder();
long n = Reader.nextLong(), b = Reader.nextLong(), d = Reader.nextLong(), sum = 0, cur, cnt = 0;
while(n-- > 0)
if((cur = Reader.nextLong()) <= b) {
sum += cur;
if(sum > d) {
sum = 0;
cnt++;
}
}
s.append(cnt).append('\n');
System.out.print(s);
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) throws UnsupportedEncodingException {
reader = new BufferedReader(
new InputStreamReader(input, "UTF-8") );
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 String nextLine() throws IOException {
return reader.readLine();
}
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() );
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
a55e2251317feabac58eea0fcab9d7ac
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.Scanner;
public class solution{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int size=scan.nextInt();
int waster=scan.nextInt();
int sum=0;
int counter=0;
for(int i=0;i<n;i++)
{
int x=scan.nextInt();
if(x<=size)
{
sum+=x;
if(sum>waster)
{
counter++;
sum=0;
}
}
}
System.out.println(counter);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
f74b281929133fc625ff25720ff19770
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.Scanner;
public class solution{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int size=scan.nextInt();
int waster=scan.nextInt();
int sum=0;
int counter=0;
for(int i=0;i<n;i++)
{
int x=scan.nextInt();
if(x<=size)
{
sum+=x;
if(sum>waster)
{
counter++;
sum=0;
}
}
}
System.out.print(counter);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
43837255696569af0b3cf1c60daa179e
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
//package acm;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.HashMap;
import java.util.Scanner;
/**
*
* @author adMath.min
*/
import java.math.*;
public class Acm {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int d = sc.nextInt();
int sum = 0;
int s = 0;
int r = 0;
while (x > 0) {
r = sc.nextInt();
if(r<=y) {
sum += r;
if (sum > d) {
s++;
sum=0;
}
}
x--;
}
System.out.println(s);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
82be6cdbd985e634afc74ba434dc411c
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class juicer
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String[] info = reader.readLine().split(" ");
int oranges = Integer.parseInt(info[0]);
int maxSize = Integer.parseInt(info[1]);
int dWaste = Integer.parseInt(info[2]);
info = reader.readLine().split(" ");
int total = 0;
int count = 0;
int[] orange = new int[oranges];
for (int i = 0; i < oranges; i++)
{
orange[i] = Integer.parseInt(info[i]);
if (orange[i] <= maxSize)
{
total += orange[i];
if (total > dWaste)
{
count++;
total = 0;
}
}
}
writer.write(count + "\n");
reader.close();
writer.close();
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
fe41249e61d2c513daad16789b736c99
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class juicer
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] info = reader.readLine().split(" ");
int oranges = Integer.parseInt(info[0]);
int maxSize = Integer.parseInt(info[1]);
int dWaste = Integer.parseInt(info[2]);
info = reader.readLine().split(" ");
int total = 0;
int count = 0;
int[] orange = new int[oranges];
for (int i = 0; i < oranges; i++)
{
orange[i] = Integer.parseInt(info[i]);
if (orange[i] <= maxSize)
{
total += orange[i];
if (total > dWaste)
{
count++;
total = 0;
}
}
}
System.out.println(count);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
2c5e8090ad43e3bcb2974be502b5f1f4
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int b = sc.nextInt();
int d = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
long sum = 0;
int c = 0;
for(int i=0;i<n;i++){
if (arr[i] > b)
continue;
sum+=arr[i];
if (sum > d){
sum = 0;
c++;
}
}
System.out.println(c);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
65c57ba925e10d68effa1ef39e9d94c5
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.Scanner;
public class CF709_D2_A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int b = sc.nextInt();
int d = sc.nextInt();
long sum = 0;
int c = 0;
for (int i=0;i<n;i++){
int x = sc.nextInt();
if (x <= b)
sum += x;
if (sum > d){
sum = 0;
c++;
}
}
//System.out.println(sum +" % " + d +" = "+sum%d);
System.out.println(c);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
e95acaa1d5d66a0658684bbb1ea27885
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class abc
{
public static void main(String ar[])throws IOException
{
Scanner sc=new Scanner(System.in);
int n,b,c=0,e=0,d;
n=sc.nextInt();
b=sc.nextInt();
d=sc.nextInt();
int A[]=new int[n];
for(int i=0;i<n;i++)
{
A[i]=sc.nextInt();
if(A[i]<=b)
{
if((e+A[i])<=d)
{
e+=A[i];
}
else
{
e=0;
c++;
}
}
}
System.out.println(c);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
0e474abf38445cec851e4c1e303282b1
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class abc
{
public static void main(String ar[])throws IOException
{
Scanner sc=new Scanner(System.in);
int n,b,c=0,e=0,d;
n=sc.nextInt();
b=sc.nextInt();
d=sc.nextInt();
int A[]=new int[n];
for(int i=0;i<n;i++)
{
A[i]=sc.nextInt();
if(A[i]<=b)
{
if((e+A[i])<=d)
{
e+=A[i];
}
else
{
e=0;
c++;
}
}
}
System.out.println(c);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
60c451cd0b52bd39659036f89b4288f6
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.io.*;
import java.util.Locale;
import java.util.StringTokenizer;
public class SolutionTest implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new SolutionTest(), "", 256 * (1L << 20)).start();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
// in = new BufferedReader(new FileReader("src/codeforces/input.txt"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
// solution
void solve() throws IOException {
out.println(juicer709A());
}
private int juicer709A() throws IOException {
int numberOfOranges = readInt();
int maxOrangeSize = readInt();
int wasteLevel = readInt();
int numberOfEmpty = 0;
int orangeSizes = 0;
for (int i = 0; i < numberOfOranges; i++) {
int orange = readInt();
if (orange > maxOrangeSize)
continue;
orangeSizes += orange;
if (orangeSizes > wasteLevel) {
numberOfEmpty++;
orangeSizes = 0;
}
}
return numberOfEmpty;
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
e39df57d17f565996729d5f8ca55932b
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.*;
public class Pair {
static class FastReader {
private BufferedReader br;
private StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
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 long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws Exception {
try {
FastReader sc = new FastReader();
long n = sc.nextInt();
long b = sc.nextInt();
long d = sc.nextInt();
long squeeze=0;
long count=0;
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
squeeze += a<=b?a:0;
if ((squeeze) > d) {
squeeze = 0;
count++;
}
}
System.out.println(count);
}
catch (Exception e) {
}
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
ba3674df3b8e65878a5aaf48798e8c29
|
train_000.jsonl
|
1472056500
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1,βa2,β...,βan. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
256 megabytes
|
import java.util.Scanner;
public class Comp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int b = input.nextInt();
int d = input.nextInt();
int t = 0;
int s;
int f = 0;
for (int i = 0; i < n; i++) {
s = input.nextInt();
if (s <= b) {
t += s;
}
if (t > d) {
t =0;
f++;
}
}
System.out.println(f);
}
}
|
Java
|
["2 7 10\n5 6", "1 5 10\n7", "3 10 10\n5 7 7", "1 1 1\n1"]
|
1 second
|
["1", "0", "1", "0"]
|
NoteIn the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Java 8
|
standard input
|
[
"implementation"
] |
06e9649963715e56d97297c6104fbc00
|
The first line of the input contains three integers n, b and d (1ββ€βnββ€β100β000, 1ββ€βbββ€βdββ€β1β000β000)Β β the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied. The second line contains n integers a1,βa2,β...,βan (1ββ€βaiββ€β1β000β000)Β β sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
| 900 |
Print one integerΒ β the number of times Kolya will have to empty the waste section.
|
standard output
| |
PASSED
|
ce97b43c7c4a570a3026be64bc7f3856
|
train_000.jsonl
|
1332860400
|
In some country live wizards. They like to make weird bets.Two wizards draw an acyclic directed graph with n vertices and m edges (the graph's vertices are numbered from 1 to n). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same.Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to k. The sinks are numbered from 1 to k in the similar way.To make a bet, they, as are real wizards, cast a spell, which selects a set of k paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the i-th sink has a path going to it from the ai's source. Then let's call pair (i,βj) an inversion if iβ<βj and aiβ>βaj. If the number of inversions among all possible pairs (i,βj), such that (1ββ€βiβ<βjββ€βk), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one).Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number p.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class E
{
private static int MOD;
public static void main(String [] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(tokenizer.nextToken());
int m = Integer.parseInt(tokenizer.nextToken());
MOD = Integer.parseInt(tokenizer.nextToken());
ArrayList<Integer> [] rev = new ArrayList[n];
for(int i = 0 ; i < n ; i++)
rev[i] = new ArrayList<Integer>();
int [] inDegree = new int[n];
int [] outDegree = new int[n];
for(int i = 0 ; i < m ; i++)
{
tokenizer = new StringTokenizer(reader.readLine());
int a = Integer.parseInt(tokenizer.nextToken()) - 1;
int b = Integer.parseInt(tokenizer.nextToken()) - 1;
outDegree[a]++;
inDegree[b]++;
rev[b].add(a);
}
int k = 0;
for(int i = 0 ; i < n ; i++)
if(inDegree[i] == 0 && outDegree[i] != 0)
k++;
int [] sinks = new int[k];
int pSink = 0;
int [] sources = new int[k];
int pSource = 0;
for(int i = 0 ; i < n ; i++)
{
if(inDegree[i] == 0 && outDegree[i] != 0)
sources[pSource++] = i;
if(inDegree[i] != 0 && outDegree[i] == 0)
sinks[pSink++] = i;
}
int [][] matrix = new int[k][k];
for(int i = 0 ; i < k ; i++)
{
int [] calc = new int[n];
Arrays.fill(calc, -1);
calc[sources[i]] = 1;
for(int j = 0 ; j < n ; j++)
if(calc[j] == -1)
dfs(j, rev, calc);
for(int j = 0 ; j < k ; j++)
matrix[i][j] = calc[sinks[j]];
}
int det = determinant(matrix);
int sign = 1;
for(int i = 0 ; i < n ; i++)
if(outDegree[i] == 0 && inDegree[i] == 0)
{
int row = Arrays.binarySearch(sources, i);
row = -row-1;
int col = Arrays.binarySearch(sinks, i);
col = -col-1;
if((row+col)%2 != 0)
sign *= -1;
}
det *= sign;
det += MOD;
det %= MOD;
writer.println(det);
writer.flush();
writer.close();
}
private static int determinant(int [][] matrix)
{
int n = matrix.length;
//row echelon form
int sign = 1;
for(int i = 0 ; i < matrix.length ; i++)
{
int nonZero = i;
while(nonZero < n && matrix[nonZero][i] == 0)
nonZero++;
if(nonZero == n)
return 0;
if(nonZero != i)
sign *= -1;
for(int j = 0 ; j < n ; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[nonZero][j];
matrix[nonZero][j] = temp;
}
int inv = power(matrix[i][i], MOD-2);
for(int j = i+1 ; j < n ; j++)
{
int coef = (int)((1L*inv*matrix[j][i])%MOD);
for(int k = i ; k < n ; k++)
{
matrix[j][k] = (int)((matrix[j][k] - 1L*matrix[i][k]*coef)%MOD);
matrix[j][k] += MOD;
matrix[j][k] %= MOD;
}
}
}
long det = 1;
for(int i = 0 ; i < n ; i++)
{
det *= matrix[i][i];
det %= MOD;
}
det *= sign;
det += MOD;
det %= MOD;
return (int) det;
}
private static int power(int a, int b)
{
if(b == 0)
return 1;
if(b%2 == 1)
return (int)((1L*a*power(a, b-1))%MOD);
long res = power(a, b/2);
res *= res;
res %= MOD;
return (int) res;
}
private static int dfs(int at, ArrayList<Integer> [] rev, int [] calc)
{
if(calc[at] != -1)
return calc[at];
int res = 0;
for(int to : rev[at])
{
res += dfs(to, rev, calc);
if(res >= MOD)
res -= MOD;
}
return calc[at] = res;
}
}
|
Java
|
["4 2 1000003\n1 3\n2 4", "4 2 1000003\n4 1\n3 2", "4 4 1000003\n2 1\n2 4\n3 1\n3 4", "6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6", "5 2 1000003\n5 1\n3 4"]
|
3 seconds
|
["1", "1000002", "0", "0", "1"]
|
NoteIn the first sample, there is exactly one set of paths β . The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin.In the second sample there is exactly one set of paths β . There is exactly one inversion. Therefore, the first wizard gets -1 coin. .In the third sample, there are two sets of paths, which are counted with opposite signs.In the fourth sample there are no set of paths at all.In the fifth sample, there are three sources β the vertices with the numbers (2, 3, 5) and three sinks β the vertices with numbers (1, 2, 4). For a single set of paths are 2 inversions, that is, their number is even.
|
Java 8
|
standard input
|
[
"graphs",
"dfs and similar",
"math",
"matrices"
] |
3247c416952cd4335ec9319cd0ef0da2
|
The first line contains three space-separated integers n, m, p (1ββ€βnββ€β600, 0ββ€βmββ€β105, 2ββ€βpββ€β109β+β7). It is guaranteed that p is prime number. Next m lines contain edges of the graph. Each line contains a pair of space-separated integers, aiΒ bi β an edge from vertex ai to vertex bi. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.
| 2,900 |
Print the answer to the problem β the total winnings of the first player modulo a prime number p. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
|
standard output
| |
PASSED
|
5cce311b9ca94408bbd6f809bbb909be
|
train_000.jsonl
|
1332860400
|
In some country live wizards. They like to make weird bets.Two wizards draw an acyclic directed graph with n vertices and m edges (the graph's vertices are numbered from 1 to n). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same.Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to k. The sinks are numbered from 1 to k in the similar way.To make a bet, they, as are real wizards, cast a spell, which selects a set of k paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the i-th sink has a path going to it from the ai's source. Then let's call pair (i,βj) an inversion if iβ<βj and aiβ>βaj. If the number of inversions among all possible pairs (i,βj), such that (1ββ€βiβ<βjββ€βk), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one).Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number p.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Collection;
import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.TreeSet;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyScanner in = new MyScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
public void solve(int testNumber, MyScanner in, PrintWriter out) {
int k=0;
int n = in.nextInt(),m=in.nextInt(),p=in.nextInt();
Vertex[] vs = new Vertex[n];
for (int i = 0; i < n; i++){
vs[i] = new Vertex(i);
}
int[] inDeg = new int[n],outDeg = new int[n];
for (int i = 0; i < m; i++) {
int s = in.nextInt()-1,t=in.nextInt()-1;
vs[s].add(new Edge(vs[t]));
outDeg[s]++;
inDeg[t]++;
}
for (int i = 0; i < n; i++) {
if(inDeg[i]==0 && outDeg[i]>0)k++;
}
int[] source=new int[k];
int[] sink = new int[k];
long res = 1;
for (int i = 0,i1=0,i2=0; i < n; i++) {
if(inDeg[i]==0 && outDeg[i]>0)source[i1++] = i;
if(outDeg[i]==0 && inDeg[i]>0)sink[i2++] = i;
if(inDeg[i]==0 && outDeg[i]==0)res *= (i1+i2)%2==0 ? 1 : -1;
}
long[][] A = new long[k][k];
for (int i = 0; i < k; i++) {
int[] ways = new int[n];
Arrays.fill(ways, -1);
for (int j = 0; j < k; j++)
ways[sink[j]] = j==i ? 1 : 0;
for (int j = 0; j < k; j++)
A[j][i] = dfs(vs[source[j]],ways,p);
}
res *= Matrix.determinantDestructive(A, p);
if(res<0)res += p;
out.println(res);
}
private int dfs(Vertex cur, int[] ways,int modulus) {
if(ways[cur.id]>=0)return ways[cur.id];
int res = 0;
for(Edge e:cur){
res += dfs(e.to,ways,modulus);
if(res >= modulus)res -= modulus;
}
ways[cur.id] = res;
return res;
}
}
class MyScanner {
private final InputStream in;
public MyScanner(InputStream in){
this.in = in;
}
public int nextInt(){
try{
int c=in.read();
if(c==-1) return c;
while(c!='-'&&(c<'0'||'9'<c)){
c=in.read();
if(c==-1) return c;
}
if(c=='-') return -nextInt();
int res=0;
do{
res*=10;
res+=c-'0';
c=in.read();
}while('0'<=c&&c<='9');
return res;
}catch(Exception e){
return -1;
}
}
}
class Vertex extends ArrayList<Edge>{
public final int id;
public Vertex(int id){
this.id=id;
}
}
class Edge {
public Vertex to;
public Edge(Vertex to) {
this.to = to;
}
}
class Matrix {
public static int determinantDestructive(long[][] A, int modPrime) {
int n = A.length;
long res = 1;
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++) {
if (Math.abs(A[j][i]) > Math.abs(A[pivot][i])) {
pivot = j;
}
}
// rank < n
if (A[pivot][i] == 0) return 0;
if(i!=pivot){
ArrayUtils.swap(A, i, pivot);
res = -res;
if(res<0)res += modPrime;
}
res = res * A[i][i] % modPrime;
long inv = MathUtils.inverse(A[i][i],modPrime);
for (int j = i + 1; j < n; j++) {
long mul = A[j][i] * inv % modPrime;
for (int k = i; k < n; k++) {
A[j][k] -= A[i][k] * mul % modPrime;
if(A[j][k] < 0)A[j][k] += modPrime;
}
}
}
return (int) res;
}
}
class ArrayUtils {
public static <T> void swap(T[] vs, int i, int j) {
T tmp = vs[i];
vs[i] = vs[j];
vs[j] = tmp;
}
}
class MathUtils {
public static long powMod(long value, long exponent, int modulo) {
if(modulo==1)return 0;
if(value >= modulo) value %= modulo;
long res = 1;
while(exponent>0){
if((exponent&1)==1){
res = res * value % modulo;
}
value = value * value % modulo;
exponent >>= 1;
}
return res;
}
public static long inverse(long value, int modPrime) {
return powMod(value,modPrime-2,modPrime);
}
}
|
Java
|
["4 2 1000003\n1 3\n2 4", "4 2 1000003\n4 1\n3 2", "4 4 1000003\n2 1\n2 4\n3 1\n3 4", "6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6", "5 2 1000003\n5 1\n3 4"]
|
3 seconds
|
["1", "1000002", "0", "0", "1"]
|
NoteIn the first sample, there is exactly one set of paths β . The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin.In the second sample there is exactly one set of paths β . There is exactly one inversion. Therefore, the first wizard gets -1 coin. .In the third sample, there are two sets of paths, which are counted with opposite signs.In the fourth sample there are no set of paths at all.In the fifth sample, there are three sources β the vertices with the numbers (2, 3, 5) and three sinks β the vertices with numbers (1, 2, 4). For a single set of paths are 2 inversions, that is, their number is even.
|
Java 6
|
standard input
|
[
"graphs",
"dfs and similar",
"math",
"matrices"
] |
3247c416952cd4335ec9319cd0ef0da2
|
The first line contains three space-separated integers n, m, p (1ββ€βnββ€β600, 0ββ€βmββ€β105, 2ββ€βpββ€β109β+β7). It is guaranteed that p is prime number. Next m lines contain edges of the graph. Each line contains a pair of space-separated integers, aiΒ bi β an edge from vertex ai to vertex bi. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.
| 2,900 |
Print the answer to the problem β the total winnings of the first player modulo a prime number p. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
|
standard output
| |
PASSED
|
f22936c4632bc4afcf0bb9e9b0ce95f4
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
int[] parent;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
parent = new int[m + 1];
int ans = 0;
for (int i = 0; i < n; i++) {
int k = in.nextInt();
if (k == 0) ans++;
int[] a = new int[k];
for (int j = 0; j < k; j++) {
a[j] = in.nextInt();
if (parent[a[j]] == 0) parent[a[j]] = a[j];
}
for (int j = 1; j < k; j++) {
union_sets(a[0], a[j]);
}
}
int leaders = 0;
for (int i = 1; i <= m; i++) {
if (parent[i] == i) leaders++;
}
if (leaders > 1) ans += leaders - 1;
out.println(ans);
}
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b)
parent[b] = a;
}
}
static class InputReader {
private StringTokenizer tokenizer;
private BufferedReader reader;
public InputReader(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private void fillTokenizer() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public String next() {
fillTokenizer();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
dc416a401d04549d1abbf81e3f58e08b
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class MyClass {
public static void main(String args[]) {
FastReader sc = new FastReader();
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<Integer> adj[] = new ArrayList[m+1];
for (int i = 1; i <=m; i++) {
adj[i] = new ArrayList<Integer>();
}
for(int i=1;i<=n;i++){
int num = sc.nextInt();
for(int j=0;j<num;j++){
int l = sc.nextInt();
adj[l].add(i);
}
}
int arr[] = new int[n+1];
int size[] = new int[n+1];
Dsu d = new Dsu(n);
for(int i=1;i<=n;i++){
arr[i]=i;
size[i]=1;
}
int f=0;
for(int i=1;i<=m;i++){
if(adj[i].size()==0){
f++;
}
if(adj[i].size()<=1){continue;}
for(int j=1;j<adj[i].size();j++){
int x = adj[i].get(j-1);
int y = adj[i].get(j);
d.union(x,y,size,arr);
}
}
if(f==m){
System.out.println(n);
return;
}
int ans = d.components()-1;
System.out.println(ans);
}
}
class Dsu{
private int numcomponents;
Dsu(int num){
numcomponents = num;
}
public int components(){
return numcomponents;
}
public int root(int arr[],int num){
int i = num;
while(i != arr[i]){
arr[i] = arr[arr[i]];
i = arr[i];
}
return i;
}
public void union(int x, int y, int size[],int arr[]){
int roota = root(arr,x);
int rootb = root(arr,y);
if(roota==rootb){return;}
if(size[roota]<size[rootb]){
arr[roota] = rootb;
size[rootb] += size[roota];
}
else{
arr[rootb] = roota;
size[roota] += size[rootb];
}
numcomponents--;
}
}
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 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
cea35ba86291d78636ab6916f393a98f
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**
* Created by akshaysharma on 28/11/16.
*/
public class Problem277A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
boolean is_z = true;
int[][] graph = new int[N][N];
ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
for (int i = 0; i < M; i++) {
arr.add(i, new ArrayList<>(M));
}
for (int i = 0; i < N; i++) {
int m = sc.nextInt();
if(m!=0){
is_z = false;
}
for (int j = 0; j < m; j++) {
int index = sc.nextInt() - 1;
ArrayList<Integer> arr_2 = arr.get(index);
for (int item : arr_2) {
if (item != i) {
graph[item][i] = 1;
graph[i][item] = 1;
}
}
arr_2.add(i);
}
}
int count = 0;
boolean[] visited = new boolean[N];
for (int i = 0; i < N; i++) {
if (!visited[i]) {
visited[i] = !visited[i];
count++;
bfs(i, graph, visited, N);
}
}
if(is_z){
System.out.print(count);
}
else{
System.out.print(count-1);
}
}
public static void bfs(int i, int[][] graph, boolean[] visited, int N) {
Queue<Integer> queue = new LinkedList<>();
queue.add(i);
while (!queue.isEmpty()) {
int u = queue.remove();
for (int v = 0; v < N; v++) {
if (graph[u][v] != 0 && !visited[v]) {
visited[v] = true;
queue.add(v);
}
}
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
76327aeea8181db35d1c3b885cf41011
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
//1: Declare/Define variables
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
StringTokenizer st = new StringTokenizer(in.readLine());
QuickUnion quick;
HashSet<Integer> parents = new HashSet();
int numOfEmployees = Integer.parseInt(st.nextToken()),
numKnownLanguages,
minCost = 0,
mainLanguage,
language;
//2: Create a quickunion
quick = new QuickUnion();
//3: Unite relevant languages
for (int i = 0; i < numOfEmployees; i++) {
//A: Get the number of languages that employee i knows
st = new StringTokenizer(in.readLine());
numKnownLanguages = Integer.parseInt(st.nextToken());
//B: Process the languages
if (numKnownLanguages >=1) {
//a: Get employee i's main language
mainLanguage = Integer.parseInt(st.nextToken());
//b: Check if employee i's main language exists
if (!quick.existsInSet(mainLanguage)) {
quick.getParents().put(mainLanguage, mainLanguage);
}
//c: Unite or set the parent of each language
for (int j = 1; j < numKnownLanguages; j++) {
language = Integer.parseInt(st.nextToken());
if (!quick.existsInSet(language)) {
quick.getParents().put(language, mainLanguage);
} else {
quick.unite(mainLanguage, language);
}
}
} else if (numKnownLanguages == 0) {
//Increase the minimum cost if an employee doesn't know any languages
minCost++;
}
}
//4: Find the number of forests
for (Map.Entry<Integer, Integer> entry : quick.getParents().entrySet()) {
parents.add(quick.root(entry.getKey()));
}
//5: Print the result
minCost += (parents.size() > 0) ? parents.size() - 1 : 0;
System.out.println(minCost);
}
}
class QuickUnion {
//Attribute(s)
private HashMap<Integer, Integer> parents;
//Constructor
public QuickUnion() {
parents = new HashMap();
}
//Behaviors
public int root(int node) {
while (node != parents.get(node)) {
node = parents.get(node);
}
return node;
}
public boolean find(int nodeA, int nodeB) {
return root(nodeA) == root(nodeB);
}
public void unite(int nodeA, int nodeB) {
//1: Find the parents of nodeA and nodeB
int parentA = root(nodeA), parentB = root(nodeB);
//2: Assign nodeB's parent as nodeA's parent
parents.put(parentA, parentB);
}
public HashMap<Integer, Integer> getParents() {
return parents;
}
public boolean existsInSet(int language) {
return parents.containsKey(language);
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
3203c11ea5c0ee3011e549b6e58d54d0
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
static InputStreamReader reader = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(reader);
static StringTokenizer st;
static undirectedGraph_ListBased languageGraph = new undirectedGraph_ListBased();
public static int getMinCost(HashMap<Integer, Node> nodes) {
//1: Declare/Define variables
int minCost = 0;
//2:Visit each node
for (Map.Entry<Integer, Node> node : nodes.entrySet()) {
if (node.getValue().status.equals("Unvisited")) {
minCost++;
depthFirstSearch_Visit(nodes, node.getValue());
}
}
//3: Return the result
return (minCost >= 1) ? minCost - 1 : 0;
}
public static void depthFirstSearch_Visit(HashMap<Integer, Node> nodes, Node chosenNode) {
//1: Change the status of the chosenNode to "visited"
chosenNode.status = "Visited";
//2: Explore the nodes connected to the chosenNode
for (Map.Entry<Integer, Node> node : chosenNode.connectionList.entrySet()) {
if (node.getValue().status.equals("Unvisited")) {
depthFirstSearch_Visit(nodes, node.getValue());
}
}
//3: Change the status of the chosenNode to "finished"
chosenNode.status = "Finished";
}
public static int insertIntoGraph(int numOfEmployees, int numOfLanguages, int minCost) throws IOException {
//1: Declare/Define variables
int language, otherLanguages;
//2: Insert languages into the graph
for (int i = 0; i < numOfEmployees; i++) {
//A: Get the number of languages that x employee knows
st = new StringTokenizer(in.readLine());
numOfLanguages = Integer.parseInt(st.nextToken());
if (numOfLanguages == 0) {
minCost++;
} else {
language = Integer.parseInt(st.nextToken());
if (!languageGraph.getNodes().containsKey(language)) {
languageGraph.getNodes().put(language, new Node(language));
}
for (int j = 1; j < numOfLanguages; j++) {
//a: Get the other languages that the employee knows
otherLanguages = Integer.parseInt(st.nextToken());
//b: Check if the language exists in the language graph
if (!languageGraph.getNodes().containsKey(otherLanguages)) {
languageGraph.getNodes().put(otherLanguages, new Node(otherLanguages));
}
//c: Connect the language to the language added first on this line(and vice versa)
if (!languageGraph.getNodes().get(language).connectionList.containsKey(otherLanguages)) {
languageGraph.getNodes().get(language).connectionList.put(otherLanguages, languageGraph.getNodes().get(otherLanguages));
languageGraph.getNodes().get(otherLanguages).connectionList.put(language, languageGraph.getNodes().get(language));
}
}
}
}
//3: Return the initial minimum cost
return minCost;
}
public static void main(String[] args) throws IOException {
//1: Declare/Define variables
st = new StringTokenizer(in.readLine());
int numOfEmployees = Integer.parseInt(st.nextToken()), minCost = 0;
//2: Insert the languages into the graph
minCost = insertIntoGraph(numOfEmployees, 0, minCost) + getMinCost(languageGraph.nodes);
//3: Print the result
System.out.println(minCost);
}
}
class Node {
//Attributes
//Can be replaced by any type of value
int value;
//Status indicates 3 states={Unvisited, Visited, Finished}
String status = "Unvisited";
Node predecessor = null;
//A list of nodes connected to this node
HashMap<Integer, Node> connectionList = new HashMap();
//Constructor(s)
Node(int value) {
this.value = value;
}
//Methods
//Getters
public int getValue() {
return value;
}
public HashMap<Integer, Node> getConnectionList() {
return connectionList;
}
//Setters
public void setValue(int value) {
this.value = value;
}
}
class undirectedGraph_ListBased {
//Attributes
HashMap<Integer, Node> nodes = new HashMap();
//Behaviors
public HashMap<Integer, Node> getNodes() {
return nodes;
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
c61ebd0268d8cb4b73c1933337caaca5
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.*;
public class Test{
static int graph[][];
static boolean visited [];
static int n,m;
public static void dfs(int i){
visited[i] = true;
for(int j=0;j<m;j++){
if(graph[i][j] == 1)
for(int k=0;k<n;k++){
if(graph[k][j] == 1){
if(!visited[k])
dfs(k);
}
}
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
graph = new int[n][m];
visited = new boolean[n];
for(int i=0;i<n;i++){
int nl = sc.nextInt();
for(int j=0;j<nl;j++) graph[i][sc.nextInt()-1] = 1;
}
int ans = -1;
boolean f = false;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(graph[i][j] == 1){
f = true;
break;
}
if(!f) ans++;
for(int i=0;i<n;i++){
if(!visited[i]){
dfs(i);
ans++;
}
}
System.out.println(ans);
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
2c6c1e757158e5fbbe07598f24b961dc
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Set;
import java.util.Stack;
public class Solution {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int ans = 0;
int n = s.nextInt();
int l = s.nextInt();
HashMap<Integer, Integer>[] lang = new HashMap[n];
int flag = 0;
for (int i = 0; i < n; i++) {
lang[i] = new HashMap<>();
int m = s.nextInt();
if (m != 0) {
flag = 1;
}
for (int j = 0; j < m; j++) {
lang[i].put(s.nextInt(), 1);
}
}
if (flag == 0) {
System.out.println(n);
return;
}
int[] vis = new int[n];
for (int i = 0; i < n; i++) {
if (vis[i] == 0) {
ans++;
dfs(lang, vis, i);
}
}
System.out.println(ans - 1);
p.flush();
p.close();
}
public static void dfs(HashMap<Integer, Integer>[] lang, int[] vis, int curr) {
if (vis[curr] == 1) {
return;
}
vis[curr] = 1;
Set<Integer> lan = lang[curr].keySet();
for (int i = 0; i < lang.length; i++) {
for (int temp : lan) {
if (lang[i].containsKey(temp)) {
dfs(lang, vis, i);
}
}
}
}
public static void union(int[] arr, int[] size, int a, int b) {
int root_a = root(arr, a);
int root_b = root(arr, b);
if (size[root_a] >= size[root_b]) {
arr[root_b] = root_a;
size[root_a] += size[root_b];
} else {
arr[root_a] = root_b;
size[root_b] += size[root_a];
}
}
public static int root(int[] arr, int i) {
while (arr[i] != i) {
i = arr[i];
}
return i;
}
public static long power(long n, long x) {
if (x == 0) {
return 1;
}
long half = power(n, x / 2);
if (x % 2 == 1) {
return half * half * n;
} else {
return half * half;
}
}
public static boolean check(ArrayList[] graph, int curr, int[] vis) {
Stack<Integer> st = new Stack<>();
long vert = 0;
long edge = 0;
vert = 1;
st.push(curr);
while (!st.isEmpty()) {
int temp = st.pop();
vis[temp] = 1;
ArrayList<Integer> nbrs = graph[temp];
edge += nbrs.size();
for (int i = 0; i < nbrs.size(); i++) {
int nbr = nbrs.get(i);
if (vis[nbr] == 0) {
st.push(nbr);
vis[nbr] = 1;
vert++;
}
}
}
if (edge == (vert * (vert - 1))) {
return true;
} else {
return false;
}
}
public static class node {
int index;
HashMap<Integer, Integer> nbrs;
node(int i) {
this.index = i;
this.nbrs = new HashMap<>();
}
}
static class pair implements Comparable<pair> {
Integer x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
public static ArrayList Divisors(long n) {
ArrayList<Long> div = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
div.add(i);
if (n / i != i)
div.add(n / i);
}
}
return div;
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
954a70530eafe01b951de3e1fce70c81
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Set;
import java.util.Stack;
public class Solution {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int ans = 0;
int n = s.nextInt();
int l = s.nextInt();
HashMap<Integer, Integer>[] lang = new HashMap[n];
int flag = 0;
for (int i = 0; i < n; i++) {
lang[i] = new HashMap<>();
int m = s.nextInt();
if (m != 0) {
flag = 1;
}
for (int j = 0; j < m; j++) {
lang[i].put(s.nextInt(), 1);
}
}
if (flag == 0) {
System.out.println(n);
return;
}
ArrayList<pair> edges = new ArrayList<>();
for (int i = 0; i < n; i++) {
HashMap<Integer, Integer> temp = lang[i];
Set<Integer> tempset = temp.keySet();
for (int k : tempset) {
for (int j = i + 1; j < n; j++) {
if (lang[j].containsKey(k)) {
edges.add(new pair(i, j));
edges.add(new pair(j, i));
}
}
}
}
int[] arr = new int[n];
int[] size = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i;
size[i] = 1;
}
for (int i = 0; i < edges.size(); i++) {
union(arr, size, edges.get(i).x, edges.get(i).y);
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] == i) {
ans++;
}
}
System.out.println(ans - 1);
p.flush();
p.close();
}
public static void union(int[] arr, int[] size, int a, int b) {
int root_a = root(arr, a);
int root_b = root(arr, b);
if (size[root_a] >= size[root_b]) {
arr[root_b] = root_a;
size[root_a] += size[root_b];
} else {
arr[root_a] = root_b;
size[root_b] += size[root_a];
}
}
public static int root(int[] arr, int i) {
while (arr[i] != i) {
i = arr[i];
}
return i;
}
public static long power(long n, long x) {
if (x == 0) {
return 1;
}
long half = power(n, x / 2);
if (x % 2 == 1) {
return half * half * n;
} else {
return half * half;
}
}
public static boolean check(ArrayList[] graph, int curr, int[] vis) {
Stack<Integer> st = new Stack<>();
long vert = 0;
long edge = 0;
vert = 1;
st.push(curr);
while (!st.isEmpty()) {
int temp = st.pop();
vis[temp] = 1;
ArrayList<Integer> nbrs = graph[temp];
edge += nbrs.size();
for (int i = 0; i < nbrs.size(); i++) {
int nbr = nbrs.get(i);
if (vis[nbr] == 0) {
st.push(nbr);
vis[nbr] = 1;
vert++;
}
}
}
if (edge == (vert * (vert - 1))) {
return true;
} else {
return false;
}
}
public static class node {
int index;
HashMap<Integer, Integer> nbrs;
node(int i) {
this.index = i;
this.nbrs = new HashMap<>();
}
}
static class pair implements Comparable<pair> {
Integer x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
public static ArrayList Divisors(long n) {
ArrayList<Long> div = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
div.add(i);
if (n / i != i)
div.add(n / i);
}
}
return div;
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
fa8683ae298808c62e90a78da4c977fb
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class A277{
static int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
while ((a & 1) == 0)
a >>= 1;
do {
while ((b & 1) == 0)
b >>= 1;
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
b = (b - a);
} while (b != 0);
return a << k;
}
static int lcm(int a,int b){
return a*b/gcd(a,b);
}
public static int binary(int arr[],int n, int a,int beg,int end){
while(beg<=end){
int mid=beg+(end-beg)/2;
if(arr[mid]==a)
return mid;
if(arr[mid]<a)
end=mid-1;
else
beg=mid+1;
}
return -1;
}
public static void sieve(int t){
boolean srr[]=new boolean[t+1];
for(int i=2;i*i<=t;i++)
{
if(!srr[i])
{
for(int k=i*i;k<=t;k+=i)
srr[k]=true;
}
}
for(int i=2;i<t+1;i++){
if(!srr[i])
System.out.println(i);
}
}
static int power(int x, int y, int p)
{
int 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 class Custom implements Comparable<Custom>{
int d;
int f;
public Custom(int d,int f){
this.d=d;
this.f=f;
}
public int compareTo(Custom t){
return this.d-t.d;
}
}
void solve() throws IOException {
int n=nextInt();
int m=nextInt();
ArrayList<HashSet<Integer>> adj=new ArrayList<>();
for(int i=0;i<m;i++)
adj.add(new HashSet<>());
int ans=0;
for(int i=0;i<n;i++){
int k=nextInt();
int x=0;
if(k!=0){
k--;
x=nextInt()-1;
adj.get(x).add(x);
}
else{ans++;}
while(k--!=0){
int y=nextInt()-1;
adj.get(x).add(y);
adj.get(y).add(x);
}
}
boolean set=true;
HashSet<Integer> visited=new HashSet<>();
for(int i=0;i<m;i++){
if(!visited.contains(i)&&adj.get(i).size()>0){
// System.out.println(i+"FDD");
if(set){
ans--;
set=false;
}
ans++;
Queue<Integer> qu=new LinkedList<>();
qu.add(i);
while(qu.size()>0){
int p=qu.poll();
visited.add(p);
for(int j:adj.get(p)){
if(!visited.contains(j)){
qu.add(j);
}
}
}
}
}
pw.println(ans);
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
String nextLine()throws IOException{
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
StringTokenizer st;
BufferedReader br;
PrintWriter pw;
Scanner sc;
void run() throws IOException {
sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
long time = System.currentTimeMillis();
solve();
System.err.println("time = " + (System.currentTimeMillis() - time));
pw.close();
}
public static void main(String[] args) throws IOException {
new A277().run();
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
bbed65e681f8df713b507271fa4720e7
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
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.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class force1 {
static PrintWriter pw = new PrintWriter(System.out);
static HashSet<Integer> h;
static HashSet<Integer> p;
static void asd(int n,int c,int[] x) {
//System.out.println(Arrays.toString(x));
if(c==n-1) {
if(isprime(x[0]+x[n-1]))
print(x);
return;
}
for(int i=2;i<=n;i++) {
if(!h.contains(i) && isprime(x[c]+i)) {
h.add(i);
x[c+1]=i;
asd(n,c+1,x);
h.remove(i);
}
}
}
static boolean isprime(int n) {
return p.contains(n);
}
static void print (int[] x) {
for(int i=0;i<x.length;i++) {
if(i==x.length-1)
pw.print(x[i]);
else
pw.print(x[i]+" ");
}
pw.println();
}
static boolean [] vis;
static boolean [] t;
static ArrayList[] a2 ;
static int c;
static int c2;
static void dfs(ArrayList[] a,int s) {
for(Object x:a[s]) {
if(!vis[(int)x]) {
vis[(int)x]=true;
dfs(a,(int)x);
}
}
}
static void helper(ArrayList[] a) {
for(int i=0;i<vis.length;i++) {
if(!vis[i]) {
vis[i]=true;
if(a2[i]!=null) {
dfs(a,i);
c++;
}
else
c2++;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
BufferedReader br= new BufferedReader(new InputStreamReader( System.in));
pw = new PrintWriter(System.out);
Scanner sc =new Scanner(System .in);
int n=sc.nextInt();
int l=sc.nextInt();
ArrayList[] a=new ArrayList[n];
a2 = new ArrayList[n];
ArrayList[] al = new ArrayList[l];
for(int i=0;i<n;i++) {
int k=sc.nextInt();
for(int j=0;j<k;j++) {
int x=sc.nextInt();
if(al[x-1]==null)
al[x-1]=new ArrayList<>();
al[x-1].add(i);
if(a2[i]==null)
a2[i]=new ArrayList<>();
a2[i].add(x-1);
}
}
// System.out.println(al[2]);
for(int i=0;i<n;i++) {
a[i] = new ArrayList<>();
if(a2[i]!=null) {
for(Object x:a2[i]) {
for(Object y:al[(int)x]) {
if(!a[i].contains(y) && (int)y!=i)
a[i].add(y);
}
}
}
}
// System.out.println(a[2]);
vis=new boolean[n];
c=0;
helper(a);
if(c==0)
System.out.println(c2);
else
System.out.println(c2+c-1);
pw.flush();
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
8c7117986ad9d02f39edafc2d726cee6
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
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.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class force1 {
static PrintWriter pw = new PrintWriter(System.out);
static HashSet<Integer> h;
static HashSet<Integer> p;
static void asd(int n,int c,int[] x) {
//System.out.println(Arrays.toString(x));
if(c==n-1) {
if(isprime(x[0]+x[n-1]))
print(x);
return;
}
for(int i=2;i<=n;i++) {
if(!h.contains(i) && isprime(x[c]+i)) {
h.add(i);
x[c+1]=i;
asd(n,c+1,x);
h.remove(i);
}
}
}
static boolean isprime(int n) {
return p.contains(n);
}
static void print (int[] x) {
for(int i=0;i<x.length;i++) {
if(i==x.length-1)
pw.print(x[i]);
else
pw.print(x[i]+" ");
}
pw.println();
}
static boolean [] vis;
static boolean [] t;
static ArrayList[] a2 ;
static int c;
static int c2;
static void dfs(ArrayList[] a,int s) {
for(Object x:a[s]) {
if(!vis[(int)x]) {
vis[(int)x]=true;
dfs(a,(int)x);
}
}
}
static void helper(ArrayList[] a) {
for(int i=0;i<vis.length;i++) {
if(!vis[i]) {
vis[i]=true;
if(a2[i]!=null) {
dfs(a,i);
c++;
}
else
c2++;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
BufferedReader br= new BufferedReader(new InputStreamReader( System.in));
pw = new PrintWriter(System.out);
Scanner sc =new Scanner(System .in);
int n=sc.nextInt();
int l=sc.nextInt();
ArrayList[] a=new ArrayList[n];
a2 = new ArrayList[n];
ArrayList[] al = new ArrayList[l];
for(int i=0;i<n;i++) {
int k=sc.nextInt();
for(int j=0;j<k;j++) {
int x=sc.nextInt();
if(al[x-1]==null)
al[x-1]=new ArrayList<>();
al[x-1].add(i);
if(a2[i]==null)
a2[i]=new ArrayList<>();
a2[i].add(x-1);
}
}
// System.out.println(al[2]);
for(int i=0;i<n;i++) {
a[i] = new ArrayList<>();
if(a2[i]!=null) {
for(Object x:a2[i]) {
for(Object y:al[(int)x]) {
if(!a[i].contains(y) && (int)y!=i)
a[i].add(y);
}
}
}
}
// System.out.println(a[2]);
vis=new boolean[n];
c=0;
helper(a);
if(c==0)
System.out.println(c2);
else
System.out.println(c2+c-1);
pw.flush();
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
8f83c2b75756d3962b3491c4d25888be
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class LearningLanguagesA
{
static HashSet<Integer> p;
static ArrayList<Integer> ap;
static ArrayList<Integer> arr[]=new ArrayList[201];
static boolean vis[]=new boolean[201];
public static void AddEdge(int src,int dest)
{
arr[src].add(dest);
arr[dest].add(src);
}
public static void main(String args[])
{
int cnt=0;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int src=0;
ArrayList<ArrayList<Integer>> ar=new ArrayList<>();
for(int i=0;i<=200;i++)
{
arr[i]=new ArrayList<>();
}
Arrays.fill(vis,false);
//=new HashSet<>();
for(int i=0;i<n;i++) {
int ct = sc.nextInt();
if (ct == 0) {
cnt++;
continue;
}
ArrayList<Integer> pr=new ArrayList<>();
for (int k = 0; k < ct; k++) {
int q = sc.nextInt();
pr.add(q);
}
ar.add(pr);
src++;
}
p=new HashSet<>();
int temp=0;
for(ArrayList<Integer> a:ar)
{
if(!a.isEmpty())
for(Integer ap:a)
{
// System.out.println("Adding edge from "+temp+" to"+ap);
p.add(ap+src);
AddEdge(temp,ap+src);
}
p.add(temp);
temp++;
}
ap=new ArrayList<>(p);
int ans=-1;
while(ap.size()>0)
{
//System.out.println("Elements remaining "+ap.size());
ans++;
if(ans==-1)
{
solve(0);
}
else
{
solve(ap.get(0));
}
}
if(ans!=-1)
System.out.println(ans+cnt);
else
System.out.println(cnt);
}
public static void solve(int src)
{
if(!arr[src].isEmpty())
for(Integer a:arr[src])
{
if(!vis[a])
{
vis[a]=true;
//int temp=a;
ap.remove(a);
solve(a);
}
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
f2ae0ef8389c9230367736cd88878650
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.io.*;
import java.lang.*;
public class graph1
{
public static int n;
public static ArrayList<Integer> adj[];
public static ArrayList<Integer> adj2[];
public static ArrayList<Integer> child[];
public static boolean[] vis;
public static ArrayList<Integer> pair;
public static int[] par;
public static int[] level;
public static int[] set;
public static int[] a;
public static int[] ans;
public static int[] init;
public static int[] goal;
public static int count;
public static long INF = Long.MAX_VALUE/100;
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
// n = in.nextInt();
n = in.nextInt();
int m = in.nextInt();
//int m = in.nextInt();
//int s = in.nextInt();
//children = new ArrayList[n+1];
vis = new boolean[n+10005];
// par = new int[n+1];
//set = new int[n+1];
//level = new int[n+1];
for(int i=0; i<n+10000; i++)
{
vis [i] = false;
//par[i] = -1;
}
adj = new ArrayList[n+10005];
for(int i=0;i<=n+1000;i++)
{
adj[i] = new ArrayList<Integer>();
}
int c2 = 0;
for(int i=1; i<=n; i++)
{
int u = in.nextInt();
if(u==0)
c2++;
for(int j=0; j<u; j++)
{
int v = in.nextInt()+101;
adj[i].add(v);
adj[v].add(i);
}
}
int count = 0;
for(int i=1; i<=n; i++)
{
if(!vis[i] && adj[i].size()>0) {
bfs(i);
count++;
}
}
//pw.println(c2);
if(count==0)
count++;
pw.println(c2+count-1);
//code ends....
pw.flush();
pw.close();
}
public static void bfs(int x)
{
Queue<Integer> queue = new LinkedList<>();
queue.add(x);
vis[x] = true;
while(!queue.isEmpty())
{
int last = queue.poll();
//System.out.println(last);
for(int p : adj[last])
{
if(!vis[p])
{
queue.add(p);
vis[p] = true;
}
}
}
}
public static int fbfs(int x)
{
Queue<Integer> queue = new LinkedList<>();
queue.add(x);
//level[x] = 1;
int c = 0;
//parent[x] = 0;
//boolean[] vis = new boolean[n+1];
vis[x] = true;
while(!queue.isEmpty())
{
int last = queue.poll();
for(int p : adj[last])
{
if(!vis[p])
{
// children[last].add(a);
queue.add(p);
// System.out.println(p+" ");
vis[p] = true;
c++;
// parent[a] = last;
//level[a] = level[last]+1;
}
}
}
return c;
}
/*
public static void fun(int s)
{
if(vis[s])
return;
set[s] = count;
vis[s] = true;
fun(a[s]);
}
public static int dfs(int s)
{
for(int i=0; i<=n; i++)
{
vis [i] = false;
par[i] = -1;
level[i] = 0;
}
Stack<Integer> stk = new Stack<>();
stk.add(s);
par[s] = 0;
vis[s] = true;
while(!stk.isEmpty())
{
int last = stk.pop();
/// System.out.println(last);
boolean f = false;
for(int a : adj[last])
{
if(!vis[a])
{
//child[last].add(a);
stk.add(a);
par[a] = last;
level[a] = level[last] +1;
vis[a] = true;
}
}
}
int max = 0;
int maxi = 0;
for(int i=1; i<=n; i++)
{
if(level[i]>max)
{
max = level[i];
maxi = i;
}
}
return maxi;
}
*/
/* public static void dfsrec(int s)
{
if(vis[s])
return;
vis[s] = true;
int k = 0;
for(int p: adj[s])
{
boolean f2 = false;
if(!vis[p])
{
k++;
dfsrec(p);
}
}
par[s] = k;
}
public static int find(int i) {
if(par[i]==-1)
return i;
else
return find(par[i]);
}
public static void union( int x, int y)
{
int xset = find( x);
int yset = find(y);
par[xset] = yset;
}
*/
/* public static boolean cycle(HashSet<pair> set) {
for(pair p: set)
{
int x = find(p.x);
int y = find(p.y);
if(x==y)
return true;
union(x, y);
}
return false;
}
*/
static class pair implements Comparable<pair>
{
Long x;
Integer y;
pair(long adj,int y)
{
this.x=adj;
this.y=y;
}
public int compareTo(pair o) {
//int result = x.compareTo(o.x);
//if(result==0)
int result = -y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
static class triplet implements Comparable<triplet>
{
Integer x,y,z;
triplet(int x,int y,int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public int compareTo(triplet o)
{
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
if(result==0)
result = z.compareTo(o.z);
return result;
}
public boolean equlas(Object o)
{
if(o instanceof triplet)
{
triplet p = (triplet)o;
return x==p.x && y==p.y && z==p.z;
}
return false;
}
public String toString()
{
return x+" "+y+" "+z;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode() + new Long(z).hashCode();
}
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
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;
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
70c5d1097dad40427faf40dc86beb0f7
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp=br.readLine().trim().split(" ");
int V=Integer.parseInt(temp[0]);
int totalLanguages=Integer.parseInt(temp[1]);
ArrayList<ArrayList<Integer>> languagesKnown=new ArrayList<>();
for(int i=0;i<=V;i++)
{
languagesKnown.add(new ArrayList<>());
}
for(int i=1;i<=V;i++)
{
temp=br.readLine().trim().split(" ");
int numLanguagesKnown=Integer.parseInt(temp[0]);
for(int j=1;j<=numLanguagesKnown;j++)
{
int languageID=Integer.parseInt(temp[j]);
languagesKnown.get(i).add(languageID);
}
}
System.out.println(minCost(languagesKnown,totalLanguages));
}
public static int minCost(ArrayList<ArrayList<Integer>> languagesKnown,int totalLanguages)
{
int count=0;
int V=languagesKnown.size()-1;
for(int i=1;i<=V;i++)
{
if(languagesKnown.get(i).size()==0)
{
count++;
}
}
if(count==V)
{
return V;
}
DSU graph=new DSU(V);
int[] personKnowingLanguage=new int[totalLanguages+1];
for(int i=1;i<=V;i++)
{
for(int j=0;j<languagesKnown.get(i).size();j++)
{
int languageID=languagesKnown.get(i).get(j);
if(personKnowingLanguage[languageID]!=0)
{
graph.makeUnion(personKnowingLanguage[languageID],i);
}
else{
personKnowingLanguage[languageID]=i;
}
}
}
return graph.numComponents()-1;
}
}
class DSU{
private int[] parent;
private int[]size;
public DSU(int V)
{
parent=new int[V+1];
size=new int[V+1];
for(int i=1;i<=V;i++)
{
parent[i]=i;
size[i]=1;
}
}
public int getParent(int vertex)
{
int temp=vertex;
while(parent[vertex]!=vertex)
{
vertex=parent[vertex];
}
parent[temp]=vertex;
return vertex;
}
public void makeUnion(int u,int v)
{
int parent1=getParent(u);
int parent2=getParent(v);
if(parent1==parent2)
{
return;
}
int size1=size[parent1];
int size2=size[parent2];
if(size2<size1)
{
parent[parent2]=parent1;
size[parent1]+=size[parent2];
}
else{
parent[parent1]=parent2;
size[parent2]+=size[parent1];
}
}
public int numComponents()
{
int count=0;
int V=this.parent.length-1;
for(int i=1;i<=V;i++)
{
if(parent[i]==i)
{
count++;
}
}
return count;
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
28c585b2d15d2108675239622b572243
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class Solution {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp=br.readLine().trim().split(" ");
int V=Integer.parseInt(temp[0]);
int E=Integer.parseInt(temp[1]);
ArrayList<ArrayList<Integer>> languagesKnown=new ArrayList<>();
for(int i=0;i<=V;i++)
{
languagesKnown.add(new ArrayList<>());
}
for(int i=1;i<=V;i++)
{
temp=br.readLine().trim().split(" ");
int numLanguagesKnown=Integer.parseInt(temp[0]);
for(int j=1;j<=numLanguagesKnown;j++)
{
int languageID=Integer.parseInt(temp[j]);
languagesKnown.get(i).add(languageID);
}
}
System.out.println(minCost(languagesKnown));
}
public static int minCost(ArrayList<ArrayList<Integer>> languagesKnown)
{
int count=0;
int V=languagesKnown.size()-1;
for(int i=1;i<=V;i++)
{
if(languagesKnown.get(i).size()==0)
{
count++;
}
}
if(count==V)
{
return V;
}
DSU graph=new DSU(V);
for(int i=1;i<=V;i++)
{
for(int j=0;j<languagesKnown.get(i).size();j++)
{
int languageID=languagesKnown.get(i).get(j);
for(int k=1;k<i;k++)
{
for(int l=0;l<languagesKnown.get(k).size();l++)
{
if(languagesKnown.get(k).get(l)==languageID)
{
graph.makeUnion(k,i);
}
}
}
}
}
return graph.numComponents()-1;
}
}
class DSU{
private int[] parent;
private int[]size;
public DSU(int V)
{
parent=new int[V+1];
size=new int[V+1];
for(int i=1;i<=V;i++)
{
parent[i]=i;
size[i]=1;
}
}
public int getParent(int vertex)
{
int temp=vertex;
while(parent[vertex]!=vertex)
{
vertex=parent[vertex];
}
parent[temp]=vertex;
return vertex;
}
public void makeUnion(int u,int v)
{
int parent1=getParent(u);
int parent2=getParent(v);
if(parent1==parent2)
{
return;
}
int size1=size[parent1];
int size2=size[parent2];
if(size2<size1)
{
parent[parent2]=parent1;
size[parent1]+=size[parent2];
}
else{
parent[parent1]=parent2;
size[parent2]+=size[parent1];
}
}
public int numComponents()
{
int count=0;
int V=this.parent.length-1;
for(int i=1;i<=V;i++)
{
if(parent[i]==i)
{
count++;
}
}
return count;
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
12f14dee9f35ec686b0d04f6cea6e38a
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class A277
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().trim().split(" ");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
HashMap<Integer,ArrayList<Integer>> emp=new HashMap<>();
HashMap<Integer,ArrayList<Integer>> lang=new HashMap<>();
HashMap<Integer,ArrayList<Integer>> map=new HashMap<>();
int id[]=new int[n+1];
for(int i=1;i<=m;i++)
{
lang.put(i,new ArrayList<Integer>());
//map.put(i,new ArrayList<Integer>());
}
int c=0;
for(int i=1;i<=n;i++)
{
emp.put(i,new ArrayList<Integer>());
map.put(i,new ArrayList<Integer>());
s=br.readLine().trim().split(" ");
int k=Integer.parseInt(s[0]);
if(k!=0)
{
for(int j=1;j<=k;j++)
{
emp.get(i).add(Integer.parseInt(s[j]));
lang.get(Integer.parseInt(s[j])).add(i);
}
}
else
c++;
}
//System.out.println(emp+" "+lang);
int arr[][]=new int[n+1][m+1];
for(int i:emp.keySet())
{
//map.put(i,new ArrayList<Integer>());
List<Integer> list=emp.get(i);
for(int j:list)
{
for(int k:lang.get(j))
{
if(arr[k][j]==0)
{
if(i!=k)
{
map.get(i).add(k);
map.get(k).add(i);
}
arr[k][j]=1;
}
}
}
//System.out.println(map);
}
//System.out.println(map);
boolean visit[]=new boolean[n+1];int conn=0;
for(int i=1;i<=n;i++)
{
if(!visit[i])
{
conn++;
visit[i]=true;
Stack st=new Stack();
st.push(i);
while(st.size()!=0)
{
int a=(Integer)st.pop();
for(int j:map.get(a))
{
if(!visit[j])
{
visit[j]=true;
st.push(j);
}
}
}
}
}
if(c==n)
System.out.println(c);
else
System.out.println(conn-1);
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
4f9b4f82e62778e81909ad991c5205a2
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
/*
.
.
.
.
.
.
.
some constants
.
*/
static boolean visited[];
static ArrayList<Integer> list[];
static void dfs(int current){
visited[current]=true;
Iterator itr=list[current].listIterator();
int temp;
while(itr.hasNext()){
temp=(int)itr.next();
if(!visited[temp])
dfs(temp);
}
}
/*
.
.
.
if any
.
.
*/
public static void main(String[] args) throws IOException{
/*
.
.
.
.
.
.
*/
int n=ni();
int m=ni();
int i,j,k,l;
boolean lang[][]=new boolean[n+1][m+1];
list=new ArrayList[n+1];
for(i=0;i<=n;i++){
list[i]=new ArrayList<>();
}
int temp;
for(i=1;i<=n;i++){
temp=ni();
for(j=1;j<=temp;j++){
lang[i][ni()]=true;
lang[i][0]=true;
}
}
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(lang[i][j]){
for(k=i+1;k<=n;k++){
if(lang[k][j]){
list[i].add(k);
list[k].add(i);
}
}
}
}
}
/*
for(i=1;i<=n;i++){
sop(list[i]);
}
*/
int count=0;
j=1;
visited=new boolean[n+1];
for(i=1;i<=n;i++){
if(!visited[i]){
dfs(i);
count++;
}
}
boolean flag=false;
for(i=1;i<=n;i++){
flag |= lang[i][0];
}
if(!flag)
count++;
sop(count-1);
/*
.
.
.
.
.
.
.
*/
}
/*
temporary functions
.
.
*/
/*
fuctions
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
abcdefghijklmnopqrstuvwxyz
.
.
.
.
.
.
*/
static int modulo(int j,int m){
if(j<0)
return m+j;
if(j>=m)
return j-m;
return j;
}
static final int mod=1000000007;
static final double eps=1e-8;
static final long inf=100000000000000000L;
static final boolean debug=true;
static Reader in=new Reader();
static StringBuilder ansa=new StringBuilder();
static long powm(long a,long b,long m){
long an=1;
long c=a;
while(b>0){
if(b%2==1)
an=(an*c)%m;
c=(c*c)%m;
b>>=1;
}
return an;
}
static Random rn=new Random();
static void sop(Object a){System.out.println(a);}
static int ni(){return in.nextInt();}
static int[] nia(int n){int a[]=new int[n];for(int i=0;i<n;i++)a[i]=ni();return a;}
static long nl(){return in.nextLong();}
static long[] nla(int n){long a[]=new long[n];for(int i=0; i<n; i++)a[i]=nl();return a;}
static String ns(){return in.next();}
static String[] nsa(int n){String a[]=new String[n];for(int i=0; i<n; i++)a[i]=ns();return a;}
static double nd(){return in.nextDouble();}
static double[] nda(int n){double a[]=new double[n];for(int i=0;i<n;i++)a[i]=nd();return a;}
static class Reader{
public BufferedReader reader;
public StringTokenizer tokenizer;
public Reader(){
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());
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
| |
PASSED
|
a1a717080121e00ffd5c53e4a2e58994
|
train_000.jsonl
|
1362065400
|
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
|
256 megabytes
|
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
/**
* @author pvasilyev
* @since 30 Mar 2017
*/
public class A277 {
public static void main(String[] args) throws IOException {
final Scanner reader = new Scanner(new InputStreamReader(System.in));
final PrintWriter writer = new PrintWriter(System.out);
solve(reader, writer);
writer.close();
reader.close();
}
private static void solve(final Scanner reader, final PrintWriter writer) {
final int n = reader.nextInt();
final int m = reader.nextInt();
final HashMap<Integer, List<Integer>> g = new HashMap<>();
for (int i = 1; i <= n; ++i) {
g.put(i, new ArrayList<>());
final int mi = reader.nextInt();
for (int j = 1; j <= mi; ++j) {
final int lang = reader.nextInt();
g.get(i).add(-lang);
if (!g.containsKey(-lang)) {
g.put(-lang, new ArrayList<>());
}
g.get(-lang).add(i);
}
}
final HashSet<Integer> visited = new HashSet<>();
final HashMap<Integer, List<Integer>> ccs = new HashMap<>();
int c = 0;
for (int i = 1; i <= n; ++i) {
if (!visited.contains(i)) {
c++;
dfs(visited, g, i, ccs, c);
}
}
int nullable = 0;
for (Map.Entry<Integer, List<Integer>> integerListEntry : ccs.entrySet()) {
if (integerListEntry.getValue().size() == 1) {
nullable++;
}
}
final int nonNullable = ccs.size() - nullable;
writer.println(nullable + (nonNullable == 0 ? nonNullable : nonNullable - 1));
}
private static void dfs(final HashSet<Integer> visited, final HashMap<Integer, List<Integer>> g,
final int start, final HashMap<Integer, List<Integer>> ccs, final int c) {
visited.add(start);
if (!ccs.containsKey(c)) {
ccs.put(c, new ArrayList<>());
}
//if (start > 0) {
ccs.get(c).add(start);
//}
for (Integer outbound : g.get(start)) {
if (!visited.contains(outbound)) {
dfs(visited, g, outbound, ccs, c);
}
}
}
}
|
Java
|
["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"]
|
2 seconds
|
["0", "2", "1"]
|
NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.
|
Java 8
|
standard input
|
[
"dsu",
"dfs and similar"
] |
e2836276aee2459979b232e5b29e6d57
|
The first line contains two integers n and m (2ββ€βn,βmββ€β100) β the number of employees and the number of languages. Then n lines follow β each employee's language list. At the beginning of the i-th line is integer ki (0ββ€βkiββ€βm) β the number of languages the i-th employee knows. Next, the i-th line contains ki integers β aij (1ββ€βaijββ€βm) β the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.
| 1,400 |
Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.