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 | 982da79722691a0314ce7cce6d7a3f32 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private InputStream reader;
private byte buffer[];
private int cnt, index;
public InputReaderFast(InputStream stream){
reader = stream;
buffer = new byte[2345];
cnt = 0;
index = 0;
}
public int read(){
if(index>=cnt){
try {
cnt = reader.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
index = 0;
}
if(cnt==-1) throw new InputMismatchException();
return buffer[index++];
}
public int nextInt(){
int sign = 1, x = 0, c = read();
while (c<=32) c=read();
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | b81b0566c99f72527184a8bd82b954c6 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private InputStream reader;
private byte buffer[];
private int cnt, index;
public InputReaderFast(InputStream stream){
reader = stream;
buffer = new byte[1<<10];
cnt = 0;
index = 0;
}
public int read(){
if(index>=cnt){
try {
cnt = reader.read(buffer);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
index = 0;
if(cnt==-1) return -1;
}
return buffer[index++];
}
public int nextInt(){
int sign = 1, x = 0, c;
do{ c=read(); }while (c<=32);
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 3e2d72c45184d8834e0a74a4135d7120 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.math.BigDecimal;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.util.StringTokenizer;
import java.math.BigInteger;
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);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReader{
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
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 int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | c756b2109c4977c3b80a68b7e3e6a552 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private DataInputStream reader;
private byte buffer[];
private int cnt, index;
public InputReaderFast(InputStream stream){
reader = new DataInputStream(stream);
buffer = new byte[1<<15];
cnt = 0;
index = 0;
}
public int read(){
if(index>=cnt){
try {
cnt = reader.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
index = 0;
}
if(cnt==-1) throw new InputMismatchException();
return buffer[index++];
}
public int nextInt(){
int sign = 1, x = 0, c = read();
while (c<=32) c=read();
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 3288f965096b8af9e13aa5202d697b30 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.util.AbstractCollection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.math.BigInteger;
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);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int c[] = new int[n];
int i,j;
int w[] = new int[n];
for(i=0;i<n;++i) w[a[i]-1] = i;
for(i=0;i<n;++i) c[n-i-1] = w[b[i]-1];
out.writeln(ArrayUtils.longestIncreasingSubsequence(c));
}
}
class InputReader{
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next(){
while(tokenizer==null || !tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(reader.readLine());
}catch(Exception e){
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public int[] nextIntArray(int size){
int array[] = new int[size];
for(int i=0; i<size; ++i) array[i] = nextInt();
return array;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void close(){
out.close();
}
public void writeln(Object ... o){
for(Object x : o) out.print(x);
out.println();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
TreeSet<Pair<Integer,Integer>> t = new TreeSet<Pair<Integer, Integer>>();
for(i=0;i<n;++i) t.add(new Pair<Integer, Integer>(a[i],-i));
DataStruct.SegmentTreeMax s = new DataStruct.SegmentTreeMax(new int[n+1]);
for(Pair<Integer,Integer> p : t.toArray(new Pair[t.size()])){
int x = p.first;
i = -p.second;
s.set(i+1, s.getMax(0,i)+1);
}
return s.getMax(0,n);
}
}
class Pair<X,Y> implements Comparable<Pair>{
public X first;
public Y second;
public Pair(X _first, Y _second){
first = _first;
second = _second;
}
public int compareTo(Pair o){
int cmp1 = ((Comparable)first).compareTo(o.first);
int cmp2 = ((Comparable)second).compareTo(o.second);
return cmp1==0 ? cmp2 : cmp1;
}
}
class DataStruct {
public static class SegmentTreeMax{
int t[];
int d;
public SegmentTreeMax(int n){
for(d=1; d<n; d<<=1);
t = new int[d<<1];
}
public SegmentTreeMax(int[] array){
int n = array.length, i;
for(d=1; d<n; d<<=1);
t = new int[d<<1];
for(i=d; i<n+d; ++i) t[i] = array[i-d];
for(i=n+d; i<d+d; ++i) t[i] = Integer.MIN_VALUE;
for(i=d-1; i>0; --i) t[i] = Math.max(t[i*2], t[i*2+1]);
}
public void set(int index, int value){
index+=d;
t[index] = value;
for(index>>=1; index>0 && t[index]<value; index>>=1) t[index] = value;
}
public int getMax(int left, int right){
int res = Integer.MIN_VALUE;
for(left+=d, right+=d; left<=right; left>>=1, right>>=1){
if((left&1)==1){
res = Math.max(res, t[left]); ++left;
}
if((right&1)==0){
res = Math.max(res, t[right]); --right;
}
}
return res;
}
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 410744c6e6fcc570d84ee60adc99bd76 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.math.BigInteger;
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);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int c[] = new int[n];
int i,j;
int w[] = new int[n];
for(i=0;i<n;++i) w[a[i]-1] = i;
for(i=0;i<n;++i) c[n-i-1] = w[b[i]-1];
out.writeln(ArrayUtils.longestIncreasingSubsequence(c));
}
}
class InputReader{
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next(){
while(tokenizer==null || !tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(reader.readLine());
}catch(Exception e){
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public int[] nextIntArray(int size){
int array[] = new int[size];
for(int i=0; i<size; ++i) array[i] = nextInt();
return array;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void close(){
out.close();
}
public void writeln(Object ... o){
for(Object x : o) out.print(x);
out.println();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | b067ae9367917fdc447133197af041e8 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private InputStream reader;
private byte buffer[];
private int cnt, index;
public InputReaderFast(InputStream stream){
reader = stream;
buffer = new byte[1<<11];
cnt = 0;
index = 0;
}
public int read(){
if(index>=cnt){
try {
cnt = reader.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
index = 0;
}
if(cnt==-1) throw new InputMismatchException();
return buffer[index++];
}
public int nextInt(){
int sign = 1, x = 0, c = read();
while (c<=32) c=read();
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 8d4581895dc6a9f24608eaeba13220bd | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private InputStream reader;
private byte buffer[];
private int cnt, index;
public InputReaderFast(InputStream stream){
reader = stream;
buffer = new byte[10000];
cnt = 0;
index = 0;
}
public int read(){
if(index>=cnt){
try {
cnt = reader.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
index = 0;
}
if(cnt==-1) throw new InputMismatchException();
return buffer[index++];
}
public int nextInt(){
int sign = 1, x = 0, c = read();
while (c<=32) c=read();
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 0cc6b6190ef8ac52b40db77b40f31a53 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private DataInputStream reader;
private byte buffer[];
private int cnt, index;
public InputReaderFast(InputStream stream){
reader = new DataInputStream(stream);
buffer = new byte[1<<12];
cnt = 0;
index = 0;
}
public int read(){
if(index>=cnt){
try {
cnt = reader.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
index = 0;
}
if(cnt==-1) throw new InputMismatchException();
return buffer[index++];
}
public int nextInt(){
int sign = 1, x = 0, c = read();
while (c<=32) c=read();
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 39586a18a34f77e8f15e6f0e886588f8 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
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;
InputReaderFast in = new InputReaderFast(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReaderFast in, OutputWriter out) {
int i,n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int w[] = new int[n+1];
for(i=0;i<n;++i) w[a[i]] = i;
for(i=0;i<n;++i) a[n-i-1] = w[b[i]];
out.writeln(ArrayUtils.longestIncreasingSubsequence(a));
}
}
class InputReaderFast {
private BufferedReader reader;
public InputReaderFast(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
}
public int read(){
try {
return reader.read();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return -1;
}
public int nextInt(){
int sign = 1, x = 0, c;
do{ c=read(); }while (c<=32);
if(c=='-'){ c=read(); sign=-1; }
while (c>='0' && c<='9'){
x = x*10 + c-'0';
c = read();
}
return x*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void write(Object ... o){
for(Object x : o) out.print(x);
}
public void writeln(Object ... o){
write(o);
out.println();
}
public void close(){
out.close();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 5d62e9d79eb09a880ff280cfabe7babb | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.TreeMap;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.util.TreeSet;
import java.io.Writer;
import java.math.BigInteger;
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);
OutputWriter out = new OutputWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
}
class TaskD {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int a[] = in.nextIntArray(n);
int b[] = in.nextIntArray(n);
int c[] = new int[n];
int i,j;
int w[] = new int[n];
for(i=0;i<n;++i) w[a[i]-1] = i;
for(i=0;i<n;++i) c[n-i-1] = w[b[i]-1];
out.writeln(ArrayUtils.longestIncreasingSubsequence(c));
}
}
class InputReader{
InputStream in;
byte buffer[] = new byte[1<<12];
int cur, end;
public InputReader(InputStream stream){
in = stream;
}
public int read(){
if(end<0) throw new InputMismatchException();
if(cur>=end){
cur = 0;
try{
end = in.read(buffer);
}catch(IOException e){
throw new InputMismatchException();
}
if(end<=0) return -1;
}
return buffer[cur++];
}
public boolean isSpace(int c){
return c==' ' || c=='\n' || c=='\r' || c=='\t' || c==-1;
}
public int nextInt(){
int c = read(), res = 0, sign = 1;
while(isSpace(c)) c = read();
if(c=='-'){
sign = -1;
c = read();
}
do{
if(c>'9' || c<'0') throw new InputMismatchException();
res = res*10 + (c-'0');
c = read();
}while(!isSpace(c));
return res*sign;
}
public int[] nextIntArray(int n){
int res[] = new int[n];
for(int i=0;i<n;++i) res[i] = nextInt();
return res;
}
}
class OutputWriter{
private PrintWriter out;
public OutputWriter(Writer out){
this.out = new PrintWriter(out);
}
public OutputWriter(OutputStream out){
this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
}
public void close(){
out.close();
}
public void writeln(Object ... o){
for(Object x : o) out.print(x);
out.println();
}
}
class ArrayUtils{
public static int longestIncreasingSubsequence(int a[]){
int i, n = a.length;
int t[] = new int[n];
int d[] = new int[n];
for(i=0;i<n;++i) t[i] = Integer.MAX_VALUE;
int ans = 0;
for(i=0;i<n;++i){
int j = Arrays.binarySearch(t, a[i]);
if(j<0) j=-j-1;
t[j] = a[i];
d[j] = (j>0?d[j-1]:0)+1;
ans = Math.max(ans, d[j]);
}
return ans;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 09cd6ce5afb3c0c4a445ff94eb21dd55 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | //package manthan;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
public class D {
IntReader in;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] a = new int[n];
int[] b = new int[n];
int[] f = new int[n];
for(int i = 0;i < n;i++){
a[i] = ni();
}
for(int i = 0;i < n;i++){
b[ni()-1] = i;
}
for(int i = 0;i < n;i++){
f[i] = b[a[i]-1]+1;
}
tr(f);
int ret = 0;
int[] h = new int[n + 1];
for(int i = 0;i < n;i++){
int ind = Arrays.binarySearch(h, 0, ret + 1, -f[i]);
if(ind < 0){
ind = -ind-2;
h[ind+1] = -f[i];
if(ind >= ret)ret++;
}
}
out.println(ret+1);
}
void run() throws Exception
{
in = INPUT.isEmpty() ? new IntReader(System.in) : new IntReader(new ByteArrayInputStream(INPUT.getBytes()));
out = new PrintWriter(System.out);
solve();
out.flush();
}
public static void main(String[] args) throws Exception
{
new D().run();
}
public class IntReader {
private InputStream is;
public IntReader(InputStream is)
{
this.is = is;
}
public int nui()
{
try {
int num = 0;
while((num = is.read()) != -1 && (num < '0' || num > '9'));
num -= '0';
while(true){
int b = is.read();
if(b == -1)return num;
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return num;
}
}
} catch (IOException e) {
}
return -1;
}
public int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b == -1)return minus ? -num : num;
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public String nl()
{
try{
int b = 0;
StringBuilder sb = new StringBuilder();
while((b = is.read()) != -1 && (b == '\r' || b == '\n'));
if(b == -1)return "";
sb.append((char)b);
while(true){
b = is.read();
if(b == -1)return sb.toString();
if(b == '\r' || b == '\n')return sb.toString();
sb.append((char)b);
}
} catch (IOException e) {
}
return "";
}
}
int ni() { return in.ni(); }
int nui() { return in.nui(); }
String nl() { return in.nl(); }
void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 7186a45beae86e04319385fbc6efa264 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.io.*;
public class D {
static class Parser {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Parser(InputStream in) {
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public char nextChar() throws Exception {
byte c = read();
while (c <= ' ')
c = read();
return (char) c;
}
public int nextLong() throws Exception {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = c == '-';
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws Exception {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws Exception {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
}
static int[] ar;
public static int LIS() {
int[] M = new int[ar.length + 1];
Arrays.fill(M, ar.length);
int L = 0, l, h, mid;
for (int i = 0; i < ar.length; i++) {
l = 1;
h = M.length;
while (l < h) {
mid = l + (h - l) / 2;
if (M[mid] != ar.length && ar[M[mid]]< ar[i]) {
l = mid + 1;
} else
h = mid;
}
if (l == M.length || M[l] == ar.length || ar[M[l]] >= ar[i])
l--;
if (l == L || ar[i] < ar[M[l + 1]]) {
M[l + 1] = i;
L = Math.max(L, l + 1);
}
}
return L;
}
public static void main(String[] args) throws Exception {
Parser sc = new Parser(System.in);
int n = sc.nextLong();
ar = new int[n];
HashMap<Integer, Integer> map=new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
map.put(sc.nextLong(), i);
}
for (int i = 0; i < n; i++) {
ar[i] = -1 * map.get(sc.nextLong());
}
System.out.println(LIS());
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | cf295249b3eaaa92740ce3b2f6da5f90 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.*;
import java.util.*;
public class Opticheskiieksperiment {
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer in;
static PrintWriter out = new PrintWriter(System.out);
public static String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static int[] Tree;
public static void update(int v, int x) {
Tree[v] = x;
v /= 2;
while (v > 0) {
Tree[v] = Math.max(Tree[2 * v], Tree[2 * v + 1]);
v /= 2;
}
}
public static int RMQ(int v, int l, int r, int a, int b) {
if (l > b || r < a)
return 0;
if (l >= a && r <= b)
return Tree[v];
int m = (l + r) / 2;
int z1 = RMQ(2 * v, l, m, a, b);
int z2 = RMQ(2 * v + 1, m + 1, r, a, b);
return Math.max(z1, z2);
}
public static void main(String[] args) throws IOException {
int n = nextInt();
int k = 1;
while (k < n)
k *= 2;
Tree = new int[2 * k];
int[] x = new int[n];
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[nextInt() - 1] = i;
}
for (int i = 0; i < n; i++) {
int b = nextInt() - 1;
x[a[b]] = i;
}
int ans = 0;
int z = 0;
for (int i = 0; i < n; i++) {
z = RMQ(1, 1, k, x[i] + 1, k);
ans = Math.max(ans, z + 1);
update(x[i] + k, z + 1);
}
out.println(ans);
out.close();
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 8694aa997fe97886631dd9144e3aa2af | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | //package manthan;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class D {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static String nextToken() throws IOException{
while (st==null || !st.hasMoreTokens()){
String s = bf.readLine();
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
static int nextInt() throws IOException{
return Integer.parseInt(nextToken());
}
static String nextStr() throws IOException{
return nextToken();
}
static double nextDouble() throws IOException{
return Double.parseDouble(nextToken());
}
static int search(int l, int r, int d[], int x){
if (r < l)
return 1000000000;
int a = (l+r)/2;
if (d[a] < x)
return Math.min(a, search(l, a-1, d, x));
else
return search(a+1, r, d, x);
}
public static void main(String[] args) throws IOException{
int n = nextInt(),
a[] = new int[n],
b[] = new int[n],
c[] = new int[n],
d[] = new int[n];
for (int i=0; i<n; i++){
a[i] = nextInt()-1;
c[a[i]] = i;
}
for (int i=0; i<n; i++){
b[i] = nextInt()-1;
d[i] = c[b[i]];
}
int res[] = new int[n+1];
Arrays.fill(res, -1000000000);
res[0] = 1000000000;
for (int i=0; i<n; i++)
{
int j = search(0, res.length-1, res, d[i])-1;
if (res[j] > d[i] && d[i] > res[j+1])
res[j+1] = d[i];
}
for (int i=n; i>0; i--)
if (Math.abs(res[i]) != 1000000000){
out.println(i);
break;
}
out.flush();
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 84fe18716cf084ee0e8291ec7a4d88d1 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
/**
* Created by IntelliJ IDEA.
* User: piyushd
* Date: 3/13/11
* Time: 9:43 PM
* To change this template use File | Settings | File Templates.
*/
public class Task4 {
@SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
private final InputReader in;
private final PrintWriter out;
private final boolean testMode;
private void solve() {
int n = in.readInt();
int[] x = new int[n];
int[] y = new int[n];
int[] M = new int[n + 1];
for(int i = 0; i < n; i++){
x[in.readInt() - 1] = i;
}
for(int i = 0; i < n; i++){
y[i] = x[in.readInt() - 1];
}
M[1] = 0;
int L = 1;
int low, high, mid;
for(int i = 1; i < n; i++){
if(y[M[L]] > y[i]){
L++;
M[L] = i;
continue;
}
low = 1; high = L;
while(low < high){
mid = low + (high - low) / 2;
if(y[M[mid]] > y[i]) low = mid + 1;
else high = mid;
}
if(y[i] > y[M[low]]){
M[low] = i;
L = Math.max(L, low);
}
}
System.out.println(L);
}
private static List<Test> createTests() {
List<Test> tests = new ArrayList<Test>();
tests.add(new Test("5\n" +
"1 4 5 2 3\n" +
"3 4 2 1 5", "3"));
tests.add(new Test("3\n" +
"3 1 2\n" +
"2 3 1", "2"));
tests.add(new Test(generateTest(), "1000000"));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
// tests.add(new Test("", ""));
return tests;
}
private static String generateTest() {
StringBuilder builder = new StringBuilder();
builder.append("1000000\n");
for (int i = 1; i <= 1000000; i++)
builder.append(i).append(" ");
builder.append("\n");
for (int i = 1000000; i >= 1; i--)
builder.append(i).append(" ");
builder.append("\n");
return builder.toString();
}
private void run() {
//noinspection InfiniteLoopStatement
// while (true)
// int testCount = in.readInt();
// for (int i = 0; i < testCount; i++)
solve();
exit();
}
private Task4() {
@SuppressWarnings({"UnusedDeclaration"})
String id = getClass().getName().toLowerCase();
//noinspection EmptyTryBlock
try {
// System.setIn(new FileInputStream(id + ".in"));
// System.setOut(new PrintStream(new FileOutputStream(id + ".out")));
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
throw new RuntimeException(e);
}
in = new StreamInputReader(System.in);
out = new PrintWriter(System.out);
testMode = false;
}
@SuppressWarnings({"UnusedParameters"})
private static String check(String input, String result, String output) {
// return strictCheck(result, output);
return tokenCheck(result, output);
}
public static void main(String[] args) {
if (args.length != 0 && args[0].equals("42"))
test();
else
new Task4().run();
}
private static void test() {
List<Test> tests = createTests();
int testCase = 0;
for (Test test : tests) {
System.out.print("Test #" + testCase + ": ");
InputReader in = new StringInputReader(test.getInput());
StringWriter out = new StringWriter(test.getOutput().length());
long time = System.currentTimeMillis();
try {
new Task4(in, new PrintWriter(out)).run();
} catch (TestException e) {
time = System.currentTimeMillis() - time;
String checkResult = check(test.getInput(), out.getBuffer().toString(), test.getOutput());
if (checkResult == null)
System.out.print("OK");
else
System.out.print("WA (" + checkResult + ")");
System.out.printf(" in %.3f s.\n", time / 1000.);
} catch (Throwable e) {
System.out.println("Exception thrown:");
e.printStackTrace(System.out);
}
testCase++;
}
}
private static String tokenCheck(String result, String output) {
StringInputReader resultStream = new StringInputReader(result);
StringInputReader outputStream = new StringInputReader(output);
int index = 0;
boolean readingResult = false;
try {
while (true) {
readingResult = true;
String resultToken = resultStream.readString();
readingResult = false;
String outputToken = outputStream.readString();
if (!resultToken.equals(outputToken))
return "'" + outputToken + "' expected at " + index + " but '" + resultToken + "' received";
index++;
}
} catch (InputMismatchException e) {
if (readingResult) {
try {
outputStream.readString();
return "only " + index + " tokens received";
} catch (InputMismatchException e1) {
return null;
}
} else
return "only " + index + " tokens expected";
}
}
@SuppressWarnings({"UnusedDeclaration"})
private static String strictCheck(String result, String output) {
if (result.equals(output))
return null;
return "'" + output + "' expected but '" + result + "' received";
}
@SuppressWarnings({"UnusedDeclaration"})
private static boolean isDoubleEquals(double expected, double result, double certainty) {
return Math.abs(expected - result) < certainty || Math.abs(expected - result) < certainty * expected;
}
private Task4(InputReader in, PrintWriter out) {
this.in = in;
this.out = out;
testMode = true;
}
@SuppressWarnings({"UnusedDeclaration"})
private void exit() {
out.close();
if (testMode)
throw new TestException();
System.exit(0);
}
private static class Test {
private final String input;
private final String output;
private Test(String input, String output) {
this.input = input;
this.output = output;
}
public String getInput() {
return input;
}
public String getOutput() {
return output;
}
}
@SuppressWarnings({"UnusedDeclaration"})
private abstract static class InputReader {
public abstract int read();
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = readInt();
return array;
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++)
array[i] = readLong();
return array;
}
public double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; i++)
array[i] = readDouble();
return array;
}
public String[] readStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; i++)
array[i] = readString();
return array;
}
public char[][] readTable(int rowCount, int columnCount) {
char[][] table = new char[rowCount][columnCount];
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++)
table[i][j] = readCharacter();
}
return table;
}
public void readIntArrays(int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++)
arrays[j][i] = readInt();
}
}
}
private static class StreamInputReader extends InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public StreamInputReader(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++];
}
}
private static class StringInputReader extends InputReader {
private Reader stream;
private char[] buf = new char[1024];
private int curChar, numChars;
public StringInputReader(String stream) {
this.stream = new StringReader(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++];
}
}
private static class TestException extends RuntimeException {}
} | Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | a0fb60c7dbd66cbe3649d5aba9a9b56c | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | /**
* Created by IntelliJ IDEA.
* User: piyushd
* Date: 3/13/11
* Time: 9:43 PM
* To change this template use File | Settings | File Templates.
*/
public class Task4 {
void run(){
int n = nextInt();
int[] x = new int[n];
int[] y = new int[n];
int[] M = new int[n + 1];
for(int i = 0; i < n; i++){
x[nextInt() - 1] = i;
}
for(int i = 0; i < n; i++){
y[i] = x[nextInt() - 1];
}
M[1] = 0;
int L = 1;
int low, high, mid;
for(int i = 1; i < n; i++){
if(y[M[L]] > y[i]){
L++;
M[L] = i;
continue;
}
low = 1; high = L;
while(low < high){
mid = low + (high - low) / 2;
if(y[M[mid]] > y[i]) low = mid + 1;
else high = mid;
}
if(y[i] > y[M[low]]){
M[low] = i;
L = Math.max(L, low);
}
}
System.out.println(L);
}
int nextInt(){
try{
int c = System.in.read();
if(c == -1) return c;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return c;
}
if(c == '-') return -nextInt();
int res = 0;
do{
res *= 10;
res += c - '0';
c = System.in.read();
}while('0' <= c && c <= '9');
return res;
}catch(Exception e){
return -1;
}
}
long nextLong(){
try{
int c = System.in.read();
if(c == -1) return -1;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return -1;
}
if(c == '-') return -nextLong();
long res = 0;
do{
res *= 10;
res += c-'0';
c = System.in.read();
}while('0' <= c && c <= '9');
return res;
}catch(Exception e){
return -1;
}
}
double nextDouble(){
return Double.parseDouble(next());
}
String next(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(Character.isWhitespace(c))
c = System.in.read();
do{
res.append((char)c);
}while(!Character.isWhitespace(c=System.in.read()));
return res.toString();
}catch(Exception e){
return null;
}
}
String nextLine(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(c == '\r' || c == '\n')
c = System.in.read();
do{
res.append((char)c);
c = System.in.read();
}while(c != '\r' && c != '\n');
return res.toString();
}catch(Exception e){
return null;
}
}
public static void main(String[] args){
new Task4().run();
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 1837c1d43c6774ef761be26fe11e97fe | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Author -
* User: kansal
* Date: 3/15/11
* Time: 9:02 PM
* Contest: Manthan
*/
public class OpticalExperiment {
public static void main(String[] args) {
reader = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int[] in = new int[n];
int[] out = new int[n];
for(int i = 0; i < n; ++i) {
in[i] = nextInt() - 1;
}
for(int i = 0; i < n; ++i) {
out[nextInt()-1] = i;
}
int[] a = new int[n];
for(int i = 0; i < n; ++i) {
a[i] = out[in[i]];
}
for(int i = 0, j = n-1; i < j; ++i, --j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
int res = LIS_nlogn(a);
System.out.println(res);
}
public static BufferedReader reader;
public static PrintWriter writer;
public static StringTokenizer tokenizer = null;
static String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
static public int nextInt() {
return Integer.parseInt(nextToken());
}
private static int bSearch(int key, int len, int[] a, int[] m) {
int low = 0, high = len;
while (low < high) {
int mid = (low + high) / 2;
if (a[m[mid]] < key) {
if (low == mid)
break;
low = mid;
} else
high = mid - 1;
}
while (low < len && a[m[low + 1]] < key) ++low;
return low;
}
public static int LIS_nlogn(int[] a) {
int best = 0;
int N = a.length;
int[] m = new int[N+1];
for (int i = 0; i < N; ++i)
m[i] = 0;
for (int i = 0; i < N; ++i) {
int j = bSearch(a[i], best, a, m);
if (j == best || a[i] < a[m[j + 1]]) {
m[j + 1] = i;
best = Math.max(best, j + 1);
}
}
return best;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | b84751c1b2d4e130dd42d056a04b5bcc | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;
public class Main {
public static void main(String[] args) throws IOException {
new Thread(null, new Runnable() {
public void run() {
try {
try {
if (new File("input.txt").exists())
System.setIn(new FileInputStream("input.txt"));
} catch (SecurityException e) {}
new Main().run();
} catch (IOException e) {
e.printStackTrace();
}
}
}, "1", 1L << 24).start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
int N;
int[] a;
int[] b;
RMQ rmq;
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
N = nextInt();
a = new int [N];
b = new int [N];
for (int i = 0; i < N; i++)
a[nextInt() - 1] = i;
for (int i = 0; i < N; i++)
b[i] = a[nextInt() - 1];
// System.out.println(Arrays.toString(a));
// System.out.println(Arrays.toString(b));
rmq = new RMQ(N);
for (int i = 0; i < N; i++) {
int max = rmq.get(b[i] + 1, N - 1) + 1;
rmq.set(b[i], max);
}
out.println(rmq.get(0, N - 1));
out.close();
}
class RMQ {
int n;
int[] val;
RMQ(int n) {
this.n = n;
val = new int [2 * n];
}
void set(int ind, int nval) {
val[ind += n] = nval;
for (int v = ind >> 1; v > 0; v >>= 1) {
int l = v << 1;
val[v] = max(val[l], val[l + 1]);
}
}
int get(int l, int r) {
l += n; r += n;
int ret = 0;
while (l <= r) {
if ((l & 1) == 1) ret = max(ret, val[l]);
if ((r & 1) == 0) ret = max(ret, val[r]);
l = (l + 1) >> 1;
r = (r - 1) >> 1;
}
return ret;
}
}
/***************************************************************
* Input
**************************************************************/
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 12e17e3dc8587b10f8529336090e08b6 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes |
import java.awt.Point;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Manthan2011_D implements Runnable {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws IOException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
@Override
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
public static void main(String[] args) {
new Thread(new Manthan2011_D()).start();
}
void solve() throws IOException {
int n = readInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = readInt() - 1;
}
for (int i = 0; i < n; i++) {
y[i] = readInt() - 1;
}
int[] xx = new int[n];
for (int i = 0; i < n; i++) {
xx[x[i]] = i;
}
int[] yy = new int[n];
for (int i = 0; i < n; i++) {
yy[y[i]] = i;
}
Point[] p = new Point[n];
for (int i = 0; i < n; i++) {
p[i] = new Point(xx[i], yy[i]);
}
Arrays.sort(p, new Comparator<Point>() {
@Override
public int compare(Point p1, Point p2) {
return p1.x - p2.x;
}
});
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = p[i].y;
}
for (int i = 0; i < n / 2; i++) {
int t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
int[] d = new int[n+1];
Arrays.fill(d, (1<<28));
d[0] = a[0];
for (int i = 1; i < n; i++) {
int index = Arrays.binarySearch(d, a[i]);
if (index >= 0) throw new RuntimeException();
index = -index - 1;
if (a[i] < d[index]) {
d[index] = a[i];
}
}
for (int i = 0; i <= n; i++) {
if (d[i] == (1<<28)) {
out.print(i);
return;
}
}
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 628aa89133d23f13502bc06099ec04ff | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes |
import java.awt.Point;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Manthan2011_D implements Runnable {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws IOException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
@Override
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
public static void main(String[] args) {
new Thread(new Manthan2011_D()).start();
}
void solve() throws IOException {
int n = readInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i++) {
x[i] = readInt() - 1;
}
for (int i = 0; i < n; i++) {
y[i] = readInt() - 1;
}
int[] xx = new int[n];
for (int i = 0; i < n; i++) {
xx[x[i]] = i;
}
int[] yy = new int[n];
for (int i = 0; i < n; i++) {
yy[y[i]] = i;
}
/*
for (int i = 0; i < n; i++) {
System.err.print((xx[i]+1) + " ");
}
System.err.println();
for (int i = 0; i < n; i++) {
System.err.print((yy[i]+1) + " ");
}
System.err.println();*/
Point[] p = new Point[n];
for (int i = 0; i < n; i++) {
p[i] = new Point(xx[i], yy[i]);
}
Arrays.sort(p, new Comparator<Point>() {
@Override
public int compare(Point p1, Point p2) {
return p1.x - p2.x;
}
});
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = p[i].y;
}
/*for (int i = 0; i < n; i++) {
System.err.print((a[i]+1) + " ");
}
System.err.println();*/
for (int i = 0; i < n / 2; i++) {
int t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
int[] d = new int[n+1];
Arrays.fill(d, (1<<28));
d[0] = a[0];
for (int i = 1; i < n; i++) {
int index = Arrays.binarySearch(d, a[i]);
if (index >= 0) throw new RuntimeException();
index = -index - 1;
if (a[i] < d[index]) {
d[index] = a[i];
}
}
for (int i = 0; i <= n; i++) {
if (d[i] == (1<<28)) {
out.print(i);
return;
}
}
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | c6e20c50e2ca3b1f6d0b769f2c9084b7 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package d_dinprog;
/**
*
* @author Катерина
*/
//package d;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException {
boolean online = System.getProperty("ONLINE_JUDGE") != null;
StreamTokenizer in;
if (online)
in = new StreamTokenizer(new BufferedReader (new InputStreamReader(System.in)));
else in = new StreamTokenizer(new BufferedReader (new FileReader("input.txt")));
//Scanner in = online ? new Scanner(System.in) : new Scanner(new FileReader("input.txt"));
PrintWriter out = online ? new PrintWriter(System.out) : new PrintWriter(new FileWriter("output.txt"));
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
in.nextToken();
int n = (int) in.nval;
int a[] = new int[n];
int v[] = new int[n];
for (int i=0; i<n; i++) {
in.nextToken();
map.put((int) in.nval,i+1);
}
for (int i=n-1; i>=0; i--) {
in.nextToken();
a[i] = map.get((int) in.nval);
}
for (int i=0; i<n; i++)
v[i] = 0;
// solve
int insertPosition;
int length = 1;
v[0] = a[0];
for (int i=1; i<n; i++) {
insertPosition = -(Arrays.binarySearch(v, 0, length, a[i]) + 1);
v[insertPosition] = a[i];
if (insertPosition > length - 1)
length++;
}
out.println(length);
out.flush();
return;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 4ec7177d68c93c3d44a5c9a659f9d9c3 | train_002.jsonl | 1300033800 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package d_dinprog;
/**
*
* @author Катерина
*/
//package d;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException {
boolean online = System.getProperty("ONLINE_JUDGE") != null;
StreamTokenizer in;
if (online)
in = new StreamTokenizer(new BufferedReader (new InputStreamReader(System.in)));
else in = new StreamTokenizer(new BufferedReader (new FileReader("input.txt")));
//Scanner in = online ? new Scanner(System.in) : new Scanner(new FileReader("input.txt"));
PrintWriter out = online ? new PrintWriter(System.out) : new PrintWriter(new FileWriter("output.txt"));
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
in.nextToken();
int n = (int) in.nval;
int a[] = new int[n];
int v[] = new int[n];
for (int i=0; i<n; i++) {
in.nextToken();
map.put((int) in.nval,i+1);
v[i] = 0;
}
for (int i=n-1; i>=0; i--) {
in.nextToken();
a[i] = map.get((int) in.nval);
}
// solve
int insertPosition;
int length = 1;
v[0] = a[0];
for (int i=1; i<n; i++) {
insertPosition = -(Arrays.binarySearch(v, 0, length, a[i]) + 1);
v[insertPosition] = a[i];
if (insertPosition > length - 1)
length++;
}
out.println(length);
out.flush();
return;
}
}
| Java | ["5\n1 4 5 2 3\n3 4 2 1 5", "3\n3 1 2\n2 3 1"] | 5 seconds | ["3", "2"] | NoteFor the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | Java 6 | standard input | [
"dp",
"binary search",
"data structures"
] | b0ef9cda01a01cad22e7f4c49e74e85c | The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n. | 1,900 | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | standard output | |
PASSED | 509efb9dc939b15c46cec8020a682dfc | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | // Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main
{
static int root(int i,int arr[]){
while(arr[i] !=i){
arr[i]=arr[arr[i]];
i=arr[i];
}
return i;
}
static void union(int a,int b,long size[],int arr[]){
int root_a=root(a,arr);
int root_b=root(b,arr);
if(size[root_a] > size[root_b]){
size[root_a]+=size[root_b];
arr[root_b]=arr[root_a];
}
else{
size[root_b]+=size[root_a];
arr[root_a]=arr[root_b];
}
}
static boolean fin(int a,int b,int arr[]){
return root(a,arr) == root(b,arr);
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int n=sc.nextInt();
int m=sc.nextInt();
long size[]=new long[n+1];
int arr[]=new int[n+1];
for(int i=1;i<=n;i++)
{
arr[i]=i;
size[i]=1;
}
while(m-- >0){
int nn=sc.nextInt();
if(nn == 0) continue;
int prv=sc.nextInt();
for (int i=1;i<nn;i++){
int a=prv;
int b=sc.nextInt();
prv=b;
if(!fin(a,b,arr))
union(a,b,size,arr);
}
}
for(int i=1;i<=n;i++){
int par=root(i,arr);
System.out.print(size[par]+" ");
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | bb6f25bc700eaddb6021b1bdcd6d9c01 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CF1167C {
public static void main(String[] args) {
int n = fs.nextInt();
int m = fs.nextInt();
ArrayList<Integer> [] persons = new ArrayList[n];
while (m-- > 0) {
int k = fs.nextInt();
int person = 0, neighbour;
if (k > 0) person = fs.nextInt() - 1;
for (int i = 0; i < k-1; i++) {
neighbour = fs.nextInt() - 1;
if (persons[person] == null) persons[person] = new ArrayList<>();
if (persons[neighbour] == null) persons[neighbour] = new ArrayList<>();
persons[person].add(neighbour);
persons[neighbour].add(person);
person = neighbour;
}
}
int reach[] = new int[n];
boolean visited[] = new boolean[n];
Queue<Integer> dfsQueue = new LinkedList<>();
ArrayList<Integer> rough = new ArrayList<>();
for (int person = 0; person < n; person++) {
if (visited[person]) continue;
int counter = 0;
dfsQueue.add(person);
visited[person] = true;
while (!dfsQueue.isEmpty()) {
int currPerson = dfsQueue.poll();
counter += 1;
rough.add(currPerson);
if (persons[currPerson] == null) continue;
for (int i = 0; i < persons[currPerson].size(); i++) {
int anotherPerson = persons[currPerson].get(i);
if (visited[anotherPerson]) continue;
else {
dfsQueue.add(anotherPerson);
visited[anotherPerson] = true;
}
}
}
for (int i = 0; i < rough.size(); i++) {
reach[rough.get(i)] = counter;
}
rough.clear();
}
StringBuilder output = new StringBuilder();
for (int i = 0; i < n; i++) {
output.append(reach[i]);
output.append(" ");
}
System.out.println(output);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static FastScanner fs = new FastScanner();
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 5ed52945d3fd8668168aaa7e6164b6cd | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class C1167{
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 void union(int r[],int p[],int t[],int a,int b){
a=find(p,a);
b=find(p,b);
if(a==b)
return;
if(r[a]>r[b]){
p[b]=a;
t[a]+=t[b];
}
else if(r[a]<r[b]){
p[a]=b;
t[b]+=t[a];
}
else{
p[a]=b;
t[b]+=t[a];
r[b]++;
}
}
static int find(int p[],int a){
if(p[a]==-1)
return a;
else{
int x=find(p,p[a]);
p[a]=x;
return x;
}
}
public static void main(String args[])throws IOException{
Reader sc=new Reader();
int n=sc.nextInt();
int m=sc.nextInt();
int p[]=new int[n+1];
int t[]=new int[n+1];
int r[]=new int[n+1];
Arrays.fill(p,-1);
Arrays.fill(t,1);
for(int i=0;i<m;i++){
int x=sc.nextInt();
if(x==1)
sc.nextInt();
else if(x>1){
int y=sc.nextInt();
for(int j=0;j<x-1;j++){
int z=sc.nextInt();
union(r,p,t,y,z);
y=z;
}
}
}
for(int i=1;i<=n;i++)
System.out.print(t[find(p,i)]+" ");
System.out.println();
}
} | Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | e38c0f25504d4eca9637124725dd0b14 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class C {
static fs eggo = new fs();
static int[] set;
static class fs{
BufferedReader br;
StringTokenizer st;
public fs(){
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
String next(){
if(st.hasMoreTokens()){
return st.nextToken();
}
else{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return next();
}
}
int nextInt(){
return Integer.parseInt(next());
}
}
static int findparent(int a){
if(set[a]==a)
return a;
return set[a] = findparent(set[a]);
}
static boolean union(int a, int b){
int para = findparent(a),
parb = findparent(b);
if(para == parb){
return false;
}
else{
set[parb] = para;
}
return true;
}
public static void main(String[] args) {
int n = eggo.nextInt(),
m = eggo.nextInt();
set = new int[n];
for(int i=0;i<n;i++)
set[i] = i;
for(int i=0;i<m;i++){
int k = eggo.nextInt();
if(k<=0)
continue;
int root = (eggo.nextInt()-1);
for(int j=1;j<k;j++){
int nxt=(eggo.nextInt()-1);
union(root,nxt);
}
}
int[] count = new int[n];
for(int i=0;i<n;i++){
count[findparent(i)]++;
}
for(int i=0;i<n;i++){
if(i!=n-1)
System.out.print(""+(count[findparent(i)])+" ");
else
System.out.println(""+(count[findparent(i)]));
}
// System.out.println(Arrays.toString(set));
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 60cb5df348f340aa4539fe921ff16cd4 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class C {
void solve() throws IOException {
int n = nextInt();
DisjointSet ds = new DisjointSet(n);
int m = nextInt();
for (int i = 0; i < m; ++i) {
int k = nextInt();
int prevU = -1;
for (int j = 0; j < k; ++j) {
int u = nextInt();
if (prevU != -1) {
ds.union(prevU, u);
}
prevU = u;
}
}
boolean first = true;
for (int i = 1; i <= n; ++i) {
if (first) {
first = false;
} else {
writer.print(" ");
}
writer.print(ds.getCount(i));
}
writer.println();
}
static class DisjointSet {
private Map<Integer, Integer> map = new HashMap<>();
private Map<Integer, Integer> count = new HashMap<>();
private Map<Integer, Integer> ranks = new HashMap<>();
DisjointSet(int n) {
for (int i = 1; i <= n; ++i) {
map.put(i, i);
ranks.put(i, 1);
count.put(i, 1);
}
}
void union(int i, int j) {
int root1 = find(i);
int root2 = find(j);
if (root1 == root2) {
return;
}
int rank1 = ranks.get(root1);
int rank2 = ranks.get(root2);
int count1 = count.get(root1);
int count2 = count.get(root2);
if (rank1 > rank2) {
map.put(root2, root1);
count.put(root1, count1 + count2);
} else if (rank1 < rank2) {
map.put(root1, root2);
count.put(root2, count1 + count2);
} else {
map.put(root2, root1);
ranks.put(root1, rank1 + 1);
count.put(root1, count1 + count2);
}
// map.put(root2, root1);
// System.out.println("ds:" + map);
}
int find(int i) {
Integer root = i;
while (map.get(root) != root) {
root = map.get(root);
}
return root;
}
int getCount(int i) {
return count.get(find(i));
}
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
StringTokenizer tokenizer;
PrintWriter writer;
BufferedReader reader;
public void run() {
try {
writer = new PrintWriter(System.out);
reader = new BufferedReader(new InputStreamReader(System.in));
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new C().run();
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | d338d33172cf2f42cd79d07111abcf34 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.*;
import java.util.*;
public class tr1 {
static PrintWriter out;
static StringBuilder sb;
static int inf = (int) 1e9;
static long mod = 998244353;
static int[]ans;
static HashSet<Integer>[]ad;
static boolean []vis;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int n=sc.nextInt();
int m=sc.nextInt();
unionfind f=new unionfind(n);
while(m-->0) {
int k=sc.nextInt();
int l=-1;
while(k-->0) {
int x=sc.nextInt()-1;
if(l!=-1)
f.combine(x, l);
l=x;
}
}
// for(int i=0;i<n;i++)
// f.findSet(i);
for(int i=0;i<n;i++)
out.print(f.size[f.findSet(i)]+" ");
out.close();
}
static HashSet<Integer> re;
static int c;
static void dfs(int u) {
c++;
re.add(u);
vis[u]=true;
for(int o:ad[u])
if(!vis[o])
dfs(o);
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static class unionfind {
int[] p;
int[] size;
unionfind(int n) {
p = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
}
Arrays.fill(size, 1);
}
int findSet(int v) {
if (v == p[v])
return v;
return p[v] = findSet(p[v]);
}
boolean sameSet(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return true;
return false;
}
int max() {
int max = 0;
for (int i = 0; i < size.length; i++)
if (size[i] > max)
max = size[i];
return max;
}
void combine(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return;
if (size[a] > size[b]) {
p[b] = a;
size[a] += size[b];
} else {
p[a] = b;
size[b] += size[a];
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | f8f1dffe44e74b4832293f9b9fd53943 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
final static long MOD = 998244353;
static int[] size;
static int[] parent;
public static void main(String[] args) {
Reader in = new Reader();
int n = in.nextInt();
int m = in.nextInt();
size = new int[n+1];
parent = new int[n+1];
for(int i = 0; i<=n; i++) {
size[i] = 1;
parent[i] = i;
}
for(int i = 0; i<m; i++) {
int k = in.nextInt();
if(k==0) continue;
int p = find(in.nextInt());
for(int j = 0; j<k-1; j++) unite(p, in.nextInt());
}
for(int i = 1; i<=n; i++) {
System.out.print(size[find(i)]+" ");
}
}
public static int find(int i) {
if(parent[i]==i) return i;
return parent[i] = find(parent[i]);
}
public static void unite(int i, int j) {
int pi = find(i);
int pj = find(j);
if(pi==pj) return;
parent[pj] = pi;
size[pi]+=size[pj];
}
static class Reader {
static BufferedReader br;
static StringTokenizer st;
private int charIdx = 0;
private String s;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
public char nextChar() {
if (s == null || charIdx >= s.length()) {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
s = st.nextToken();
charIdx = 0;
}
return s.charAt(charIdx++);
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] nS(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public int nextInt() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return Long.parseLong(st.nextToken());
}
public String next() {
if (st == null || !st.hasMoreTokens())
try {
readLine();
} catch (Exception e) {
}
return st.nextToken();
}
public static void readLine() {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
}
}
} | Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 44655bddd2df746e2f3965f009a70315 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Main
{
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 class Pair implements Comparable<Pair> {
int x,y,ind;
public Pair(int x, int y,int ind) {
this.x = x;
this.y = y;
this.ind=ind;
}
public int compareTo(Pair o) {
if(x!=o.x)
return x-o.x;
return this.y - o.y;
}
public int hashCode() {
return (int)(1L*1000000000*x + y)%1000000007;
}
public boolean equals(Object o) {
if(o instanceof Pair) {
Pair other = (Pair)o;
return other.hashCode()==this.hashCode();
}
else return false;
}
public String toString(){
return x+" "+y;
}
}
//static Scanner sc = new Scanner(System.in);
static Reader sc = new Reader();
static StringBuilder sb = new StringBuilder();
static PrintWriter pw = new PrintWriter(System.out,true);
static int par[],weight[];
public static void main(String[] args) throws IOException {
//Code start from here
int n = sc.nextInt(),m = sc.nextInt();
init(n);
while(m-->0){
int k = sc.nextInt();
int arr[]= new int[k];
for(int i =0;i<k;i++){
arr[i]= sc.nextInt();
if(i>0){
union(getRoot(arr[i]),getRoot(arr[i-1]));
}
}
}
for(int i=1;i<=n;i++)
sb.append(weight[getRoot(i)]+" ");
pw.println(sb);
pw.flush();
pw.close();
}
static int getRoot(int x) {
if(par[x]==x)
return x;
return par[x]=getRoot(par[x]);
}
static void union(int a,int b) {
if(a==b)
return;
if(weight[a]>weight[b]) {
weight[a]+=weight[b];
par[b]=a;
} else{
weight[b]+=weight[a];
par[a]=b;
}
}
static void init(int n) {
par = new int[n+1];
weight= new int[n+1];
for(int i =1;i<=n;i++) {
par[i]=i;
weight[i]=1;
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 7474e6fcb6b0cb208045027f4f13a041 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes |
/*
* Remember a 6.0 student can know more than a 10.0 student.
* Grades don't determine intelligence, they test obedience.
* I Never Give Up.
* I will become Candidate Master.
* I will defeat Saurabh Anand.
* Skills are Cheap,Passion is Priceless.
*/
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import static java.lang.System.out;
import static java.util.Arrays.*;
import static java.lang.Math.*;
public class ContestMain{
private static Reader in=new Reader();
private static StringBuilder ans=new StringBuilder();
private static long MOD=1000000000+7;// 1e9+7
private static final int N=500000+7; // 2e5+7
// private static final double EPS=1e-9;
// private static final int LIM=26;
// private static final double PI=3.1415;
// private static ArrayList<Integer> v[]=new ArrayList[N];
// private static int color[]=new int[N];
private static boolean mark[]=new boolean[N];
// private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// private static ArrayList<Pair> v[]=new ArrayList[N];
private static long powmod(long x,long n){
if(n==0||x==0)
return 1;
else if(n%2==0)
return(powmod((x*x)%MOD,n/2));
else
return (x*(powmod((x*x)%MOD,(n-1)/2)))%MOD;
}
private static void shuffle(long[] arr){
for(int i=arr.length-1;i>=2;i--){
int x=new Random().nextInt(i-1);
long temp=arr[x];
arr[x]=arr[i];
arr[i]=temp;
}
}
// private static long gcd(long a, long b){
// if(b==0)return a;
// return gcd(b,a%b);
// }
// private static boolean check(int x,int y){
// if((x>=0&&x<n)&&(y>=0&&y<m)&&mat[x][y]!='X'&&!visited[x][y])return true;
// return false;
// }
static int parent[]=new int[N];
static int rank[]=new int[N];
public static void union(int a,int b){
int p1=find(a);
int p2=find(b);
if(p1==p2)return;
if(rank[p1]>=rank[p2]){
parent[p2]=p1;
rank[p1]+=rank[p2];
}
else{
parent[p1]=p2;
rank[p2]+=rank[p1];
}
}
public static int find(int a){
while(parent[a]!=a){
a=parent[a];
}
return a;
}
public static void main(String[] args) throws IOException{
int n=in.nextInt();
int m=in.nextInt();
for(int i=0;i<N;i++)
parent[i]=i;
for(int i=0;i<N;i++)
rank[i]=1;
int c,first=0,x;
while(m-->0){
c=in.nextInt();
first=-1;
for(int i=0;i<c;i++){
if(first==-1)first=in.nextInt();
else {
x=in.nextInt();
union(first,x);
}
}
}
for(int i=1;i<=n;i++)
ans.append(rank[find(i)]+" ");
out.println(ans);
}
static class Pair<T> implements Comparable<Pair>{
int l;
int r;
Pair(){
l=0;
r=0;
}
Pair(int k,int v){
l=k;
r=v;
}
@Override
public int compareTo(Pair o){
if(o.l!=l)
return (int)(l-o.l);
else
return (int)(r-o.r);
}
// Fenwick tree question comparator
// @Override
// public int compareTo(Pair o) {
// if(o.r!=r)return (int) (r-o.r);
// else return (int)(l-o.l);
// }
}
static class Reader{
BufferedReader br;
StringTokenizer st;
public Reader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null||!st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try{
str=br.readLine();
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
} | Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | e60850dc0a16786f0e98530d230c67c0 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
public class Level {
public static DS.Scanner in = new DS.Scanner();
public static void main(String[] args) throws IOException {
int n = in.nextInt();
int m = in.nextInt();
DS.Graph graph = new DS.Graph(n);
DS.Print out = new DS.Print();
boolean[] v = new boolean[n + 1];
while (m-- > 0) {
int k = in.nextInt();
int[] z = new int[k];
for (int i = 0; i < z.length; i++) {
z[i] = in.nextInt();
}
for (int i = 0; i < k - 1; i++) {
int s = z[i];
int u = z[i + 1];
graph.addEdge(s, u);
}
}
int[] cmpSize = new int[n + 1];
int[] cmpOfNode = new int[n + 1];
int cmp = 0;
for (int i = 1; i < v.length; i++) {
if (!v[i]) {
cmp++;
v[i] = true;
cmpSize[cmp] = graph.count(i, v, cmpOfNode, cmp);
}
}
for (int i = 1; i <= n; i++) {
out.print(cmpSize[cmpOfNode[i]]);
}
out.println("");
out.close();
}
public static class Algo {
public static int lowerBound(long[] z, int length, long value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value <= z[mid]) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
public static int upperBound(ArrayList<Integer> list, int length, int value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= list.get(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
public static void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; ++j) {
R[j] = arr[m + 1 + j];
}
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void sort(int arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public static ArrayList<Integer> sieve(int n) {
long sqr = (long) Math.sqrt(n);
boolean[] isPrime = new boolean[n + 1];
for (int i = 0; i <= n; i++) {
isPrime[i] = true;
}
for (int i = 2; i <= sqr; i++) {
if (isPrime[i]) {
for (int p = i * i; p <= n; p += i) {
isPrime[p] = false;
}
}
}
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i <= n; i++) {
if (isPrime[i]) {
list.add(i);
}
}
list.remove(0);
list.remove(0);
return list;
}
public static boolean isPrime(long n) {
boolean res = true;
List l = sieve((int) Math.sqrt(n));
for (int i = 0; i < l.size(); i++) {
int curPrime = (int) l.get(i);
if (n % curPrime == 0) {
res = false;
break;
}
}
return res;
}
public static ArrayList factorizatin(int n) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (n == 1) {
list.add(1);
return list;
} else if (n == 2) {
list.add(2);
return list;
} else {
if (isPrime(n)) {
System.out.println(-1);
return null;
} else {
Iterator ptr = sieve((int) Math.sqrt(n)).iterator();
if (ptr.hasNext()) {
int cur = (int) ptr.next();
// System.out.println(cur);
while (n != 1) {
System.out.println(n);
while (n % cur == 0) {
list.add(cur);
n /= cur;
}
if (ptr.hasNext()) {
cur = (int) ptr.next();
}
//System.out.println(cur);
}
}
if (n > 2) {
list.add(n);
}
}
return list;
}
}
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) {
long ab = a * b;
long gcd = gcd(a, b);
return ab / gcd;
}
public static int bs(int[] z, int k) {
int res = 0;
int l = 0;
int r = z.length - 1;
int mid = (l + r) / 2;
while (l <= r) {
if (z[mid] == k) {
res = 1;
break;
}
if (z[mid] > k) {
r = mid - 1;
} else {
l = mid + 1;
}
mid = (l + r) / 2;
}
return res;
}
public static void solve() {
int n = Integer.parseInt(in.next());
int target = Integer.parseInt(in.next());
int[] z = new int[n];
for (int i = 0; i < n; i++) {
z[i] = Integer.parseInt(in.next());
}
Arrays.sort(z);
int ptr1 = 0;
int ptr2 = n - 1;
int wind = z[ptr1] + z[ptr2];
if (wind == target) {
System.out.println(ptr1 + "-" + ptr2);
} else {
while (ptr1 < ptr2 && wind != target) {
if (wind > target) {
wind -= z[ptr2--];
wind += z[ptr2];
} else {
wind -= z[ptr1++];
wind += z[ptr1];
}
}
if ((ptr1 != 0 || ptr2 != n - 1) && ptr2 != ptr1) {
System.out.println(ptr1 + "-" + ptr2);
} else {
System.out.println(-1);
}
}
}
public static void sliding() {
int n = Integer.parseInt(in.next());
int k = Integer.parseInt(in.next());
int[] z = new int[n];
for (int i = 0; i < n; i++) {
z[i] = Integer.parseInt(in.next());
}
int wind = 0;
for (int i = 0; i < k; i++) {
wind += z[i];
}
int max = wind;
for (int i = k; i < n; i++) {
wind += z[i];
wind -= z[i - k];
max = Math.max(max, wind);
}
System.out.println(max);
}
public static ArrayList<Integer> dd(int n) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
list.add(n / i);
list.add(i);
}
}
return list;
}
}
private static class DS {
public static class Print {
private final BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append(object + " ");
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
private static class Graph {
int V = 0;
ArrayList<Integer>[] list = null;
public Graph(int v) {
this.V = v + 1;
this.list = new ArrayList[this.V];
for (int i = 0; i < list.length; i++) {
list[i] = new ArrayList<Integer>();
}
}
public void addEdge(int s, int v) {
list[s].add(v);
list[v].add(s);
}
public int count(int s, boolean[] v, int[] cmpOfNode, int cmp) {
int count = 1;
Queue<Integer> q = new LinkedList();
q.add(s);
v[s] = true;
while (q.size() > 0) {
int cur = q.poll();
cmpOfNode[cur] = cmp;
v[cur] = true;
ArrayList<Integer> st = list[cur];
for (int val : st) {
if (!v[val]) {
q.add(val);
cmpOfNode[val] = cmp;
v[val] = true;
count++;
}
}
}
return count;
}
}
private static class Stack {
private int Size;
Node top;
Node last;
Stack() {
Size = 0;
top = null;
last = null;
}
public int size() {
return this.Size;
}
public void push(Object item) {
Node node = new Node();
node.data = item;
node.next = top;
top = node;
Size++;
}
public void pop() {
if (top != null) {
top = top.next;
}
Size--;
}
public boolean isEmpty() {
return top == null;
}
public Object getTop() {
if (top != null) {
return top.data;
} else {
return null;
}
}
public void display() {
if (top != null) {
Node temp = top;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}
private class Node {
Object data;
Node next;
}
}
private static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
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;
}
BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 17c495cd52c0ae1b22f4d58a71fb2bea | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static int[] visited;
static int count = 0;
public static void dfs(TreeSet<Integer>[] graph,int u,int k){
visited[u] = k;
count++;
for (Integer i : graph[u]){
if (visited[i]==0){
dfs(graph, i,k);
}
}
}
public static void main(String[] args) throws IOException{
Reader scan = new Reader();
scan.init(System.in);
OutputStream output = System.out;
PrintWriter out = new PrintWriter(output);
int N = scan.nextInt();
int M = scan.nextInt();
TreeSet<Integer>[] graph = new TreeSet[N+1];
for (int i=0;i<=N;i++){
graph[i] = new TreeSet<Integer>();
}
for (int i=0;i<M;i++){
int n = scan.nextInt();
int[] arr = new int[n];
for (int j=0;j<n;j++){
arr[j] = scan.nextInt();
}
for (int j=1;j<n;j++){
graph[arr[j]].add(arr[j-1]);
graph[arr[j-1]].add(arr[j]);
}
}
int[] c = new int[N+1];
visited = new int[N+1];
for (int i=0;i<N;i++){
if (visited[i+1]==0){
count = 0;
dfs(graph, i+1, i+1);
c[i+1] = count;
out.print(count+" ");
}
else{
out.print(c[visited[i+1]]+" ");
}
}
out.println();
out.close();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
// 20
// 1 1
// 2 2
// 3 3
// 3 9
// 4 4
// 5 2
// 5 5
// 5 7
// 5 8
// 6 2
// 6 6
// 6 9
// 7 7
// 8 8
// 9 4
// 9 7
// 9 9
// 10 2
// 10 9 | Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | a773c06fc7d582c04cd607d501f99dd3 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class abc {
static int[] parent;
static long[] rank;
static long[] size;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] ss=br.readLine().trim().split(" ");
int n=Integer.parseInt(ss[0]);
int m=Integer.parseInt(ss[1]);
size=new long[n+1];
parent=new int[n+1];
rank=new long[n+1];
for(int i=1 ; i<parent.length ; i++) {
parent[i]=i;
rank[i]=1;
size[i]=1;
}
for(int i=0 ; i<m ; i++) {
ss=br.readLine().trim().split(" ");
int k=Integer.parseInt(ss[0]);
int[] arr=new int[k];
for(int j=0 ; j<k ; j++) {
arr[j]=Integer.parseInt(ss[j+1]);
}
int a=-1;
for(int j=0 ; j<k ; j++) {
if(a!=-1) {
union(arr[j],a);
}
a=arr[j];
}
}
for(int i=1 ; i<=n ; i++) {
System.out.print(size[findSet(i)]+" ");
}
System.out.println();
}
public static int findSet(int i) {
if(parent[i]==i) {
return i;
}
parent[i]=findSet(parent[i]);
return parent[i];
}
public static void union(int a , int b) {
int p1=findSet(a);
int p2=findSet(b);
if(p1==p2) {
return;
}
if(rank[p1]<rank[p2]) {
parent[p1]=p2;
size[p2]+=size[p1];
}else if(rank[p2]<rank[p1]) {
parent[p2]=p1;
size[p1]+=size[p2];
}else {
parent[p2]=p1;
rank[p1]++;
size[p1]+=size[p2];
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | eab599851c29158208bc7c0a4e6ab394 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class TaskF{
static ArrayList<Integer>g[];
static int comp[];
static int cComp = -1;
void dfs(int v){
comp[v] = cComp;
for (int i = 0; i < g[v].size(); i++) {
int to = g[v].get(i);
if(comp[to] == -1)dfs(to);
}
}
public static void main(String[] args) throws IOException {
TaskF task = new TaskF();
task.solve();
}
void solve() throws IOException {
Scanner in = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
g = new ArrayList[n];
comp = new int[n];
Arrays.fill(comp,-1);
for (int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int k = in.nextInt();
if(k == 0)continue;
int fst = in.nextInt() - 1;
for (int j = 0; j < k-1; j++) {
int sec = in.nextInt() - 1;
g[fst].add(sec);
g[sec].add(fst);
fst = sec;
}
}
for (int i = 0; i < n; i++) {
if(comp[i] == -1){
cComp++;
dfs(i);
}
}
int ans[] = new int[cComp+1];
for (int i = 0; i < n; i++) {
ans[comp[i]]++;
}
for (int i = 0; i < n; i++) {
out.print(ans[comp[i]] + " ");
}
out.close();
}
}
class Scanner {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st = new StringTokenizer("");
public static int nextInt() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Integer.parseInt(st.nextToken());
}
public static String next() throws IOException {
while (!st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return (st.nextToken());
}
} | Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 8616888186f0dca5285acc5c10221eed | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | //Author: Patel Rag
//Java version "1.8.0_211"
import java.util.*;
import java.io.*;
public class Main
{
static Vector<Integer> grp[] = new Vector[1000043];
static Vector<Integer> color = new Vector<>(1000043);
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); }
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong(){ return Long.parseLong(next()); }
double nextDouble(){ return Double.parseDouble(next()); }
float nextFloat() { return Float.parseFloat(next()); }
boolean nextBoolean() { return Boolean.parseBoolean(next()); }
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long modExp(long x, long n, long mod) //Modular exponentiation
{
long result = 1;
while(n > 0)
{
if(n % 2 == 1)
result = (result%mod * x%mod)%mod;
x = (x%mod * x%mod)%mod;
n=n/2;
}
return result;
}
static long gcd(long a, long b)
{
if(a==0) return b;
return gcd(b%a,a);
}
static String swap(String a,int i, int j)
{
char temp;
char[] s1 = a.toCharArray();
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
return String.valueOf(s1);
}
public static void main(String[] args)
throws IOException
{
FastReader fr = new FastReader();
int n = fr.nextInt();
int m = fr.nextInt();
for(int i = 0; i < n + m; i++) grp[i] = new Vector<>();
for(int i = 0; i < m; i++)
{
String[] str = fr.nextLine().trim().split(" ");
int k = Integer.parseInt(str[0]);
Vector<Integer> frd = new Vector<>(k);
for(int j = 0; j < k; j++)
{
int v = Integer.parseInt(str[1 + j]) - 1;
grp[v].add(n + i);
grp[n + i].add(v);
}
}
for(int i = 0; i < n+m; i++) color.add(-1);
StringBuffer out = new StringBuffer();
Vector<Integer> siz = new Vector<>();
for(int i = 0; i < n; i++)
{
if(color.get(i) == -1)
{
color.set(i,siz.size());
int cnt = 0;
Stack<Integer> s = new Stack<>();
s.push(i);
while(!s.empty())
{
int v = s.pop();
if(v < n) cnt++;
for(int j = 0; j < grp[v].size(); j++)
{
int u = grp[v].get(j);
if(color.get(u) == -1)
{
color.set(u,siz.size());
s.push(u);
}
}
}
siz.add(cnt);
}
out.append(String.valueOf(siz.get(color.get(i))) + " ");
}
System.out.println(new String(out));
}
}
class Pair<U, V> // Pair class
{
public final U first; // first field of a Pair
public final V second; // second field of a Pair
private Pair(U first, V second)
{
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
if (!first.equals(pair.first)) return false;
return second.equals(pair.second);
}
@Override
public int hashCode()
{
return 31 * first.hashCode() + second.hashCode();
}
public static <U, V> Pair <U, V> of(U a, V b)
{
return new Pair<>(a, b);
}
}
class myComp implements Comparator<Pair>
{
public int compare(Pair a,Pair b)
{
if(Math.abs((Long)a.first) > Math.abs((Long)b.first)) return 1;
if(Math.abs((Long)a.first) < Math.abs((Long)b.first)) return -1;
return 0;
}
}
class BIT //Binary Indexed Tree
{
public long[] m_array;
public BIT(long[] dat)
{
m_array = new long[dat.length + 1];
Arrays.fill(m_array,0);
for(int i = 0; i < dat.length; i++)
{
m_array[i + 1] = dat[i];
}
for(int i = 1; i < m_array.length; i++)
{
int j = i + (i & -i);
if(j < m_array.length)
{
m_array[j] = m_array[j] + m_array[i];
}
}
}
public final long prefix_query(int i)
{
long result = 0;
for(++i; i > 0; i = i - (i & -i))
{
result = result + m_array[i];
}
return result;
}
public final long range_query(int fro, int to)
{
if(fro == 0)
{
return prefix_query(to);
}
else
{
return (prefix_query(to) - prefix_query(fro - 1));
}
}
public void update(int i, long add)
{
for(++i; i < m_array.length; i = i + (i & -i))
{
m_array[i] = m_array[i] + add;
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | f1e41f77b955af90d55f20c384c2faf1 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | //Author: Patel Rag
//Java version "1.8.0_211"
import java.util.*;
import java.io.*;
public class Main
{
static Vector<Integer> grp[] = new Vector[1000043];
static Vector<Integer> color = new Vector<>(1000043);
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 long modExp(long x, long n, long mod) //Modular exponentiation
{
long result = 1;
while(n > 0)
{
if(n % 2 == 1)
result = (result%mod * x%mod)%mod;
x = (x%mod * x%mod)%mod;
n=n/2;
}
return result;
}
static long gcd(long a, long b)
{
if(a==0) return b;
return gcd(b%a,a);
}
static String swap(String a,int i, int j)
{
char temp;
char[] s1 = a.toCharArray();
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
return String.valueOf(s1);
}
public static void main(String[] args)
throws IOException
{
Reader fr = new Reader();
int n = fr.nextInt();
int m = fr.nextInt();
for(int i = 0; i < n + m; i++) grp[i] = new Vector<>();
for(int i = 0; i < m; i++)
{
int k = fr.nextInt();
Vector<Integer> frd = new Vector<>(k);
for(int j = 0; j < k; j++)
{
int v = fr.nextInt() - 1;
grp[v].add(n + i);
grp[n + i].add(v);
}
}
for(int i = 0; i < n+m; i++) color.add(-1);
StringBuffer out = new StringBuffer();
Vector<Integer> siz = new Vector<>();
for(int i = 0; i < n; i++)
{
if(color.get(i) == -1)
{
color.set(i,siz.size());
int cnt = 0;
Stack<Integer> s = new Stack<>();
s.push(i);
while(!s.empty())
{
int v = s.pop();
if(v < n) cnt++;
for(int j = 0; j < grp[v].size(); j++)
{
int u = grp[v].get(j);
if(color.get(u) == -1)
{
color.set(u,siz.size());
s.push(u);
}
}
}
siz.add(cnt);
}
out.append(String.valueOf(siz.get(color.get(i))) + " ");
}
System.out.println(new String(out));
}
}
class Pair<U, V> // Pair class
{
public final U first; // first field of a Pair
public final V second; // second field of a Pair
private Pair(U first, V second)
{
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
if (!first.equals(pair.first)) return false;
return second.equals(pair.second);
}
@Override
public int hashCode()
{
return 31 * first.hashCode() + second.hashCode();
}
public static <U, V> Pair <U, V> of(U a, V b)
{
return new Pair<>(a, b);
}
}
class myComp implements Comparator<Pair>
{
public int compare(Pair a,Pair b)
{
if(Math.abs((Long)a.first) > Math.abs((Long)b.first)) return 1;
if(Math.abs((Long)a.first) < Math.abs((Long)b.first)) return -1;
return 0;
}
}
class BIT //Binary Indexed Tree
{
public long[] m_array;
public BIT(long[] dat)
{
m_array = new long[dat.length + 1];
Arrays.fill(m_array,0);
for(int i = 0; i < dat.length; i++)
{
m_array[i + 1] = dat[i];
}
for(int i = 1; i < m_array.length; i++)
{
int j = i + (i & -i);
if(j < m_array.length)
{
m_array[j] = m_array[j] + m_array[i];
}
}
}
public final long prefix_query(int i)
{
long result = 0;
for(++i; i > 0; i = i - (i & -i))
{
result = result + m_array[i];
}
return result;
}
public final long range_query(int fro, int to)
{
if(fro == 0)
{
return prefix_query(to);
}
else
{
return (prefix_query(to) - prefix_query(fro - 1));
}
}
public void update(int i, long add)
{
for(++i; i < m_array.length; i = i + (i & -i))
{
m_array[i] = m_array[i] + add;
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 0a7d42d13966e341506c4ae9bb769e5b | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CF1167CNewsDistribution {
static HashSet<Integer> done = new HashSet<>();
public static void main(String[] args) {
FastReader s=new FastReader();
int n = s.nextInt();
int m = s.nextInt();
HashMap<Integer,ArrayList<Integer>> g = new HashMap<>();
HashSet<Integer> visited = new HashSet<>();
for (int i = 1; i <= n; i++) {
g.put(i,new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int z = s.nextInt();
int[] v = new int[z];
for (int j = 0; j < z; j++) {
v[j]=s.nextInt();
}
if(v.length>0) {
ArrayList<Integer> ej = g.get(v[0]);
for (int k = 1; k < v.length; k++) {
ArrayList<Integer> ek = g.get(v[k]);
ej.add(v[k]);
ek.add(v[0]);
g.put(v[0],ej);
g.put(v[k],ek);
}
}
}
StringBuilder a = new StringBuilder();
int[] count = new int[n+1];
for (int i = 1; i <= n; i++) {
if(!done.contains(i)) {
visited = new HashSet<>();
dfs(i,g,visited);
for (int x:visited) {
count[x]=visited.size();
}
}
a.append(count[i]+" ");
}
System.out.println(a);
}
public static void dfs(int x,HashMap<Integer, ArrayList<Integer>> g,HashSet<Integer> visited) {
if(!visited.contains(x)) {
visited.add(x);
done.add(x);
ArrayList<Integer> use= g.get(x);
for (int i = 0; i < use.size(); i++) {
dfs(use.get(i),g,visited);
}
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 141529328ed8b9a46b7c3de5b943e977 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.StringTokenizer;
public class B {
static class Reader {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
private String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
}
static class Writer {
OutputStream os = new BufferedOutputStream(System.out);
public void print(int i) throws IOException {
os.write(Integer.toString(i).getBytes());
}
public void print(char c) throws IOException {
os.write(c);
}
public void print(String str) throws IOException {
os.write(str.getBytes());
}
public void println(int i) throws IOException {
os.write(Integer.toString(i).getBytes());
os.write('\n');
}
public void println(char c) throws IOException {
os.write(c);
os.write('\n');
}
public void println(String str) throws IOException {
os.write(str.getBytes());
os.write('\n');
}
public void end() throws IOException {
os.flush();
os.close();
}
}
public static class UnionFind {
private int[] keys;
private int[] size;
public UnionFind(int n) {
keys = new int[n];
size = new int[n];
for(int i = 0; i < n; i++){
keys[i] = i;
size[i] = 1;
}
}
private int root(int value){
if(value == keys[value]) return value;
return keys[value] = root(keys[value]);
}
public void unite(int set1, int set2) {
int root1 = root(set1);
int root2 = root(set2);
if(root1 != root2) {
keys[root2] = root1;
size[root1]+= size[root2];
}
}
public boolean find(int value, int set) {
if(root(value) == root(set)) {
return true;
}
return false;
}
public int getSize(int set) {
return size[root(set)];
}
}
public static void main(String[] args) throws Exception {
Reader s = new Reader();
int n = s.nextInt();
int m = s.nextInt();
UnionFind set = new UnionFind(n+1);
int k;
int[] group;
for(int j = 0; j < m; j++){
k = s.nextInt();
group = new int[k];
for (int i = 0; i < k; i++) {
group[i] = s.nextInt();
if(i >0) {
set.unite(group[i-1], group[i]);
}
}
}
Writer w = new Writer();
for(int j = 1; j < n +1; j++) {
w.println(set.getSize(j));
}
w.end();
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | 50385039e954751b1a465f3f09b8ecf7 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;
public class B {
public static class UnionFind {
private int[] keys;
private int[] size;
public UnionFind(int n) {
keys = new int[n];
size = new int[n];
for(int i = 0; i < n; i++){
keys[i] = i;
size[i] = 1;
}
}
private int root(int value){
if(value == keys[value]) return value;
return keys[value] = root(keys[value]);
}
public void unite(int set1, int set2) {
int root1 = root(set1);
int root2 = root(set2);
if(root1 != root2) {
keys[root2] = root1;
size[root1]+= size[root2];
}
}
public boolean find(int value, int set) {
if(root(value) == root(set)) {
return true;
}
return false;
}
public int getSize(int set) {
return size[root(set)];
}
}
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
// sa.nextLine();
// File test = new File("C:\\Users\\bighosh\\test.txt");
// Scanner s = new Scanner(new FileInputStream(test));
int n = s.nextInt();
int m = s.nextInt();
UnionFind set = new UnionFind(n+1);
int k;
int[] group;
for(int j = 0; j < m; j++){
k = s.nextInt();
group = new int[k];
for (int i = 0; i < k; i++) {
group[i] = s.nextInt();
if(i >0) {
set.unite(group[i-1], group[i]);
}
}
}
BufferedOutputStream bo = new BufferedOutputStream(System.out);
for(int j = 1; j < n +1; j++) {
bo.write(Integer.toString(set.getSize(j)).getBytes());
bo.write('\n');
}
bo.flush();
bo.close();
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | eab2a01c04b2726f3de12bdbe2abf8e4 | train_002.jsonl | 1557930900 | In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. | 256 megabytes |
import java.io.*;
import java.util.*;
//import javafx.util.*;
import java.math.*;
//import java.lang.*;
public class untitled
{
static class Pair{
int x;int y;
Pair(int x,int y){
this.x=x;
this.y=y;
}
}
static class sorting implements Comparator<Pair>{
public int compare(Pair a,Pair b){
//return (a.y)-(b.y);
if(a.y==b.y){
return (a.x-b.x);
}
else{
return (a.y-b.y);
}
}
}
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[1000000]; // 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 void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static int arr[];static int size[];
public static void main(String[] args) throws IOException {
Reader s=new Reader();
int n=s.nextInt();
int m=s.nextInt();
arr=new int[n];
size=new int[n];
for(int i=0;i<n;i++){
arr[i]=-1;
size[i]=1;
}
for(int i=0;i<m;i++){
int l=s.nextInt();
if(l>0){
int a=s.nextInt()-1;
for(int j=0;j<l-1;j++){
int b=s.nextInt()-1;
union(find(a),find(b));
}
}
}
for(int i=0;i<n;i++){
System.out.print(-1*arr[find(i)]+" ");
}
}
static int find(int n){
while(arr[n]>=0){
// arr[n]=arr[arr[n]];
n=arr[n];
}
return n;
}
static void union(int a,int b){
if(a==b){
return;
}
if(size[a]>=size[b]){
arr[a]+=arr[b];
arr[b]=a;
if(size[a]==size[b]){
size[a]++;
}
return;
}
// System.out.println(a+" "+arr[a]+" "+b+" "+arr[b]);
arr[b]+=arr[a];
arr[a]=b;
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 ||
n % 3 == 0)
return false;
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long fast_pow(long base,long n,long M)
{
if(n==0)
return 1;
if(n==1)
return base;
long halfn=fast_pow(base,n/2,M);
if(n%2==0)
return ( halfn * halfn ) % M;
else
return ( ( ( halfn * halfn ) % M ) * base ) % M;
}
static long findMMI_fermat(long n,int M)
{
return fast_pow(n,M-2,M);
}
}
| Java | ["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"] | 2 seconds | ["4 4 1 4 4 2 2"] | null | Java 8 | standard input | [
"dsu",
"dfs and similar",
"graphs"
] | a7e75ff150d300b2a8494dca076a3075 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$. | 1,400 | Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. | standard output | |
PASSED | e2c65d94aff687a931d8e2ad090c805d | train_002.jsonl | 1545143700 | You are given a forest — an undirected graph with $$$n$$$ vertices such that each its connected component is a tree.The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.You task is to add some edges (possibly zero) to the graph so that it becomes a tree and the diameter of the tree is minimal possible.If there are multiple correct answers, print any of them. | 256 megabytes | import java.util.*;
import java.io.*;
public class E527 {
static ArrayList<Integer> [] adj;
static int [] depth;
static int deepest, bestDepth;
static boolean [] vis;
static int [] parent;
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt(); int m = sc.nextInt();
adj = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>();
for (int i = 0; i < m; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
adj[u].add(v);
adj[v].add(u);
}
ArrayList<Pair> middle = new ArrayList<>();
vis = new boolean[n + 1];
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
depth = new int[n + 1];
deepest = i; bestDepth = 0;
parent = new int[n + 1];
dfs(i, -1, 0);
if (bestDepth == 0) {
Pair add = new Pair();
add.node = i; add.diam = 0;
middle.add(add);
continue;
}
bestDepth = 0;
dfs(deepest, -1, 0);
int now = deepest;
while (depth[now] > (bestDepth + 1) / 2) {
now = parent[now];
}
Pair add = new Pair();
add.node = now; add.diam = bestDepth;
middle.add(add);
ans = Math.max(ans, bestDepth);
}
Collections.sort(middle, Comparator.comparingInt(q -> -q.diam));
if (middle.size() == 1) {
out.println(ans);
} else {
Pair first = middle.get(0); int connect = first.node;
int sz = middle.size();
if (middle.size() == 2) {
ans = Math.max(ans, 1 + (first.diam + 1) / 2 + (middle.get(1).diam + 1) / 2);
} else {
ans = Math.max(ans, 2 + (middle.get(1).diam + 1) / 2 + (middle.get(2).diam + 1) / 2);
ans = Math.max(ans, 1 + (first.diam + 1) / 2 + (middle.get(1).diam + 1) / 2);
}
out.println(ans);
for (int i = 1; i < sz; i++) {
out.println(connect + " " + middle.get(i).node);
}
}
out.close();
}
static class Pair {
int node; int diam;
}
static void dfs(int cur, int par, int dep) {
depth[cur] = dep;
vis[cur] = true;
parent[cur] = par;
if (depth[cur] > bestDepth) {
bestDepth = depth[cur];
deepest = cur;
}
for (Integer next: adj[cur]) {
if (next != par) {
dfs(next, cur, dep + 1);
}
}
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4 2\n1 2\n2 3", "2 0", "3 2\n1 3\n2 3"] | 2 seconds | ["2\n4 2", "1\n1 2", "2"] | NoteIn the first example adding edges (1, 4) or (3, 4) will lead to a total diameter of 3. Adding edge (2, 4), however, will make it 2.Edge (1, 2) is the only option you have for the second example. The diameter is 1.You can't add any edges in the third example. The diameter is already 2. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"dfs and similar",
"trees"
] | 90cf88fde1c23b6f3a85ee75d3a6a3ec | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 1000$$$, $$$0 \le m \le n - 1$$$) — the number of vertices of the graph and the number of edges, respectively. Each of the next $$$m$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 \le v, u \le n$$$, $$$v \ne u$$$) — the descriptions of the edges. It is guaranteed that the given graph is a forest. | 2,000 | In the first line print the diameter of the resulting tree. Each of the next $$$(n - 1) - m$$$ lines should contain two integers $$$v$$$ and $$$u$$$ ($$$1 \le v, u \le n$$$, $$$v \ne u$$$) — the descriptions of the added edges. The resulting graph should be a tree and its diameter should be minimal possible. For $$$m = n - 1$$$ no edges are added, thus the output consists of a single integer — diameter of the given tree. If there are multiple correct answers, print any of them. | standard output | |
PASSED | 4cfbc114ca9d5a11d3093aa24473446c | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.*;
public class Main{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
char[][] a = new char[input.nextInt()][input.nextInt()];
for(int i = 0; i < a.length; i++){
String s = input.next();
for(int j = 0; j < a[i].length; j++){
a[i][j] = s.charAt(j);
}
}
for(int i = 0; i < a.length; i++){
int y = 0;
for(int j = 0; j < a[i].length; j++)
if(a[i][j] == '*') y++;
if(y == 0)
for(int j = 0; j < a[i].length; j++)
a[i][j] = '0';
else break;
}
for(int i = a.length - 1; i >= 0; i--){
int y = 0;
for(int j = 0; j < a[i].length; j++)
if(a[i][j] == '*') y++;
if(y == 0)
for(int j = 0; j < a[i].length; j++)
a[i][j] = '0';
else break;
}
for(int i = 0; i < a[0].length; i++){
int y = 0;
for(int j = 0; j < a.length; j++)
if(a[j][i] == '*') y++;
if(y == 0)
for(int j = 0; j < a.length; j++)
a[j][i] = '0';
else break;
}
for(int i = a[0].length - 1; i >= 0; i--){
int y = 0;
for(int j = 0; j < a.length; j++)
if(a[j][i] == '*') y++;
if(y == 0)
for(int j = 0; j < a.length; j++)
a[j][i] = '0';
else break;
}
for(int i = 0; i < a.length; i++){
int y = 0;
for(int j = 0; j < a[i].length; j++){
if(a[i][j] != '0'){
System.out.print(a[i][j]); y++;
}
}
if(y > 0)
System.out.println();
}
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 68bb83bee232f35b05f8fc8f2f6cfdb4 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class ProblemA_14 {
final boolean ONLINE_JUDGE=System.getProperty("ONLINE_JUDGE")!=null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok=new StringTokenizer("");
void init() throws FileNotFoundException{
if (ONLINE_JUDGE){
in=new BufferedReader(new InputStreamReader(System.in));
out =new PrintWriter(System.out);
}
else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException{
while(!tok.hasMoreTokens()){
tok=new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException{
return Integer.parseInt(readString());
}
public static void main(String[] args){
new ProblemA_14().run();
}
public void run(){
try{
long t1=System.currentTimeMillis();
init();
solve();
out.close();
long t2=System.currentTimeMillis();
System.err.println("Time = "+(t2-t1));
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
void solve() throws IOException{
int n=readInt();
int m=readInt();
char[][] c=new char[n][m];
for (int i=0; i<n; i++){
c[i]=readString().toCharArray();
}
int up=n;
a: for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
if (c[i][j]=='*'){
up=i;
break a;
}
}
}
int down=0;
a: for (int i=n-1; i>=up; i--){
for (int j=0; j<m; j++){
if (c[i][j]=='*'){
down=i;
break a;
}
}
}
int left=m;
a: for (int j=0; j<m; j++){
for (int i=up; i<=down; i++){
if (c[i][j]=='*'){
left=j;
break a;
}
}
}
int right=0;
a: for (int j=m-1; j>=left; j--){
for (int i=up; i<=down; i++){
if (c[i][j]=='*'){
right=j;
break a;
}
}
}
for (int i=up; i<=down; i++){
for (int j=left; j<=right; j++){
out.print(c[i][j]);
}
out.println();
}
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | d2f6f1f89170853c15e42f626e2babaa | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Solution14A {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException{
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException{
while(!tok.hasMoreTokens()){
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException{
return Integer.parseInt(readString());
}
long readLong() throws IOException{
return Long.parseLong(readString());
}
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
public static void main(String[] args){
new Solution14A().run();
}
public void run(){
try{
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = "+(t2-t1));
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
static class Utils {
private Utils() {}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
final int MAGIC_VALUE = 50;
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
void solve() throws IOException{
int n = readInt();
int m = readInt();
char[][] map = new char[n][m];
int minX = m;
int minY = n;
int maxX = -1;
int maxY = -1;
for(int i = 0; i < n; i++){
map[i] = readString().toCharArray();
for(int j = 0; j < m; j++){
if(map[i][j] == '*'){
maxX = max(j, maxX);
maxY = max(i, maxY);
minX = min(j, minX);
minY = min(i, minY);
}
}
}
for(int i = minY; i <= maxY; i++){
for(int j = minX; j <= maxX; j++)
out.print(map[i][j]);
out.println();
}
}
static double distance(long x1, long y1, long x2, long y2){
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long gcd(long a, long b){
while(a != b){
if(a < b) a -=b;
else b -= a;
}
return a;
}
static long lcm(long a, long b){
return a * b /gcd(a, b);
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | c3126a1488b50c6a0a2840cfa33c3cd5 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes |
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][]a = new char [n][m];
for (int i = 0; i < n; i++) {
a[i] = sc.next().toCharArray();
}
int max_x = 0,max_y = 0,min_x = n,min_y = m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] =='*'){
max_x = Math.max(max_x, i);
max_y = Math.max(max_y, j);
min_x = Math.min(min_x, i);
min_y = Math.min(min_y, j);
}
}
}
for (int i = min_x; i <=max_x; i++) {
for (int j = min_y; j <=max_y; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 37ab4b1e19165ed04a2bb125a38d3a73 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class A {
public static void main(String[] Args) throws IOException {
new A().solve();
}
static int[] dx_ = { 0, 0, 1, -1 };
static int[] dy_ = { 1, -1, 0, 0 };
ArrayList<Integer>[] graph;
ArrayList<Point> map = new ArrayList<Point>();
// DISCUSS TIME LIMIT EXCEEDED!!!! PROPER DATA STRUCTURE!!!
// Use array if possible or the most efficient data structure
void solve() throws IOException {
int r = si(), c = si();
int rmin = r, rmax = 0, cmin = c, cmax = 0;
char[][] v = new char[r][c];
for (int i = 0; i < r; i++) {
v[i] = ss().toCharArray();
if (new String(v[i]).contains("*")) {
rmin = Math.min(rmin, i);
rmax = Math.max(rmax, i);
}
for (int j = 0; j < v[i].length; j++) {
if (v[i][j] == '*') {
cmin = Math.min(cmin, j);
cmax = Math.max(cmax, j);
}
}
}
for (int i = rmin; i <= rmax; i++) {
for (int j = cmin; j <= cmax; j++) {
System.out.print(v[i][j]);
}
System.out.println("");
}
}
// ----------------------- Library ------------------------
// ----------------------- GRAPH ------------------------
/**
* important for speed!!! PrintWriter out=new PrintWriter(new
* OutputStreamWriter(System.out)); out.print(...); out.close();
*
* @param v
*/
// leap year is 29
int[] year = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
void initSystem() throws IOException {
if (br != null)
br.close();
br = new BufferedReader(new InputStreamReader(System.in));
}
void initFile() throws IOException {
if (br != null)
br.close();
br = new BufferedReader(new InputStreamReader(new FileInputStream(
"input.txt")));
}
void printWriter() {
try {
PrintWriter pr = new PrintWriter("output.txt");
pr.println("hello world");
pr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void comparator() {
Point[] v = new Point[10];
Arrays.sort(v, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
if (a.x != b.x)
return -(a.x - b.x);
return a.y - b.y;
}
});
}
double distance(Point a, Point b) {
double dx = a.x - b.x, dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
Scanner in = new Scanner(System.in);
String ss() {
return in.next();
}
char[] sc() {
return in.next().toCharArray();
}
String sline() {
return in.nextLine();
}
int si() {
return in.nextInt();
}
long sl() {
return in.nextLong();
}
int[] sai(int n) {
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
return a;
}
int[] si(int n) {
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
return a;
}
String[] ss(int n) {
String[] a = new String[n];
for (int i = 0; i < a.length; i++) {
a[i] = ss();
}
return a;
}
int[] sai_(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
return a;
}
BufferedReader br;
StringTokenizer tokenizer;
{
br = new BufferedReader(new InputStreamReader(System.in));
}
void tok() throws IOException {
tokenizer = new StringTokenizer(br.readLine());
}
int toki() throws IOException {
return Integer.parseInt(tokenizer.nextToken());
}
int[] rint(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
a[i] = Integer.parseInt(tokenizer.nextToken());
return a;
}
int[] rint_(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = Integer.parseInt(tokenizer.nextToken());
return a;
}
String[] rstrlines(int n) throws IOException {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = br.readLine();
}
return a;
}
long tokl() {
return Long.parseLong(tokenizer.nextToken());
}
double tokd() {
return Double.parseDouble(tokenizer.nextToken());
}
String toks() {
return tokenizer.nextToken();
}
String rline() throws IOException {
return br.readLine();
}
List<Integer> toList(int[] a) {
List<Integer> v = new ArrayList<Integer>();
for (int i : a)
v.add(i);
return v;
}
static void pai(int[] a) {
System.out.println(Arrays.toString(a));
}
static int toi(Object s) {
return Integer.parseInt(s.toString());
}
static int[] dx3 = { 1, -1, 0, 0, 0, 0 };
static int[] dy3 = { 0, 0, 1, -1, 0, 0 };
static int[] dz3 = { 0, 0, 0, 0, 1, -1 };
static int[] dx = { 1, 0, -1, 1, -1, 1, 0, -1 }, dy = { 1, 1, 1, 0, 0, -1,
-1, -1 };
static int INF = 2147483647; // =2^31-1 // -8
static long LINF = 922337203854775807L; // -8
static short SINF = 32767; // -32768
// finds GCD of a and b using Euclidian algorithm
public int GCD(int a, int b) {
if (b == 0)
return a;
return GCD(b, a % b);
}
static List<String> toList(String[] a) {
return Arrays.asList(a);
}
static String[] toArray(List<String> a) {
String[] o = new String[a.size()];
a.toArray(o);
return o;
}
static int[] pair(int... a) {
return a;
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 6d89d38ccc9af7966ef279c4e11079c3 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main4 {
static class Main {
public Scanner sc;
public void run() {
int n = sc.nextInt(), m = sc.nextInt();
char[][] field = new char[n][];
for (int i = 0; i < n; i++)
field[i] = sc.next().toCharArray();
int minx = Integer.MAX_VALUE, miny = Integer.MAX_VALUE;
int maxx = 0, maxy = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (field[i][j] == '*') {
minx = Math.min(minx, j);
miny = Math.min(miny, i);
maxx = Math.max(maxx, j);
maxy = Math.max(maxy, i);
}
}
}
for (int i = miny; i <= maxy; i++) {
for (int j = minx; j <= maxx; j++) {
System.out.print(field[i][j]);
}
System.out.println();
}
}
}
public static void main(String[] args) {
Main main = new Main();
try { main.sc = new Scanner(new FileInputStream("test.in")); }
catch (FileNotFoundException e) { main.sc = new Scanner(System.in); }
main.run();
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | df1c5670559f9f50c5f5c2e702c6d0f5 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.Scanner;
public class Letter {
public static void main(String[]args){
Scanner reader=new Scanner(System.in);
int n,m;
n=reader.nextInt();
m=reader.nextInt();
reader.nextLine();
int minCol = m, minRow = n, maxCol = 0, maxRow = 0;
String[]str=new String[n];
for(int i=0;i<n;i++){
str[i]=reader.nextLine();
int f=str[i].indexOf('*');
if(f<0)continue;
int l=str[i].lastIndexOf('*');
minRow=Math.min(minRow,i);
maxRow=Math.max(maxRow, i);
minCol=Math.min(minCol, f);
maxCol=Math.max(maxCol,l);
}
reader.close();
for(int i=0;i<maxRow-minRow+1;i++){
System.out.println(str[i+minRow].substring(minCol, maxCol+1));
}
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 0c25fe87206a51fdc2567da55b66f73a | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class Letter {
public static final int MX = 100;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
PrintWriter output = new PrintWriter(System.out);
int n, m, mxl = MX, mxr = -1, mxu = MX, mxd = -1;
String[] tmp = input.nextLine().split(" ");
n = Integer.parseInt(tmp[0]); m = Integer.parseInt(tmp[1]);
String[] maze = new String[n];
for(int i = 0; i < n; i++) {
maze[i] = input.nextLine();
for(int j = 0; j < m; j++) {
if(maze[i].charAt(j) == '*') {
mxl = Math.min(mxl, j);
mxr = Math.max(mxr, j);
mxu = Math.min(mxu, i);
mxd = Math.max(mxd, i);
}
}
}
for(int i = mxu; i <= mxd; i++) {
for(int j = mxl; j <= mxr; j++) {
output.print(maze[i].charAt(j));
}
if(i != mxd) output.println();
}
input.close();
output.close();
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | da2f7ba6bcff416629e26751b116788d | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int I=s.nextInt();
int J=s.nextInt();
char p[][]=new char[I][J];
int uMinI=I;
int uMinJ=J;
int uMaxI=0;
int uMaxJ=0;
p[0]=s.nextLine().toCharArray();
for(int i=0;i<I;i++)
{
p[i]=s.nextLine().toCharArray();
}
for(int i=0;i<I;i++)
{
for(int j=0;j<J;j++)
{
if(p[i][j]=='*')
{
if(uMinI>i)
{
uMinI=i;
}
if(uMinJ>j)
{
uMinJ=j;
}
if(uMaxI<i)
{
uMaxI=i;
}
if(uMaxJ<j)
{
uMaxJ=j;
}
}
}
}
for(int i=uMinI;i<=uMaxI;i++)
{
for(int j=uMinJ;j<=uMaxJ;j++)
{
System.out.print(p[i][j]);
}
System.out.print('\n');
}
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | fcb8ce82120749ea7f5c873479e01c44 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.Scanner;
public class p14a {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
char[][] grid = new char[n][m];
for (int i = 0; i < grid.length; i++) {
String te = in.next();
grid[i] = te.toCharArray();
}
int min = 999;
int max = -1;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if(grid[i][j] == '*') {
min = Math.min(min, i);
max = Math.max(max, i);
}
}
}
int minR = 999;
int maxR = -1;
for (int i = 0; i < grid[0].length; i++) {
for (int j = 0; j < grid.length; j++) {
if(grid[j][i] == '*') {
minR = Math.min(minR, i);
maxR = Math.max(maxR, i);
}
}
}
for (int i = min; i <= max; i++) {
for (int j = minR; j <=maxR; j++) {
System.out.print(grid[i][j]);
}
System.out.println();
}
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | e069cf26d04923e431a00aa421090fca | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.*;
public class Letter14A
{
public static void main(String[] args)
{
// Set up scanner
Scanner sc = new Scanner(System.in);
// System.out.println("Enter n");
int n = sc.nextInt();
// System.out.println("Enter m");
int m = sc.nextInt();
String[][] a = new String[n][m];
int leastrow = -1;
int bigrow = -1;
int leastcol = -1;
int bigcol = -1;
for (int r=0; r<n; r++)
{
// System.out.println("Input next row");
String row = sc.next();
for (int c=0; c<m; c++)
{
String ch = row.substring(c, c+1);
a[r][c] = ch;
// If we see a *, the values can change
if (ch.equals("*"))
{
bigrow = r;
if (leastrow == -1)
{
leastrow = r;
}
if (leastcol == -1 || c < leastcol)
{
leastcol = c;
}
if (c > bigcol)
{
bigcol = c;
}
}
}
}
for (int r = leastrow; r<=bigrow; r++)
{
for (int c = leastcol; c<= bigcol; c++)
{
System.out.print(a[r][c]);
}
System.out.println();
}
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 7d0f6d1ace7a179a5fe38dcd82f97319 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
in.nextLine();
char[][] l = new char[n][m];
for (int i = 0; i < n; i++) {
String s = in.nextLine();
for (int j = 0; j < m; j++)
l[i][j] = s.charAt(j);
}
int left = 0;
int right = m - 1;
int top = 0;
int bot = n - 1;
boolean[] has_vert = new boolean[m];
boolean[] has_hor = new boolean[n];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (l[i][j] == '*')
has_hor[i] = true;
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++)
if (l[i][j] == '*')
has_vert[j] = true;
int j = 0;
while (true) {
if (has_vert[j]) {
left = j;
break;
}
j++;
}
j = m - 1;
while (true) {
if (has_vert[j]) {
right = j;
break;
}
j--;
}
int i = 0;
while (true) {
if (has_hor[i]) {
top = i;
break;
}
i++;
}
i = n - 1;
while (true) {
if (has_hor[i]) {
bot = i;
break;
}
i--;
}
for (i = top; i <= bot; i++) {
for (j = left; j <= right; j++)
out.print(l[i][j]);
out.println();
}
out.close();
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | eb27d84bcf9c6683b168734998a1dd13 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.LinkedList;
public class Main {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
// BufferedReader input = new BufferedReader(new FileReader("input.txt"));
// BufferedWriter output = new BufferedWriter(new FileWriter("output.txt"));
StreamTokenizer in = new StreamTokenizer(input);
in.nextToken();
int n = (int)in.nval;
in.nextToken();
int m = (int)in.nval;
input.readLine();
StringBuffer[] mas = new StringBuffer[n];
for (int i = 0; i < n; i++) {
mas[i] = new StringBuffer(input.readLine());
}
int[] left = new int[n];
int[] right = new int[n];
String star = new String("*");
for (int i = 0; i < n; i++) {
left[i] = mas[i].indexOf(star);
mas[i].reverse();
right[i] = mas[i].indexOf(star);
if (right[i] != -1) {
right[i] = m - right[i] - 1;
}
mas[i].reverse();
}
int minl = Integer.MAX_VALUE;
int maxr = Integer.MIN_VALUE;
for (int j = 0; j < n; j++) {
if ((left[j] < minl) && left[j] != -1) {
minl = left[j];
}
if (right[j] > maxr) {
maxr = right[j];
}
}
for (int i = 0; i < n; i++) {
mas[i] = new StringBuffer(mas[i].substring(minl, maxr + 1));
}
for (int i = 0; i < n; i++) {
if (left[i] == -1) {
int j = i;
while ((j < n) && (left[j] == -1)) {
j++;
}
if ((j == n) || (i == 0)) {
i = j - 1;
}
else {
output.write(mas[i].toString() + '\n');
}
}
else {
output.write(mas[i].toString() + '\n');
}
}
input.close();
output.close();
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | ad28c975d2f9265d1a83a29de1e5df51 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n, m;
n = cin.nextInt();
m = cin.nextInt();
cin.nextLine();
int x1 = 51, x2 = -1, y1 = 51, y2 = -1;
String[] s = new String[n];
for(int i=0; i<n; i++) {
s[i] = cin.nextLine();
for(int j=0; j<m; j++) {
if(s[i].charAt(j) == '*') {
if(i < x1) x1 = i;
if(i > x2) x2 = i;
if(j < y1) y1 = j;
if(j > y2) y2 = j;
}
}
}
for(int i=x1; i<=x2; i++) {
for(int j=y1; j<=y2; j++) {
System.out.print(s[i].charAt(j));
}
System.out.println();
}
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 294c1ababe7c23d531216f771e79ee78 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.io.BufferedReader;
import static java.lang.Math.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
public class Main implements Runnable {
public static final String taskname = "A";
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
public static void main(String[] args) {
new Thread(new Main()).start();
}
void solve() throws IOException {
int n = nextInt(), m = nextInt();
char[][] c = new char[n][];
for(int i = 0; i < n; ++i)
c[i] = nextLine().toCharArray();
int left = 100, right = -1, top = 100, bottom = -1;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j) {
if(c[i][j] == '*') {
if(i < left) left = i;
if(i > right) right = i;
if(j < top) top = j;
if(j > bottom) bottom = j;
}
}
for(int i = left; i <= right; ++i) {
for(int j = top; j <= bottom; ++j)
out.print(c[i][j]);
out.println();
}
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader(new File(taskname + ".in")));
out = new PrintWriter(System.out);
// out = new PrintWriter(new File(taskname + ".out"));
solve();
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String nextLine() throws IOException {
tok = null;
return in.readLine();
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
} | Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 3033365488405a8b790a9a87f0a53ed5 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: DOAN Minh Quy
* Date: 7/25/13
* Time: 12:45 PM
*/
public class Task14A {
public static void main(String[] args) {
new Task14A().run();
}
void run() {
Scanner reader = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Task14AWriter writer = new Task14AWriter(System.out);
int n = reader.nextInt();
int m = reader.nextInt();
char[][] send = new char[n][m];
for( int i = 0 ; i < n ; ++i){
send[i] = reader.next().toCharArray();
}
int top = -1, bottom = -1 , left = -1, right = -1;
for(int j = 0 ; j < m && left == -1 ; ++j){
for(int i = 0 ; i < n && left == -1 ; ++i){
if ( send[i][j] == '*' ){
left = j;
}
}
}
for(int j = m-1 ; j >= 0 && right == -1 ; --j){
for(int i = 0 ; i < n && right == -1 ; ++i){
if ( send[i][j] == '*' ){
right = j;
}
}
}
for(int i = 0 ; i < n && top == -1 ; ++i){
for(int j = 0 ; j < m && top == -1 ; ++j){
if ( send[i][j] == '*' ){
top = i;
}
}
}
for(int i = n-1 ; i >= 0 && bottom == -1 ; --i){
for(int j = 0 ; j < m && bottom == -1 ; ++j){
if ( send[i][j] == '*' ){
bottom = i;
}
}
}
for(int i = top ; i <= bottom ; ++i){
for(int j = left ; j <= right ; ++j){
writer.print(send[i][j]);
}
writer.println();
}
writer.close();
}
}
class Task14AWriter {
private final PrintWriter writer;
public Task14AWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
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();
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | ba9faaac63b7da21720027b0fc0fd6db | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | //~ 20:38:19
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] p = toIntArray(br.readLine());
String[] lines = new String[p[0]];
for(int i=0;i<lines.length;i++){
lines[i] = br.readLine();
}
String out = process(lines);
bw.write(out,0,out.length());
br.close();
bw.close();
}
static String process(String[] lines){
int left=lines[0].length(),right=-1;
int top=-1,bot=-1;
for(int i=0;i<lines.length;i++){
String cl = lines[i];
if(top==-1){
if(cl.indexOf('*')!=-1){
top = i;
}
}
int index = cl.indexOf('*');
int lastIndex = cl.lastIndexOf('*');
if(top!=-1 && index !=-1){
left = Math.min(left,index);
bot = i;
right = Math.max(right,lastIndex);
}
}
String out = "";//new String[bot-top+1];
for(int i=top;i<=bot;i++){
out += lines[i].substring(left,right+1)+"\r\n";
}
return out;
}
static int[] toIntArray(String line){
String[] p = line.trim().split("\\s+");
int[] out = new int[p.length];
for(int i=0;i<out.length;i++) out[i] = Integer.valueOf(p[i]);
return out;
}
static String toFnOut(String fn){
if(fn.lastIndexOf('.')!=-1){
return fn.substring(0,fn.lastIndexOf('.'))+".out";
}else return fn + ".out";
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 830aafc61b8d6ebd4679307900216a90 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes | import java.util.*;
public class CBR14A {
static boolean testh(int i, String[] a) {
for (int j = 0; j <= a[i].length()-1; j++)
if (a[i].charAt(j) == "*".toCharArray()[0])
return false;
return true;
}
static boolean testv(int i, String[] a) {
for (int j = 0; j <= a.length-1; j++)
if (a[j].charAt(i) == "*".toCharArray()[0])
return false;
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
String a[] = new String[n];
for (int i=0; i<n; i++)
a[i] = in.next();
int u = 0, d = 0, l = 0, r = 0;
for (int i = 0; i <= n-1; i++) {
if (testh(i, a))
u++;
else
break;
}
for (int i = n-1; i >=0; i--) {
if (testh(i, a))
d++;
else
break;
}
for (int i = 0; i <= m-1; i++) {
if (testv(i, a))
l++;
else
break;
}
for (int i = m-1; i >=0; i--) {
if (testv(i, a))
r++;
else
break;
}
for (int i=u; i<=n-1-d; i++)
for(int j=l; j<=m-1-r; j++)
if (j == (m-1-r))
System.out.println(a[i].charAt(j));
else
System.out.print(a[i].charAt(j));
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 0f2c7e383176d42a3a855f07da785641 | train_002.jsonl | 1274283000 | A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides. | 64 megabytes |
import static java.lang.Math.*;
import java.util.Scanner;
@SuppressWarnings("unused")
public class round14A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char [][] x = new char [n][m];
for(int i = 0 ; i < n ; ++i)
x[i] = sc.next().toCharArray();
int left = m - 1;
int right = 0;
int up = n - 1;
int down = 0;
for(int i = 0 ; i < n ; ++i){
for(int j = 0 ; j < m ; ++j){
if(x[i][j] == '.')
continue;
if(i < up)
up = i;
if(j < left)
left = j;
if(i > down)
down = i;
if(j > right)
right = j;
}
}
for(int i = up ; i <= down ; ++i){
for(int j = left ; j <= right ; ++j){
System.out.print(x[i][j]);
}
System.out.println();
}
}
}
| Java | ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"] | 1 second | ["***\n*..\n***\n*..\n***", "***\n*.*\n***"] | null | Java 6 | standard input | [
"implementation"
] | d715095ff068f4d081b58fbfe103a02c | The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square. | 800 | Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better. | standard output | |
PASSED | 35473d2813063c7bd7dfb9b285701cac | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Soly
{
static final int INF = Integer.MAX_VALUE;
static int mergeSort(int[] a,int [] c, int begin, int end)
{
int inversion=0;
if(begin < end)
{
inversion=0;
int mid = (begin + end) >>1;
inversion+= mergeSort(a,c, begin, mid);
inversion+=mergeSort(a, c,mid + 1, end);
inversion+=merge(a,c, begin, mid, end);
}
return inversion;
}
/////////////////////////////////////////////////////
static int merge(int[] a,int[]c, int b, int mid, int e)
{
int n1 = mid - b + 1;
int n2 = e - mid;
int[] L = new int[n1+1], R = new int[n2+1];
int[] L1 = new int[n1+1], R1 = new int[n2+1];
//inversion
int inversion=0;
for(int i = 0; i < n1; i++) {
L[i] = a[b + i];
L1[i] = c[b + i];
}
for(int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
R1[i] = c[mid + 1 + i];
}
L[n1] = R[n2] = INF;
for(int k = b, i = 0, j = 0; k <= e; k++)
if(L[i] <= R[j]){
a[k] = L[i];
c[k] = L1[i++];
}
else
{
a[k] = R[j];
c[k] = R1[j++];
inversion=inversion+(n1-i);
}
return inversion;
}
//////////////////////////////////////////////////////////////
static int l;
static int va(char[]a){
Stack<Character>s= new Stack<>();
int op=0,c=0;
for (int p = 0; p < a.length; p++) {
if (a[p]==')'){
if (!s.isEmpty()){
if (s.peek()=='('){
op--;
s.pop();
}
else{
++c;
s.push(')');
}
}
else {
++c;
s.push(')');
}
}
else if(a[p]=='(') {
++op;
if (!s.isEmpty()){
if (s.peek()=='('){
s.push(a[p]);
}
else s.push('(');
}
else s.push('(');
}
}
if (s.isEmpty())return 0;
else {
if (op>0&&c>0)return 50;
if (s.peek()=='('){
//1 opening
l=s.size();
return 1;
}
else {
//2 closing
l=s.size();
return 2;
}
}
}
/////////////////////////////////////////////////////////////////////////////////
static public int maxSubArray(int[] nums) {
boolean s= false,s2=false;
int ans2=Integer.MIN_VALUE;
int ans=0,sum=0;
for (int i = 0; i < nums.length; i++) {
if (nums[i]>=0)s=true;
if (nums[i]<0)s2=true;
ans2=Math.max(ans2,nums[i]);
if (sum+nums[i]>0)sum+=nums[i];
else sum=0;
ans=Math.max(ans,sum);
}
if (!s2)return ans;
if (!s)return ans2;
else {
return ans;
}
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
try (PrintWriter or = new PrintWriter(System.out)) {
int t = in.nextInt();
while (t-->0){
long n =in.nextLong(),a=in.nextLong(),b=in.nextLong();
if (n==1){
or.println(n*a);
continue;
}
if (n%2==0)or.println(Math.min(a*n,b*(n/2)));
else or.println(Math.min(a*(n-1),b*(n/2))+a);
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public 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();
}
}
}
class Triple{
int r,c,x,y;
public Triple(int r, int c, int x, int y) {
this.r = r;
this.c = c;
this.x = x;
this.y = y;
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 43ad04bdcb057e5fd2bd4d269d495959 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes |
import java.util.Scanner;
public class WaterBuyingTwo
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int q;
q=Integer.parseInt(in.nextLine().trim());
for(int i=0 ; i<q ; i++)
{
String[] temp = in.nextLine().split(" ");
costing(Long.valueOf(temp[0]),Long.valueOf(temp[1]),Long.valueOf(temp[2]));
}
//costing(array,q);
}
public static void costing(long n ,long a,long b)
{
if(n%2 == 0)
{
System.out.println((n/2)*b > n*a ? n*a : (n/2)*b);
}
if(n == 1)
{
System.out.println(a);
}
else if(n%2 == 1)
{
System.out.println(((n/2)*b+a) > (n*a) ? (n*a) : ((n/2)*b+a));
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 8597843e2833644076edcefc95d8aa9a | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
public class WaterBuy {
public static void main(String[] args) throws Exception {
MyScanner sc = new MyScanner();
int q=sc.ni();
while(q-->0){
long n = sc.nl();
long a = sc.nl();
long b = sc.nl();
long ans = 0L;
ans = (n/2)*Math.min(2*a,b)+(n%2)*a;
System.out.println(ans);
}
}
/////////// TEMPLATE FROM HERE /////////////////
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
float nf() {
return Float.parseFloat(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 8555fa3d6e5489357c96851e8bee42e0 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
//Mann Shah [ DAIICT ].
public class Main {
public static void main(String[] Args) {
Scanner in = new Scanner(System.in);
int mod = 1000000007;
int q = in.nextInt();
while(q-->0) {
long n = in.nextLong();
int a = in.nextInt();
int b = in.nextInt();
long ans= n*a;
long ans2=0;
if(n%2==0) {
ans2= (n/2)*b;
}
else {
ans2= ((n/2)*b)+a;
}
System.out.println(Math.min(ans2, ans));
}
in.close();
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 37b1499db2064b4655af533aa6e21096 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | //package CodeForces;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class waterBuying {
public static void main(String []args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
int q=Integer.parseInt(br.readLine());
while(q-->0) {
String[] s=br.readLine().split(" ");
long n=Long.parseLong(s[0]);
int a=Integer.parseInt(s[1]);
int b=Integer.parseInt(s[2]);
if(n==1) {
System.out.println(a);
}
else if(n%2==0) {
long ans1=n*a;
long ans2=(n/2)*b;
System.out.println(Math.min( ans1, ans2 ));
}
else {
long ans1=n*a;
long ans2=a + ((n-1)/2)*b ;
System.out.println(Math.min( ans1 , ans2 ));
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 6821d24d649360f90b8a0261acc591b9 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
public class water {
static Scanner s= new Scanner (System.in);
public static void main(String [] args) {
int n=s.nextInt();
long i,liters,price1,price2,compare1=0,compare2=0;
for(i=0;i<n;i++) {
liters=s.nextLong();
price1=s.nextLong();
price2=s.nextLong();
if(liters==1)
System.out.println(price1);
else if(liters%2!=0) {
liters=liters-1;
compare2=((liters/2)*price2)+price1;
liters =liters+1;
compare1=liters*price1;
System.out.println(Math.min((long)compare1,(long)compare2));
}
else if(liters%2==0) {
compare1=liters*price1;
compare2=(liters/2)*price2;
System.out.println(Math.min((long)compare1,(long)compare2));
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | c6647daf900bc5018d89f7494e53331b | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | //package yahia;
import java.util.*;
public class Main {
public static void main (String []args) {
Scanner sc =new Scanner (System.in);
int n=sc.nextInt();
for (;n>0;--n) {
long q=sc.nextLong();int a=sc.nextInt();int b=sc.nextInt();
if (q%2==0) {
if (b>2*a) {
long w =a*q;
System.out.println(w);
}else {
long w =(long) (.5*q*b);
System.out.println(w);
}
}else{
if (b>2*a) {
long w =a*q;
System.out.println(w);
}else {
long w=(long)((q-1)/2*b+a);
System.out.println(w);
}
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 7385dfef4d66b92bcda6921f3d62fa0a | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | // package PCS;
import java.math.BigInteger;
import java.util.Scanner;
public class BuyWater {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int queries = reader.nextInt();
for (int i=0;i<queries;i++) {
BigInteger water = new BigInteger(reader.next());
BigInteger a = new BigInteger(reader.next());
BigInteger b = new BigInteger(reader.next());
if (a.multiply(new BigInteger("2")).compareTo(b) <= 0) {
System.out.println(water.multiply(a));
continue;
} else {
if (water.equals(new BigInteger("1"))) {
System.out.println(a);
continue;
}
BigInteger multiply = water.divide(new BigInteger("2"));
if (water.divideAndRemainder(new BigInteger("2"))[1].equals(new BigInteger("0"))) {
System.out.println(multiply.multiply(b));
} else {
System.out.println(multiply.multiply(b).add(a));
}
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 5f3e804996fe054e5e84cb90d0247999 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class A {
public static void main(String[] args) throws Exception {
//BufferedReader bufferedReader = new BufferedReader(new FileReader("input.txt"));
//Scanner scanner = new Scanner(bufferedReader);
//Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
FastScanner scanner = new FastScanner();
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
long[] array = scanner.nextLongArray();
long ans;
long n = array[0]; long a = array[1]; long b = array[2];
if (a < (b / 2 + b % 2)) {
ans = a * n;
} else {
ans = (n / 2) * b + (n % 2) * a;
}
System.out.println(ans);
}
}
private static class FastScanner {
private BufferedReader br;
public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); }
public int[] nextIntArray() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
int[] array = new int[strings.length];
for (int i = 0; i < array.length; i++)
array[i] = Integer.parseInt(strings[i]);
return array;
}
public int nextInt() throws IOException { return Integer.parseInt(br.readLine()); }
public long[] nextLongArray() throws IOException {
String line = br.readLine();
String[] strings = line.trim().split("\\s+");
long[] array = new long[strings.length];
for (int i = 0; i < array.length; i++)
array[i] = Long.parseLong(strings[i]);
return array;
}
public long nextLong() throws IOException { return Long.parseLong(br.readLine()); }
public String nextLine() throws IOException { return br.readLine(); }
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | b460b35ce22bee048db4a76102bccff0 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
public class ProblemA {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
long n, a, b, result;
for (int i=0; i < q; i++) {
n = in.nextLong();
a = in.nextLong();
b = in.nextLong();
if (a <= b / 2.0)
System.out.println(a * n);
else {
result = b * (n / 2);
result += (n % 2) * a;
System.out.println(result);
}
}
in.close();
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 647ab8cf06f7a7fc1cc18b4b9bff4c2b | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
public class WaterBuy{
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long a[]=new long[n];
long t=0;
for(int i=0;i<n;i++){
long l=sc.nextLong();
long m=sc.nextLong();
long p=sc.nextInt();
if(m<=p/2||l<2){
//System.out.println("hello");
//System.out.println(m<=p/2);
a[i]=(long)a[i]+(l*m);}
else{
//System.out.println("ho");
t=(long)l/2;
a[i]=(long)a[i]+(t*p);
if(l%2!=0)
a[i]=(long)a[i]+m;
}
}
for(int i=0;i<n;i++)
System.out.println(a[i]);
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 398ee3ef8faa5dc55cdd45d1b67f6926 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.lang.*;
import java.util.*;
public class water_buying{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int q=sc.nextInt();
for(int i=0;i<q;i++){
long n=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
if((2*a)<b){
System.out.println(n*a);
}
else
System.out.println(((n/2)*b + (n%2)*a));
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 61fef36cb946004efdc1ddd3bd8d4078 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class geek {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t=s.nextInt();
while(t-->0) {
long n = s.nextLong();
long a=s.nextLong();
long b=s.nextLong();
long min=Math.min(a,b);
long max=Math.max(a,b);
if(n==1){
System.out.println(a);
}else{
if(n%2==0){
long min1=Math.min(n*a,(n/2)*b);
System.out.println(min1);
}else{
long ans=((n-1)/2)*b+a;
long ans1=(n*a);
System.out.println(Math.min(ans1,ans));
}
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 3ca0e56e2dd00fb4cf294fd1c5e43058 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class Waterbuying {
public long findMinCost(long n, int a, int b) {
if (b >= 2*a) {
return (long)a*n;
}
else {
return a*n + n/2*(b-2*a);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Waterbuying w = new Waterbuying();
int q = sc.nextInt();
long n;
int a,b;
for (int it = 0; it < q;it++) {
n = sc.nextLong();
a = sc.nextInt();
b = sc.nextInt();
System.out.println(w.findMinCost(n, a, b));
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | c7b683f932ee902c61915424d016cba4 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class Waterbuying {
public long findMinCost(long n, int a, int b) {
if (b >= 2*a) {
return (long)a*n;
}
else {
return a*n + n/2*(b-2*a);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Waterbuying w = new Waterbuying();
int q = sc.nextInt();
long n;
int a,b;
for (int it = 0; it < q;it++) {
n = sc.nextLong();
a = sc.nextInt();
b = sc.nextInt();
System.out.println(w.findMinCost(n, a, b));
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 315f6fcf1ae7e1f783905db721b26e3a | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
for (int z = 0; z < q; z++) {
long n = scan.nextLong();
int a = scan.nextInt();
int b = scan.nextInt();
if (n == 1) {
System.out.println(a);
} else if (a == b) {
if (n % 2 == 0) {
System.out.println((b * n) / 2);
} else {
System.out.println(((b * (n - 1)) / 2) + a);
}
} else if (n % 2 == 0) {
System.out.println(Math.min(a * n, ((b * n) / 2)));
} else if (a < b) {
System.out.println(Math.min(((b * (n - 1)) / 2) + a, a * n));
} else {
System.out.println(((b * (n - 1)) / 2) + a);
}
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | d4acc1e1fc8bea2d3da39fba7fcd8529 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Title:CF540_Div3_WaterBuying Description:
*
* @author Susan Cappuccino
* @date 2019年2月20日下午7:01:45
* @version 1.0
*/
public class CF540_Div3_WaterBuying {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
double arr[][] = new double[(int) num][3];
double result[] = new double[(int) num];
DecimalFormat decimalFormat = new DecimalFormat("##0");// 格式化设置
for (int i = 0; i < num; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = sc.nextDouble();
}
}
for (int i = 0; i < num; i++) {
double temp = Math.min(arr[i][1], ((double) arr[i][2]) / 2);
if (arr[i][0] % 2 == 0)
result[i] = arr[i][0] * temp;
else
result[i] = (arr[i][0] - 1) * temp + arr[i][1];
}
for (int i = 0; i < num; i++)
System.out.println(decimalFormat.format(result[i]));
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | ec580c5c216625915eba819ac559fb35 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
double n, a, b, d;
for (int i = 0; i < q; i++) {
n = sc.nextDouble();
a = sc.nextDouble();
b = sc.nextDouble();
d = b / 2.0;
if (a < d) {
System.out.println((long) (n * a));
} else {
System.out.println((long) (Math.floor(n / 2) * b + (n%2 * a)));
}
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 1335d91b6ad6edb35fc48437085e66ad | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
import java.lang.*;
public class solution
{
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
int q=s.nextInt();
long n;
long a,b;
for(int i=0;i<q;i++)
{
n=s.nextLong();
a=s.nextLong();
b=s.nextLong();
if(n==1)
{
System.out.println(a);
}
else
{
if(a<=(b/2))
{
System.out.println(n*a);
}
else
{
long res=(n/2)*b;
res=res+(n%2)*a;
System.out.println(res);
}
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | d22621988e57dce654dab5ba84f87f06 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int z = s.nextInt();
for(int i = 0;i<z;i++){
long n = s.nextLong();long a = s.nextLong();long b = s.nextLong();
long minimum = 0;
if(b<2*a){
minimum =(n/2*b)+(n%2*a);}else{minimum =n*a;};
System.out.println(minimum);
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 88ae6d2c7de6015573d8efe9d14da33b | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
for(int i = in.nextInt();i > 0; i--){
long n = in.nextLong();
long a = in.nextLong();
long b = in.nextLong();
long ans = 0;
if(2*a > b){
if(n%2 == 0){
ans = (n/2)*b;
}
else{
ans = (long)(Math.floor(n/2)*b)+a;
}
}
else{
ans = a*n;
}
System.out.print(ans+"\n");
}
in.close();
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | a91a675c86291a13335afdf951ac54b8 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.*;
public class Div2 {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
long q[]=new long[in.nextShort()], a,b;
long n,ax,bx;
for(short i=0;i<q.length;i++){
n=in.nextLong(); a=in.nextShort(); b=in.nextShort();
if(n==1){
q[i]=n*a;
continue;
}
else if(n%2==0&&n*a<=(n/2)*b){
q[i]=n*a;
continue;
}
else if(n%2==1&&n*a>=((n/2)*b)+a){
q[i]=((n/2)*b)+a;
continue;
}
else if(n%2==1&&n*a<=((n/2)*b)+a){
q[i]=n*a;
continue;
}
else if(n*a>=(n/2)*b&&n%2==0){
q[i]=(n/2)*b;
continue;
}
}
for(short i=0;i<q.length;i++)
System.out.println(q[i]);
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 19ebb8ae46c4232ae8efa1f39a4ba962 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | import java.util.Scanner;
import java.lang.Math;
public class WaterBuyEdit {
private static long minCost (long n, long a, long b) {
return (n/2)*Math.min(2*a, b) + (n % 2) * a;
}
public static void main (String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int numQueries = sc.nextInt();
while (numQueries-- > 0) {
long n = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
System.out.println(minCost(n, a, b));
}
}
}
}
| Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 204d76240dbc2cd9cf2f8d0516928710 | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes | /* _____ ___
/\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ محمد أبوحسن*
/ \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \
/ \ / \ | | | | / \ / \ / \ / \ / \ / \ | /
/ \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/
*/
//M O H A M M A D || A M E E N || S A E D || A B O H A S A N
import java.util.*;
public class Mohammad {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
long n = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
if(a*2<b)
System.out.println(n*a);
else
System.out.println((n/2*b) + (n%2)*a);
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 652e8b934269f1a56afd76c417d1a65c | train_002.jsonl | 1550586900 | Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. | 256 megabytes |
/* _____ ___
/\ /\ | | | | /\ /\ /\ /\ /\ /\ | \ محمد أبوحسن*
/ \ / \ | | |___| /__\ / \ / \ / \ / \ /__\ | \
/ \ / \ | | | | / \ / \ / \ / \ / \ / \ | /
/ \/ \ |_____| | | / \ / \/ \ / \/ \ / \ |___/
*/
//M O H A M M A D || A M E E N || S A E D || A B O H A S A N
import java.util.*;
public class Mohammad {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
long n = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
if(a*2<b)
System.out.println(n*a);
else
System.out.println((n/2*b) + (n%2)*a);
}
}
} | Java | ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"] | 1 second | ["10\n9\n1000\n42000000000000"] | null | Java 8 | standard input | [
"math"
] | beab56c5f7d2996d447320a62b0963c2 | The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \le n_i \le 10^{12}, 1 \le a_i, b_i \le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. | 800 | Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. | standard output | |
PASSED | 6cb20bdfaf032ad66b4eee89d4ffcbbb | train_002.jsonl | 1460729700 | You are given an undirected graph that consists of n vertices and m edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edges with an endpoint in this vertex change the color to red.Find the minimum possible number of moves required to make the colors of all edges equal. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author AlexFetisov
*/
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);
TaskB_CROC solver = new TaskB_CROC();
solver.solve(1, in, out);
out.close();
}
static class TaskB_CROC {
int[] u;
List<Integer> left;
List<Integer> right;
List<Edge>[] g;
int n;
public void solve(int testNumber, InputReader in, PrintWriter out) {
n = in.nextInt();
int m = in.nextInt();
g = new List[n];
for (int i = 0; i < n; ++i) {
g[i] = new ArrayList<Edge>();
}
for (int i = 0; i < m; ++i) {
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
char c = in.nextString().charAt(0);
g[a].add(new Edge(b, c == 'R' ? 0 : 1));
g[b].add(new Edge(a, c == 'R' ? 0 : 1));
}
List<Integer> res1 = solve(0);
List<Integer> res2 = solve(1);
if (res1 == null) {
res1 = res2;
}
if (res2 != null && res1.size() > res2.size()) {
res1 = res2;
}
if (res1 == null) {
out.println(-1);
return;
}
out.println(res1.size());
for (int i : res1) {
out.print((i + 1) + " ");
}
out.println();
}
private List<Integer> solve(int color) {
u = new int[n];
Arrays.fill(u, -1);
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < n; ++i) {
if (u[i] == -1) {
left = new ArrayList<Integer>();
right = new ArrayList<Integer>();
if (!dfs(i, 0, color)) {
return null;
}
if (left.size() < right.size()) {
res.addAll(left);
} else {
res.addAll(right);
}
}
}
return res;
}
boolean dfs(int v, int side, int c) {
if (u[v] != -1) {
return u[v] == side;
}
u[v] = side;
if (side == 0) {
left.add(v);
} else {
right.add(v);
}
for (Edge e : g[v]) {
int newSide = e.color == c ? side : 1 - side;
if (!dfs(e.to, newSide, c)) {
return false;
}
}
return true;
}
class Edge {
int to;
int color;
public Edge(int to, int color) {
this.to = to;
this.color = color;
}
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
}
}
| Java | ["3 3\n1 2 B\n3 1 R\n3 2 B", "6 5\n1 3 R\n2 3 R\n3 4 B\n4 5 R\n4 6 R", "4 5\n1 2 R\n1 3 R\n2 3 B\n3 4 B\n1 4 B"] | 1 second | ["1\n2", "2\n3 4", "-1"] | null | Java 8 | standard input | [
"dfs and similar",
"graphs"
] | 5bb8347fd91f00245c3acb4a5eeaf17f | The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of vertices and edges, respectively. The following m lines provide the description of the edges, as the i-th of them contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the indices of the vertices connected by the i-th edge, and a character ci () providing the initial color of this edge. If ci equals 'R', then this edge is initially colored red. Otherwise, ci is equal to 'B' and this edge is initially colored blue. It's guaranteed that there are no self-loops and multiple edges. | 2,200 | If there is no way to make the colors of all edges equal output - 1 in the only line of the output. Otherwise first output k — the minimum number of moves required to achieve the goal, then output k integers a1, a2, ..., ak, where ai is equal to the index of the vertex that should be used at the i-th move. If there are multiple optimal sequences of moves, output any of them. | standard output | |
PASSED | c96f9954d6f69a8c2a5eeb8935897683 | train_002.jsonl | 1460729700 | You are given an undirected graph that consists of n vertices and m edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edges with an endpoint in this vertex change the color to red.Find the minimum possible number of moves required to make the colors of all edges equal. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
FastScanner in;
PrintWriter out;
class Edge {
int to;
int color;
public Edge(int to, int color) {
super();
this.to = to;
this.color = color;
}
@Override
public String toString() {
return "Edge [to=" + to + ", color=" + color + "]";
}
}
ArrayList<Edge>[] g;
boolean[] was;
ArrayList<Integer> cur = new ArrayList<>();
ArrayList<Integer> changed;
int[] chang;
boolean dfs(int v, int ch, int needColor) {
was[v] = true;
chang[v] = ch;
cur.add(v);
if (ch == 1) {
changed.add(v);
}
for (Edge e : g[v]) {
if (!was[e.to]) {
if (!dfs(e.to, e.color ^ needColor ^ ch, needColor)) {
return false;
}
} else {
if ((e.color ^ ch ^ chang[e.to]) != needColor) {
return false;
}
}
}
return true;
}
void solve() {
int n = in.nextInt();
int m = in.nextInt();
g = new ArrayList[n];
for (int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int fr = in.nextInt() - 1;
int to = in.nextInt() - 1;
int color = in.next().equals("R") ? 1 : 0;
Edge e1 = new Edge(to, color);
Edge e2 = new Edge(fr, color);
g[fr].add(e1);
g[to].add(e2);
}
was = new boolean[n];
chang = new int[n];
ArrayList<Integer> bestAnswer = null;
for (int needColor = 0; needColor < 2; needColor++) {
ArrayList<Integer> answer = new ArrayList<>();
boolean[] was2 = new boolean[n];
boolean failed = false;
for (int i = 0; i < n; i++) {
if (was2[i]) {
continue;
}
ArrayList<Integer> best = null;
for (int ch = 0; ch < 2; ch++) {
cur.clear();
changed = new ArrayList<>();
if (dfs(i, ch, needColor)) {
if (best == null || best.size() > changed.size()) {
best = changed;
}
}
for (int v : cur) {
was[v] = false;
was2[v] = true;
}
}
if (best == null) {
failed = true;
break;
}
for (int x : best) {
answer.add(x);
}
}
if (failed) {
continue;
}
if (bestAnswer == null || bestAnswer.size() > answer.size()) {
bestAnswer = answer;
}
}
if (bestAnswer == null) {
out.println(-1);
} else {
out.println(bestAnswer.size());
for (int i : bestAnswer) {
out.print((i + 1) + " ");
}
}
}
void run() {
try {
in = new FastScanner(new File("object.in"));
out = new PrintWriter(new File("object.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new B().runIO();
}
} | Java | ["3 3\n1 2 B\n3 1 R\n3 2 B", "6 5\n1 3 R\n2 3 R\n3 4 B\n4 5 R\n4 6 R", "4 5\n1 2 R\n1 3 R\n2 3 B\n3 4 B\n1 4 B"] | 1 second | ["1\n2", "2\n3 4", "-1"] | null | Java 8 | standard input | [
"dfs and similar",
"graphs"
] | 5bb8347fd91f00245c3acb4a5eeaf17f | The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of vertices and edges, respectively. The following m lines provide the description of the edges, as the i-th of them contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the indices of the vertices connected by the i-th edge, and a character ci () providing the initial color of this edge. If ci equals 'R', then this edge is initially colored red. Otherwise, ci is equal to 'B' and this edge is initially colored blue. It's guaranteed that there are no self-loops and multiple edges. | 2,200 | If there is no way to make the colors of all edges equal output - 1 in the only line of the output. Otherwise first output k — the minimum number of moves required to achieve the goal, then output k integers a1, a2, ..., ak, where ai is equal to the index of the vertex that should be used at the i-th move. If there are multiple optimal sequences of moves, output any of them. | standard output | |
PASSED | 416ffc7804a7ec51d01fa63f005afae4 | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
for (int counter = 1; counter <= t; counter++) {
String[] data1 = in.readLine().split(" ");
int x1 = Integer.parseInt(data1[0]);
int y1 = Integer.parseInt(data1[1]);
int z1 = Integer.parseInt(data1[2]);
String[] data2 = in.readLine().split(" ");
int x2 = Integer.parseInt(data2[0]);
int y2 = Integer.parseInt(data2[1]);
int z2 = Integer.parseInt(data2[2]);
// Match a 2's with b 1's
int a = Math.min(z1, y2);
int sum = 2 * a;
z1 -= a;
y2 -= a;
// Match a 2's with b 2's
int m = Math.min(z1, z2);
z1 -= m;
z2 -= m;
// Match a 2's with b 0'z
m = Math.min(z1, x2);
z1 -= m;
x2 -= m;
// Match a 1's with b 1's
m = Math.min(y1, y2);
y1 -= m;
y2 -= m;
// Match a 1's with b 0's
m = Math.min(y1, x2);
y1 -= m;
x2 -= m;
// Match a 1's with b 2's
m = Math.min(y1, z2);
y1 -= m;
z2 -= m;
sum -= m * 2;
System.out.println(sum);
}
}
}
| Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | dc9cc9bc1de6e4674d6d2d17032c71aa | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.util.*;
public class asd
{
public static void main(String ars[])
{ ArrayList<String> Input = new ArrayList<String>();
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int z1=s.nextInt();
int y1=s.nextInt();
int x1=s.nextInt();
int z2=s.nextInt();
int y2=s.nextInt();
int x2=s.nextInt();
int sum=0;
int min=Math.min(x1,y2);
x1-=min;
y2-=min;sum+=(2*min);
if(x1>0)
{
min=Math.min(x1,x2);
x1-=min;
x2-=min;
}
if(x1>0)
{
min=Math.min(x1,z2);
x1-=min;
z2-=min;
}
min=Math.min(y1,y2);
y1-=min;
y2-=min;
if(y1>0)
{
min=Math.min(y1,z2);
y1-=min;
z2-=min;
}
if(y1>0)
{
min=Math.min(y1,x2);
y1-=min;
x2-=min;
sum-=(2*min);
}
System.out.println(sum);
}
}
}
| Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | bec3abde5f10760876233a0e5e2e130d | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | //package cp;
import java.io.*;
import java.util.*;
public class CF {
static FastReader sc = new FastReader();
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solve();
}
//solve();
}
static void solve() {
int x0 = sc.nextInt();
int x1 = sc.nextInt();
int x2 = sc.nextInt();
int y0 = sc.nextInt();
int y1 = sc.nextInt();
int y2 = sc.nextInt();
int m;
m = Math.min(x0,y2);
x0-=m;
y2-=m;
m = Math.min(x1,y0);
x1-=m;
y0-=m;
m = Math.min(x2,y1);
x2-=m;
y1-=m;
int sum = 2 * m;
sum-=2*Math.min(x1,y2);
System.out.println(sum);
}
//fast input reader
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | 2f8e0bf8e82cae2c015d30fd44dab156 | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.util.*;
import java.io.*;
//***************** Agar firdaus bar roo-e zameen ast,Hameen ast-o hameen ast-o hameen ast. *****************\\
public class Main{
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t-->0){
String[] s = br.readLine().split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
int c = Integer.parseInt(s[2]);
s = br.readLine().split(" ");
int x = Integer.parseInt(s[0]);
int y = Integer.parseInt(s[1]);
int z = Integer.parseInt(s[2]);
int min = Math.min(c,y);
long ans = 2*min;
c-=min;
y-=min;
if(c>0) z-=c;
z-=a;
b-=x;
if(y>0) b-=y;
if(b>0 && z>0) ans-=2*Math.min(b,z);
pw.println(ans);
}
pw.flush();
pw.close();
}
}
| Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | ea8f25f91dfd4c271e7d906f5db8c7aa | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class MAin{
static int mod = 1000000007;
static InputReader in = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int t=in.readInt();
while(t-->0){
int a=in.readInt();
int b=in.readInt();
int c=in.readInt();
int x=in.readInt();
int y=in.readInt();
int z=in.readInt();
int k=Math.min(c, y);
int ans=2*k;
c=c-k;
y=y-k;
if((a+c)>=z) {
System.out.println(ans);
}
else
System.out.println(ans-2*(z-(a+c)));
}
}
static String reverse(String s1) {
String s2="";
for(int i=s1.length()-1;i>=0;i--) {
s2+=s1.charAt(i);
}
return s2;
}
static boolean isVowel(char c) {
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
return true;
return false;
}
static String removeChar(String s,int a,int b) {
return s.substring(0,a)+s.substring(b,s.length());
}
static int[] nextIntArray(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nextLongArray(int n){
long[]arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nextIntArray1(int n) {
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nextLongArray1(int n){
long[]arr= new long[n+1];
int i=1;
while(i<=n) {
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long x, long y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n;
else {
long r=pow(n,m/2);
if(m%2==0)
return r*r;
else
return r*r*n;
}
}
static long max(long a,long b,long c) {
return Math.max(Math.max(a, b),c);
}
static long min(long a,long b,long c) {
return Math.min(Math.min(a, b), c);
}
static class Pair implements Comparable<Pair> {
int a, b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair o) {
if (this.a != o.a)
return Integer.compare(this.a, o.a);
else
return Integer.compare(this.b, o.b);
// return 0;
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair) o;
return p.a == a && p.b == b;
}
return false;
}
public int hashCode() {
return new Integer(a).hashCode() * 31 + new Integer(b).hashCode();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int 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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public 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 | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | 4fd930be2ec2b23e7297c888df62c56e | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class experiments
{
public static void main(String args[]) throws IOException
{
FastScanner fs = new FastScanner();
int T = fs.nextInt();
while(T-->0)
{
int arr1[] = fs.arrayIn(3);
int arr2[] = fs.arrayIn(3);
int count =0;
count += 2*Math.min(arr1[2], arr2[1]);
arr1[2] -= Math.min(arr1[2], arr2[1]);
arr2[1] -= Math.min(arr1[2],arr2[1]);
count -= 2*Math.max(0, arr2[2]-arr1[0]-arr1[2]);
System.out.println(count);
}
}
static final Random random = new Random();
static void ruffleSort(int arr[])
{
int n = arr.length;
for(int i=0; i<n; i++)
{
int j = random.nextInt(n),temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static class Pairs implements Comparable<Pairs>
{
int value,index;
Pairs(int value, int index)
{
this.value = value;
this.index = index;
}
public int compareTo(Pairs p)
{
return Integer.compare(value,p.value);
}
}
}
class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer("");
String next() throws IOException
{
while(!str.hasMoreTokens())
str = new StringTokenizer(br.readLine());
return str.nextToken();
}
char nextChar() throws IOException {
return next().charAt(0);
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
float nextfloat() throws IOException
{
return Float.parseFloat(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
byte nextByte() throws IOException
{
return Byte.parseByte(next());
}
int [] arrayIn(int n) throws IOException
{
int arr[] = new int[n];
for(int i=0; i<n; i++)
{
arr[i] = nextInt();
}
return arr;
}
}
| Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | cb8cd8c1dc3759402d5b9f4c9ae6eea8 | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.io.*;
import java.util.*;
public class Contest665_B {
final static int MOD = (int) 1e9 + 7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0){
long x1 = sc.nextLong(), y1 = sc.nextLong(), z1 = sc.nextLong();
long x2 = sc.nextLong(), y2 = sc.nextLong(), z2 = sc.nextLong();
long min = Math.min(z1,y2);
y2 -= min;
long remove = Math.max(0,y1-x2-y2);
System.out.println(2*(min-remove));
}
}
} | Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | df8f24adbe3655e485c9aa3372927b7a | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1 {
private void solve(Scan scan, Print print) throws Exception {
int T = scan.nextInt();
while (T-- > 0) {
final int N=3;
int one[]=new int[N];
scan.next(one);
int two[]=new int[N];
scan.next(two);
long ans=0l;
if(one[0]>=two[2])
{
one[0]-=two[2];
two[2]=0;
}
else
{
two[2]-=one[0];
one[0]=0;
}
if(one[2]>=two[2])
{
one[2]-=two[2];
two[2]=0;
}
else
{
two[2]-=one[2];
one[2]=0;
ans-=Math.min(one[1], two[2])*2l;
}
ans+=Math.min(one[2], two[1])*2l;
print.println(ans);
}
}
public static void main(String args[]) throws Exception {
Scan scan = new Scan();
Print print = new Print();
Q1 o = new Q1();
o.solve(scan, print);
print.close();
}
public static class Print {
private final BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void println(int object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(int object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(long object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(long object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(boolean object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(boolean object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(Object object[]) throws IOException {
print(Arrays.deepToString(object));
bw.append("\n");
}
public void print(Object object[]) throws IOException {
print(Arrays.deepToString(object));
}
public void close() throws IOException {
bw.close();
}
}
static class Scan {
private byte[] buf = new byte[1024 * 1024 * 4];
private int index;
private InputStream in;
private int total;
public Scan() {
in = System.in;
}
public int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public int nextInt() throws IOException {
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();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public double nextDouble() throws IOException {
double doub = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doub *= 10;
doub += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doub += (n - '0') * temp;
n = scan();
} else
throw new InputMismatchException();
}
}
return doub * neg;
}
public long nextLong() throws IOException {
long ret = 0;
long c = scan();
while (c <= ' ') {
c = scan();
}
boolean neg = (c == '-');
if (neg) {
c = scan();
}
do {
ret = ret * 10 + c - '0';
} while ((c = scan()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public String next() throws IOException {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n) || n == ' ') {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
public void next(int arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
}
public void next(long arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
}
public char[] next(char c) throws IOException {
char arr[] = next().toCharArray();
return arr;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
} | Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | f85800a8eafed3af91a031190c79ba43 | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1 {
private void solve(Scan scan, Print print) throws Exception {
int T = scan.nextInt();
while (T-- > 0) {
final int N=3;
int a[]=new int[N];
scan.next(a);
int b[]=new int[N];
scan.next(b);
long ans=0l;
if(a[0]>=b[2])
{
a[0]-=b[2];
b[2]=0;
}
else
{
b[2]-=a[0];
a[0]=0;
if(a[2]>=b[2])
{
a[2]-=b[2];
b[2]=0;
}
else
{
b[2]-=a[2];
a[2]=0;
if(b[0]>=a[1])
{
b[0]-=a[1];
a[1]=0;
}
else
{
a[1]-=b[0];
b[0]=0;
ans-=(Math.min(a[1], b[2]))*2l;
}
}
}
ans+=Math.min(a[2], b[1])*2l;
print.println(ans);
}
}
public static void main(String args[]) throws Exception {
Scan scan = new Scan();
Print print = new Print();
Q1 o = new Q1();
o.solve(scan, print);
print.close();
}
public static class Print {
private final BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void println(int object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(int object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(long object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(long object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(boolean object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(boolean object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(Object object[]) throws IOException {
print(Arrays.deepToString(object));
bw.append("\n");
}
public void print(Object object[]) throws IOException {
print(Arrays.deepToString(object));
}
public void close() throws IOException {
bw.close();
}
}
static class Scan {
private byte[] buf = new byte[1024 * 1024 * 4];
private int index;
private InputStream in;
private int total;
public Scan() {
in = System.in;
}
public int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public int nextInt() throws IOException {
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();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public double nextDouble() throws IOException {
double doub = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doub *= 10;
doub += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doub += (n - '0') * temp;
n = scan();
} else
throw new InputMismatchException();
}
}
return doub * neg;
}
public long nextLong() throws IOException {
long ret = 0;
long c = scan();
while (c <= ' ') {
c = scan();
}
boolean neg = (c == '-');
if (neg) {
c = scan();
}
do {
ret = ret * 10 + c - '0';
} while ((c = scan()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public String next() throws IOException {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n) || n == ' ') {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
public void next(int arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
}
public void next(long arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
}
public char[] next(char c) throws IOException {
char arr[] = next().toCharArray();
return arr;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
} | Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | d01658c92fe073a2f99e0fa3468a217d | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1 {
private void solve(Scan scan, Print print) throws Exception {
int T = scan.nextInt();
while (T-- > 0) {
final int N=3;
int a[]=new int[N];
scan.next(a);
int b[]=new int[N];
scan.next(b);
long ans=0l;
int m=Math.min(b[2], a[0]);
b[2]-=m;
a[0]-=m;
m=Math.min(a[2], b[1]);
a[2]-=m;
b[0]-=m;
ans+=2*m;
m=Math.min(a[2], b[2]);
a[2]-=m;
b[2]-=m;
m=Math.min(a[1], b[0]);
b[0]-=m;
a[1]-=m;
ans-=2*Math.min(b[2],a[1]);
print.println(ans);
}
}
public static void main(String args[]) throws Exception {
Scan scan = new Scan();
Print print = new Print();
Q1 o = new Q1();
o.solve(scan, print);
print.close();
}
public static class Print {
private final BufferedWriter bw;
public Print() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void println(int object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(int object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(long object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(long object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(boolean object[]) throws IOException {
print(Arrays.toString(object));
bw.append("\n");
}
public void print(boolean object[]) throws IOException {
print(Arrays.toString(object));
}
public void println(Object object[]) throws IOException {
print(Arrays.deepToString(object));
bw.append("\n");
}
public void print(Object object[]) throws IOException {
print(Arrays.deepToString(object));
}
public void close() throws IOException {
bw.close();
}
}
static class Scan {
private byte[] buf = new byte[1024 * 1024 * 4];
private int index;
private InputStream in;
private int total;
public Scan() {
in = System.in;
}
public int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public int nextInt() throws IOException {
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();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public double nextDouble() throws IOException {
double doub = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doub *= 10;
doub += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doub += (n - '0') * temp;
n = scan();
} else
throw new InputMismatchException();
}
}
return doub * neg;
}
public long nextLong() throws IOException {
long ret = 0;
long c = scan();
while (c <= ' ') {
c = scan();
}
boolean neg = (c == '-');
if (neg) {
c = scan();
}
do {
ret = ret * 10 + c - '0';
} while ((c = scan()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public String next() throws IOException {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n) || n == ' ') {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
public void next(int arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
}
public void next(long arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
}
public char[] next(char c) throws IOException {
char arr[] = next().toCharArray();
return arr;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
} | Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output | |
PASSED | 4c12d36d794ef5dc06faf0a97e9c428f | train_002.jsonl | 1598020500 | You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\ 0 & \mbox{if }a_i = b_i \\ -a_i b_i & \mbox{if }a_i < b_i \end{cases}$$$You'd like to make $$$\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum? | 256 megabytes | import java.util.Scanner;
public class Scratch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t =scanner.nextInt();
for (int i = 0; i <t ; i++) {
int x1=scanner.nextInt();
int y1=scanner.nextInt();
int z1=scanner.nextInt();
int x2=scanner.nextInt();
int y2=scanner.nextInt();
int z2=scanner.nextInt();
long res = 0;
int x = Math.min(z1,y2);
res += 2*x;
z1 = z1 - x;
y2 = y2 - x;
x=Math.min(z2,x1);
x1 = x1 - x;
z2 = z2 - x;
x= Math.min(y1,x2);
y1 = y1 - x;
x2 = x2 -x;
x=Math.min(y1,y2);
y1 = y1 - x;
y2 = y2 - x;
x=Math.min(y1,z2);
y1 = y1 - x;
z2 = z2 - x;
res = res - 2*x;
System.out.println(res);
}
}
} | Java | ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"] | 2 seconds | ["4\n2\n0"] | NoteIn the first sample, one of the optimal solutions is:$$$a = \{2, 0, 1, 1, 0, 2, 1\}$$$$$$b = \{1, 0, 1, 0, 2, 1, 0\}$$$$$$c = \{2, 0, 0, 0, 0, 2, 0\}$$$In the second sample, one of the optimal solutions is:$$$a = \{0, 2, 0, 0, 0\}$$$$$$b = \{1, 1, 0, 1, 0\}$$$$$$c = \{0, 2, 0, 0, 0\}$$$In the third sample, the only possible solution is:$$$a = \{2\}$$$$$$b = \{2\}$$$$$$c = \{0\}$$$ | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | e1de1e92fac8a6db3222c0d3c26843d8 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \le x_1, y_1, z_1 \le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \le x_2, y_2, z_2 \le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$. | 1,100 | For each test case, print the maximum possible sum of the sequence $$$c$$$. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.