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 | 13453693801671d083f29baa97cba676 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.util.*;
import java.io.*;
import java.text.*;
import java.math.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import java.lang.Math.*;
public class cover_points {
public static void main(String[] args) throws Exception {
new cover_points().run();
}
public void run() throws Exception {
FastIO file = new FastIO();
int n = file.nextInt();
long ans = 0;
for (int i = 0; i < n; i++) {
long a = file.nextLong();
long b = file.nextLong();
if (a == b) ans = Math.max(ans, a * 2);
else {
ans = Math.max(ans, a + b);
}
}
System.out.println(ans);
}
public static class FastIO {
BufferedReader br;
StringTokenizer st;
public FastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long pow(long n, long p, long mod) {
if (p == 0)
return 1;
if (p == 1)
return n % mod;
if (p % 2 == 0) {
long temp = pow(n, p / 2, mod);
return (temp * temp) % mod;
} else {
long temp = pow(n, (p - 1) / 2, mod);
temp = (temp * temp) % mod;
return (temp * n) % mod;
}
}
public static long pow(long n, long p) {
if (p == 0)
return 1;
if (p == 1)
return n;
if (p % 2 == 0) {
long temp = pow(n, p / 2);
return (temp * temp);
} else {
long temp = pow(n, (p - 1) / 2);
temp = (temp * temp);
return (temp * n);
}
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
}
| Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | a75230bf1ccb790de523aea34ee1a5ca | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;
public class hacker
{
/*FAtt gyi bbhai*/
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static String reverse(String str)
{
String rev = "";
for(int i=str.length()-1;i>=0;i--)
{
rev+=str.charAt(i);
}
return rev;
}
public static void main(String[] args)
{
new hacker().run();
}
void run()
{
FastReader sc = new FastReader();
int n = sc.nextInt();
int max =0;
for(int i =0;i<n;i++)
{
int x = sc.nextInt();
int y = sc.nextInt();
if(max<x+y)
{
max = x+y;
}
}
System.out.println(max);
}
}
| Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | 40233f4e084920931083c2fd5b916efa | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class a{
static long[] count,count1,count2;
static long[] arr;
static char[] ch,ch1;
static int[] darr,farr;
static Character[][] mat,mat1;
static long x,h;
static long maxl;
static double dec;
static String s;
static long minl;
static int mx = (int)1e6;
static long mod = 998244353l;
// static int minl = -1;
// static long n;
static int n,n1,n2;
static long a,l,g;
static long b;
static long c;
static long d;
static long y,z;
static int m;
static long k;
static int q;
static String[] str,str1;
static Set<Integer> set,set1,set2;
static List<Integer> list,list1,list2;
static LinkedList<Character> ll;
static Map<Integer,Integer> map;
static StringBuilder sb,sb1,sb2;
public static void solve(){
FastScanner sc = new FastScanner();
n = sc.nextInt();
double maxl = -1;
long ans = 0;
for(int i = 0 ; i < n; i++){
x = sc.nextLong();
y = sc.nextLong();
ans = Math.max(ans,(x+y));
}
System.out.println(ans);
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
// Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
int t = 1;
// int l = 1;
while(t > 0){
// x = sc.nextLong();
// y = sc.nextLong();
// z = sc.nextLong();
// a = sc.nextLong();
// b = sc.nextLong();
// c = sc.nextLong();
// d = sc.nextLong();
// n = sc.nextLong();
// n = sc.nextInt();
// m = sc.nextInt();
// x = sc.nextLong();
// y = sc.nextLong();
// x = sc.newxtLong();
// y = sc.nextLong();
// n1 = sc.nextInt();
// n = sc.nextLong();
// k = sc.nextLong();
// ch = sc.next().toCharArray();
// if(l == 61)
// System.out.println(Arrays.toString(ch));
// ch1 = sc.next().toCharArray();
// m = sc.nextLong();
// k = sc.nextLong();
// arr = new long[n];
// for(int i = 0 ; i < n ; i++){
// arr[i] = sc.nextLong();
// }
// darr = new int[n];
// for(int i = 0; i < n ; i++){
// darr[i] = sc.nextInt();
// }
// farr = new int[n];
// for(int i = 0; i < n ; i++){
// farr[i] = sc.nextInt();
// }
// n1 = sc.nextInt();
// darr = new long[n1];
// for(int i = 0; i < n1 ; i++){
// darr[i] = sc.nextLong();
// }
// mat = new Character[n][m];
// for(int i = 0 ; i < n ; i++){
// String s = sc.next();
// for(int j = 0 ; j < m ; j++){
// mat[i][j] = s.charAt(j);
// }
// }
// str = new String[n];
// for(int i = 0 ; i < n ; i++)
// str[i] = sc.next();
// System.out.println(solve()?"Yes":"No");
solve();
// System.out.println(solve());
t -= 1;
}
}
public static int log(long n){
if(n == 0 || n == 1)
return 0;
if(n == 2)
return 1;
double num = Math.log(n);
double den = Math.log(2);
if(den == 0)
return 0;
return (int)(num/den);
}
public static long gcd(long a,long b){
if(b%a == 0)
return a;
return gcd(b%a,a);
}
public static void swap(int i,int j){
long temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
static final Random random=new Random();
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class Node{
int first;
int second;
Node(int f,int s){
this.first = f;
this.second = s;
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | 5259c95cd048e3e9fd1c655e39bdb506 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class Cover{
public static void main(String args[])throws IOException{
Scanner ob=new Scanner(System.in);
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=ob.nextInt();
long m=0;
for(int i=0;i<n;i++){
long x=ob.nextInt();
long y=ob.nextInt();
if(m<(x+y))
m=x+y;
}
System.out.println(m);
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | a7d069c08cbd08ed84118e1148a4c85d | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;
import java.io.IOException;import java.io.PrintStream;import java.io.PrintWriter;
import java.security.AccessControlException;import java.util.Arrays;import java.util.Collection;
import java.util.Comparator;import java.util.List;import java.util.Map;import java.util.Objects;
import java.util.Scanner;import java.util.TreeMap;import java.util.function.Function;
import java.util.stream.Collectors;import java.util.stream.IntStream;import java.util.stream.LongStream;
import java.util.stream.Stream;public class _p001047B {static public void main(final String[] args)
throws IOException{p001047B._main(args);}
//begin p001047B.java
static private class p001047B extends Solver{public p001047B(){nameIn="in/900/p001047B.in"
;singleTest=true;}@Override public void solve()throws IOException{int n=sc.nextInt
();if(sc.hasNextLine()){sc.nextLine();}long res=0;for(int _if0=0;_if0<n;_if0++){
int x=sc.nextInt();int y=sc.nextInt();if(sc.hasNextLine()){sc.nextLine();}if(x+y
>res){res=x+y;}}pw.println(res);}static public void _main(String[]args)throws IOException
{new p001047B().run();}}
//end p001047B.java
//begin net/leksi/contest/Pair.java
static private class Pair<K,V>{private K k;private V v;public Pair(final K t,final
V u){this.k=t;this.v=u;}public K getKey(){return k;}public V getValue(){return
v;}}
//end net/leksi/contest/Pair.java
//begin net/leksi/contest/Solver.java
static private abstract class Solver{protected String nameIn=null;protected String
nameOut=null;protected boolean singleTest=false;protected boolean preprocessDebug
=false;protected boolean doNotPreprocess=false;protected PrintStream debugPrintStream
=null;protected Scanner sc=null;protected PrintWriter pw=null;final static String
SPACE=" ";final static String SPACES="\\s+";private void process()throws IOException
{if(!singleTest){int t=lineToIntArray()[0];while(t-->0){solve();}}else{solve();}
}abstract protected void solve()throws IOException;protected int[]lineToIntArray
()throws IOException{return Arrays.stream(sc.nextLine().trim().split(SPACES)).mapToInt
(Integer::valueOf).toArray();}protected long[]lineToLongArray()throws IOException
{return Arrays.stream(sc.nextLine().trim().split(SPACES)).mapToLong(Long::valueOf
).toArray();}protected void run()throws IOException{boolean done=false;try{if(nameIn
!=null && new File(nameIn).exists()){try(FileInputStream fis=new FileInputStream
(nameIn);PrintWriter pw0=select_output();){done=true;sc=new Scanner(fis);pw=pw0;
process();}}}catch(IOException ex){}catch(AccessControlException ex){}if(!done){
try(PrintWriter pw0=select_output();){sc=new Scanner(System.in);pw=pw0;process()
;}}}private PrintWriter select_output()throws FileNotFoundException{if(nameOut!=
null){return new PrintWriter(nameOut);}return new PrintWriter(System.out);}public
static Map<Integer,List<Integer>>mapi(final int[]a){return IntStream.range(0,a.
length).collect(()->new TreeMap<Integer,List<Integer>>(),(res,i)->{if(!res.containsKey
(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList()));}else{res.get(a[
i]).add(i);}},Map::putAll);}public static Map<Long,List<Integer>>mapi(final long
[]a){return IntStream.range(0,a.length).collect(()->new TreeMap<Long,List<Integer
>>(),(res,i)->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors
.toList()));}else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>Map<T,List
<Integer>>mapi(final T[]a){return IntStream.range(0,a.length).collect(()->new TreeMap
<T,List<Integer>>(),(res,i)->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(
i).collect(Collectors.toList()));}else{res.get(a[i]).add(i);}},Map::putAll);}public
static<T>Map<T,List<Integer>>mapi(final T[]a,Comparator<T>cmp){return IntStream
.range(0,a.length).collect(()->new TreeMap<T,List<Integer>>(cmp),(res,i)->{if(!res
.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList()));}else
{res.get(a[i]).add(i);}},Map::putAll);}public static Map<Integer,List<Integer>>mapi
(final IntStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Integer
,List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect
(Collectors.toList()));}else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public
static Map<Long,List<Integer>>mapi(final LongStream a){int[]i=new int[]{0};return
a.collect(()->new TreeMap<Long,List<Integer>>(),(res,v)->{if(!res.containsKey(v
)){res.put(v,Stream.of(i[0]).collect(Collectors.toList()));}else{res.get(v).add(
i[0]);}i[0]++;},Map::putAll);}public static<T>Map<T,List<Integer>>mapi(final Stream
<T>a,Comparator<T>cmp){int[]i=new int[]{0};return a.collect(()->new TreeMap<T,List
<Integer>>(cmp),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect
(Collectors.toList()));}else{res.get(v).add(i[0]);}},Map::putAll);}public static
<T>Map<T,List<Integer>>mapi(final Stream<T>a){int[]i=new int[]{0};return a.collect
(()->new TreeMap<T,List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,
Stream.of(i[0]).collect(Collectors.toList()));}else{res.get(v).add(i[0]);}},Map::
putAll);}public static List<int[]>listi(final int[]a){return IntStream.range(0,a
.length).mapToObj(i->new int[]{a[i],i}).collect(Collectors.toList());}public static
List<long[]>listi(final long[]a){return IntStream.range(0,a.length).mapToObj(i->
new long[]{a[i],i}).collect(Collectors.toList());}public static<T>List<Pair<T,Integer
>>listi(final T[]a){return IntStream.range(0,a.length).mapToObj(i->new Pair<T,Integer
>(a[i],i)).collect(Collectors.toList());}public static List<int[]>listi(final IntStream
a){int[]i=new int[]{0};return a.mapToObj(v->new int[]{v,i[0]++}).collect(Collectors
.toList());}public static List<long[]>listi(final LongStream a){int[]i=new int[]
{0};return a.mapToObj(v->new long[]{v,i[0]++}).collect(Collectors.toList());}public
static<T>List<Pair<T,Integer>>listi(final Stream<T>a){int[]i=new int[]{0};return
a.map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public static
String join(final int[]a){return Arrays.stream(a).mapToObj(Integer::toString).collect
(Collectors.joining(SPACE));}public static String join(final long[]a){return Arrays
.stream(a).mapToObj(Long::toString).collect(Collectors.joining(SPACE));}public static
<T>String join(final T[]a){return Arrays.stream(a).map(v->Objects.toString(v)).collect
(Collectors.joining(SPACE));}public static<T>String join(final T[]a,final Function
<T,String>toString){return Arrays.stream(a).map(v->toString.apply(v)).collect(Collectors
.joining(SPACE));}public static<T>String join(final Collection<T>a){return a.stream
().map(v->Objects.toString(v)).collect(Collectors.joining(SPACE));}public static
<T>String join(final Collection<T>a,final Function<T,String>toString){return a.stream
().map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public static<T
>String join(final Stream<T>a){return a.map(v->Objects.toString(v)).collect(Collectors
.joining(SPACE));}public static<T>String join(final Stream<T>a,final Function<T,
String>toString){return a.map(v->toString.apply(v)).collect(Collectors.joining(SPACE
));}public static<T>String join(final IntStream a){return a.mapToObj(Integer::toString
).collect(Collectors.joining(SPACE));}public static<T>String join(final LongStream
a){return a.mapToObj(Long::toString).collect(Collectors.joining(SPACE));}public
static List<Integer>list(final int[]a){return Arrays.stream(a).mapToObj(Integer
::valueOf).collect(Collectors.toList());}public static List<Integer>list(final IntStream
a){return a.mapToObj(Integer::valueOf).collect(Collectors.toList());}public static
List<Long>list(final long[]a){return Arrays.stream(a).mapToObj(Long::valueOf).collect
(Collectors.toList());}public static List<Long>list(final LongStream a){return a
.mapToObj(Long::valueOf).collect(Collectors.toList());}public static<T>List<T>list
(final Stream<T>a){return a.collect(Collectors.toList());}public static<T>List<T
>list(final T[]a){return Arrays.stream(a).collect(Collectors.toList());}}
//end net/leksi/contest/Solver.java
}
| Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | 923b3d8a821197a12f7088a033d39e75 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
int add = 0,k=0,result=0;
Scanner sr = new Scanner(System.in);
int n = sr.nextInt();
int[] a = new int[n];
while(n>0) {
/*
* hasNext()这个方法是如果此扫描器的输入中有另一个标记,则返回 true。
* 在等待要扫描的输入时,此方法可能阻塞。扫描器将不执行任何输入。所以循环会一直下去。
* 此情况可以设置一个终止符(hasNext(String xx))或者break条件来终止扫描器执行输入
*/
while(sr.hasNext()) {
int ch = sr.nextInt();
a[add] = a[add] + ch;
k++;
if(k==2)
break;
}
k=0;
add++;
n--;
}
result = a[0];
for(int i=1;i<a.length;i++) {
if(result < a[i])
result = a[i];
}
System.out.println(result);
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | 74409b6f78700c093c8a3ed782dbb6f7 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Cr511 {
private void solve(PrintStream out, InputStream in) {
Scanner scanner = new Scanner(in);
int n = scanner.nextInt();
int l = 0;
int tl;
for(int i = 0; i < n; i++) {
tl = scanner.nextInt() + scanner.nextInt();
l = l < tl ? tl : l;
}
System.out.println(l);
}
public static void main(String args[]) {
new Cr511().solve(System.out, System.in);
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | 2bc619badbffff1435c71c10927172b1 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.util.Scanner;
/**
*
* @author loymoev
*/
public class cf1047b {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = 0;
for(int i = 0; i<n;i++){
int x = sc.nextInt();
int y = sc.nextInt();
if (x + y > m){
m = x + y;
}
}
System.out.println(m);
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | d42e9ae9eec60243c40b857508bfe933 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.util.Scanner;
public class okaygod {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int X=-1,Y=-1;
double dis = 0;
for(int i=0;i<n;i++){
int x1 = sc.nextInt();
int y1 = sc.nextInt();
double temp = x1+y1;
if(temp>dis){
dis = temp;
X = x1;
Y = y1;
}
}
System.out.println(X+Y);
}
}
| Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | cce0111f46847629b2c891943e6c1267 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.util.Scanner;
public class CFR511_B {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n,toplam=0;
n=input.nextInt();
int[] xs = new int[n];
int[] ys = new int[n];
for (int i=0;i<n;i++) {
xs[i] = input.nextInt();
ys[i] = input.nextInt();
if(xs[i] + ys[i] > toplam) toplam = xs[i] + ys[i];
}
System.out.println(toplam);
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | 1b49c8e426262ecfe252f5a2a1c474f0 | train_004.jsonl | 1537540500 | There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle. | 256 megabytes | import java.io.*;
import java.util.*;
public class CoverPoint{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n=Integer.parseInt(br.readLine());
int mx=0,my=0;int mxdist=0;
for(int i=0;i<n;i++){
int[] in=new int[2];
st=new StringTokenizer(br.readLine());
in[0]=Integer.parseInt(st.nextToken());
in[1]=Integer.parseInt(st.nextToken());
int dist=in[0]+in[1];
if(dist>mxdist){
mxdist=dist;
mx=in[0];
my=in[1];
}
}
int c=my+mx;
System.out.println(c);
}
} | Java | ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"] | 1 second | ["3", "4"] | NoteIllustration for the first example: Illustration for the second example: | Java 8 | standard input | [
"geometry",
"math"
] | 7c41fb6212992d1b3b3f89694b579fea | First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$). | 900 | Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer. | standard output | |
PASSED | fe429913b2b8b5680fae55c889d3bce6 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class P490B {
static final int MAX = 1000000 + 1;
private static void solve() {
int n = nextInt();
int[] map = new int[MAX];
int[] sMap = new int[MAX];
for (int i = 0; i < n; i++) {
int f = nextInt();
int s = nextInt();
sMap[s]++;
map[f] = s == 0 ? -1 : s;
}
int old = 0;
for (int i = 1; i < MAX; i++) {
if (map[i] != 0 && sMap[i] == 0) {
old = i;
break;
}
}
StringBuilder result = new StringBuilder();
result.append(old + " ");
int even = 0;
int len2 = n / 2 + 1;
for (int i = 0; i < len2; i++) {
if (even < MAX && map[even] > 0) {
even = map[even];
result.append(even + " ");
}
if (old < MAX && map[old] > 0) {
old = map[old];
result.append(old + " ");
}
}
System.out.println(result.toString());
}
private static void run() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
private static StringTokenizer st;
private static BufferedReader br;
private static PrintWriter out;
private static String next() {
while (st == null || !st.hasMoreElements()) {
String s;
try {
s = br.readLine();
} catch (IOException e) {
return null;
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
private static int nextInt() {
return Integer.parseInt(next());
}
private static long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
public static void main(String[] args) {
run();
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 6d37fe9be586597d5549861f7d82851c | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
private static int dx[] = { 1, 0, -1, 0 };
private static int dy[] = { 0, -1, 0, 1 };
private static final long INF = (long) Math.pow(10, 16);
private static final int INT_INF = Integer.MAX_VALUE;
private static final long NEG_INF = Long.MIN_VALUE;
private static final int NEG_INT_INF = Integer.MIN_VALUE;
private static final double EPSILON = 1e-10;
private static final long MAX = (long) 1e12;
private static final long MOD = 100000007;
private static final int MAXN = 300005;
private static final int MAXA = 1000007;
private static final int MAXLOG = 22;
private static final double PI = Math.acos(-1);
public static void main(String[] args) throws IOException {
InputReader in = new InputReader(System.in);
// Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// InputReader in = new InputReader(new FileInputStream("src/test.in"));
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("src/test.out")));
/*
*/
int n = in.nextInt();
int arr[] = new int[MAXA];
int[] cnt = new int[MAXA];
Arrays.fill(arr, -1);
for(int i = 0; i < n; i++) {
int a = in.nextInt();
int b = in.nextInt();
arr[a] = b;
cnt[a]++;
cnt[b]++;
}
int first = -1;
for(int i = 0; i < cnt.length; i++) {
if(cnt[i] == 1 && arr[i] != -1) {
first = i;
break;
}
}
int ans[] = new int[MAXA];
ans[1] = first;
ans[2] = arr[0];
for(int i = 1; i <= n - 2; i++) {
ans[i + 2] = arr[ans[i]];
}
for(int i = 1; i <= n; i++) {
out.print(ans[i] + " ");
}
in.close();
out.flush();
out.close();
System.exit(0);
}
/*
* return the number of elements in list that are less than or equal to the val
*/
private static long upperBound(List<Long> list, long val) {
int start = 0;
int len = list.size();
int end = len - 1;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
long v = list.get(mid);
if (v == val) {
start = mid;
while(start < end) {
mid = (start + end) / 2;
if(list.get(mid) == val) {
if(mid + 1 < len && list.get(mid + 1) == val) {
start = mid + 1;
}
else {
return mid + 1;
}
}
else {
end = mid - 1;
}
}
return start + 1;
}
if (v > val) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (list.get(mid) < val) {
return mid + 1;
}
return mid;
}
private static boolean isPalindrome(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
String str1 = sb.reverse().toString();
return str.equals(str1);
}
private static String getBinaryStr(long n, int j) {
String str = Long.toBinaryString(n);
int k = str.length();
for (int i = 1; i <= j - k; i++) {
str = "0" + str;
}
return str;
}
private static long modInverse(long r) {
return bigMod(r, MOD - 2, MOD);
}
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
private static long ceil(long n, long x) {
long div = n / x;
if(div * x != n) {
div++;
}
return div;
}
private static int ceil(int n, int x) {
int div = n / x;
if(div * x != n) {
div++;
}
return div;
}
private static int abs(int x) {
if (x < 0) {
return -x;
}
return x;
}
private static double abs(double x) {
if (x < 0) {
return -x;
}
return x;
}
private static long abs(long x) {
if(x < 0) {
return -x;
}
return x;
}
private static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
private static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
private static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
private static int log(long x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long min(long a, long b) {
if (a < b) {
return a;
}
return b;
}
private static int min(int a, int b) {
if (a < b) {
return a;
}
return b;
}
private static long max(long a, long b) {
if (a < b) {
return b;
}
return a;
}
private static int max(int a, int b) {
if (a < b) {
return b;
}
return a;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public int[] nextIntArr(int n) {
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr(int n) {
long arr[] = new long[n];
for(int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[] nextIntArr1(int n) {
int arr[] = new int[n + 1];
for(int i = 1; i <= n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr1(int n) {
long arr[] = new long[n + 1];
for(int i = 1; i <= n; i++) {
arr[i] = nextLong();
}
return arr;
}
public void close() {
try {
if(reader != null) {
reader.close();
}
}
catch(Exception e) {
}
}
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | d6320f8d9866f089a5259005ace2c57e | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.HashMap;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Harshal Khodifad
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
HashMap<Integer, Integer> forward = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> backward = new HashMap<Integer, Integer>();
boolean arr[] = new boolean[1000001];
boolean brr[] = new boolean[1000001];
for (int i = 0; i < n; i++) {
int a = in.nextInt();
int b = in.nextInt();
arr[a] = true;
brr[b] = true;
forward.put(a, b);
backward.put(b, a);
}
int x = -1;
int y = -1;
for (int i = 1; i < 1000001; i++) {
if (arr[i] == true && brr[i] == false) {
x = i;
} else if (arr[i] == false && brr[i] == true)
y = i;
}
int final_a[] = new int[n];
final_a[0] = x;
final_a[n - 1] = y;
int aa = x;
final_a[1] = forward.get(0);
//System.out.println(x+" "+y+" "+final_a[1]);
for (int i = 2; i < n; i++) {
//System.out.println(i);
final_a[i] = forward.get(final_a[i - 2]);
}
for (int i = 0; i < n; i++)
out.print(final_a[i] + " ");
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | ff71f43aeb93652d99a3b678785572d4 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.awt.Point;
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Queues {
static class Solver {
public void solve(MyReader in, PrintWriter out) {
int n = in.nextInt();
Map<Integer,Node> map = new HashMap<>();
for (int i = 0; i < n; i++) {
int u = in.nextInt();
int v = in.nextInt();
if(v == 0){
map.put(u,map.getOrDefault(u,new Node(u)));
continue;
}
if(!map.containsKey(u))map.put(u,new Node(u));
if(!map.containsKey(v))map.put(v,new Node(v));
Node l = map.get(u);
Node r = map.get(v);
l.right = r;
r.left = l;
}
Node first = null,second;
for (Node node : map.values()){
if(node.left == null && node.val!= 0){
first = node;
break;
}
}
second = map.get(0).right;
while (first!=null && second!=null){
out.print(first.val+" "+second.val+" ");
first = first.right;
second = second.right;
}
while (first!=null){
out.print(first.val+" ");
first = first.right;
}
while (second!=null){
out.print(second.val+" ");
second = second.right;
}
}
static class Node{
int val;
Node left,right;
public Node(int val) {
this.val = val;
}
}
}
public static void main(String[] args) {
MyReader mr = new MyReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
new Solver().solve(mr, out);
out.close();
}
// static int[][] dir = {{0,1}, {1,0},{-1,0}, {0,-1}};
// static int[][] dir = {{0,1}, {1,0},{-1,0}, {0,-1}, {-1,-1},{-1,1}, {1,1}, {1,-1}};
static class MyReader {
BufferedReader br;
StringTokenizer st;
MyReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception 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 res = "";
try {
res = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
Integer[] nextIntegerArray(int n) {
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
Long[] nextLongArray(int n) {
Long[] arr = new Long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
String[] nextStringArray(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = next();
}
return arr;
}
}
static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 8365d48faf2925cfb70ef61284ca577b | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class Queue {
public static void main(String[] args) {
FastScanner scanner = new FastScanner();
int N = scanner.nextInt();
int[] front = new int[1000002];
int[] back = new int[1000002];
Arrays.fill(back, -1);
HashSet<Integer> occ = new HashSet<>();
for(int i = 0; i < N; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
front[a] = b;
back[b] = a;
occ.add(a);
occ.add(b);
}
int c1 = -1;
int c2 = front[0];
for(int num : occ) {
if (back[num] == -1) {
c1 = num;
break;
}
}
int[] out = new int[N];
int cur = 0;
while(c1 != 0) {
out[cur++] = c1;
int temp = c1;
c1 = c2;
c2 = front[temp];
}
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < N; i++) {
if (i > 0) stringBuilder.append(" ");
stringBuilder.append(out[i]);
}
System.out.println(stringBuilder.toString());
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(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 readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | cbe7b2e01ee9b286446a3c74a9cad8c8 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
public class C490B {
static int n ;
static A [] x;
static int a[] = new int[1000001] , b[] = new int[1000001];
static StringTokenizer st;
static int ans [] , id [];
public static void main(String[] args)throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
n = Int(in.readLine()); x = new A[n]; ans = new int[n]; id = new int[n];
Arrays.fill(a , -1); Arrays.fill(b, -1);
int i , j , first = 0 , second = 0;
for(i = 0;i < n;i ++){
st = new StringTokenizer(in.readLine());
x[i] = new A(Int(st.nextToken()) , Int(st.nextToken()) , i);
if(x[i].a == 0)first = i;
if(x[i].b == 0)second = i;
a[x[i].a] = i; b[x[i].b] = i;
}
i = 1;
A y = x[first];
while(y.b != 0 && i < n){
ans[i] = y.b; id[i] = y.id;
if(i + 2 >= n)break;
y = x[ a[y.b] ];
i += 2;
}
i = n % 2 == 0?n - 2:n - 1;
if(n - 1 == i)
y = x[second];
else
y = x[ b[ ans[n - 1] ] ];
if(n % 2 == 0){
y = x[second];i = n - 2;
while(i >= 0 && y.a != 0){
ans[i] = y.a;
if(i - 2 < 0)break;
y = x[ b[y.a] ];
i -= 2;
}
}else{
for(i = 0;i < 1000001;i ++)
if(a[i] != -1 && b[i] == -1)break;
y = x[a[i]]; i = 0;
while(i < n && y.b != 0){
ans[i] = y.a;
i += 2;
if(i >= n || a[y.b] == -1)break;
y = x[ a[y.b] ];
}
ans[n - 1] = y.b;
}
for(i = 0;i < n;i ++)
out.write(ans[i] + " ");
out.close();
}
static int Int(String s){
return Integer.valueOf(s);
}
}
class A{
int a , b , id;
public A(int a , int b , int id){
this.a = a;
this.b = b;
this.id = id;
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 5f3930016a79b12e70dcd20f95a17bbe | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class Codeforces {
InputStream is;
PrintWriter out;
String INPUT = "";
//----------------------------------------------------------------------------------------------------//
void solve() {
int m=(int)(1e6)+1;
int n=ni();
int x[]=new int[m];
boolean bol[]=new boolean[m];
int ans[]=new int[n];
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
int a=ni();
int b=ni();
bol[b]=true;
set.add(a);
set.add(b);
x[a]=b;
}
int first=0;
for (Integer s : set) {
if(!bol[s]){
first=s;
break;
}
}
int i=1;
int j=0;
while(i<n){
ans[i]=x[j];
j=x[j];
i=i+2;
}
i=0;
j=first;
while(i<n){
ans[i]=j;
j=x[j];
i=i+2;
}
for (int k = 0; k < n; k++) {
out.print(ans[k]+" ");
}
}
//----------------------------------------------------------------------------------------------------//
boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= (int) Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
Vector<Integer> sieveOfEratosthenes(int n) {
boolean[] prime = new boolean[n + 1];
for (int i = 0; i < n; i++) {
prime[i] = true;
}
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int j = p * p; j <= n; j += p) {
prime[j] = false;
}
}
}
Vector<Integer> v = new Vector<>();
for (int i = 2; i <= n; i++) {
if (prime[i]) {
v.add(i);
}
}
return v;
}
void swap(int a[], int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
long nCr(int n, int k) {
long C[] = new long[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = Math.min(i, k); j > 0; j--) {
C[j] = C[j] + C[j - 1];
}
}
return C[k];
}
int gcd(int s, int l) {
if (s == 0) {
return l;
}
return gcd(l % s, s);
}
int power(long x, long y, int m) { //log(y)
if (y == 0) {
return 1;
}
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (int) ((y % 2 == 0) ? p : (x * p) % m);
}
int modInverse(int a, int m) // O(Log m) whem m is prime (fermat's little theorem)
{
if (gcd(a, m) != 1) {
return -1;
} else {
return power(a, m - 2, m);
}
}
public HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) {
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer>> list
= new LinkedList<>(hm.entrySet());
//->change o1,o2 for reverseorder
Collections.sort(list, (Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) -> (o1.getValue()).compareTo(o2.getValue()));
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<>();
list.forEach((aa) -> {
temp.put(aa.getKey(), aa.getValue());
});
return temp;
}
void run() throws Exception {
is = System.in;//oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Codeforces().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) {
return -1;
}
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b));
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) {
map[i] = ns(m);
}
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj) {
System.out.println(Arrays.deepToString(o));
}
}
class Pair {
/*SORTING PAIRS BY COMPARING Y OF PAIR.
Pair[] p=new Pair[n];
p[i]=new Pair(i,ni());
Arrays.sort(p,( p1, p2) -> {return p1.y-p2.y;});
*/
int x;
int y;
Pair(int u, int v) {
x = u;
y = v;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Pair)) {
return false;
}
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
class Dsu {
int par[];
int size[];
int cnt;
Set<Integer> roots = new HashSet<>();
//boolean belongs[];
Dsu(int n) {
cnt = n;//cnt=0;
par = new int[n];
size = new int[n];
//belongs=new boolean[n];
for (int i = 0; i < n; i++) {
par[i] = i;
size[i] = 1;
roots.add(i);
//belongs[i]=false;
}
}
int root(int i) {
if (i == par[i]) {
return i;
}
return par[i] = root(par[i]);
}
boolean find(int p, int q) {
return root(p) == root(q);
}
void union(int p, int q) {
int a = root(p);
int b = root(q);
if (a != b) {
cnt--;
}
/*if(!belongs[p]&&!belongs[q])cnt++;
else if(belongs[p]&&belongs[q]){
if(a!=b)cnt--;
}
belongs[p]=belongs[q]=true;*/
if (a == b) {
return;
}
if (size[a] < size[b]) {
int temp = a;
a = b;
b = temp;
}
par[b] = a;
size[a] += size[b];
roots.add(a);
if (roots.contains(b)) {
roots.remove(b);
}
}
Set<Integer> getRoots() {
return roots;
}
int count() {
return cnt;
}
}
}
//isPrime(int)
//Vector<Integer> sieveOfEratosthenes(int n){PRIME NO <=n}
//swap(arr,i,j)
//HashMap sortByValue(map);
//long comb=nCr(5,2);
//int gcd(s,l);
//Pair p=new Pair(x,y);
// Dsu dsu=new Dsu(n); | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 924c0eb74cdbedf9e6502b0f0b8868d4 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class P490B {
static class pair implements Comparable<pair>{
int data;
int index;
public pair(int data,int index) {
// TODO Auto-generated constructor stub
this.data=data;
this.index=index;
}
@Override
public int compareTo(pair o) {
if(data>o.data)
return 1;
else if(data==o.data)
return 0;
else
return -1;
}
};
public static int BinarySearch(int key, pair array[]){
int left=0;
int right=array.length-1;
int index;
while(true){
index = (left+right)/2;
if(array[index].data>key){
right = index-1;
}else if(array[index].data<key){
left = index+1;
}
else{
return index;
}
if(right<left)
break;
}
return -1;
}
public static void main(String[] args) {
int n;
int a_index=-1, b_index=-1;
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
pair[] a= new pair [n];
pair[] b= new pair [n];
pair[] b_sorted = new pair[n];
for(int i=0; i<n; i++){
a[i]= new pair(sc.nextInt(), i);
b[i]= new pair(sc.nextInt(), i);
b_sorted[i] = b[i];
}
Arrays.sort(a);
Arrays.sort(b_sorted);
//
// for( pair i : a){
// System.out.println(i.data);
// System.out.println(i.index);
// }
//
int secdata=-1;
if(a[0].data==0)
secdata = b[a[0].index].data;
int index ;
int [] solution = new int [n];
solution[1]=secdata;
for(int i=3; i<n; i+=2){
index = BinarySearch(secdata, a);
solution[i]=b[a[index].index].data;
secdata = solution[i];
}
int firstdata=-1;
for(int i=0; i<n; i++){
if(BinarySearch(a[i].data, b_sorted)==-1){
firstdata = a[i].data;
}
}
solution[0]=firstdata;
for(int i=2; i<n; i+=2){
index = BinarySearch(firstdata, a);
solution[i]=b[a[index].index].data;
firstdata = solution[i];
}
for(int i : solution){
System.out.print(i+" ");
}
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 3dc0d4ae1b612b9713afe2307c4bc7d8 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.BufferedWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
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);
BQueue solver = new BQueue();
solver.solve(1, in, out);
out.close();
}
static class BQueue {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] ans = new int[n + 1];
HashSet<Integer> hs = new HashSet<>();
HashMap<Integer, Integer> hm = new HashMap<>(); //infront -> index
int[][] arr = new int[n + 1][2];
for (int i = 1; i <= n; i++) {
int a = in.nextInt(), b = in.nextInt();
arr[i][0] = a;
arr[i][1] = b;
hm.put(a, i);
if (hs.contains(a)) hs.remove(a);
else hs.add(a);
if (hs.contains(b)) hs.remove(b);
else hs.add(b);
}
Iterator<Integer> itr = hs.iterator();
int start = itr.next();
int end = itr.next();
for (int i = 1; i <= n; i++) {
if (arr[i][0] == start) break;
if (arr[i][0] == end) {
start = start ^ end ^ (end = start);
break;
}
}
ans[1] = start;
ans[n] = end;
for (int i = 1; i < n; i++) {
int element = ans[i];
int before = ans[i - 1];
int after = arr[hm.get(before)][1];
ans[i + 1] = after;
}
for (int i = 1; i <= n; i++) out.printsc(ans[i]);
out.println();
out.flush();
}
}
static class OutputWriter {
private final PrintWriter writer;
private ArrayList<String> res = new ArrayList<>();
private StringBuilder sb = new StringBuilder("");
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
sb.append(objects[i]);
}
res.add(sb.toString());
sb = new StringBuilder("");
}
public void printsc(Object... objects) {
for (int i = 0; i < objects.length; i++) {
sb.append(objects[i]).append(' ');
}
}
public void close() {
// writer.flush();
writer.close();
}
public void flush() {
for (String str : res) writer.printf("%s\n", str);
res.clear();
sb = new StringBuilder("");
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public InputReader(FileInputStream file) {
this.stream = file;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = (res << 3) + (res << 1) + c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | ba516d96ec1e51c385e61b9664b44b73 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class B {
static StringTokenizer st;
static BufferedReader br;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n=nextInt();
int [] aa=new int[n+1];
int [] b=new int[n+1];
Map<Integer,Integer>mp=new HashMap<Integer, Integer>();
Map<Integer,Integer>mp1=new HashMap<Integer, Integer>();
Set<Integer>s11=new HashSet<Integer>();
Set<Integer>s211=new HashSet<Integer>();
int uzun1=0;
int uzun2=0;
for (int i =1; i <=n; i++) {
aa[i+1-1]=nextInt();
b[i+1-1]=nextInt();
s211.add(b[i]);
s11.add(aa[i]);
mp.put(aa[i],b[i]);
mp1.put(b[i],aa[i]);
}
s11.removeAll(s211);
int h=0;
for(Integer i:s11){
h=i;
}
System.out.print(h+" ");
int aab=0;
int aac=0;
int bb=h;
for (int i =1; i <n; i++) {
System.out.print(mp.get(aab)+" ");
aac=aab;
aab=bb;
bb=mp.get(aac);
}
pw.close();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st==null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 5b05893b4f3f561795115e91712811f9 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
/**
* @author Don Li
*/
public class StudentQueue3 {
void solve() {
int n = in.nextInt();
Map<Integer, Integer> map = new HashMap<>(n);
Set<Integer> A = new HashSet<>(), B = new HashSet<>();
for (int i = 0; i < n; i++) {
int a = in.nextInt(), b = in.nextInt();
map.put(a, b);
A.add(a);
B.add(b);
}
for (int i : B) A.remove(i);
int[] res = new int[n + 1];
res[1] = A.iterator().next();
for (int i = 2; i <= n; i++) {
res[i] = map.get(res[i - 2]);
}
for (int i = 1; i < n + 1; i++) {
if (i > 1) out.print(' ');
out.print(res[i]);
}
out.println();
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new StudentQueue3().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 07ed223af19a3fb6f7c19f631991193c | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class StudentQueue2 {
static final int MAXN = (int) (1e6 + 5);
void solve() {
int n = in.nextInt();
int[] prev = new int[MAXN], next = new int[MAXN];
Arrays.fill(prev, -1);
Arrays.fill(next, -1);
for (int i = 0; i < n; i++) {
int a = in.nextInt(), b = in.nextInt();
next[a] = b;
prev[b] = a;
}
int h1 = next[0], h2 = -1;
for (int i = 1; i < MAXN; i++) {
if (prev[i] == -1 && next[i] != -1) h2 = i;
}
for (int i = 0; i < n; i++) {
if (i > 0) out.print(' ');
if (i % 2 == 0) {
out.print(h2);
h2 = next[h2];
} else {
out.print(h1);
h1 = next[h1];
}
}
out.println();
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new StudentQueue2().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | e3b0387efd69b68c76847bee53b92fd6 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class StudentQueue {
void solve() {
int n = in.nextInt();
Map<Integer, Node> map = new HashMap<>();
int[] a = new int[n], b = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
b[i] = in.nextInt();
if (!map.containsKey(a[i])) map.put(a[i], new Node(a[i]));
if (!map.containsKey(b[i])) map.put(b[i], new Node(b[i]));
Node x = map.get(a[i]), y = map.get(b[i]);
x.nxt = y.id;
y.pre = x.id;
}
int s1 = 0, s2 = -1;
for (int id : map.keySet()) {
if (id != 0 && map.get(id).pre == -1) {
s2 = id;
break;
}
}
s1 = map.get(s1).nxt;
for (int i = 0; i < n; i++) {
if (i > 0) out.print(' ');
if (i % 2 == 0) {
out.print(s2);
s2 = map.get(s2).nxt;
} else {
out.print(s1);
s1 = map.get(s1).nxt;
}
}
out.println();
}
static class Node {
int id;
int pre = -1, nxt = -1;
Node(int id) {
this.id = id;
}
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new StudentQueue().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 80d4717ee7fe26cbd9ca45f8f0582830 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | /**
*
* @author sarthak
*/
import java.util.*;
import java.math.*;
import java.io.*;
public class rnd279_B {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static Stack<Integer> sc=new Stack();
static Stack<Integer> ft=new Stack<>();
static int[] vis;
static ArrayList<Integer>[] G,RG;
public static void dfs(int v){
vis[v]=1;
for(int ad:G[v])
if(vis[ad]==0)
dfs(ad);
sc.add(v);
}
public static void rdfs(int v){
vis[v]=1;
if(G[v]!=null)
for(int ad:G[v])
if(vis[ad]==0)
rdfs(ad);
ft.push(v);
}
public static void main(String[] args){
FastScanner s = new FastScanner(System.in);
StringBuilder op=new StringBuilder();
int n=s.nextInt();
G=new ArrayList[1000000+6];
RG=new ArrayList[1000000+6];
for(int i=1;i<=1000000+5;i++){
G[i]=new ArrayList<>();
RG[i]=new ArrayList<>();
}
Set<Integer> set=new TreeSet<>();
int st=0;
int en=0;
int x=0,y=0;
for(int i=0;i<n;i++){
int a=s.nextInt();
int b=s.nextInt();
set.add(a);set.add(b);
if(a==0)st=b;
if(b==0)en=a;
if(a!=0&&b!=0){
G[a].add(b);
RG[b].add(a);
x=a;y=b;
}
}
vis=new int[1000000+6];
if(n<=3){
if(n==2)System.out.println(en+" "+st);
else System.out.println(x+" "+ st+" "+y);
return;
}
dfs(st);
for(int i:set)
if(i!=0&&vis[i]==0&&set.contains(i))
rdfs(i);
while(!ft.isEmpty()||!sc.isEmpty()){
if(!ft.isEmpty())
op.append(ft.pop()+" ");
if(!sc.isEmpty()){
op.append(sc.pop()+" ");
}
}
System.out.println(op);
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 783d1e2d5a5d4cb2e6e785a33059f5e1 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static class FastScanner {
BufferedReader s;
StringTokenizer st;
public FastScanner(InputStream InputStream) {
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(InputStream));
}
public FastScanner(File f) throws FileNotFoundException {
st = new StringTokenizer("");
s = new BufferedReader(new FileReader(f));
}
public int nextInt() throws IOException {
if (st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
public BigInteger big() throws IOException {
if (st.hasMoreTokens())
return new BigInteger(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return big();
}
}
public double nextDouble() throws IOException {
if (st.hasMoreTokens())
return Double.parseDouble(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return nextDouble();
}
}
public long nextLong() throws IOException {
if (st.hasMoreTokens())
return Long.parseLong(st.nextToken());
else {
st = new StringTokenizer(s.readLine());
return nextLong();
}
}
public String next() throws IOException {
if (st.hasMoreTokens())
return st.nextToken();
else {
st = new StringTokenizer(s.readLine());
return next();
}
}
public String nextLine() throws IOException {
return s.readLine();
}
public void close() throws IOException {
s.close();
}
}
public static void main(String[] args) throws java.lang.Exception {
FastScanner in = new FastScanner(System.in);
int n = in.nextInt();
HashMap<Integer, Integer> map = new HashMap();
HashMap<Integer, Integer> par = new HashMap();
int p2= 0, p1 =0;
for(int x = 0; x < n; x++) {
int a = in.nextInt();
int b = in.nextInt();
map.put(a, b);
par.put(b, a);
if(a == 0) {
p2 = b;
}
}
for(int ele : map.keySet()) {
if(ele == 0) continue;
if(!par.containsKey(ele)) {
p1 = ele;
break;
}
}
StringBuilder sb = new StringBuilder("");
for(int x = 0; x < n; x++) {
if((x & 1) == 0) {
sb.append(p1 +" ");
if(map.containsKey(p1))
p1 = map.get(p1);
}
else {
sb.append(p2 + " ");
if(map.containsKey(p2))
p2 = map.get(p2);
}
}
System.out.println(sb);
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 02f2b64163acd581d0ae8f56626db841 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution {
// public static final double eps = 1e-9;
// public static final int mod = 1000000007;
// public static final double pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
public static final int sievesize = (int) (1e6 + 10);
// public static boolean[] prime;
// public static final int dx[] = {1, -1, 0, 0};
// public static final int dy[] = {0, 0, 1, -1};
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, OutputWriter out) throws IOException {
int n = in.nextInt();
boolean[] a = new boolean[sievesize], b = new boolean[sievesize];
int[] store = new int[sievesize];
int[] res = new int[n];
for(int i = 0; i < n; i++) {
int f = in.nextInt(), s = in.nextInt();
store[f] = s;
a[f] = true;
b[s] = true;
}
for(int i = 0; i <= sievesize; i++) {
if(a[i] && !b[i]) {
res[0] = i;
break;
}
}
res[1] = store[0];
for(int i = 2; i < n; i++) {
res[i] = store[res[i - 2]];
}
out.printIntArray(res);
}
// static class Node {
// LinkedList<Integer> list;
//
// public Node() {
// list = new LinkedList<>();
// }
// }
//
// static class Pair implements Comparable<Pair> {
//
// long first, second;
//
// public Pair(long first, long second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compareTo(Pair o) {
// return (int) (this.second-o.second);
// }
// }
}
// public static int[] shuffle(int arr[], Random gen) {
// for(int i = 0, n = arr.length; i < n; i++)
// {
// int index = gen.nextInt(n - i) + i;
// int temp = arr[i];
// arr[i] = arr[index];
// arr[index] = temp;
// }
// return arr;
// }
//
// public static boolean triangle(long a, long b, long c){
// return ((a+b) > c && (b+c) >a && (a+c) > b);
// }
//
// public static boolean isPerfectSquare(long n){
// if (n < 0)
// return false;
// long test = (long) (Math.sqrt(n) + 0.5);
// return test*test == n;
// }
//
// public static int isnum(char s){
// if(s=='1' || s=='2' || s=='3' || s=='4' || s=='5' || s=='6' || s=='7' || s=='8' || s=='9' || s=='0')
// return 1;
// return 0;
// }
//
// public static long nextPowerOf2(long n){
// n--;
// n |= n >> 1;
// n |= n >> 2;
// n |= n >> 4;
// n |= n >> 8;
// n |= n >> 16;
// n++;
// return n;
// }
//
// public static int gcd(int a, int b){
// int t;
// while (b > 0){
// a = a%b;
// t = a;
// a = b;
// b = t;
// }
// return a;
// }
//
// public static void sieve(int n){
// prime=new boolean[sievesize+1];
// Arrays.fill(prime, true);
// long x, y;
// for(x = 2; x * x <= sievesize; x++)
// {
// if(prime[(int) x])
// for(y = x * 2; y <= sievesize; y += x)
// prime[(int) y]=false;
// }
// }
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int[] nextIntArray(int size) {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
int[] arr=new int[size];
for(int i = 0; i < size; i++)
arr[i] = Integer.parseInt(tokenizer.nextToken());
return arr;
}
public long[] nextLongArray(int size) {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
long[] arr=new long[size];
for(int i = 0; i < size; i++)
arr[i] = Long.parseLong(tokenizer.nextToken());
return arr;
}
public double[] nextDoubleArray(int size) {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
double[] arr=new double[size];
for(int i = 0; i < size; i++)
arr[i] = Double.parseDouble(tokenizer.nextToken());
return arr;
}
public String[] nextStringArray(int size) throws IOException {
String[] arr=new String[size];
for(int i = 0; i < size; i++)
arr[i] = reader.readLine();
return arr;
}
public char[] nextCharArray(int size) throws IOException {
char[] arr=reader.readLine().toCharArray();
return arr;
}
}
static class OutputWriter {
public PrintWriter pw;
public OutputWriter(OutputStream stream) {
pw = new PrintWriter(stream);
}
public void println(Object str) {
pw.println(str);
}
public void print(Object str) {
pw.print(str + " ");
}
public void close() {
pw.close();
}
public void flush() {
pw.flush();
}
public void printStringArray(String[] arr) {
for(int i = 0; i < arr.length; i++)
pw.println(arr[i]);
}
public void printIntArray(int[] arr) {
for(int i = 0; i < arr.length; i++)
pw.print(arr[i] + " ");
}
public void printLongArray(long[] arr) {
for(int i = 0; i < arr.length; i++)
pw.print(arr[i] + " ");
}
public void printCharArray(char[] arr) {
for(int i = 0; i < arr.length; i++)
pw.print(arr[i] + " ");
}
public void printFloatArray(float[] arr) {
for(int i = 0; i < arr.length; i++)
pw.print(arr[i] + " ");
}
public void printDoubleArray(double[] arr) {
for(int i = 0; i < arr.length; i++)
pw.print(arr[i] + " ");
}
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 6c6d8aa44350033dd1be5469695d3f59 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
FastScanner in;
int cnt;
Edge[] edge;
int[] f,d,ans;
void solve(int x,int sta){
while(x>0){
//ans[sta]=new int();
ans[sta]=x;
sta+=2;
x=f[x];
}
}
void input(){
in =new FastScanner(System.in);
f=new int[1100100];
d=new int[1100100];
ans=new int[1100100];
Arrays.fill(f,-1);
int n=in.nextInt();
for(int i=0;i<n;i++){
int u=in.nextInt();
int v=in.nextInt();
f[u]=v;
d[u]++;d[v]++;
}
solve(f[0],2);
for(int i=0;i<=1e6;i++){
if(d[i]==1&&f[i]>=0){
solve(i,1);
break;
}
}
for(int i=1;i<=n;i++){
if(i==1)
System.out.print(ans[i]);
else
System.out.print(" "+ans[i]);
}
}
public static void main(String args[]){
new Main().input();
}
}
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());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class Edge{
int u,v,next;
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 23a448887ecdda4383e976619a790775 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Math
{
static int[] arr = new int[1000002] ;
static HashSet<Integer> left = new HashSet<Integer>(200005);
static HashSet<Integer> right = new HashSet<Integer>(200005);;
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// int mx = 0;
// List<Integer> l = new ArrayList<>();
int x , y ;
for(int i=0 ; i<n ; i++){
x = in.nextInt();
y = in.nextInt();
// l.add(x);
arr[x] = y ;
left.add(x);
right.add(y);
//mx = java.lang.Math.max(mx, java.lang.Math.max(x,y)) ;
}
left.removeAll(right);
int[] a = new int[n];
String index = left.toString();
int val = arr[0];
int c = 1 ;
while(val != 0){
a[c] = val ;
val = arr[val];
c+=2;
}
int val2 = Integer.parseInt(index.substring(1, index.length() - 1)); ;
c=0;
while(val2 != 0){
a[c] = val2 ;
val2 = arr[val2];
c+=2;
}
for(int i=0 ; i<n ;i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 49a27d542ee4df13bcd04d1627bda651 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Math
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), x, y;
int[] arr = new int[1000010], res = new int[n + 1];
Set<Integer> fst = new HashSet<>(200005), snd = new HashSet<>(200005);
for (int i = 0; i < n; i++) {
x = in.nextInt();
y = in.nextInt();
arr[x] = y;
fst.add(x);
snd.add(y);
}
fst.removeAll(snd);
String index = fst.toString();
int t= 0, j = 2;
while (arr[t] != 0) {
t = arr[t];
res[j] = t;
j += 2;
}
t = Integer.parseInt(index.substring(1, index.length() - 1));
j = 1;
while (t != 0) {
res[j] = t;
j += 2;
t = arr[t];
}
for (int i = 1; i < n + 1; i++) {
System.out.print(res[i] + " ");
}
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 8613e8b46e7ac7eb5993674854ab61a4 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Set;
public class C {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int n = s.nextInt();
HashMap<Integer, Integer> front = new HashMap<>();
HashMap<Integer, Integer> back = new HashMap<>();
for (int i = 0; i < n; i++) {
int a = s.nextInt();
int b = s.nextInt();
front.put(a, b);
back.put(b, a);
}
int val = 0;
int[] ans = new int[n];
int j = 1;
while (front.containsKey(val)) {
if (front.get(val) == 0)
break;
ans[j] = front.get(val);
j += 2;
val = front.get(val);
}
j = n - 2;
val = 0;
while (back.containsKey(val)) {
if (back.get(val) == 0)
break;
ans[j] = back.get(val);
j -= 2;
val = back.get(val);
}
if (n % 2 == 0) {
for (int i = 0; i < n; i++) {
p.print(ans[i] + " ");
}
p.flush();
p.close();
return;
}
Set<Integer> fronts = front.keySet();
for (int temp : fronts) {
if (!back.containsKey(temp)) {
ans[0] = temp;
j = 2;
while (front.containsKey(temp)) {
ans[j] = front.get(temp);
j += 2;
temp = front.get(temp);
}
}
}
for (int i = 0; i < n; i++) {
p.print(ans[i] + " ");
}
p.flush();
p.close();
}
static class Pair<T, V> {
T first; // maximum distance Node
V second; // distance of maximum distance node
// Constructor
Pair(T first, V second) {
this.first = first;
this.second = second;
}
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
static class pair implements Comparable<pair> {
Integer x, y;
pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 710e20230f749c1c41f7e0d49c541798 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class LightOJ {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
int n, a, b, c,d;
int[] original, mapping2, aft, bef;
public void solve(int caseNum, InputReader in, PrintWriter out) {
n = in.nextInt();
mapping2 = new int[10000000];
original = new int[n];
aft = new int[10000000];
bef = new int[10000000];
Arrays.fill(mapping2, -1);
Arrays.fill(aft, -1);
Arrays.fill(bef, -1);
for (int i=0;i<n;i++){
a = in.nextInt();
b = in.nextInt();
aft[a] = i;
bef[b] = i;
//storing 2nd number of queue in variable c
if (a==0)
c = b;
mapping2[a] = b;
}
//Find 1st num
for (int i=0;i<10000000;i++){
if (aft[i]!=-1 && bef[i]==-1){
original[0] = i;
break;
}
}
//adding 2nd num
original[1] = c;
for (int i=0;i<n;i++){
out.printf("%d ",original[i]);
if (i<n-2)
original[i+2] = mapping2[original[i]];
}
}
}
class InputReader {
public BufferedReader reader;
public 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 long nextLong(){
return Long.parseLong(next());
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | eeaab0d038d67491f37717a59a5ac90a | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class LightOJ {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
int n, a, b, c,d;
int[] original, mapping2, aft, bef;
public void solve(int caseNum, InputReader in, PrintWriter out) {
n = in.nextInt();
mapping2 = new int[1000001];
original = new int[n];
aft = new int[1000001];
bef = new int[1000001];
Arrays.fill(mapping2, -1);
Arrays.fill(aft, -1);
Arrays.fill(bef, -1);
for (int i=0;i<n;i++){
a = in.nextInt();
b = in.nextInt();
aft[a] = i;
bef[b] = i;
//storing 2nd number of queue in variable c
if (a==0)
c = b;
mapping2[a] = b;
}
//Find 1st num
for (int i=0;i<1000001;i++){
if (aft[i]!=-1 && bef[i]==-1){
original[0] = i;
break;
}
}
//adding 2nd num
original[1] = c;
for (int i=0;i<n;i++){
out.printf("%d ",original[i]);
if (i<n-2)
original[i+2] = mapping2[original[i]];
}
}
}
class InputReader {
public BufferedReader reader;
public 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 long nextLong(){
return Long.parseLong(next());
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 3280dee0734758395ebd28059c34e82f | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
public class Main {
private static Map<Integer, Integer> map = new HashMap<>();
private static Map<Integer, Integer> findStart = new HashMap<>();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] result = new int[n];
while(n-- > 0) {
int n1 = scan.nextInt();
int n2 = scan.nextInt();
map.put(n1, n2);
findStart.put(n1, 1);
}
for(int key : map.keySet()) {
findStart.remove(map.get(key));
}
int startNum = (int) findStart.keySet().toArray()[0];
for(Integer i=1, value = map.get(0); i<result.length; i+=2) {
result[i] = value;
value = map.get(value);
}
for(Integer i=0, value = startNum; i<result.length; i+=2) {
result[i] = value;
value = map.get(value);
}
for(int i=0; i<result.length; i++) {
System.out.print(result[i]+" ");
}
System.out.println();
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 7fd422ad1a693dd9e444376d21504548 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Train {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int[] a = new int[n];
int[][] nxt = new int[n][2];
int[] leftOcc = new int[1000001], f = new int[1000001];
Arrays.fill(leftOcc, -1);
for(int i = 0; i < n; i++)
{
int x = sc.nextInt(), y = sc.nextInt();
nxt[i][0] = x;
nxt[i][1] = y;
f[x]++; f[y]++;
leftOcc[x] = i;
}
for(int i = 1; a[0] == 0; i++)
if(f[i] == 1 && leftOcc[i] != -1)
a[0] = i;
for(int i = 1, idx = leftOcc[0]; i < n; i++)
{
a[i] = nxt[idx][1];
idx = leftOcc[a[i-1]];
}
for(int i = 0; i < n; i++)
out.format("%d%s",a[i], i == n - 1 ? '\n' : ' ');
out.flush();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 8bd084079a2c4b57f22233b9646d674d | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
public class _490B {
static class Node {
int value;
Node head;
Node tail;
Node next;
public Node(int value) {
this.value = value;
head = tail = this;
next = null;
}
public List<Integer> toList() {
List<Integer> list = new ArrayList<>();
for (Node i=head; i!=null; i=i.next) {
list.add(i.value);
}
return list;
}
}
static final int MAX = 1111111;
static int n;
static int[] father = new int[MAX];
static Node[] list = new Node[MAX];
static {
for (int i=0; i<father.length; i++) father[i] = i;
for (int i = 0; i < list.length; i++) {
list[i] = new Node(i);
}
}
static int find(int i) {
if (father[i] != i) father[i] = find(father[i]);
return father[i];
}
static void union(int i, int j) {
i = find(i);
j = find(j);
if (i != j) {
father[j] = i;
list[i].tail.next = list[j].head;
list[i].tail = list[j].tail;
}
}
public static void main(String[] args) throws Exception {
Reader.init(System.in);
BufferedWriter cout = new BufferedWriter(new OutputStreamWriter(System.out));
n = Reader.nextInt();
Set<Integer> cand = new HashSet<>();
int second = -1;
for (int i = 0; i < n; i++) {
int u = Reader.nextInt();
int v = Reader.nextInt();
if (u == 0) second = v;
if (u != 0 && v != 0) union(u, v);
if (u > 0) cand.add(u);
if (v > 0) cand.add(v);
}
int first = -1;
for (int i : cand)
if (find(i) != find(second)) {
first = find(i);
break;
}
List<Integer> a = list[find(first)].toList();
List<Integer> b = list[find(second)].toList();
StringBuilder ans = new StringBuilder();
for (int i = 0, j = 0; i < a.size() || j < b.size(); ) {
if (i < a.size()) {
ans.append(a.get(i++));
ans.append(' ');
}
if (j < b.size()) {
ans.append(b.get(j++));
ans.append(' ');
}
}
System.out.println(ans.toString());
cout.close();
}
static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
final U _1;
final V _2;
private Pair(U key, V val) {
this._1 = key;
this._2 = val;
}
public static <U extends Comparable<U>, V extends Comparable<V>> Pair<U, V> instanceOf(U _1, V _2) {
return new Pair<U, V>(_1, _2);
}
@Override
public String toString() {
return _1 + " " + _2;
}
@Override
public int hashCode() {
int res = 17;
res = res * 31 + _1.hashCode();
res = res * 31 + _2.hashCode();
return res;
}
@Override
public int compareTo(Pair<U, V> that) {
int res = this._1.compareTo(that._1);
if (res < 0 || res > 0) return res;
else return this._2.compareTo(that._2);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Pair)) return false;
Pair<?, ?> that = (Pair<?, ?>) obj;
return _1.equals(that._1) && _2.equals(that._2);
}
}
/**
* Class for buffered reading int and double values
*/
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
static class ArrayUtil {
static void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static void swap(long[] a, int i, int j) {
long tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static void swap(double[] a, int i, int j) {
double tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static void swap(char[] a, int i, int j) {
char tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static void swap(boolean[] a, int i, int j) {
boolean tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static void reverse(int[] a, int i, int j) {
for (; i < j; i++, j--)
swap(a, i, j);
}
static void reverse(long[] a, int i, int j) {
for (; i < j; i++, j--)
swap(a, i, j);
}
static void reverse(double[] a, int i, int j) {
for (; i < j; i++, j--)
swap(a, i, j);
}
static void reverse(char[] a, int i, int j) {
for (; i < j; i++, j--)
swap(a, i, j);
}
static void reverse(boolean[] a, int i, int j) {
for (; i < j; i++, j--)
swap(a, i, j);
}
static long sum(int[] a) {
int sum = 0;
for (int i : a)
sum += i;
return sum;
}
static long sum(long[] a) {
long sum = 0;
for (long i : a)
sum += i;
return sum;
}
static double sum(double[] a) {
double sum = 0;
for (double i : a)
sum += i;
return sum;
}
static int max(int[] a) {
int max = Integer.MIN_VALUE;
for (int i : a)
if (i > max) max = i;
return max;
}
static int min(int[] a) {
int min = Integer.MAX_VALUE;
for (int i : a)
if (i < min) min = i;
return min;
}
static long max(long[] a) {
long max = Long.MIN_VALUE;
for (long i : a)
if (i > max) max = i;
return max;
}
static long min(long[] a) {
long min = Long.MAX_VALUE;
for (long i : a)
if (i < min) min = i;
return min;
}
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 345174cdec5c39adf0361bd424c5caee | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] answer = new int[n];
Map<Integer, Integer> data = new HashMap<>();
Map<Integer, Integer> data2 = new HashMap<>();
for (int i = 0; i < n; i++) {
int a = in.nextInt();
int b = in.nextInt();
data.put(a, b);
data2.put(b, a);
}
int count = n / 2;
int prev = 0;
if (n > 2)
data2.remove(0);
for (int i = 1; i <= count; i++) {
answer[i * 2 - 1] = data.get(prev);
data2.remove(answer[i * 2 - 1]);
prev = answer[i * 2 - 1];
}
int first = data2.keySet().iterator().next();
while (data2.containsKey(first))
first = data2.get(first);
prev = first;
answer[0] = first;
count = n / 2 + n % 2;
for (int i = 1; i < count; i++) {
answer[i * 2] = data.get(prev);
prev = answer[i * 2];
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++)
sb.append(answer[i]).append(" ");
System.out.println(sb.toString());
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | abdbdcac3374749ffa7f19fdc02c0831 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
public class CF_490B {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = Integer.parseInt(s.nextLine());
HashMap<Integer, Integer> map1 = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> map2 = new HashMap<Integer, Integer>();
HashMap<Integer, Boolean> parsed = new HashMap<Integer, Boolean>();
int[] line = new int[n];
int[] fronts = new int[n];
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(s.nextLine());
int front = Integer.parseInt(st.nextToken());
int back = Integer.parseInt(st.nextToken());
fronts[i] = front;
map1.put(front, back);
map2.put(back, front);
}
//System.out.println(map2);
int next = 0;
int nextIdx = 1;
while(nextIdx < n) {
parsed.put(next, true);
line[nextIdx] = map1.get(next);
next = line[nextIdx];
nextIdx += 2;
//System.out.println(1);
}
parsed.put(next, true);
//System.out.println(parsed);
if(n % 2 == 0) {
int next2 = 0;
int nextIdx2 = n-2;
while(nextIdx2 >= 0) {
line[nextIdx2] = map2.get(next2);
next2 = line[nextIdx2];
nextIdx2 -= 2;
//System.out.println(2);
}
} else {
int next2 = 0;
int k = 0;
while(parsed.containsKey(fronts[k]) && k < n) {
k++;
//System.out.println(3);
}
next2 = fronts[k];
while(map2.containsKey(next2)) {
next2 = map2.get(next2);
}
line[0] = next2;
int nextIdx2 = 2;
while(nextIdx2 < n) {
line[nextIdx2] = map1.get(next2);
next2 = line[nextIdx2];
nextIdx2 += 2;
//System.out.println(5);
}
}
for(int x : line) System.out.print(x + " ");
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 1bedb578945dafef285538936453fed3 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int iti=-1;
int ni=-1;
HashMap<Integer,Integer> aa = new HashMap<>();
HashMap<Integer,Integer>bb=new HashMap<>();
for(int i=0;i<n;i++){
int a=sc.nextInt();
int b = sc.nextInt();
aa.put(a,b);
if(!bb.containsKey(a)){
bb.put(a,0);
}
if(!bb.containsKey(b)){
bb.put(b, 0);
}
bb.put(a, bb.get(a)+1);
bb.put(b, bb.get(b)-1);
if(a==0) ni=b;
}
for(Integer f: bb.keySet()){
if(bb.get(f)==1) {iti=f;
break;
}
}
StringBuffer buf = new StringBuffer();
buf.append(iti+" "+ni+" ");
int maeee=iti;
int usirooo=ni;
for(int i=2;i<n;i++){
if(i%2==0){
maeee=aa.get(maeee);
buf.append(maeee);
buf.append(" ");
}else{
usirooo=aa.get(usirooo);
buf.append(usirooo);
buf.append(" ");
}
}
System.out.println(buf);
}
} | Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 82374448754ab2587e91875acfe65454 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.*;
import java.io.*;
public class cf279p2 {
public static void main(String args[]){
Map<Integer,Integer> m=new HashMap<>();
Scanner sc=new Scanner(System.in);
int f,b,i=0,n1;
int n=sc.nextInt();
n1=n-1;
int[] ar=new int[n];
s[] in=new s[n];
int[] fl=new int[1000001];
while(i<n){
in[i]=new s();
in[i].f=sc.nextInt();
in[i].b=sc.nextInt();
m.put(in[i].f,in[i].b);
fl[in[i].b]=1;
i++;
}
int val=0;
i=1;
while(i<n){
ar[i]=m.get(val);
i+=2;
val=m.get(val);
}
int first=0;
for(i=0;i<n;i++){
if(fl[in[i].f]==0){
first=in[i].f;
break;
}
}
i=2;
val=first;
ar[0]=first;
while(i<n){
ar[i]=m.get(val);
i+=2;
val=m.get(val);
}
for(int x:ar)
System.out.print(x+" ");
}
}
class s{
int b,f;
s(){
b=0;
f=0;
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | 3828b717fe8ff46527fd6e33bda948e6 | train_004.jsonl | 1416733800 | During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class j2 {
public static void main(String[] args) {
Scanner jk=new Scanner(System.in);
int n=jk.nextInt();
int [] ak=new int[1000010];
int [][] map=new int[1000010][3];
int [] ans=new int[1000010];
int fir=0;
int sec=0;
ArrayList<Integer> con=new ArrayList<Integer>();
for (int i=0;i<n;i++){
int a=jk.nextInt();
int b=jk.nextInt();
if (a==0) sec=b;
else{
ak[a]=b;
}
map[a][1]++;
map[b][2]++;
}
for (int i=1;i<=1000001;i++){
if (map[i][1]+map[i][2]==1&&map[i][2]==0){
fir=i;
break;
}
}
int next=11;
ans[1]=fir;
ans[2]=sec;
int count=3;
while (count<=n){
if (count%2==0){
ans[count]=ak[sec];
sec=ans[count];
}
else
{
ans[count]=ak[fir];
fir=ans[count];
}
count++;
}
for (int i=1;i<count;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
}
| Java | ["4\n92 31\n0 7\n31 0\n7 141"] | 2 seconds | ["92 7 31 141"] | NoteThe picture illustrates the queue for the first sample. | Java 8 | standard input | [
"dsu",
"implementation"
] | 1aaced1322c47f1908f2bc667bca1dbe | The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist. The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | 1,500 | Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | standard output | |
PASSED | d159a9ca5fcc44820555bb3128cde050 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | //package codeforces;
import java.io.IOException;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
/**
* @author muhossain
* @since 2020-09-28
*/
public class B1417 {
public static void main(String[] args) {
FasterScanner fs = new FasterScanner();
int T = fs.nextInt();
StringBuilder result = new StringBuilder();
for (int t = 0; t < T; t++) {
StringBuilder out = new StringBuilder();
int n = fs.nextInt();
int u = fs.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = fs.nextInt();
}
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int i = 0; i < n; i++) {
frequencyMap.put(nums[i], frequencyMap.getOrDefault(nums[i], 0) + 1);
}
boolean wentLeft = false;
for (int i = 0; i < n; i++) {
if (nums[i] < (u + 1) / 2) {
out.append("1 ");
} else if (nums[i] > u / 2) {
out.append("0 ");
} else {
if (!wentLeft) {
out.append("1 ");
} else {
out.append("0 ");
}
wentLeft = !wentLeft;
}
}
result.append(out.substring(0, out.length() - 1)).append("\n");
}
System.out.print(result);
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 89a13fd2d06345daeda99c9565f8ca3d | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class B673
{
public static void func(int n, int arr[], int T){
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++){
if (map.containsKey(arr[i])){
ArrayList<Integer> temp = map.get(arr[i]);
temp.add(i);
map.put(arr[i], temp);
}
else{
ArrayList<Integer> temp = new ArrayList<>();
temp.add(i);
map.put(arr[i], temp);
}
}
int ans[] = new int[n];
for (Integer c : map.keySet()){
int x = T - c;
if (map.containsKey(x)){
if (map.get(x).size() == 0) continue;
if (x == c){
boolean first = true;
for (int i = 0; i < map.get(c).size(); i++){
if (first){
ans[map.get(c).get(i)] = 0;
}
else{
ans[map.get(c).get(i)] = 1;
}
first = !first;
}
map.put(c, new ArrayList<>());
}
else{
for (int i = 0; i < map.get(c).size(); i++){
ans[map.get(c).get(i)] = 0;
}
map.put(c, new ArrayList<>());
for (int i = 0; i < map.get(x).size(); i++){
ans[map.get(x).get(i)] = 1;
}
map.put(x, new ArrayList<>());
}
}
else{
for (int i = 0; i < map.get(c).size(); i++){
ans[map.get(c).get(i)] = 0;
}
map.put(c, new ArrayList<>());
}
}
for (int i = 0; i < n; i++){
System.out.print(ans[i] + " ");
}
System.out.println();
return;
}
public static void main (String[] args) throws java.lang.Exception{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- != 0){
int n = s.nextInt();
int T = s.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) arr[i] = s.nextInt();
func(n, arr, T);
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | d86ef719c75cf1c7df3bf4a81ce49c89 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.math.BigInteger;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
static InputReader rd=new InputReader(System.in);
public static void main(String[] args) {
int t=rd.readInt();
while(t-->0) {
int n=rd.readInt();
int T=rd.readInt();
ArrayList<Helper> C;
C=new ArrayList<Helper>();
int taken[]=new int[n];
for(int i=0;i<n;i++) {
C.add(new Helper(rd.readInt(),i));
taken[i]=-1;
}
int i=0;int j=n-1;
Collections.sort(C,new Comparator<Helper>() {
@Override
public int compare(Helper o1, Helper o2) {
// TODO Auto-generated method stub
return (int) (o1.val-o2.val);
}
});
if(T%2!=0)
{
int k=0;
int p=0;
for( i=0;i<n;i++) {
if(C.get(i).val==T/2) {
p++;
}
}
for( i=0;i<n;i++) {
if(C.get(i).val<T/2)taken[C.get(i).index]=1;
if(C.get(i).val>T/2)taken[C.get(i).index]=0;
if(C.get(i).val==T/2)taken[C.get(i).index]=1;
}
}
else {
int p=0;
int m=Integer.MAX_VALUE;
for( i=0;i<n;i++) {
if(C.get(i).val==T/2) {
m=Math.min(i, m);
p++;
}
}
for( i=0;i<n;i++) {
if(C.get(i).val<T/2) {
taken[C.get(i).index]=1;
}
else if(C.get(i).val>T/2){
taken[C.get(i).index]=0;
}
}
for(i=m;i<m+p/2;i++) {
taken[C.get(i).index]=1;
}
for(i=m+p/2;i<m+p;i++) {
taken[C.get(i).index]=0;
}
}
for( i=0;i<n;i++) {
System.out.print(taken[i]+" ");
}
System.out.println();
}
}
static class Helper{
long val;int index;
public Helper(long val,int index) {
this.val=val;
this.index=index;
// TODO Auto-generated constructor stub
}
}
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 boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 8204c580b538037c84db080f02fa1b13 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static void main(String[] args)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int T=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
int comps[]=new int[n];
for(int i=0;i<n;i++) {
comps[i]=T-a[i];
}
int c[]=new int[n];
//Set<Integer> set0=new HashSet<>();
//Set<Integer> set1=new HashSet<>();
HashMap<Integer,Integer> map0=new HashMap<>();
HashMap<Integer,Integer> map1=new HashMap<>();
for(int i=0;i<n;i++) {
int v=comps[i];
if(!map0.containsKey(v)) {
if(map0.containsKey(a[i])) {
int x=map0.get(a[i]);
x++;
map0.put(a[i],x);
}else
map0.put(a[i],1);
c[i]=0;
}
else if(!map1.containsKey(v)) {
if(map1.containsKey(a[i])) {
int x=map1.get(a[i]);
x++;
map1.put(a[i],x);
}else
map1.put(a[i],1);
c[i]=1;
}
else {
if(map0.get(v)<=map1.get(v)) {
if(map0.containsKey(a[i])) {
int x=map0.get(a[i]);
x++;
map0.put(a[i],x);
}else
map0.put(a[i],1);
c[i]=0;
}
else {
if(map1.containsKey(a[i])) {
int x=map1.get(a[i]);
x++;
map1.put(a[i],x);
}else
map1.put(a[i],1);
c[i]=1;
}
}
}
for(int i=0;i<n;i++) {
System.out.print(c[i]+" ");
}
System.out.println();
}
}
}
/*class Pair{
int a;
int b;
Pair(int a,int b){
this.a=a;
this.b=b;
}
}*/
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 45ebc06fff22506c68856cf3acba3a3c | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) throws java.lang.Exception {
Reader pm =new Reader();
//Scanner pm = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int t = pm.nextInt();
while(t-- > 0){
int n = pm.nextInt();
int[] a = new int[n];
int k = pm.nextInt();
HashMap<Integer, TreeSet<Integer>> hm = new HashMap<>();
for(int i=0;i<n;i++) {
a[i] = pm.nextInt();
TreeSet<Integer> ts = new TreeSet<>();
if(hm.get(a[i]) != null)
ts = hm.get(a[i]);
ts.add(i);
hm.put(a[i], ts);
}
HashSet<Integer> c = new HashSet<>();
HashSet<Integer> d = new HashSet<>();
HashSet<Integer> hs1 = new HashSet<>();
HashSet<Integer> hs2 = new HashSet<>();
HashMap<Integer, Integer> last = new HashMap<>();
//System.out.println(hm);
for(int i=0;i<n;i++){
int rem = k - a[i];
if(rem != a[i]) {
if(c.contains(rem)) {
d.add(a[i]);
hs1.add(i);
}
else {
c.add(a[i]);
hs2.add(i);
}
}
else if(rem == a[i]) {
if(last.getOrDefault(a[i], 0) == 0) {
c.add(a[i]);
hs2.add(i);
last.put(a[i], 1);
}
else {
d.add(a[i]);
hs1.add(i);
last.put(a[i], 0);
}
}
// if(hm.get(a[i]) != null) {
// TreeSet<Integer> t2 = hm.get(a[i]);
// if(t2.size() == 1) {
// hm.remove(a[i]);
// }
// else if(t2.size() >= 1) {
// t2.remove(i);
// hm.put(a[i], ts);
// }
// }
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<n;i++) {
if(hs2.contains(i))
sb.append("0 ");
else
sb.append("1 ");
}
System.out.println(sb);
}
//end of tests
}
//end of main class
static int countInRange(int arr[], int n, int x, int y) {
// initialize result
int count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
static int lowerIndex(int arr[], int n, int x) {
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
// function to find last index <= y
static int upperIndex(int arr[], int n, int y) {
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
public static StringBuilder dec_to_bin(long n) {
// decimal to binary upto 30 binaries
if(n==0) {
StringBuilder str=new StringBuilder("");
for(int i=0;i<30;i++) {
str.append("0");
}
return str;
}
StringBuilder str=new StringBuilder("");
while(n!=0) {
str.append(n%2L);
n/=2L;
}
str=str.reverse();
StringBuilder tmp_str=new StringBuilder("");
int len=str.length();
while(len!=30) {
tmp_str.append("0");
len++;
}
tmp_str.append(str);
return tmp_str;
}
private static int binarySearchPM(int[] arr, int key){
int n = arr.length;
int mid = -1;
int begin = 0,end=n;
while(begin <= end){
mid = (begin+end) / 2;
if(mid == n){
return n;
}
if(key < arr[mid]){
end = mid-1;
} else if(key > arr[mid]){
begin = mid+1;
} else {
return mid;
}
}
//System.out.println(begin+" "+end);
return -begin; //expected Index
}
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();
}
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 92a331a5f59a6e0ba7019a60668ddd29 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes |
import java.util.*;
import java.io.*;
public class Two_Arrays_B {
static int mod = (int) (1e9 + 7);
public static void main(String[] args) throws java.lang.Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken()),T=Integer.parseInt(st.nextToken());
int a[]=new int[n],cnt=0;
st = new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
a[i]=Integer.parseInt(st.nextToken());
if(2*a[i]<T) {
System.out.print(0+" ");
}else if(2*a[i]==T) {
System.out.print((++cnt)%2+" ");
}else {
System.out.print(1+" ");
}
}
System.out.println();
}
}
static class CP {
static long binary_Expo(long a, long b) { // calculating a^b
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res *= a;
--b;
}
a *= a;
b=(b<<1);
}
return res;
}
static long Modular_Expo(long a, long b) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res = (res * a) % mod;
--b;
}
a = (a * a) % mod;
b /= 2;
}
return res;
}
static int i_gcd(int a, int b) {// iterative way to calculate gcd.
while (true) {
if (b == 0)
return a;
int c = a;
a = b;
b = c % b;
}
}
static long gcd(long a, long b) {// here b is the remainder
if (b == 0)
return a; //because each time b will divide a.
return gcd(b, a % b);
}
static long ceil_div(long a, long b) { // a numerator b denominator
return (a + b - 1) / b;
}
static int getIthBitFromInt(int bits, int i) {
return (bits >> (i - 1)) & 1;
}
static int upper_Bound(int a[], int x) {//closest to the left+1
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static int lower_Bound(int a[], int x) {//closest to the right
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static void sort(int a[]) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int i = 0; i < a.length; i++)
q.add(a[i]);
for (int i = 0; i < a.length; i++)
a[i] = q.poll();
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | f8526242ef2786b19a2b49c300f90603 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.*;
import java.io.*;
public class Two_Arrays_B {
static int mod = (int) (1e9 + 7);
public static void main(String[] args) throws java.lang.Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken()),T=Integer.parseInt(st.nextToken());
int a[]=new int[n],cnt=0;
st = new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
a[i]=Integer.parseInt(st.nextToken());
if(2*a[i]<T) {
System.out.print(0+" ");
}else if(2*a[i]==T) {
System.out.print((cnt++)%2+" ");
}else {
System.out.print(1+" ");
}
}
System.out.println();
}
}
static class CP {
static long binary_Expo(long a, long b) { // calculating a^b
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res *= a;
--b;
}
a *= a;
b=(b<<1);
}
return res;
}
static long Modular_Expo(long a, long b) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res = (res * a) % mod;
--b;
}
a = (a * a) % mod;
b /= 2;
}
return res;
}
static int i_gcd(int a, int b) {// iterative way to calculate gcd.
while (true) {
if (b == 0)
return a;
int c = a;
a = b;
b = c % b;
}
}
static long gcd(long a, long b) {// here b is the remainder
if (b == 0)
return a; //because each time b will divide a.
return gcd(b, a % b);
}
static long ceil_div(long a, long b) { // a numerator b denominator
return (a + b - 1) / b;
}
static int getIthBitFromInt(int bits, int i) {
return (bits >> (i - 1)) & 1;
}
static int upper_Bound(int a[], int x) {//closest to the left+1
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static int lower_Bound(int a[], int x) {//closest to the right
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static void sort(int a[]) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int i = 0; i < a.length; i++)
q.add(a[i]);
for (int i = 0; i < a.length; i++)
a[i] = q.poll();
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 929c05cde72b1416509c3a18a7580120 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int n= Integer.parseInt(st.nextToken());
int T= Integer.parseInt(st.nextToken());
int[] a = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < a.length; i++) {
a[i]= Integer.parseInt(st.nextToken());
}
int[] ans=new int[n];
TreeSet<Integer> ones = new TreeSet<Integer>();
boolean putinOne=false;
for (int i = 0; i < a.length; i++) {
if((a[i]==(T-a[i]) && putinOne) || (a[i]!=(T-a[i]) && ones.contains(a[i])))
{
ans[i]=1;
}
else
{
ones.add(Math.abs(T-a[i]));
}
if(a[i]==T-a[i])putinOne=!putinOne;
}
for (int i = 0; i < ans.length; i++) {
pw.print(ans[i] + " ");
}
pw.println();
}
pw.close();
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 1a7d76983d04de925b07637a65b14d9b | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException{
try {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while(t-->0)
{
int n =fs.nextInt();
int T =fs.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=fs.nextInt();
}
int flag=0;
Vector <Integer> v = new Vector<>();
for(int i=0;i<n;i++)
{
if(a[i]<(double)T/2)
{
v.add(1);
}
if(a[i]>(double)T/2)
{
v.add(0);
}
if(a[i]==(double)T/2)
{
if(flag==1)
{
v.add(1);
flag=0;
}
else
{
v.add(0);
flag=1;
}
}
}
for(int i=0;i<n;i++)
{
System.out.print(v.get(i)+" ");
}
System.out.println();
}
}
catch (Exception e)
{
return;
}
}
static class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 037e0e7a9db4e848712e74710db85c67 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | //package codeforces.r673_1417;
import java.util.*;
public class B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int i = 0; i < T; i++) {
int n = scanner.nextInt();
int unlucky = scanner.nextInt();
int[] array = new int[n];
for (int j = 0; j < n; j++) {
array[j] = scanner.nextInt();
}
int[] colored = color(array, n, unlucky);
for (int j = 0; j < n; j++) {
System.out.print(colored[j] + " ");
}
System.out.println();
}
}
static class Element {
boolean isColored;
List<Integer> indices;
int value;
public Element(int value) {
this.value = value;
this.indices = new ArrayList<>();
this.isColored = false;
}
}
private static int[] color(int[] array, int n, int unlucky) {
int[] colored = new int[n];
Map<Integer, Integer> colorMap = new HashMap<>();
Map<Integer, Element> elementMap = new HashMap<>();
List<Element> elements = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
if (elementMap.containsKey(array[i])) {
elementMap.get(array[i]).indices.add(i);
} else {
Element e = new Element(array[i]);
e.indices.add(i);
elementMap.put(array[i], e);
elements.add(e);
}
}
Collections.sort(elements, Comparator.comparingInt(e -> e.value));
for (int i = 0; i < elements.size(); i++) {
Element e = elements.get(i);
if (e.isColored) {
continue;
}
int reverse = unlucky - e.value;
if (reverse == e.value) {
for (int j = 0; j < e.indices.size() / 2; j++) {
colorMap.put(e.indices.get(j), 0);
}
for (int j = e.indices.size()/2; j <e.indices.size() ; j++) {
colorMap.put(e.indices.get(j), 1);
}
e.isColored = true;
} else {
for (int j = 0; j < e.indices.size(); j++) {
colorMap.put(e.indices.get(j), 0);
}
if (elementMap.containsKey(reverse)) {
Element reverseElement = elementMap.get(reverse);
for (int j = 0; j < reverseElement.indices.size(); j++) {
colorMap.put(reverseElement.indices.get(j), 1);
}
reverseElement.isColored = true;
}
e.isColored = true;
}
}
for (int i = 0; i < n; i++) {
colored[i] = colorMap.get(i);
}
return colored;
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 0bf0364bc05842044c3cdaeb9925623d | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class CF673B {
static PrintWriter pw;
static class FastReader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public FastReader() {
this(System.in);
}
public FastReader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long l() {
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 int i() {
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 double d() throws IOException {
return Double.parseDouble(next());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public void scanIntArr(int[] arr) {
for (int li = 0; li < arr.length; ++li)
arr[li] = i();
}
public void scanIntIndexArr(int[] arr) {
for (int li = 0; li < arr.length; ++li) {
arr[li] = i() - 1;
}
}
public void printIntArr(int[] arr) {
for (int li = 0; li < arr.length; ++li)
pw.print(arr[li] + " ");
pw.println();
}
public void printLongArr(long[] arr) {
for (int li = 0; li < arr.length; ++li)
pw.print(arr[li] + " ");
pw.println();
}
public void scanLongArr(long[] arr) {
for (int i = 0; i < arr.length; ++i) {
arr[i] = l();
}
}
public void shuffle(int[] arr) {
for (int i = arr.length; i > 0; --i) {
int r = (int) (Math.random() * i);
int temp = arr[i - 1];
arr[i - 1] = arr[r];
arr[r] = temp;
}
}
public int findMax(int[] arr) {
return Arrays.stream(arr).max().getAsInt();
}
}
public static void main(String[] args) throws IOException {
FastReader fr = new FastReader();
pw = new PrintWriter(System.out);
int i, j, n, m, temp, t, k;
t = fr.i();
while(t-->0) {
n = fr.i();
m = fr.i();
int arr[] = new int[n];
int ans[] = new int[n];
HashMap<Integer, Integer> mp = new HashMap<>();
fr.scanIntArr(arr);
int ct=0;
for(i=0;i<n;i++) {
if(m%2==0 && arr[i]==m/2)
++ct;
}
for(i=0;i<n;i++) {
if(arr[i]>m || !mp.containsKey(m-arr[i])) {
if(mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i])+1);
}
else {
mp.put(arr[i],1);
}
}
}
for(i=0;i<n;i++) {
if(mp.containsKey(arr[i]) && mp.get(arr[i])>0) {
ans[i]=1;
mp.put(arr[i], mp.get(arr[i])-1);
}
}
ct/=2;
for(i=0;i<n;i++) {
if(arr[i]==m/2 && ct>0) {
--ct;
pw.print("1 ");
}
else
pw.print(ans[i]+" ");
}
pw.println();
// fr.printIntArr(ans);
}
pw.flush();
pw.close();
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 5ed369a36b3d957c480824c07aa3a3c8 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class TwoArray1 {
static int[] arr;
static int n;
static int k;
public static void main(String[] args) throws IOException {
PrintWriter pw = new PrintWriter(System.out);
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int T = sc.nextInt();
int[] arr = new int[n];
int c = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (x * 2 % 2 == 0 && x * 2 == T) {
x=(c++) %2;
}
else if( x*2<T) x=0;
else x=1;
pw.print(x+" ");
}
pw.println();
}
pw.flush();
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
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 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 {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 57408eabe963593ba2732013c45b2226 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
solution(in, out);
in.close();
out.close();
}
public static void solution(Scanner in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
algo(in, out);
}
}
public static void algo(Scanner in, PrintWriter out) {
int n = in.nextInt();
long t = in.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = in.nextInt();
}
int prev = 0;
for (int i = 0; i < n; i++) {
long v = A[i];
if (2*v < t ) {
System.out.print(0);
} else if (2*v > t ) {
System.out.print(1);
} else {
System.out.print(prev);
prev = prev ^ 1;
}
if (i == n - 1) {
System.out.println();
} else {
System.out.print(" ");
}
}//for
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | dcef715fcdc8d9da0bb53202c6a1e220 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.*;
public class S
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int test=scan.nextInt();
while(test>0)
{
test--;
int n=scan.nextInt();
int t=scan.nextInt();
HashMap<Integer,Integer> count=new HashMap<>();
int ans[]=new int[n];
int y=scan.nextInt();
ans[0]=1;
count.put(y,1);
for(int i=1;i<n;i++)
{
int temp=scan.nextInt();
int diff=t-temp;
if(count.containsKey(diff))
{
if(diff==temp)
{
int c=count.get(diff);
if(c%2!=0)
ans[i]=0;
else
ans[i]=1;
count.put(diff,count.get(diff)+1);
}
else
{
ans[i]=0;
}
}
else
{
count.put(temp,1);
ans[i]=1;
}
}
for(int k: ans)
System.out.print(k+" ");
System.out.println();
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 41dcc95097fb61231f2fb52b9f7fb6bc | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.Scanner;
import java.util.StringJoiner;
public class _B_ {
public static void main(String[] args) {
final Scanner in = new Scanner(System.in);
for (int tt = in.nextInt(); tt > 0; tt--) {
final int n = in.nextInt(), t = in.nextInt();
final int[] a = new int[n];
final int mid = t >> 1;
int count = 0;
for (int i = 0; i < n; i++) {
if ((a[i] = in.nextInt()) == mid)
count++;
}
count >>= 1;
final StringJoiner sj = new StringJoiner(" ");
for (int i = 0; i < n; i++) {
sj.add(a[i] < mid || (t & 1) == 1 && a[i] == mid || a[i] == mid && count-- > 0 ? "0" : "1");
}
System.out.println(sj);
}
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 18a9a7cce1ff47c62de0e55dd5357096 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private static InputReader in;
private static PrintWriter out;
public static void main(String[] args) {
in = new InputReader();
out = new PrintWriter(System.out);
int t = 1;
t = in.nextInt();
for (int i = 0; i < t; ++i) {
solve();
}
out.close();
}
private static void solve() throws RuntimeException {
int n = in.nextInt();
int t = in.nextInt();
int[] a = new int[n];
HashSet<Integer> set = new HashSet<>();
HashMap<Integer, Integer> which = new HashMap<>();
int cnt = 0;
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
set.add(a[i]);
if (t % 2 == 0 && a[i] == t / 2) ++cnt;
}
for (int i = 0; i < n; ++i) {
if (which.containsKey(a[i])) continue;
if (a[i] > t) {
which.put(a[i], 1);
continue;
}
if (set.contains(t - a[i])) {
if (t / 2 != a[i]) {
which.put(t - a[i], 1);
which.put(a[i], 0);
}
} else which.put(a[i], 1);
}
int done = 0;
for (int i = 0; i < n; ++i) {
if (which.containsKey(a[i])) out.print(which.get(a[i]) + " ");
else {
if (done < cnt / 2) {
out.print(0 + " ");
} else out.print(1 + " ");
++done;
}
}
out.print("\n");
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 10f6c6a5a03f136badab1cd08b51367f | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringTokenizer st;
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int T = Integer.parseInt(st.nextToken());
int[] a = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
solve(n, T, a);
}
br.close();
}
private static void solve(int n, int T, int[] a) {
int[] res = new int[n];
HashSet<Integer> s = new HashSet<>();
for (int i = 0; i < n; i++) {
if (s.contains(a[i])) {
res[i] = 1;
if (2 * a[i] == T) {
s.remove(a[i]);
}
} else {
s.add(T - a[i]);
}
}
for (int x : res) {
System.out.printf("%d ", x);
}
System.out.println();
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 86a53f8e2618570701f6e93b12c9df32 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TwoArrays {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int matches = Integer.valueOf(in.nextLine());
String[] result = new String[matches];
for (int i = 0; i < matches; i++) {
int unlucky = Arrays.stream(in.nextLine().split(" ")).map(Integer::valueOf).mapToInt(n -> n).toArray()[1];
int[] array = Arrays.stream(in.nextLine().split(" ")).map(Integer::valueOf).mapToInt(n -> n).toArray();
Map<Integer, Integer> intMap = new HashMap<>(array.length);
boolean meta = false;
StringBuilder sb = new StringBuilder();
for (int j=0; j<array.length; j++) {
if (sb.length()>0) {
sb.append(" ");
}
if (intMap.containsKey(array[j])) {
if (intMap.get(array[j]) == array[j]) {
if (!meta) {
sb.append("1");
} else {
sb.append("0");
}
meta = !meta;
} else {
sb.append("1");
}
} else {
sb.append("0");
intMap.put(unlucky-array[j], array[j]);
}
}
result[i] = sb.toString();
}
Arrays.stream(result).forEach(System.out::println);
}
}
| Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | f5e13a14834a306a4d2dcad1e1400f64 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.Scanner;
public class Solution2 {
public static void main(String[] args) {
/*
* 7 7
5 5 1 2 3 4 6
*/
Scanner scan = new Scanner(System.in);
Solution2 sol = new Solution2();
int t=scan.nextInt();
while(t-->0) {
int n=scan.nextInt();
int k=scan.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=scan.nextInt();
}
//Arrays.parallelSort(arr);
int[] ans=new int[n];
boolean alt=false;
for(int i=0;i<n;i++) {
if(k-arr[i]>arr[i]) {
ans[i]=0;
}else if(k-arr[i]==arr[i]) {
if(!alt) {
ans[i]=1;
alt=true;
}
else {
ans[i]=0;
alt=false;
}
}
else {
ans[i]=1;
}
}
for(int i=0;i<n;i++) {
if(i>0) System.out.print(" ");
System.out.print(ans[i]);
}
System.out.println();
}
scan.close();
}
public static int bs(int[] arr, int s, int l, int r) {
for(int i=l;i<=r;i++) {
if(arr[i]==s) return i;
}
return -1;
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | d309662d476521ead61aff54eae12483 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class MAIN {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int t = userInput.nextInt();
for (int runs = 0; runs < t; runs++) {
int n = userInput.nextInt();
int T = userInput.nextInt();
int[] a = new int[n];
Group g1 = new Group();
Group g2 = new Group();
for (int i = 0; i < a.length; i++) {
a[i] = userInput.nextInt();
}
int[] locs = new int[n];
for (int i=0;i<a.length;++i)
{
int diff = T - a[i];
boolean g1Contains = g1.hashMap.containsKey(diff);
boolean g2Contains = g2.hashMap.containsKey(diff);
if (g1Contains && g2Contains)
{
int v1 = g1.hashMap.get(diff);
int v2 = g2.hashMap.get(diff);
if (v1 < v2)
{
g1.hashMap.put(a[i], v1+1);
locs[i] = 0;
}
else
{
g2.hashMap.put(a[i], v2+1);
locs[i] = 1;
}
}
else if (g1Contains && !g2Contains)
{
g2.hashMap.put(a[i], 1);
locs[i] = 1;
}
else if (!g1Contains)
{
g1.hashMap.put(a[i], 1);
locs[i] = 0;
}
}
for (int i = 0; i < locs.length; i++)
{
System.out.print(locs[i]+" ");
}
System.out.println();
}
userInput.close();
}
}
class Group
{
HashMap<Integer, Integer> hashMap;
int val;
Group()
{
val = 0;
hashMap = new HashMap<Integer, Integer>();
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 6f1b1885d298d0e3eb31a6b9edadabf1 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public class Main {
static PrintWriter w = new PrintWriter(System.out);
static Reader sc = new Reader();
public static void main(String args[]) throws IOException {
int tc = sc.ni();
for (int i = 0; i < tc; i++) {
// w.print("Case #"+(i+1)+": ");
solve();
}
w.close();
}
//// SOLUTION BEGIN <--------------------------------------->
public static void solve() {
int n = sc.ni();
int t = sc.ni();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.ni();
}
HashMap<Integer, Integer> white0 = new HashMap<>();
HashMap<Integer, Integer> black1 = new HashMap<>();
for (int i = 0; i < n; i++) {
if (!white0.containsKey(Math.abs(arr[i] - t))) {
white0.put(arr[i], 1);
arr[i] = 0;
continue;
}
if (!black1.containsKey(Math.abs(arr[i] - t))) {
black1.put(arr[i], 1);
arr[i] = 1;
continue;
}
if ((white0.get(Math.abs(arr[i] - t))) <= (black1.get(Math.abs(arr[i] - t)))) {
white0.put(arr[i],white0.get(arr[i])+1);
arr[i] = 0;
} else{
black1.put(arr[i],black1.get(arr[i])+1);
arr[i] = 1;
}
}
for (int i : arr) {
w.print(i + " ");
}
w.println();
}
//// SOLUTION END <------------------------------------------->
// Class for Fast I/O------------------------------
static class Reader {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public double nd() {
return Double.parseDouble(next());
}
public String nline() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | b702323d41cf4bccfdd8dcee208fe6d8 | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B
{
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
static StringBuilder sb=new StringBuilder();
static long MOD = (long)(998244353);
// Main Class Starts Here
public static void main(String args[])throws IOException
{
// Write your code.
int t = in();
while(t-->0) {
int n =in();
long T = lin();
long a[] = lan(n);
int color [] = new int[n];
int sum = 0;
for(int i =0 ; i< n ;i++){
if(a[i] <= (T/2)) color[i] = 1;
if(T % 2 == 0 && a[i] == (T/2)) sum++;
}
sum /= 2;
for(int i =0 ;i < n;i++){
if(T % 2 == 0 && a[i] == (T/2) && sum > 0){
color[i] = 0;
sum--;
}
}
for(int x:color) app(x+" ");
app("\n");
}
out.printLine(sb);
out.close();
}
public static int[] an(int n) {
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=in();
return ar;
}
public static long[] lan(int n) {
long ar[]=new long[n];
for(int i=0;i<n;i++)
ar[i]=lin();
return ar;
}
public static String atos(Object ar[]) {
return Arrays.toString(ar);
}
public static int in() {
return in.readInt();
}
public static long lin() {
return in.readLong();
}
public static String sn() {
return in.readString();
}
public static void prln(Object o) {
out.printLine(o);
}
public static void prn(Object o) {
out.print(o);
}
public static void display(int a[]) {
out.printLine(Arrays.toString(a));
}
public static HashMap<Integer,Integer> hm(int a[]){
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0; i < a.length;i++) {
int keep = (int)map.getOrDefault(a[i],0);
map.put(a[i],++keep);
}
return map;
}
public static void app(Object o) {
sb.append(o);
}
public static long calcManhattanDist(int x1,int y1,int x2,int y2) {
long xa = Math.abs(x1-x2);
long ya = Math.abs(y1-y2);
return (xa+ya);
}
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 long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 1277cd17ac3639c4e74dabe56ba3412b | train_004.jsonl | 1601219100 | RedDreamer has an array $$$a$$$ consisting of $$$n$$$ non-negative integers, and an unlucky integer $$$T$$$.Let's denote the misfortune of array $$$b$$$ having length $$$m$$$ as $$$f(b)$$$ — the number of pairs of integers $$$(i, j)$$$ such that $$$1 \le i < j \le m$$$ and $$$b_i + b_j = T$$$. RedDreamer has to paint each element of $$$a$$$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $$$c$$$ and $$$d$$$ so that all white elements belong to $$$c$$$, and all black elements belong to $$$d$$$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $$$f(c) + f(d)$$$ is minimum possible.For example: if $$$n = 6$$$, $$$T = 7$$$ and $$$a = [1, 2, 3, 4, 5, 6]$$$, it is possible to paint the $$$1$$$-st, the $$$4$$$-th and the $$$5$$$-th elements white, and all other elements black. So $$$c = [1, 4, 5]$$$, $$$d = [2, 3, 6]$$$, and $$$f(c) + f(d) = 0 + 0 = 0$$$; if $$$n = 3$$$, $$$T = 6$$$ and $$$a = [3, 3, 3]$$$, it is possible to paint the $$$1$$$-st element white, and all other elements black. So $$$c = [3]$$$, $$$d = [3, 3]$$$, and $$$f(c) + f(d) = 0 + 1 = 1$$$. Help RedDreamer to paint the array optimally! | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B
{
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
static StringBuilder sb=new StringBuilder();
static long MOD = (long)(998244353);
// Main Class Starts Here
public static void main(String args[])throws IOException
{
// Write your code.
int t = in();
while(t-->0) {
int n =in();
long T = lin();
long a[] = lan(n);
HashMap<Long, Integer> fr = new HashMap<>();
for(long x:a){
int occr = fr.getOrDefault(x, 0);
fr.put(x, ++occr);
}
//prln(fr);
HashMap<Long, Integer> clr = new HashMap<>();
int half = 0;
int color [] = new int [n];
for(int i =0 ;i < n;i++) {
long x = a[i];
long other = T - x;
if (x == other) {
half = fr.get(x) / 2;
clr.put(x, 0);
continue;
}
if (fr.containsKey(x) && fr.containsKey(other)) {
clr.put(x, 1);
clr.put(other, -1);
fr.remove(x);
}
//prln(fr);
}
//prln(clr+" , "+half);
for(int i =0 ;i < n;i++){
int val = clr.getOrDefault(a[i],-1);
if(val == 1) color[i] = 1;
if(val == 0 && half > 0){
color[i] = 1;
half--;
}
}
for(int i =0 ;i < n;i++){
app(color[i]+" ");
}
app("\n");
}
out.printLine(sb);
out.close();
}
public static int[] an(int n) {
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=in();
return ar;
}
public static long[] lan(int n) {
long ar[]=new long[n];
for(int i=0;i<n;i++)
ar[i]=lin();
return ar;
}
public static String atos(Object ar[]) {
return Arrays.toString(ar);
}
public static int in() {
return in.readInt();
}
public static long lin() {
return in.readLong();
}
public static String sn() {
return in.readString();
}
public static void prln(Object o) {
out.printLine(o);
}
public static void prn(Object o) {
out.print(o);
}
public static void display(int a[]) {
out.printLine(Arrays.toString(a));
}
public static HashMap<Integer,Integer> hm(int a[]){
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0; i < a.length;i++) {
int keep = (int)map.getOrDefault(a[i],0);
map.put(a[i],++keep);
}
return map;
}
public static void app(Object o) {
sb.append(o);
}
public static long calcManhattanDist(int x1,int y1,int x2,int y2) {
long xa = Math.abs(x1-x2);
long ya = Math.abs(y1-y2);
return (xa+ya);
}
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 long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
} | Java | ["2\n6 7\n1 2 3 4 5 6\n3 6\n3 3 3"] | 1 second | ["1 0 0 1 1 0 \n1 0 0"] | null | Java 8 | standard input | [
"sortings",
"greedy",
"math"
] | 6458ad752e019ffb87573c8bfb712c18 | The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$T$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le T \le 10^9$$$) — the number of elements in the array and the unlucky integer, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,100 | For each test case print $$$n$$$ integers: $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (each $$$p_i$$$ is either $$$0$$$ or $$$1$$$) denoting the colors. If $$$p_i$$$ is $$$0$$$, then $$$a_i$$$ is white and belongs to the array $$$c$$$, otherwise it is black and belongs to the array $$$d$$$. If there are multiple answers that minimize the value of $$$f(c) + f(d)$$$, print any of them. | standard output | |
PASSED | 7a05525f0ae71e45087fae26ead1678d | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.*;
import java.util.*;
public class check {
int INF = (int)1e9;
long MOD = 1000000007;
void solve(InputReader in, PrintWriter out) throws IOException {
int n = in.nextInt();
int[] d = new int[n];
int s = 0;
for(int i=0; i<n; i++) {
d[i] = in.nextInt();
s += d[i];
}
Arrays.sort(d);
if(d[0]!=0) {
out.println(-1);
return;
}
if(d[n-1]==0) {
out.println(0);
return;
}
int j = -1, k = -1;
if(s%3==0) {
// pass
} else if(s%3==1) {
for(int i=0; i<n; i++) {
if(d[i]%3==1) {
j = i;
break;
}
}
if(j==-1) {
for(int i=0; i<n; i++) {
if(d[i]%3==2) {
j = i;
break;
}
}
for(int i=j+1; i<n; i++) {
if(d[i]%3==2) {
k = i;
break;
}
}
}
} else {
for(int i=0; i<n; i++) {
if(d[i]%3==2) {
j = i;
break;
}
}
if(j==-1) {
for(int i=0; i<n; i++) {
if(d[i]%3==1) {
j = i;
break;
}
}
for(int i=j+1; i<n; i++) {
if(d[i]%3==1) {
k = i;
break;
}
}
}
}
boolean df = false;
for(int i=n-1; i>=0; i--) {
if(i!=j && i!=k) {
if(d[i]==0 && !df) continue;
out.print(d[i]);
df = true;
}
}
if(!df) out.print(0);
out.println();
}
public static void main(String[] args) throws IOException {
if(args.length>0 && args[0].equalsIgnoreCase("d")) {
DEBUG_FLAG = true;
}
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int t = 1;//in.nextInt();
long start = System.nanoTime();
while(t-- >0) {
new check().solve(in, out);
}
long end = System.nanoTime();
debug("\nTime: " + (end-start)/1e6 + " \n\n");
out.close();
}
static boolean DEBUG_FLAG = false;
static void debug(String s) {
if(DEBUG_FLAG) System.out.print(s);
}
public static void show(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static class InputReader {
static BufferedReader br;
static StringTokenizer st;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 8f4aa59311fd6a3e5a8f4ce341267664 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.util.*;
import java.io.*;
public class b
{
public static void main(String[] arg) throws IOException
{
new b();
}
public b() throws IOException
{
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
int sum = 0;
for(int i = 0; i < n; i++)
{
int v = in.nextInt();
list.add(v);
sum += v;
}
Collections.sort(list, Collections.reverseOrder());
if(list.get(list.size()-1) != 0)
{
System.out.println(-1);
}
else
{
if(sum%3 == 1)
{
for(int i = list.size()-1; i >= 0; i--)
{
if(list.get(i)%3 == 1)
{
list.remove(i);
sum--;
break;
}
}
if(sum%3 == 1)
{
for(int i = list.size()-1; i >= 0; i--)
{
if(list.get(i)%3 == 2)
{
list.remove(i);
sum -= 2;
break;
}
}
for(int i = list.size()-1; i >= 0; i--)
{
if(list.get(i)%3 == 2)
{
list.remove(i);
sum -= 2;
break;
}
}
}
}
else if(sum%3 == 2)
{
for(int i = list.size()-1; i >= 0; i--)
{
if(list.get(i)%3 == 2)
{
list.remove(i);
sum -= 2;
break;
}
}
if(sum%3 == 2)
{
for(int i = list.size()-1; i >= 0; i--)
{
if(list.get(i)%3 == 1)
{
list.remove(i);
sum--;
break;
}
}
for(int i = list.size()-1; i >= 0; i--)
{
if(list.get(i)%3 == 1)
{
list.remove(i);
sum--;
break;
}
}
}
}
StringBuilder sb = new StringBuilder();
int start = 0;
while(start < list.size() && list.get(start) == 0) start++;
for(int i = start; i < list.size(); i++) sb.append(list.get(i));
if(sb.length() == 0) sb.append(0);
if(sum%3 != 0) System.out.println(0);
else System.out.println(sb.toString());
}
in.close(); out.close();
}
class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
String next() throws IOException
{
while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
void close() throws IOException
{
br.close();
}
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 2c897612a01752145be1f5e9071ba838 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | // John Lewis
// Date:
// Program:
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
// Variables
int n = input.nextInt();
int[] allDigits = new int[n];
int[] digitCount = new int[10];
int sumDigits = 0;
int[] digitsToRemoveMinusOne = {2, 5, 8};
int[] digitsToRemoveMinusTwo = {1, 4, 7};
boolean debug = false;
for (int i = 0; i < n; i++) {
allDigits[i] = input.nextInt();
digitCount[allDigits[i]]++;
sumDigits += allDigits[i];
}
Arrays.sort(allDigits);
if (debug) {
for (int i = 0; i < 10; i++) {
out.print(digitCount[i] + " ");
}
out.println();
for (int i = 0; i < n; i++) {
System.out.print(allDigits[i] + " ");
}
out.println(sumDigits + "\n");
}
if (allDigits[0] == 0) {
if (sumDigits == 0) {
out.println(0);
out.close();
System.exit(0);
}
if (sumDigits % 3 == 0) {
//System.out.println("NO WAY");
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < digitCount[i]; j++) {
out.print(i);
}
}
out.println();
}
else {
// need to find nearest multiple of three
//System.out.println("PLEASE HERE");
boolean madeChange = false;
if ((sumDigits + 1) % 3 == 0) {
//System.out.println("Are we going here");
for (int i = 0; i < 3; i++) {
if (digitCount[digitsToRemoveMinusOne[i]] >= 1) {
// need to remove that one from the list
digitCount[digitsToRemoveMinusOne[i]]--;
madeChange = true;
break;
}
if (i == 2) {
for (int j = 0; j < 3; j++) {
if (digitCount[digitsToRemoveMinusTwo[j]] >= 2) {
digitCount[digitsToRemoveMinusTwo[j]] -= 2;
madeChange = true;
break;
}
if (j != 2){
if (digitCount[digitsToRemoveMinusTwo[j]] == 1 && digitCount[digitsToRemoveMinusTwo[j+1]] == 1){
digitCount[digitsToRemoveMinusTwo[j]]--;
digitCount[digitsToRemoveMinusTwo[j+1]]--;
madeChange = true;
break;
}
}
else{
if (digitCount[digitsToRemoveMinusTwo[j]] == 1 && digitCount[digitsToRemoveMinusTwo[j-2]] == 1){
digitCount[digitsToRemoveMinusTwo[j]]--;
digitCount[digitsToRemoveMinusTwo[j-2]]--;
madeChange = true;
break;
}
}
}
}
}
}
else if ((sumDigits + 2) % 3 == 0) {
//out.println("SHOULD BE HERE");
for (int i = 0; i < 3; i++) {
if (digitCount[digitsToRemoveMinusTwo[i]] >= 1) {
digitCount[digitsToRemoveMinusTwo[i]]--;
madeChange = true;
break;
}
if (i == 2){
for (int j = 0; j < 3; j++) {
if (digitCount[digitsToRemoveMinusOne[j]] >= 2) {
digitCount[digitsToRemoveMinusOne[j]] -= 2;
madeChange = true;
break;
}
if (j != 2){
if (digitCount[digitsToRemoveMinusOne[j]] == 1 && digitCount[digitsToRemoveMinusOne[j+1]] == 1){
digitCount[digitsToRemoveMinusOne[j]]--;
digitCount[digitsToRemoveMinusOne[j+1]]--;
madeChange = true;
break;
}
}
else{
if (digitCount[digitsToRemoveMinusOne[j]] == 1 && digitCount[digitsToRemoveMinusOne[j-2]] == 1){
digitCount[digitsToRemoveMinusOne[j]]--;
digitCount[digitsToRemoveMinusOne[j-2]]--;
madeChange = true;
break;
}
}
}
}
}
}
if (madeChange) {
sumDigits = 0;
for (int i = 0; i < 10; i++) {
sumDigits += digitCount[i] * i;
}
//System.out.println(sumDigits);
if (sumDigits > 0) {
//System.out.println("GET HERE?");
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < digitCount[i]; j++) {
out.print(i);
}
}
out.println();
}
else {
out.println(0);
}
}
else {
out.println(0);
}
}
}
else {
//System.out.println("WHERE ARE WE");
out.println(-1);
}
out.close();
} // END MAIN
}
class input {
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 String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | ef8ee208083645603f7f5bfc421b11f1 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.util.*;
import java.io.*;
public class P214B {
private static void solve() {
int n = nextInt();
Map<Integer, List<Integer>> map = new HashMap<>();
int[] cnt = new int[10];
int sum = 0;
boolean zero = false;
for (int i = 0; i < n; i++) {
int x = nextInt();
cnt[x]++;
zero |= (x == 0);
sum += x;
map.computeIfAbsent(x % 3, (k) -> new ArrayList<>()).add(x);
}
if (map.get(1) != null) {
Collections.sort(map.get(1));
}
if (map.get(2) != null) {
Collections.sort(map.get(2));
}
if (!zero) {
System.out.println(-1);
return;
} else {
int r = sum % 3;
if (r == 1) {
List<Integer> one = map.get(1);
if (one == null) {
List<Integer> two = map.get(2);
if (two != null && two.size() >= 2) {
cnt[two.get(0)]--;
cnt[two.get(1)]--;
} else {
System.out.println(-1);
return;
}
} else {
cnt[one.get(0)]--;
}
} else if (r == 2) {
List<Integer> one = map.get(1);
List<Integer> two = map.get(2);
if (two == null) {
if (one != null && one.size() >= 2) {
cnt[one.get(0)]--;
cnt[one.get(1)]--;
} else {
System.out.println(-1);
return;
}
} else {
cnt[two.get(0)]--;
}
}
}
StringBuilder ans = new StringBuilder();
for (int i = 9; i > 0; i--) {
for (int j = 0; j < cnt[i]; j++) {
ans.append(i);
}
}
if (ans.length() > 0) {
for (int i = 0; i < cnt[0]; i++) {
ans.append(0);
}
} else {
ans = new StringBuilder("0");
}
System.out.println(ans.toString());
}
private static void run() {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
private static StringTokenizer st;
private static BufferedReader br;
private static PrintWriter out;
private static String next() {
while (st == null || !st.hasMoreElements()) {
String s;
try {
s = br.readLine();
} catch (IOException e) {
return null;
}
st = new StringTokenizer(s);
}
return st.nextToken();
}
private static int nextInt() {
return Integer.parseInt(next());
}
private static long nextLong() {
return Long.parseLong(next());
}
public static void main(String[] args) {
run();
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 84acdddd6216485e71a2c580fd5a02ac | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF214B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] aa = new int[10];
int sum = 0;
for (int i = 0; i < n; i++) {
int d = Integer.parseInt(st.nextToken());
aa[d]++;
sum += d;
}
if (aa[0] == 0) {
System.out.println(-1);
return;
}
if (sum % 3 == 1) {
if (aa[1] > 0) {
aa[1]--;
} else if (aa[4] > 0) {
aa[4]--;
} else if (aa[7] > 0) {
aa[7]--;
} else if (aa[2] >= 2) {
aa[2] -= 2;
} else if (aa[2] > 0 && aa[5] > 0) {
aa[2]--;
aa[5]--;
} else if (aa[5] >= 2) {
aa[5] -= 2;
} else if (aa[2] > 0 && aa[8] > 0) {
aa[2]--;
aa[8]--;
} else if (aa[5] > 0 && aa[8] > 0) {
aa[5]--;
aa[8]--;
} else if (aa[8] >= 2) {
aa[8] -= 2;
}
} else if (sum % 3 == 2) {
if (aa[2] > 0) {
aa[2]--;
} else if (aa[5] > 0) {
aa[5]--;
} else if (aa[8] > 0) {
aa[8]--;
} else if (aa[1] >= 2) {
aa[1] -= 2;
} else if (aa[1] > 0 && aa[4] > 0) {
aa[1]--;
aa[4]--;
} else if (aa[4] >= 2) {
aa[4] -= 2;
} else if (aa[1] > 0 && aa[7] > 0) {
aa[1]--;
aa[7]--;
} else if (aa[4] > 0 && aa[7] > 0) {
aa[4]--;
aa[7]--;
} else if (aa[7] >= 2) {
aa[7] -= 2;
}
}
boolean zero = true;
for (int d = 1; d <= 9; d++)
if (aa[d] > 0) {
zero = false;
break;
}
if (zero) {
System.out.println(0);
return;
}
StringBuilder sb = new StringBuilder();
for (int d = 9; d >= 0; d--)
for (int i = 0; i < aa[d]; i++)
sb.append(d);
System.out.println(sb);
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 1280ca4e1574caa71cb8d2fd0c0b4d6b | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import javax.print.attribute.standard.PrintQuality;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
public class Main {
static int mod = (int) 1e9 + 7;
public static void main(String[] args){
FastReader sc = new FastReader();
StringBuilder sb=new StringBuilder();
int n=sc.nextInt();
long sum=0;
Integer[] arr=new Integer[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
sum+=arr[i];
}
Arrays.sort(arr,Collections.reverseOrder());
if(arr[0]==0){
System.out.println(0);
return;
}
if(arr[n-1]!=0){
System.out.println(-1);
return;
}
sum=sum%3;
int[] m=new int[n];
for(int i=0;i<n;i++)m[i]=arr[i]%3;
boolean z=false;
if(sum==0)z=true;
else if(sum==1){
int j=-1,k=-1;
for(int i=n-1;i>=0;i--){
if(m[i]==1){
z=true;
arr[i]=-1;
break;
}
if(m[i]==2){
if(j==-1)j=i;
else k=i;
}
}
if(!z)if(j!=-1 && k!=-1){
arr[j]=-1;arr[k]=-1;z=true;
}
}
else{
int j=-1,k=-1;
for(int i=n-1;i>=0;i--){
if(m[i]==2){
z=true;
arr[i]=-1;
break;
}
if(m[i]==1){
if(j==-1)j=i;
else k=i;
}
}
if(!z)if(j!=-1 && k!=-1){
arr[j]=-1;arr[k]=-1;z=true;
}
}
if(z){
for(int i=0;i<n;i++){
if(arr[i]!=-1)sb.append(arr[i]);
}
if(sb.charAt(0)=='0') System.out.println(0);
else System.out.println(sb);
}
else System.out.println(-1);
}
}
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 | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | a2cbb1ee14b2bc43ee10a3bfa5a79e7a | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.TreeMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
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);
HomeTask solver = new HomeTask();
solver.solve(1, in, out);
out.close();
}
static class HomeTask {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int arr[] = new int[n];
TreeMap<Integer, Integer> map = new TreeMap<>();
int sum = 0;
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
sum += arr[i];
if (!map.containsKey(arr[i]))
map.put(arr[i], 1);
else {
int cnt = map.get(arr[i]);
map.put(arr[i], cnt + 1);
}
}
if (!map.containsKey(0)) {
out.println(-1);
return;
}
if (sum % 3 == 1) {
for (int i = 1; i <= 9 && sum % 3 == 1; i += 3) {
if (map.containsKey(i)) {
sum -= i;
map.put(i, map.get(i) - 1);
}
}
}
if (sum % 3 == 2) {
for (int i = 2; i <= 9 && sum % 3 == 2; i += 3) {
if (map.containsKey(i) && map.get(i) > 0) {
sum -= i;
map.put(i, map.get(i) - 1);
}
}
}
if (sum % 3 == 1) {
for (int i = 2; i <= 9 && sum % 3 != 0; i += 3) {
if (map.containsKey(i) && map.get(i) > 0) {
sum -= i;
map.put(i, map.get(i) - 1);
i -= 3;
}
}
}
if (sum % 3 == 2) {
for (int i = 1; i <= 9 && sum % 3 != 0; i += 3) {
if (map.containsKey(i) && map.get(i) > 0) {
sum -= i;
map.put(i, map.get(i) - 1);
i -= 3;
}
}
}
if (sum == 0) {
map.put(0, Math.min(1, map.get(0)));
}
StringBuilder sb = new StringBuilder();
for (int i = 9; i >= 0; i--) {
if (map.containsKey(i) && map.get(i) > 0) {
for (int j = 0; j < map.get(i); j++) {
sb.append(i);
}
}
}
out.println(sb.toString());
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | a4266d2796fabf1f0f40650a4dc84e01 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes |
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
public class Main{
public static void main(String[]args)throws IOException{
PrintWriter pw=new PrintWriter(System.out,true);
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(bf.readLine());
int[]a=new int[10];
String s=bf.readLine();
String[]sa=s.split(" ");
boolean fivetwo=false;
long sum=0;
for(int i=0;i<n;i++){
int x=Integer.parseInt(sa[i]);
sum+=x;
if(x==0)
fivetwo=true;
a[x]++;
}
if(fivetwo){
boolean flag=sum%3==0;
if(sum%3==1){
for(int i=1;i<10;i+=3){
if(a[i]>0){
a[i]--;
sum-=i;
flag=true;
break;
}
}
int c=0;
for(int i=2;i<10 && !flag;i+=3){
while(a[i]>0){
a[i]--;
sum-=i;
c++;
if(c==2){
flag=true;
break;
}
}
}
}else
if(sum%3==2){
for(int i=2;i<10;i+=3){
if(a[i]>0){
a[i]--;
sum-=i;
flag=true;
break;
}
}
int c=0;
for(int i=1;i<10 && !flag;i+=3){
while(a[i]>0){
a[i]--;
sum-=i;
c++;
if(c==2){
flag=true;
break;
}
}
}
}
if(flag && sum>0){
for(int i=9;i>=0;i--){
for(int j=0;j<a[i];j++){
System.out.print(i);
}
}
System.out.println();
}else
if(sum==0)
System.out.println(0);
else
System.out.println(-1);
}
else
System.out.println(-1);
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | b6962f04f00affbb83e65b4c95d05b7e | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes |
import java.io.*;
import static java.lang.Math.abs;
import java.math.BigInteger;
import java.util.*;
public class Main {
//<editor-fold defaultstate="collapsed" desc="Fast input">
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//</editor-fold>
public static void main(String[] args) throws Exception {
// FastReader read = new FastReader();
PrintWriter print = new PrintWriter(System.out);
input.init(System.in);
int n = input.nextInt();
int [] arr = new int[10];
int sum = 0;
for (int i = 0; i < n; i++) {
int temp = input.nextInt();
arr[temp]++;
sum += temp;
}
if (arr[0] == 0) {
System.out.println("-1");
return;
}
if (sum % 3 == 1) {
if (arr[1] > 0) {
arr[1]--;
sum -= 1;
}
else if (arr[4] > 0) {
arr[4]--;
sum -= 4;
}
else if (arr[7] > 0) {
arr[7]--;
sum -= 7;
}
else if (arr[2] >= 2) {
arr[2] -= 2;
sum -= 4;
}
else if (arr[5] > 0 && arr[2] > 0) {
arr[5]--;
arr[2]--;
sum -= 7;
}
else if (arr[5] >= 2) {
arr[5] -= 2;
sum -= 10;
}
else if (arr[5] > 0 && arr[8] > 0) {
arr[5]--;
arr[8]--;
sum -= 13;
}
else if (arr[8] >= 2) {
arr[8] -= 2;
sum -= 16;
}
}
else if (sum % 3 == 2) {
if (arr[2] > 0) {
arr[2]--;
sum -= 2;
}
else if (arr[1] >= 2) {
arr[1]-= 2;
sum -= 2;
}
else if (arr[5] > 0) {
arr[5]--;
sum -= 5;
}
else if (arr[4] > 0 && arr[1] > 0) {
arr[4]--;
arr[1]--;
sum -= 5;
}
else if (arr[8] > 0 ) {
arr[8]--;
sum -= 8;
}
else if (arr[1] > 0 && arr[7] > 0) {
arr[1]--;
arr[7]--;
sum -= 8;
}
else if (arr[4] >= 2 ) {
arr[4] -= 2;
sum -= 8;
}
else if (arr[7] >= 2) {
arr[7] -= 2;
sum -= 14;
}
}
if (sum == 0) {
System.out.println(0);
return;
}
StringBuilder s = new StringBuilder();
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < arr[i]; j++) {
s.append(i);
}
}
print.println(s);
print.close();
}
}
//<editor-fold defaultstate="collapsed" desc="input">
class input {
static java.io.BufferedReader reader;
static java.util.StringTokenizer tokenizer;
static void init(java.io.InputStream input) {
reader = new java.io.BufferedReader(new java.io.InputStreamReader(input));
tokenizer = new java.util.StringTokenizer("");
}
static String next() throws Exception {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new java.util.StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws Exception {
return Integer.parseInt(next());
}
static long nextLong() throws Exception {
return Long.parseLong(next());
}
}
//</editor-fold>
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | bfc0a3673ee29735a0645d0c92255755 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
InputStream is;
static PrintWriter out;
String INPUT = "";
static long mod = (long)1e9+7L;
public void solve(){
int n = ni();
int[] a = na(n);
int[] map = new int[10];
for(int i = 0; i < n; i++)
map[a[i]]+=1;
if(map[0] == 0){
out.println("-1");
return;
}
//if(n == 100000)
//out.println(Arrays.toString(map));
int[] ans = new int[10];
for(int i = 0; i < map.length; i++){
ans[i] = i%3 == 0 ? map[i] : map[i] - map[i]%3;
map[i] = i%3 == 0 ? 0 : map[i]%3;
}
for(int i = 1; i < map.length; i++){
if(ans[i] > 0){
if(ans[i] >= 3){
ans[i] -= 3;
map[i] += 3;
}
else if(ans[i] > 0){
map[i] = ans[i];
ans[i] = 0;
}
}
}
int[] set = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//if(n == 100000){
//out.println(Arrays.toString(ans));
//out.println(Arrays.toString(map));
//}
int[] p = new int[9];
dfs(map, set, p, 0);
//if(n == 100000)
//out.println(Arrays.toString(maxp));
StringBuilder sb = new StringBuilder("");
for(int i = 0; i < p.length; i++){
ans[set[i]] += maxp[i];
}
//if(n == 100000)
// out.println(Arrays.toString(ans));
for(int i = 9; i >= 0; i--){
for(int j = 0; j < ans[i]; j++){
sb.append(Integer.toString(i));
}
}
//if(n!=100000)
out.println(new String(sb).charAt(0) == '0' ? "0": new String(sb));
//if(n == 100000){
// out.println(new String(sb).length());
//}
}
int[] maxp = new int[9];
int maxs = 0, maxl = 0;
void dfs(int[] map, int[] set, int[] p, int h ){
if(h < p.length){
for(int i = 0; i <= map[set[h]]; i++){
p[h] = i;
dfs(map, set, p, h+1);
}
}
else{
int l = 0, s = 0;
for(int i = 0; i < p.length; i++){
l += p[i];
s = s+ p[i]*set[i];
}
if(l > maxl && s%3 == 0){
//out.println(Arrays.toString(p)+" "+s);
maxl = l; maxp = Arrays.copyOf(p, p.length);
}
else if(s%3 == 0 && l == maxl){
boolean large = true;
for(int i = p.length-1; i >= 0; i--){
if(maxp[i] > p[i]){
large = false;
}
}
if(large){
//out.println(Arrays.toString(p));
maxp = Arrays.copyOf(p, p.length);
}
}
}
}
void run(){
is = new DataInputStream(System.in);
out = new PrintWriter(System.out);
int t=1;while(t-->0)solve();
out.flush();
}
public static void main(String[] args)throws Exception{new Main().run();}
long mod(long v, long m){if(v<0){long q=(Math.abs(v)+m-1L)/m;v=v+q*m;}return v%m;}
long mod(long v){if(v<0){long q=(Math.abs(v)+mod-1L)/mod;v=v+q*mod;}return v%mod;}
//Fast I/O code is copied from uwi code.
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte(){
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns(){
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n){
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m){
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n){
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni(){
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl(){
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
static int i(long x){return (int)Math.round(x);}
static class Pair implements Comparable<Pair>{
long fs,sc;
Pair(long a,long b){
fs=a;sc=b;
}
public int compareTo(Pair p){
if(this.fs>p.fs)return 1;
else if(this.fs<p.fs)return -1;
else{
return i(this.sc-p.sc);
}
//return i(this.sc-p.sc);
}
public String toString(){
return "("+fs+","+sc+")";
}
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 832d4e95ef380b2263d4f46997d39608 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.*;
import java.util.*;
public class B214
{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] aa = new int[10];
int sum = 0;
for (int i = 0; i < n; i++) {
int d = Integer.parseInt(st.nextToken());
aa[d]++;
sum += d;
}
if (aa[0] == 0) {
System.out.println(-1);
return;
}
if (sum % 3 == 1) {
if (aa[1] > 0) {
aa[1]--;
} else if (aa[4] > 0) {
aa[4]--;
} else if (aa[7] > 0) {
aa[7]--;
} else if (aa[2] >= 2) {
aa[2] -= 2;
} else if (aa[2] > 0 && aa[5] > 0) {
aa[2]--;
aa[5]--;
} else if (aa[5] >= 2) {
aa[5] -= 2;
} else if (aa[2] > 0 && aa[8] > 0) {
aa[2]--;
aa[8]--;
} else if (aa[5] > 0 && aa[8] > 0) {
aa[5]--;
aa[8]--;
} else if (aa[8] >= 2) {
aa[8] -= 2;
}
} else if (sum % 3 == 2) {
if (aa[2] > 0) {
aa[2]--;
} else if (aa[5] > 0) {
aa[5]--;
} else if (aa[8] > 0) {
aa[8]--;
} else if (aa[1] >= 2) {
aa[1] -= 2;
} else if (aa[1] > 0 && aa[4] > 0) {
aa[1]--;
aa[4]--;
} else if (aa[4] >= 2) {
aa[4] -= 2;
} else if (aa[1] > 0 && aa[7] > 0) {
aa[1]--;
aa[7]--;
} else if (aa[4] > 0 && aa[7] > 0) {
aa[4]--;
aa[7]--;
} else if (aa[7] >= 2) {
aa[7] -= 2;
}
}
boolean zero = true;
for (int d = 1; d <= 9; d++)
if (aa[d] > 0) {
zero = false;
break;
}
if (zero) {
System.out.println(0);
return;
}
StringBuilder sb = new StringBuilder();
for (int d = 9; d >= 0; d--)
for (int i = 0; i < aa[d]; i++)
sb.append(d);
System.out.println(sb);
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | c4d3ff9c6ba1cb935ebcef878d07f881 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.AbstractQueue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.AbstractCollection;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
B214 solver = new B214();
solver.solve(1, in, out);
out.close();
}
static class B214 {
boolean modPresent(ArrayList<Integer>[] mods, int mod) {
if (mod == 0) return true;
if (mod == 1) {
if (mods[1].isEmpty()) {
if (mods[2].size() < 2) return false;
PriorityQueue<Integer> mins = new PriorityQueue<>(mods[2]);
Integer min1 = mins.poll();
Integer min2 = mins.poll();
mods[2].remove(min1);
mods[2].remove(min2);
} else {
PriorityQueue<Integer> mins = new PriorityQueue<>(mods[1]);
Integer min = mins.poll();
mods[1].remove(min);
}
} else {
if (mods[2].isEmpty()) {
if (mods[1].size() < 2) return false;
PriorityQueue<Integer> mins = new PriorityQueue<>(mods[1]);
Integer min1 = mins.poll();
Integer min2 = mins.poll();
mods[1].remove(min1);
mods[1].remove(min2);
} else {
PriorityQueue<Integer> mins = new PriorityQueue<>(mods[2]);
Integer min = mins.poll();
mods[2].remove(min);
}
}
return true;
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n];
boolean containsZero = false;
int sum = 0;
ArrayList<Integer>[] mods = new ArrayList[3];
for (int i = 0; i < 3; ++i) mods[i] = new ArrayList<>();
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
containsZero |= a[i] == 0;
mods[a[i] % 3].add(a[i]);
sum = (sum + a[i]) % 3;
}
if (!containsZero) {
out.println(-1);
return;
}
if (!modPresent(mods, sum)) {
out.println(-1);
return;
}
PriorityQueue<Integer> nums = new PriorityQueue<>((x, y) -> y - x);
for (int i = 0; i < 3; ++i) {
nums.addAll(mods[i]);
}
StringBuilder ans = new StringBuilder();
while (!nums.isEmpty()) {
ans.append(nums.poll());
}
while (ans.length() > 1 && ans.charAt(0) == '0') {
ans.deleteCharAt(0);
}
out.println(ans);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | b121c5363f5520ef9eca1f456aad0d61 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//Scanner s = new Scanner(System.in);
int n = Integer.parseInt(reader.readLine());
int sum=0,freq[] = new int[10];
String line = reader.readLine();
for (int i=0;i < n;i++)
freq[line.charAt(i * 2) - '0']++;
if (freq[0] == 0)
{
System.out.println(-1);
return;
}
for (int i = 1;i < 10;i++)
sum += freq[i] * i;
int mod = sum % 3;
if (mod == 1)
{
if (freq[1] != 0)
{
freq[1]--;
sum -= 1;
}
else
if (freq[4] != 0)
{
freq[4]--;
sum -= 4;
}
else
if (freq[7] != 0)
{
freq[7]--;
sum -= 7;
}
else
if (freq[2] > 1)
{
freq[2] -= 2;
sum -= 4;
}
else
if (freq[5] != 0 && freq[2] != 0)
{
freq[5]--;
freq[2]--;
sum -= 7;
}
else
if(freq[5]>1)
{
freq[5] -=2;
sum -=10;
}
else
if(freq[5]!=0 && freq[8]!=0)
{
sum -= 13;
freq[5]--;
freq[8]--;
}
else
if(freq[8]>1)
{
sum -= 16;
freq[8] -=2;
}
else
{
System.out.println(-1);
return;
}
}
if (mod == 2)
{
if (freq[2] != 0)
{
freq[2]--;
sum -= 2;
}
else
if (freq[5] != 0)
{
freq[5]--;
sum -= 5;
}
else
if (freq[8] != 0)
{
freq[8]--;
sum -= 8;
}
else
if (freq[1] > 1)
{
freq[1] -= 2;
sum -= 2;
}
else
if (freq[1] != 0 && freq[4] != 0)
{
freq[1]--;
freq[4]--;
sum -= 5;
}
else
if (freq[1] != 0 && freq[7] != 0)
{
freq[1]--;
freq[7]--;
sum -= 8;
}
else
if (freq[4] > 1)
{
freq[4] -= 2;
sum -= 8;
}
else
if(freq[7]>1)
{
sum -= 14;
freq[7] -=2;
}
else
{
System.out.println(-1);
return;
}
}
if (sum == 0)
{
System.out.println(0);
return;
}
for (int i=9;i > -1;i--)
for (int j=freq[i];j > 0;j--)
System.out.print(i);
}
} | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | db3fb364d999ff6c50b9c1f9a721ad39 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.*;
public class code
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static class FastWriter
{
OutputStream os;
public FastWriter()
{
os = new BufferedOutputStream(System.out);
}
void println(String content)throws IOException
{
print(content);
print("\n");
}
void print(String content)throws IOException
{
os.write(content.getBytes());
}
void flush()throws IOException
{
os.flush();
}
}
static long P = 1000000007L;
public static void main(String args[])throws IOException
{
FastReader scn = new FastReader();
int[] count = new int[10];
int[] modulo = new int[3];
int n = scn.nextInt();
int mod = 0;
for(int i = 0; i < n; i++)
{
int input = scn.nextInt();
mod = (mod + input)%3;
count[input]++;
modulo[input%3]++;
}
if(count[0] == 0)
{
System.out.println(-1);
return;
}
if(mod == 1)
{
if(modulo[1] >= 1)
{
for(int i = 1; i <= 9; i = i + 3)
if(count[i] >= 1)
{
count[i]--;
break;
}
}
else
{
int counter = 2;
for(int i = 2; i <= 9; i = i + 3)
{
if(counter == 0)
break;
if(count[i] >= 1)
{
count[i]--;
counter = counter - 1;
i = i - 3;
}
}
}
}
if(mod == 2)
{
if(modulo[2] >= 1)
{
for(int i = 2; i <= 9; i = i + 3)
if(count[i] >= 1)
{
count[i]--;
break;
}
}
else
{
int counter = 2;
for(int i = 1; i <= 9; i = i + 3)
{
if(counter == 0)
break;
if(count[i] >= 1)
{
count[i]--;
counter = counter - 1;
i = i - 3;
}
}
}
}
StringBuilder result = new StringBuilder();
for(int i = 9; i >= 0; i--)
{
if(i == 0 && result.length() == 0)
{
result.append(0);
break;
}
while(count[i] -- > 0)
result.append(i);
}
System.out.println(result.toString());
}
public static int log4(int base)
{
int exponent = 0;
while(base % 4 == 0)
{
exponent++;
base = base/4;
}
return exponent;
}
// public static long dfs(Node current, int l, int r, Node[] nodes)
// {
// current.visited = true;
// long ans = 1;
// for(Node next: current.neighbors)
// {
// if(!next.visited && next.sorted_index <= r && next.sorted_index >= l)
// {
// long p = dfs(next, l, r, nodes);
// ans = (ans * ( p + 1))%P;
// }
// }
// current.visited = false;
// return ans;
// }
}
class Pair
{
int a, b;
public Pair(int a, int b)
{
this.a = a;
this.b = b;
}
}
// class Node implements Comparable<Node>
// {
// int weight;
// int sorted_index;
// ArrayList<Node> neighbors;
// boolean visited;
// public Node()
// {
// neighbors = new ArrayList<>();
// }
// public int compareTo(Node n)
// {
// return this.weight - n.weight;
// }
// }
// class MutableInt
// {
// int count;
// ArrayList<Integer> list;
// public MutableInt()
// {
// list = new ArrayList<>();
// }
// }
// class TrieNode
// {
// HashMap<Character, TrieNode> map;
// boolean visited;
// int cost;
// public TrieNode()
// {
// map = new HashMap<>();
// }
// public TrieNode add(char c, boolean isGood, MutableInt result, int k)
// {
// if(map.containsKey(c))
// {
// TrieNode n = map.get(c);
// return n;
// }
// else
// {
// TrieNode n = new TrieNode();
// if(!isGood)
// n.cost = this.cost + 1;
// else
// n.cost = this.cost;
// if(n.cost <= k)
// result.count = result.count + 1;
// map.put(c, n);
// return n;
// }
// }
// } | Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | fc733dd87c6d9c7baa5ff4cac52838cd | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] ar = new int[10];
int rest = 0;
for (int i = 0; i < n; i++) {
int k = in.nextInt();
ar[k]++;
rest += k;
rest %= 3;
}
if (ar[0] == 0) {
out.println(-1);
return;
}
boolean flag = false;
if (rest == 0) {
flag = true;
} else {
for (int j = 1; j <= 9; j++) {
if (j % 3 == rest && ar[j] > 0) {
ar[j]--;
flag = true;
break;
}
}
if (!flag) {
outer:
for (int i = 1; i <= 9; i++) {
for (int j = i; j <= 9; j++) {
if ((i + j) % 3 == rest) {
if (i == j && ar[i] > 1) {
flag = true;
ar[i] -= 2;
break outer;
} else if (i != j && ar[i] > 0 && ar[j] > 0) {
flag = true;
ar[i]--;
ar[j]--;
break outer;
}
}
}
}
}
}
boolean containsNonZero = false;
for (int i = 1; i < ar.length; i++) {
containsNonZero |= ar[i] > 0;
}
if (flag && containsNonZero) {
for (int i = 9; i >= 0; i--) {
if (ar[i] > 0) {
for (int j = 0; j < ar[i]; j++) {
out.print(i);
}
}
}
} else {
out.print(0);
}
out.println();
}
}
static class InputReader {
StringTokenizer st;
BufferedReader br;
public InputReader(InputStream is) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
this.br = br;
}
public String next() {
if (st == null || !st.hasMoreTokens()) {
String nextLine = null;
try {
nextLine = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (nextLine == null)
return null;
st = new StringTokenizer(nextLine);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 3b4299bf32b8987d2f67b1c035465a90 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Hometask {
public static int[] arr = new int[10];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i,j,k,n,sum,x,l,m;
boolean is,is1;
// int[] arr;
while (input.hasNext()){
n = input.nextInt(); sum = 0;
for (i=0; i<n; i++){
x = input.nextInt();
if (x == 0) arr[0]++; else if (x == 1) arr[1]++; else if (x == 2) arr[2]++; else if (x == 3) arr[3]++;
else if (x == 4) arr[4]++; else if (x == 5) arr[5]++; else if (x == 6) arr[6]++; else if (x == 7) arr[7]++;
else if (x == 8) arr[8]++; else if (x == 9) arr[9]++;
sum += x;
}
if (arr[0] >= 1 && sum == 0 ) {
System.out.println(0); return;
}
//Arrays.sort(arr); //System.out.println(arr);
// System.out.println(arr[0]);
if (arr[0] >= 1){
if (sum % 3 == 0 && sum != 0) {
print();
}
else {
l = sum % 3 ; is= false; //System.out.println(sum + " " + arr[4]);
if (l == 1 || l == 2)
{
for (i=1; i<=2; i++){
if (arr[1]>=i){
if((sum - 1 * i ) % 3 == 0) {
arr[1] -= i; print(); is = true;
return;
}
}
if (arr[2]>=i){
if ((sum - 2 * i ) % 3 == 0) {
arr[2] -= i; print(); is = true;
return;
}
}
/* if (arr[3]>=1){
sum -= 3 * i;
if (sum % 3 == 0) {
arr[3] -= i; print(); is = true;
break;
}
}*/
if (arr[4]>=i){
//System.out.println(sum);
if ((sum - 4 * i ) % 3 == 0) {
arr[4] -= i; print(); is = true;
return;
}
}
if (arr[5]>=i){
if ((sum - 5 * i ) % 3 == 0) {
arr[5] -= i; print(); is = true;
return;
}
}
/* if (arr[6]>=1){
if ((sum - 6 * i ) % 3 == 0) {
arr[6] -= i; print(); is = true;
break;
}
}*/
if (arr[7]>=i){
if ((sum - 7 * i ) % 3 == 0) {
arr[7] -= i; print(); is = true;
return;
}
}
if (arr[8]>=i){
if ((sum - 8 * i ) % 3 == 0) {
arr[8] -= i; print(); is = true;
return;
}
}
/*if (arr[9]>=1){
sum -= 9 * i;
if (sum % 3 == 0) {
arr[9] -= i; print(); is = true;
break;
}
}*/
}
if (arr[9] >0 )
for (i=0; i<arr[9]; i++) System.out.print(9);
if (arr[6] > 0)
for (i=0; i<arr[6]; i++) System.out.print(6);
if (arr[3] > 0) {
for (i = 0; i < arr[3]; i++) System.out.print(3);
}
if ((arr[9] >0 || arr[6] > 0 || arr[3] > 0) && arr[0] > 0) for (i=0; i<arr[0]; i++) System.out.print(0);
else if (!is)
System.out.print(0);
System.out.println();
}
}
}
else System.out.println(-1);
/* for (i=0; i<n; i++) {
for (j = 0; j < n; j++) {
if (arr[i] > arr[j]) {
x = arr[i];
arr[i] = arr[j];
arr[j] = x;
}
}
}*/
/* if (arr[n-1] == 0) is = true;
*//*for (i=0; i<n; i++)
System.out.print(arr[i]);*//*
if (is){
if (sum % 3 == 0) {
for (int r : arr)
System.out.print(r); return;
}
if (one >= 2) {
for (i=1; i<=one; i++){
if ((sum-i) % 3 == 0){
for (int r : arr) {
if (r == 1 && i != 0){
i--;
continue;
}
System.out.print(r);
}
return;
}
}
}
for (i=1; i<=n; i++){ //System.out.print((sum-arr[i])+" " );System.out.println();
for (j=0; j<=n-i; j++) { l = 0; if (arr[j] == 0 || arr[j] == arr[j+1]) continue;
for (k=j,m=0; m<i; k++,m++) {
l += arr[k];
}
if ((sum - l) % 3 == 0) { is = true; //System.out.println("lkjhg" + i + " " + j);
for (m=0; m<n; m++){
if (j<=m && m<i+j) continue;
System.out.print(arr[m]);
}
return;
}
}
}
if (!is1) System.out.println(0);
}
else
System.out.println(-1);*/
}
}
public static void print(){
int i,j; //System.out.println(arr[0] + " dfg");
for (i=9; i>0; i--)
{
for (j=0; arr[i]>j; j++) System.out.print(i);
}
if (arr[0] > 0 && (arr[1] > 0 || arr[3] > 0 || arr[2] > 0 || arr[4] > 0 || arr[5] > 0 || arr[7] > 0 || arr[8] > 0 || arr[6] > 0 || arr[9] > 0))
{
for (i=0; i<arr[0]; i++) System.out.print(0);
}
else if (arr[0] > 0) System.out.print(0);
System.out.println();
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | f5112ae3040c5dc2754abf9a1d13c0b7 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] digitsCount = new int[10];
int sumDigits = 0;
boolean hasZero = false;
for (int i = 0; i < n; i++) {
int cur = in.nextInt();
digitsCount[cur]++;
if (cur == 0) hasZero = true;
else sumDigits += cur;
}
if (!hasZero) {
out.println(-1);
return;
}
if (sumDigits % 3 == 1) {
if (digitsCount[1] > 0) {
digitsCount[1]--;
sumDigits -= 1;
} else if (digitsCount[4] > 0) {
digitsCount[4]--;
sumDigits -= 4;
} else if (digitsCount[7] > 0) {
digitsCount[7]--;
sumDigits -= 7;
} else if (digitsCount[2] >= 2) {
digitsCount[2] -= 2;
sumDigits -= 4;
} else if (digitsCount[2] > 0 && digitsCount[5] > 0) {
digitsCount[2]--;
digitsCount[5]--;
sumDigits -= 2 + 5;
} else if (digitsCount[5] >= 2) {
digitsCount[5] -= 2;
sumDigits -= 5 + 5;
} else if (digitsCount[2] > 0 && digitsCount[8] > 0) {
digitsCount[2]--;
digitsCount[8]--;
sumDigits -= 2 + 8;
} else if (digitsCount[5] > 0 && digitsCount[8] > 0) {
digitsCount[5]--;
digitsCount[8]--;
sumDigits -= 8 + 5;
} else if (digitsCount[8] >= 2) {
digitsCount[8] += 2;
sumDigits -= 8 + 8;
} else {
out.println(-1);
return;
}
}
if (sumDigits % 3 == 2) {
if (digitsCount[2] > 0) {
digitsCount[2]--;
sumDigits -= 2;
} else if (digitsCount[5] > 0) {
digitsCount[5]--;
sumDigits -= 5;
} else if (digitsCount[8] > 0) {
digitsCount[8]--;
sumDigits -= 8;
} else if (digitsCount[1] >= 2) {
digitsCount[1] -= 2;
sumDigits -= 1 + 1;
} else if (digitsCount[4] > 0 && digitsCount[1] > 0) {
digitsCount[4]--;
digitsCount[1]--;
sumDigits -= 4 + 1;
} else if (digitsCount[4] >= 2) {
digitsCount[4] -= 2;
sumDigits -= 4 + 4;
} else if (digitsCount[7] > 0 && digitsCount[1] > 0) {
digitsCount[7]--;
digitsCount[1]--;
sumDigits -= 7 + 1;
} else if (digitsCount[7] > 0 && digitsCount[4] > 0) {
digitsCount[7]--;
digitsCount[4]--;
sumDigits -= 7 + 4;
} else if (digitsCount[7] >= 2) {
digitsCount[7] -= 2;
sumDigits -= 7 + 7;
} else {
out.println(-1);
return;
}
}
int nonZero = 0;
for (int i = 9; i >= 0; i--) {
if (i == 0 && nonZero == 0) {
out.print(0);
break;
}
while (digitsCount[i] > 0) {
digitsCount[i]--;
nonZero++;
out.print(i);
}
}
}
}
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 next() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | fe1fff6edde1e9858fba399a2670c065 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class HomeTask {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N;
static ArrayList<Integer> numbers = new ArrayList();
static ArrayList<Integer> mod1 = new ArrayList();
static ArrayList<Integer> mod2 = new ArrayList();
public static void go(){
StringBuilder sb = new StringBuilder();
for(int x:numbers) sb.append(x);
sb.reverse();
int sz = sb.length();
boolean all = true;
for(int i=0;i<sz;i++){
if(sb.charAt(i)!='0'){
all = false;
}
}
if(all) System.out.println("0");
else System.out.println(sb);
System.exit(0);
}
public static void main(String[]args)throws Throwable{
N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
long sum = 0;
for(int i=0;i<N;i++){
numbers.add(Integer.parseInt(st.nextToken()));
sum += numbers.get(i);
}
boolean allzeros = true, zero = false;
for(int x:numbers){
if(x==0) zero = true;
else allzeros = false;
}
if(!zero){
System.out.println("-1");
}
else if(allzeros) System.out.println("0");
if(!zero||allzeros) return;
Collections.sort(numbers);
for(int x:numbers){
if(x%3==1) mod1.add(x);
else if(x%3==2) mod2.add(x);
}
Collections.sort(mod1); Collections.sort(mod2);
if(sum%3==0){
go();
}
else if(sum%3==1){
if(mod1.isEmpty()&&mod2.size()<2){
System.out.println("0");
return;
}
if(!mod1.isEmpty())numbers.remove(mod1.get(0));
else{
numbers.remove(mod2.get(0));
numbers.remove(mod2.get(1));
}
go();
}
else if(sum%3==2){
if(mod2.isEmpty()&&mod1.size()<2){
System.out.println("0");
return;
}
if(!mod2.isEmpty()){
numbers.remove(mod2.get(0));
go();
}
else {
numbers.remove(mod1.get(0));
numbers.remove(mod1.get(1));
go();
}
}
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 98384ee6d4ec7ab26f80e039a8e6dc83 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class HomeTask {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N;
static ArrayList<Integer> numbers = new ArrayList();
static ArrayList<Integer> mod1 = new ArrayList();
static ArrayList<Integer> mod2 = new ArrayList();
public static void go(){
StringBuilder sb = new StringBuilder();
for(int x:numbers) sb.append(x);
sb.reverse();
int sz = sb.length();
boolean all = true;
for(int i=0;i<sz;i++){
if(sb.charAt(i)!='0'){
all = false;
}
}
if(all) System.out.println("0");
else System.out.println(sb);
System.exit(0);
}
public static void main(String[]args)throws Throwable{
N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
long sum = 0;
for(int i=0;i<N;i++){
numbers.add(Integer.parseInt(st.nextToken()));
sum += numbers.get(i);
}
boolean allzeros = true, zero = false;
for(int x:numbers){
if(x==0) zero = true;
else allzeros = false;
}
if(!zero){
System.out.println("-1");
}
else if(allzeros) System.out.println("0");
if(!zero||allzeros) return;
Collections.sort(numbers);
for(int x:numbers){
if(x%3==1) mod1.add(x);
else if(x%3==2) mod2.add(x);
}
Collections.sort(mod1); Collections.sort(mod2);
if(sum%3==0){
go();
}
else if(sum%3==1){
if(mod1.isEmpty()&&mod2.size()<2){
System.out.println("0");
return;
}
if(!mod1.isEmpty())numbers.remove(mod1.get(0));
else{
numbers.remove(mod2.get(0));
numbers.remove(mod2.get(1));
}
go();
}
else if(sum%3==2){
if(mod2.isEmpty()&&mod1.size()<2){
System.out.println("0");
return;
}
if(!mod2.isEmpty()){
numbers.remove(mod2.get(0));
go();
}
else {
numbers.remove(mod1.get(0));
numbers.remove(mod1.get(1));
go();
}
}
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | babd898bc2ddae7ea556d474ac445f49 | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
init(System.in);
int n = nextInt();
ArrayList<Integer> mod1 = new ArrayList<>();
ArrayList<Integer> mod2 = new ArrayList<>();
ArrayList<Integer> nums = new ArrayList<>();
int sum = 0;
for (int i = 0; i < n; i++) {
int num = nextInt();
sum += num;
if (num % 3 == 0) nums.add(num);
else if (num % 3 == 1) mod1.add(num);
else mod2.add(num);
}
mod1.sort(Collections.reverseOrder());
mod2.sort(Collections.reverseOrder());
if (sum % 3 == 1) {
if (mod1.size() > 0) mod1.remove(mod1.size() - 1);
else {
mod2.remove(mod2.size() - 1);mod2.remove(mod2.size() - 1);
}
}
else if (sum % 3 == 2){
if (mod2.size() > 0) mod2.remove(mod2.size() - 1);
else {
mod1.remove(mod1.size() - 1);mod1.remove(mod1.size() - 1);
}
}
nums.addAll(mod1); nums.addAll(mod2);
nums.sort(Comparator.reverseOrder());
if (nums.isEmpty() || nums.get(nums.size() - 1) != 0) System.out.println(-1);
else {
if (nums.get(0) == 0) System.out.println(0);
else System.out.println(nums.toString().replace(", ", "").replace("[", "").replace("]", ""));
}
}
//Input Reader
private static BufferedReader reader;
private static StringTokenizer tokenizer;
private static void init(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
tokenizer = new StringTokenizer("");
}
private static String next() throws IOException {
String read;
while (!tokenizer.hasMoreTokens()) {
read = reader.readLine();
if (read == null || read.equals(""))
return "-1";
tokenizer = new StringTokenizer(read);
}
return tokenizer.nextToken();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
// private static long nextLong() throws IOException {
// return Long.parseLong(next());
// }
//
// // Get a whole line.
// private static String line() throws IOException {
// return reader.readLine();
// }
//
// private static double nextDouble() throws IOException {
// return Double.parseDouble(next());
// }
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 3e98fdc933210063f6544e6320bd298a | train_004.jsonl | 1343662200 | Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class HomeTask {
public static void main(String asd[])throws Exception
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int s[]=new int[10];
int a[]=new int[n];
int c=0;int m=0;
for(int i=0;i<n;i++)
{
m+=a[i]=in.nextInt();
s[a[i]]++;
}
if(s[0]==0)
{
System.out.println(-1);return;
}
else if(s[0]==n)
{
System.out.println(0);return;
}
else{
if(m%3==0)
{
for(int i=9;i>=0;i--)
{
for(int j=1;j<=s[i];j++)
System.out.print(i);
}
return;
}
else if(m%3==1)
{
if(s[1]!=0)
{
s[1]-=1;n-=1;
}
else if(s[4]!=0)
{
s[4]-=1;n-=1;
}
else if(s[7]!=0)
{
s[7]-=1;n-=1;
}
else if(s[2]>=2)
{
s[2]-=2;n-=2;
}
else if(s[2]!=0 && s[5]!=0)
{
s[2]-=1;s[5]-=1;n-=2;
}
else if(s[5]>=2)
{
s[5]-=2;n-=2;
}
else if(s[2]!=0 && s[8]!=0)
{
s[2]-=1;s[8]-=1;n-=2;
}
else if(s[8]!=0 && s[5]!=0)
{
s[8]-=1;s[5]-=1;n-=2;
}
else if(s[8]>=2)
{
s[8]-=2;n-=2;
}
}
else if(m%3==2)
{
if(s[2]!=0)
{
s[2]-=1;n-=1;
}
else if(s[5]!=0)
{
s[5]-=1;n-=1;
}
else if(s[8]!=0)
{
s[8]-=1;n-=1;
}
else if(s[1]>=2)
{
s[1]-=2;n-=2;
}
else if(s[4]!=0 && s[1]!=0)
{
s[4]-=1;s[1]-=1;n-=2;
}
else if(s[4]>=2)
{
s[4]-=2;n-=2;
}
else if(s[7]!=0 && s[1]!=0)
{
s[7]-=1;s[1]-=1;n-=2;
}
else if(s[7]!=0 && s[4]!=0)
{
s[7]-=1;s[4]-=1;n-=2;
}
else if(s[7]>=2)
{
s[7]-=2;n-=2;
}
}
}
if(s[0]==n)
{
System.out.println(0);return;
}
for(int i=9;i>=0;i--)
for(int j=1;j<=s[i];j++)
System.out.print(i);
}
}
| Java | ["1\n0", "11\n3 4 5 4 5 3 5 3 4 4 0", "8\n3 2 5 1 5 2 2 3"] | 2 seconds | ["0", "5554443330", "-1"] | NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math",
"brute force"
] | b263917e47e1c84340bcb1c77999fd7e | A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space. | 1,600 | On a single line print the answer to the problem. If such number does not exist, then you should print -1. | standard output | |
PASSED | 2c39696e2baec826adf8262cc11bc805 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String args[]) throws IOException{
InputReader scan = new InputReader();
int n = scan.nextInt(), m = scan.nextInt();
int[] p = new int[n];
for(int i = 0; i < n; i++) p[i] = scan.nextInt();
for(int i = 0; i < m; i++){
int l = scan.nextInt()-1, r = scan.nextInt()-1, x = scan.nextInt()-1;
if(x < l || x > r){
System.out.println("Yes");
continue;
}
int counter = 0;
for(int j = l; j <= r; j++)
if(p[j] < p[x]) counter++;
if(counter + l == x) System.out.println("Yes");
else System.out.println("No");
}
}
//Custom Scanner
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | be20a0b4723fa446b8e4586430380009 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] P = new int[n];
for(int i = 0; i < n; i++){
P[i] = sc.nextInt();
}
for(int i = 0; i < m; i++){
int l = sc.nextInt();
int r = sc.nextInt();
int x = sc.nextInt();
if(l > x || r < x){
System.out.println("Yes");
}
else{
int cntLess = 0;
for(int j = l-1; j < r; j++){
if(P[j] < P[x-1]) cntLess++;
}
if(P[cntLess+l-1] == P[x-1]) System.out.println("Yes");
else System.out.println("No");
}
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(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 readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextLong();
}
return a;
}
}
} | Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 8bd2d7b85f7bae2cee40e77139df51a3 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args){
FastScanner sc = new FastScanner();
int n = sc.nextInt();
int m = sc.nextInt();
int[] P = new int[n];
for(int i = 0; i < n; i++){
P[i] = sc.nextInt();
}
for(int i = 0; i < m; i++){
int l = sc.nextInt();
int r = sc.nextInt();
int x = sc.nextInt();
if(l > x || r < x){
System.out.println("Yes");
}
else{
int cntLess = 0;
for(int j = l-1; j < r; j++){
if(P[j] < P[x-1]) cntLess++;
}
if(P[cntLess+l-1] == P[x-1]) System.out.println("Yes");
else System.out.println("No");
}
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(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 readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextLong();
}
return a;
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 7d8779b111709839d5d50506537d6c22 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Two {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.call(in,out);
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n , m , l ,r ,k , b;
n = in.nextInt();
m = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i <n ; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i <m ; i++) {
b = 0;
l = in.nextInt() -1;
r = in.nextInt() -1;
k = in.nextInt() -1;
for (int j = l; j <=r ; j++) {
if(arr[j]<arr[k]){
b++;
}
}
if(l<=k && r>=k){
if(arr[l+b] == arr[k]){
out.println("Yes");
}
else
out.println("No");
}
else {
out.println("Yes");
}
}
}
}
static final Random random=new Random();
static void shuffleSort(int[] arr) {
int n=arr.length;
for (int i=0; i<n; i++) {
int a=random.nextInt(n), temp=arr[a];
arr[a]=arr[i];
arr[i]=temp;
}
Arrays.sort(arr);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
}
} | Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 4e807eb4a850b40032dc6c9c5693c5a8 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.util.*;
public class B416 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int p[] = new int[n];
for(int i = 0; i < n; ++i) {
p[i] = sc.nextInt();
}
int l, r, x;
for(int i = 0; i < m; ++i) {
l = sc.nextInt() - 1;
r = sc.nextInt() - 1;
x = sc.nextInt() - 1;
int less = 0;
for(int j = l; j <= r; ++j) {
if(p[j] < p[x]) {
less++;
}
}
if(l + less != x) {
System.out.println("No");
} else {
System.out.println("Yes");
}
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | b9688f42992fe941772a6cb66b6fbca3 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.util.*;
public class B416_ {
static int n;
static int a[];
static int N;
static ArrayList<ArrayList<Integer>> tree;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = new int[n];
N = 25 * n;
int m = sc.nextInt();
tree = new ArrayList<>(N);
for(int i = 0; i < n; ++i) {
a[i] = sc.nextInt();
}
for(int i = 0; i < N; ++i) {
tree.add(new ArrayList<>());
}
buildMergeTree(0, 0, n-1);
// for(int i = 0; i < N; ++i) {
// if(tree.get(i).size() > 0) {
// System.out.println(i + " " + tree.get(i));
// }
// }
for(int i = 0; i < m; ++i) {
int l = sc.nextInt() - 1;
int r = sc.nextInt() - 1;
int x = sc.nextInt() - 1;
if(l + cntLess(0, 0, n-1, l, r, a[x]) != x) {
System.out.println("No");
} else {
System.out.println("Yes");
}
}
}
static void buildMergeTree(int cur, int l, int r) {
if(l == r) {
tree.get(cur).add(a[l]);
return;
}
int mid = (l + r) / 2;
buildMergeTree(cur * 2 + 1, l, mid);
buildMergeTree(cur * 2 + 2, mid + 1, r);
tree.set(cur, merge(tree.get(cur * 2 + 1), tree.get(cur * 2 + 2)));
}
static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) {
int asize = a.size();
int bsize = b.size();
ArrayList<Integer> ans = new ArrayList<>(asize + bsize);
int p1 = 0;
int p2 = 0;
while(p1 < asize && p2 < bsize) {
if(a.get(p1) < b.get(p2)) {
ans.add(a.get(p1));
p1++;
} else {
ans.add(b.get(p2));
p2++;
}
}
while(p1 < asize) {
ans.add(a.get(p1));
p1++;
}
while(p2 < bsize) {
ans.add(b.get(p2));
p2++;
}
return ans;
}
static int cntLess(int cur, int l, int r, int x, int y, int k) {
if(x > r || y < l) {
return 0;
}
if(x <= l && y >= r) {
return cntLess(tree.get(cur), k);
}
int mid = (l + r) / 2;
return cntLess(cur * 2 + 1, l, mid, x, y, k) + cntLess(cur * 2 + 2, mid + 1, r, x, y, k);
}
static int cntLess(ArrayList<Integer> a, int k) {
//find the last element smaller than k
int l = 0;
int r = a.size() - 1;
while(l < r) {
int mid = (l + r + 1) / 2;
if(a.get(mid) < k) {
l = mid;
} else {
r = mid - 1;
}
}
if(a.get(l) >= k) {
return 0;
} else {
return l + 1;
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 9efe73f20131c5abd5d44d8c34b7efae | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class CF811B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] original = new int[n];
for (int i = 0; i < n; i++) {
original[i] = sc.nextInt();
}
PrintWriter pw = new PrintWriter(System.out);
for (int i = 0; i < m; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
int k = sc.nextInt();
int page = original[k - 1];
int c = 0;
for (int j = l - 1; j < r; j++) {
if (original[j] < page)
c++;
}
if(c == k-l)
pw.println("Yes");
else
pw.println("No");
}
pw.flush();
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 2831abbab2fe6b09d08491f1423ac351 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
static final Scanner s = new Scanner(System.in);
static IntStream REPS(int v){
return IntStream.range(0,v);
}
public static void main(String[]__){
int n=s.nextInt(),m=s.nextInt();
int[]ar=REPS(n).map(i->Integer.parseInt(s.next())).toArray();
for(int i=0;i<m;i++) {
int
l=Integer.parseInt(s.next())-1,
r=Integer.parseInt(s.next())-1,
x=Integer.parseInt(s.next())-1,
bef=ar[x],
more=0,less=0;
for(int j=l;j<=r;j++) {
if(bef<ar[j]) more++;
if(bef>ar[j]) less++;
}
//System.out.println(less+" "+more);
System.out.println(l+less<=x&&x<=r-more?"Yes":"No");
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 5e49e98900c831cee3b6b3f5a7500270 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author vikas.k
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
for (int i = 1; i <= m; i++) {
int x = in.nextInt();
int y = in.nextInt();
int v = in.nextInt();
int count = 0;
for (int j = x; j <= y; j++) {
if (a[j] < a[v]) count++;
}
//out.print(count);
out.println(x + count == v ? "Yes" : "No");
}
}
}
static class FastScanner {
private BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
public FastScanner(InputStream inputStream) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
if (stringTokenizer == null || !stringTokenizer.hasMoreElements()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | d1d21f8ad2c543c82bb69f4640354c04 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 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 A {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), q = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
while (q-- > 0) {
int l = sc.nextInt() - 1, r = sc.nextInt() - 1, pos = sc.nextInt() - 1;
int less = 0;
for (int i = l; i <= r; i++) if (arr[i] < arr[pos]) less++;
if (less + l == pos) out.println("Yes");
else out.println("No");
}
out.flush();
out.close();
}
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();
}
}
} | Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | d98b22be04c64ec372ccc61d52d60566 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.Random;
public class B {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), q = sc.nextInt();
int arr [] = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
while (q-- > 0) {
int l = sc.nextInt() - 1, r = sc.nextInt() - 1, pos = sc.nextInt() - 1;
int less = 0;
for (int i = l; i <= r; i++) if (arr[i] < arr[pos]) less++;
if (less + l == pos) out.println("Yes");
else out.println("No");
}
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader s) {
br = new BufferedReader(s);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 5365d89d88f90f21179c37f192dd365f | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Omar Yasser
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner sc, PrintWriter out) {
int n = sc.nextInt(), q = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
while (q-- > 0) {
int l = sc.nextInt() - 1, r = sc.nextInt() - 1, pos = sc.nextInt() - 1;
int less = 0;
for (int i = l; i <= r; i++) if (arr[i] < arr[pos]) less++;
if (less + l == pos) out.println("Yes");
else out.println("No");
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
public String next() {
while (st == null || !st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 77728cd0fefbc59297f66b1cae00bc38 | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Tarek
*/
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);
BVladikAndComplicatedBook solver = new BVladikAndComplicatedBook();
solver.solve(1, in, out);
out.close();
}
static class BVladikAndComplicatedBook {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
while (m-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
int x = in.nextInt();
l--;
r--;
x--;
int c = 0;
for (int i = l; i <= r; i++) {
if (a[i] < a[x]) c++;
}
if (c == (x - l)) {
out.println("Yes");
} else {
out.println("No");
}
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 4da93bec106a02d15d3399fa403caa3d | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Tarek
*/
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);
BVladikAndComplicatedBook solver = new BVladikAndComplicatedBook();
solver.solve(1, in, out);
out.close();
}
static class BVladikAndComplicatedBook {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
while (m-- > 0) {
int l = in.nextInt();
int r = in.nextInt();
int x = in.nextInt();
l--;
r--;
x--;
int c = 0;
for (int i = l; i <= r; i++) {
if (a[i] < a[x]) c++;
}
//out.println(c);
if (c == (x - l)) {
out.println("Yes");
} else {
out.println("No");
}
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output | |
PASSED | 13c6eceeef854df735eae97b4041932b | train_004.jsonl | 1495877700 | Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
new B();
}
int len,p[];
B() {
Scanner in = new Scanner(System.in);
len = in.nextInt();
int times = in.nextInt();
p = new int[len];
for(int i=0;i<len;i++){
p[i] = in.nextInt();
}
main:
for(int q=1;q<=times;q++){ //O(n)
int l = in.nextInt()-1, r = in.nextInt()-1, x = in.nextInt()-1;
int n = p[x],count=0;
for(int i=l;i<=r;i++){ //O(n)
if(p[i]<n)
count++;
if(count>x-l){ //that's a problem
System.out.println("No");
continue main;
}
}
System.out.println(count==x-l?"Yes":"No");
}
in.close();
}
}
/*
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
*/
| Java | ["5 5\n5 4 3 2 1\n1 5 3\n1 3 1\n2 4 3\n4 4 4\n2 5 3", "6 5\n1 4 3 2 5 6\n2 4 3\n1 6 2\n4 5 4\n1 3 3\n2 6 3"] | 2 seconds | ["Yes\nNo\nYes\nYes\nNo", "Yes\nNo\nYes\nNo\nYes"] | NoteExplanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No". | Java 8 | standard input | [
"implementation",
"sortings"
] | 44162a97e574594ac0e598368e8e4e14 | First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book. Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct. Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik. | 1,200 | For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.