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
9a140207cfe437cad996160255140931
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.io.*; import java.util.*; public class cf { public static int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public static void main(String args[]) { PrintWriter out =new PrintWriter(System.out); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); Integer arr[]=new Integer[n]; for(int i = 0 ;i<n;i++) arr[i]=sc.nextInt(); Arrays.sort(arr); int gcd =arr[1]-arr[0]; for(int i = 1 ;i<n;i++) gcd=GCD(gcd,arr[i]-arr[i-1]); int points=0; for(int i = 1 ;i<n;i++) points+=(arr[i]-arr[i-1])/gcd-1; System.out.println(points); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
5187636b20654342ed464e12de718ef8
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static Long gcd(Long a, Long b) { if (a == 0 || b == 0) return a+b; while (b > 0) { Long r = a % b; a = b; b = r; } return a; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Long[] a = new Long[n]; for (int i=0; i<n; i++) a[i] = in.nextLong(); Arrays.sort(a); Long d = 0L; for (int i=1; i<n; i++) d = gcd(d, a[i]-a[i-1]); Long ans = 0L; for (int i=1; i<n; i++) ans += (a[i]-a[i-1])/d - 1; System.out.format("%d\n", ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
eeb27c52884e8ea59f6e12be9e92968b
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class VKCUP { public static Long gcd(Long a,Long b) { if(b==0)return a; else return gcd(b,a%b); } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n=cin.nextInt(); Long a[]=new Long[n+10]; Long dif[]=new Long[n+10]; for(int i=1;i<=n;i++) a[i]=cin.nextLong(); Arrays.sort(a,1,n+1); long A[] = new long[n + 10]; for (int i = 1; i <= n; ++i)A[i] = a[i]; for(int i=1;i<n;i++) dif[i]=A[i+1]-A[i]; Long GCD=0L; for(int i=1;i<n;i++) { GCD=gcd(GCD,dif[i]); } Long ans=0L; for(int i=1;i<n;i++) ans+=dif[i]/GCD-1; System.out.println(ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
e2181bb7b78c96763bfabae1616a33c2
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class VKCUP { public static Long gcd(Long a,Long b) { if(b==0)return a; else return gcd(b,a%b); } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n=cin.nextInt(); Long A[]=new Long[n+10]; Long dif[]=new Long[n+10]; for(int i=1;i<=n;i++) A[i]=cin.nextLong(); Arrays.sort(A,1,n+1); for(int i=1;i<n;i++) dif[i]=A[i+1]-A[i]; Long GCD=0L; for(int i=1;i<n;i++) { GCD=gcd(GCD,dif[i]); } Long ans=0L; for(int i=1;i<n;i++) ans+=dif[i]/GCD-1; System.out.println(ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
f1417960e7eeaa4f00b3f954216ce5dd
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main { private final static String[] local = {"3 1 2 3"}; static class Task { public static Long gcd(Long a,Long b) { if(b==0)return a; else return gcd(b,a%b); } private void solve() { int n=ni(); Long A[]=new Long[n+1]; for(int i=1;i<=n;i++) A[i]=nl(); Arrays.parallelSort(A,1,n+1); Long sum; Long GCD=sum=A[2] - A[1]; for(int i=2;i<n;i++) { GCD=gcd(GCD,A[i + 1] - A[i]); sum+=A[i+1]-A[i]; } out.println(sum / GCD - n + 1); } void run(String INPUT, boolean oj){this.oj=oj;out=new PrintWriter(System.out);inputStream = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());long startTime=System.currentTimeMillis();solve();out.flush();long endTime=System.currentTimeMillis();debug(endTime - startTime + "ms");} private InputStream inputStream;private PrintWriter out;private boolean oj;private byte[] inbuf=new byte[1024];private int lenbuf=0,ptrbuf=0; private int readByte(){if(lenbuf == -1)throw new RuntimeException();if(ptrbuf>=lenbuf){ptrbuf=0;try{lenbuf=inputStream.read(inbuf);}catch(IOException e){throw new RuntimeException();}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 int ni(){return Integer.parseInt(ns());}private long nl(){return Long.parseLong(ns());} private String ns(){int b = skip();StringBuilder sb = new StringBuilder();while (!(isSpaceChar(b))) {sb.appendCodePoint(b);b = readByte();}return sb.toString(); } private int debugCnt = 0;private void debug(Object... o){if(!oj) System.out.println("debug:"+ ++debugCnt + " " + Arrays.deepToString(o));} } public static void main(String[] args){ boolean oj = System.getProperty("ONLINE_JUDGE") != null; //boolean oj = true; if(oj)new Task().run(null, true);else{int test = 1;for(String aLocal : local){if(aLocal.equals(""))continue;System.out.println("Test" + test++ + ":");new Task().run(aLocal,false);}} } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
1d59081f3058fc63fd4aa5f1ff9da2f5
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; public class VKCUP { public static Long gcd(Long a,Long b) { if(b==0)return a; else return gcd(b,a%b); } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n=cin.nextInt(); Long A[]=new Long[n+10]; Long dif[]=new Long[n+10]; for(int i=1;i<=n;i++) A[i]=cin.nextLong(); Arrays.sort(A,1,n+1); for(int i=1;i<n;i++) dif[i]=A[i+1]-A[i]; Long GCD=0L; for(int i=1;i<n;i++) { GCD=gcd(GCD,dif[i]); } Long ans=0L; for(int i=1;i<n;i++) ans+=dif[i]/GCD-1; System.out.println(ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
38ad5092814adf0291f58f4bc5b5aa4a
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
// Don't place your source in a package import java.util.*; import java.lang.*; import java.io.*; // Please name your class Main public class Main { public static int gcd(int a,int b){ if(a==0)return b; else return gcd(b%a,a); } public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n=in.nextInt(); Integer []a=new Integer[n]; for(int i=0;i<n;i++)a[i]=in.nextInt(); Arrays.sort(a); int g=0; int ans=0; for(int i=0;i<n-1;i++){ int d=a[i+1]-a[i]; g=gcd(d,g); } for(int i=0;i<n-1;i++){ int d=a[i+1]-a[i]; ans+=d/g-1; } System.out.println(ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
da8b27bd16e9eba92f027ead1fc199c1
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static Long gcd(Long a, Long b) { if (a == 0 || b == 0) return a+b; while (b > 0) { Long r = a % b; a = b; b = r; } return a; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Long[] a = new Long[n]; for (int i=0; i<n; i++) a[i] = in.nextLong(); Arrays.sort(a); Long d = 0L; for (int i=1; i<n; i++) d = gcd(d, a[i]-a[i-1]); Long ans = 0L; for (int i=1; i<n; i++) ans += (a[i]-a[i-1])/d - 1; System.out.format("%d\n", ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
1515b667fa690a64648e83d7977c3262
train_004.jsonl
1521300900
There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class KeMeTao { public static Long gcd(Long a, Long b) { if (a == 0 || b == 0) return a+b; while (b > 0) { Long r = a % b; a = b; b = r; } return a; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Long[] a = new Long[n]; for (int i=0; i<n; i++) a[i] = in.nextLong(); Arrays.sort(a); Long d = 0L; for (int i=1; i<n; i++) d = gcd(d, a[i]-a[i-1]); Long ans = 0L; for (int i=1; i<n; i++) ans += (a[i]-a[i-1])/d - 1; System.out.format("%d\n", ans); } }
Java
["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"]
1 second
["1", "0", "8"]
NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.
Java 8
standard input
[]
805d82c407c1b34f217f8836454f7e09
The first line contains a single integer n (3 ≀ n ≀ 100 000) β€” the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.
1,800
Print a single integer m β€” the smallest number of points you should add on the line to make the distances between all neighboring points equal.
standard output
PASSED
6053ef50d8492a1cebe06a69a62871bd
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.Iterator; import java.util.Scanner; import java.util.ArrayList; public class Sets { public static void main(String[] args) { class Item { public Item(int N) { vals = new int[N]; has_val = new boolean[201]; } int[] vals; boolean[] has_val; } ArrayList<Item> items = new ArrayList<Item>(); Scanner s = new Scanner(System.in); int N = s.nextInt(), M = N*(N-1)/2; for(int m=0; m<M; m++) { int NN = s.nextInt(); Item item = new Item(NN); items.add(item); for (int nn=0; nn<NN; nn++) { int val = s.nextInt(); item.vals[nn] = val; item.has_val[val] = true; } } ArrayList<ArrayList<Integer>> sets = new ArrayList<ArrayList<Integer>>(); boolean[] has_already = new boolean[201]; if (N == 2) { ArrayList<Integer> set; Item item = items.get(0); set = new ArrayList<Integer>(); for (int i=0; i<item.vals.length-1; i++) set.add(item.vals[i]); sets.add(set); set = new ArrayList<Integer>(); set.add(item.vals[item.vals.length-1]); sets.add(set); }else for(;;) { if (items.size() == 0) break; Item item = items.get(0); items.remove(0); int val = item.vals[0]; ArrayList<Item> items_to_del = new ArrayList<Item>(); Iterator<Item> e_item = items.iterator(); while(e_item.hasNext()) { Item c_item = e_item.next(); ArrayList<Integer> setA = null, setB = null, setC = null; for(int i=0; i<item.vals.length; i++) if (c_item.has_val[item.vals[i]]) { items_to_del.add(c_item); setA = new ArrayList<Integer>(); setB = new ArrayList<Integer>(); for(int ind=0; ind<item.vals.length; ind++){ int c_val = item.vals[ind]; if (has_already[c_val]) continue; has_already[c_val] = true; if (c_item.has_val[c_val]) setA.add(c_val); else setB.add(c_val); } setC = new ArrayList<Integer>(); for(int ind=0; ind<c_item.vals.length; ind++){ int c_val = c_item.vals[ind]; if (!item.has_val[c_val]) { if (has_already[c_val]) break; has_already[c_val] = true; setC.add(c_val); } } break; } if (setA != null && setA.size() > 0) sets.add(setA); if (setB != null && setB.size() > 0) sets.add(setB); if (setC != null && setC.size() > 0) sets.add(setC); } Iterator<Item> e_item_to_del = items_to_del.iterator(); while(e_item_to_del.hasNext()){ Item item_to_del = e_item_to_del.next(); items.remove(item_to_del); } } // Print Results Iterator<ArrayList<Integer>> e_item = sets.iterator(); while(e_item.hasNext()) { ArrayList<Integer> set = e_item.next(); Iterator<Integer> ee_item = set.iterator(); System.out.printf("%d ", set.size()); while(ee_item.hasNext()) System.out.printf("%d ", ee_item.next()); System.out.printf("\n"); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
ea1714f86493ac313e78f5561b972d43
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; public class Sets { public void run() { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); Scanner scanner = new Scanner(in); int n = scanner.nextInt(); int[][] papers = new int[n * (n - 1) / 2][]; int[] id = new int[200 + 1]; boolean[][] has = new boolean[papers.length][200 + 1]; boolean[] present = new boolean[200 + 1]; for (int i = 0; i < papers.length; i++) { int k = scanner.nextInt(); papers[i] = new int[k]; for (int j = 0; j < k; j++) { papers[i][j] = scanner.nextInt(); present[papers[i][j]] = true; has[i][papers[i][j]] = true; } Arrays.sort(papers[i]); } int ids = 0; for (int i = 1; i <= 200; i++) { if (present[i] && id[i] == 0) { int[] two = new int[2]; int found = 0; for (int j = 0; j < papers.length && found < 2; j++) { if (has[j][i]) two[found++] = j; } if (found == 2) { ids++; int p = 0; int q = 0; while (p < papers[two[0]].length && q < papers[two[1]].length) { if (papers[two[0]][p] == papers[two[1]][q]) { id[papers[two[0]][p]] = ids; id[papers[two[1]][q]] = ids; p++; q++; } else if (papers[two[0]][p] < papers[two[1]][q]) { p++; } else { q++; } } } } } // The only case left is when there is a single group, because // if there are more groups, then each will be used more than once in // the resulting sets. The only exception is when there are 2 groups // at the beginning and 1 group in the end: 1 group can be achieved // either from 1 group (no action), or from 2 groups // (2 * (2 - 1) / 2 == 1). for (int i = 1; i <= 200; i++) if (present[i] && id[i] == 0) { ++ids; for (int j = i; j <= 200; j++) { if (present[j] && id[j] == 0) { id[j] = ids; } } } // A corner case. Our algorithm will put everything into a single group // in this case. But we need to have exactly 2 groups. if (n == 2) { id[papers[0][0]] = ++ids; } for (int i = 1; i <= ids; i++) { int count = 0; for (int j = 1; j <= 200; j++) if (id[j] == i) count++; System.out.print(count); for (int j = 1; j <= 200; j++) if (id[j] == i) System.out.print(" " + j); System.out.println(); } } public static void main(String[] args) { new Sets().run(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
e95f05b7e3bc6475f342c901934383e7
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
//package yandex_cval2; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class B { public static void out(ArrayList<ArrayList<Integer>> list) { int n = list.size(); for (int i = 0; i < n; i++) { int ki = list.get(i).size(); System.out.print(ki); for (int o = 0; o < ki; o++) { System.out.print(" " + list.get(i).get(o)); } System.out.println(); } } public static ArrayList<Integer> peres(ArrayList<Integer> A, ArrayList<Integer> B) { ArrayList<Integer> AB = new ArrayList<Integer>(); HashSet<Integer> set = new HashSet<Integer>(); for(int i = 0; i < A.size(); i ++){ set.add(A.get(i)); } for(int i = 0; i < B.size(); i ++){ if(set.contains(B.get(i))){ AB.add(B.get(i)); } } return AB; } /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); int k = n * (n - 1) / 2; ArrayList<ArrayList<Integer>> AB = new ArrayList<ArrayList<Integer>>(); ArrayList<ArrayList<Integer>> ai = new ArrayList<ArrayList<Integer>>(); for(int i = 0; i < 201; i++) ai.add( new ArrayList<Integer>()); for (int i = 0; i < k; i++) { int ki = in.nextInt(); ArrayList<Integer> A = new ArrayList<Integer>(); for (int o = 0; o < ki; o++) { int x = in.nextInt(); ai.get(x).add(i); A.add(x); } AB.add(A); } if (n == 2) { ArrayList<Integer> A = new ArrayList<Integer>(); A.add( AB.get(0).get(0)); list.add(A); A = new ArrayList<Integer>(); for (int i = 1; i < AB.get(0).size(); i++) { A.add( AB.get(0).get(i)); } list.add(A); } else { for (int i = 1; i < 201; i++) if (ai.get(i) != null && ai.get(i).size() > 0) { int l1 = ai.get(i).get(0); int l2 = ai.get(i).get(1); ArrayList<Integer> per = peres(AB.get(l1), AB.get(l2)); list.add(per); for(int o = 0; o < per.size(); o++){ ai.set( per.get(o), null); } } } out(list); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
c65eac2576911f41e1f0d57830aa2f5f
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; import static java.lang.Math.*; public class B { public static void main(String[] args){ Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = (N*(N-1))/2; int[][] S = new int[M][]; for(int i = 0; i < M;i++){ int s = in.nextInt(); S[i] = new int[s]; for(int j = 0; j < s;j++) S[i][j] = in.nextInt(); } if(N == 2){ System.out.println("1 "+S[0][0]); System.out.print(S[0].length-1); for(int i = 1; i < S[0].length;i++) System.out.print(" "+S[0][i]); System.out.println(); System.exit(0); } int num = S[0][0]; int[] all = new int[1000]; for(int i = 0; i < M; i++){ boolean contains = false; for(int j:S[i]) if(j == num) contains = true; if(contains){ for(int j:S[i]) all[j]++; } } ArrayList<Integer>[] ans = new ArrayList[N]; for(int i = 0; i < N;i++) ans[i] = new ArrayList<Integer>(); for(int i = 0; i < 1000; i++) if(all[i] == N-1) ans[0].add(i); int at = 1; for(int i = 0; i < M;i++){ boolean contains = false; for(int j:S[i]) if(j == num) contains = true; if(contains){ for(int j:S[i]){ if(all[j] != N-1) ans[at].add(j); } at++; } } for(ArrayList<Integer> A: ans){ System.out.print(A.size()); for(int a:A) System.out.print(" "+(a)); System.out.println(); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
4dcabd87e84309a325b87fe8836872b9
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.PrintWriter; import java.math.BigInteger; import java.util.HashMap; import java.util.Scanner; public class B { private static int[] dsu; private static int relax(int n) { if (dsu[n] != n) { dsu[n] = relax(dsu[n]); } return dsu[n]; } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); if (n == 2) { int k = in.nextInt(); out.println(1 + " " + in.nextInt()); out.print(k - 1); for (int j = 0; j < k - 1; ++j) { out.print(" " + in.nextInt()); } out.println(); } else { HashMap<Integer, BigInteger> masks = new HashMap<Integer, BigInteger>(); BigInteger curMask = BigInteger.ONE; for (int i = 0; i < n * (n - 1) / 2; ++i) { int k = in.nextInt(); for (int j = 0; j < k; ++j) { int cur = in.nextInt(); if (!masks.containsKey(cur)) { masks.put(cur, curMask); } else { masks.put(cur, masks.get(cur).add(curMask)); } } curMask = curMask.shiftLeft(1); } dsu = new int[201]; for (int i = 0; i < 201; ++i) { dsu[i] = i; } for (int i = 1; i <= 200; ++i) { if (!masks.containsKey(i)) { continue; } for (int j = i + 1; j <= 200; ++j) { if (!masks.containsKey(j)) { continue; } if (masks.get(i).compareTo(masks.get(j)) == 0) { dsu[j] = relax(i); } } } boolean[] seen = new boolean[201]; for (int i = 1; i < 201; ++i) { if (!masks.containsKey(i) || seen[i]) { continue; } seen[i] = true; int all = 1; for (int j = i + 1; j < 201; ++j) { if (!masks.containsKey(j)) { continue; } if (relax(i) == relax(j)) { ++all; } } out.print(all + " " + i); for (int j = i + 1; j < 201; ++j) { if (!masks.containsKey(j) || seen[j]) { continue; } if (relax(i) == relax(j)) { seen[j] = true; out.print(" " + j); } } out.println(); } } in.close(); out.close(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
702161281936ae00cb6826a7be40464d
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; public class a { public static void main(String[] argrs) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = (n*n-n)/2; int[][] sets = new int[k][]; for(int i=0; i<k; i++) { int t = in.nextInt(); sets[i] = new int[t]; for(int j=0; j<t; j++) sets[i][j] = in.nextInt(); } if(k==1) { System.out.printf("1 %d%n", sets[0][0]); System.out.print(sets[0].length-1); for(int i=1; i<sets[0].length; i++) System.out.print(" " + sets[0][i]); System.out.println(); return; } HashSet<String> res = new HashSet<String>(); for(int i=1; i<k; i++) { String[] strs = intersect(sets[0], sets[i]); if(strs != null) for(String s : strs) res.add(s); } for(String str : res) if(str!=null) System.out.println(str); } public static String[] intersect(int[] a, int[] b) { ArrayList<Integer> ret1 = new ArrayList<Integer>(); ArrayList<Integer> ret2 = new ArrayList<Integer>(); ArrayList<Integer> ret3 = new ArrayList<Integer>(); for(int i=0; i<a.length; i++) for(int j=0; j<b.length; j++) if(a[i]==b[j]) ret1.add(a[i]); for(int i=0; i<a.length; i++) { boolean found = false; for(int j=0; j<b.length; j++) if(a[i] == b[j]) found = true; if(!found) ret2.add(a[i]); } for(int i=0; i<b.length; i++) { boolean found = false; for(int j=0; j<a.length; j++) if(b[i] == a[j]) found = true; if(!found) ret3.add(b[i]); } if(ret1.size()==0) return null; Collections.sort(ret1); String str1 = ret1.size() + ""; for(int i : ret1) str1 = str1 + " " + i; Collections.sort(ret2); String str2 = ret2.size() + ""; for(int i : ret2) str2 = str2 + " " + i; Collections.sort(ret3); String str3 = ret3.size() + ""; for(int i : ret3) str3 = str3 + " " + i; String[] ret = new String[3]; ret[0] = str1; ret[1] = str2; ret[2] = str3; return ret; } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
a98928010cf2de2e4afc1989c65576d7
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import static java.lang.Math.*; import static java.lang.System.currentTimeMillis; import static java.lang.System.exit; import static java.lang.System.arraycopy; import static java.util.Arrays.sort; import static java.util.Arrays.binarySearch; import static java.util.Arrays.fill; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) { } new Thread(null, new Runnable() { public void run() { try { new Main().run(); } catch (Throwable e) { e.printStackTrace(); exit(999); } } }, "1", 1 << 23).start(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); int MAXD = 200; int n, m; private void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); n = nextInt(); m = n * (n - 1) / 2; if (m == 1) { int k = nextInt(); out.println("1 " + nextInt()); out.print(k - 1); for (int i = 0; i < k - 1; i++) out.print(" " + nextInt()); out.println(); } else { BitSet[][] sets = new BitSet[MAXD][n - 1]; int[] cnts = new int[MAXD]; for (int i = 0; i < m; i++) { int k = nextInt(); BitSet set = new BitSet(MAXD); for (int j = 0; j < k; j++) { int num = nextInt() - 1; set.set(num); sets[num][cnts[num]++] = set; } } Set<BitSet> res = new HashSet<BitSet>(); for (int i = 0; i < MAXD; i++) if (cnts[i] != 0) { BitSet set = new BitSet(MAXD); set.flip(0, MAXD); for (int j = 0; j < n - 1; j++) { set.and(sets[i][j]); } res.add(set); } for (BitSet set : res) { out.print(set.cardinality()); for (int i = 0; i < MAXD; i++) if (set.get(i)) { out.print(' '); out.print(i + 1); } out.println(); } } in.close(); out.close(); } void chk(boolean b) { if (b) return; System.out.println(new Error().getStackTrace()[1]); exit(999); } void deb(String fmt, Object... args) { System.out.printf(Locale.US, fmt + "%n", args); } String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
9b5cccc556c3d6607831d264e562010c
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * May 6, 2011Β  * @author parisel */ public class Sets { int N; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] tok; String s; private String[] getTok() throws IOException {return br.readLine().split(" ");} private int getInt() throws IOException {return Integer.valueOf(br.readLine());} private int[] getInt(int N) throws IOException { int[] data= new int[N]; tok= br.readLine().split(" "); for (int i=0; i<N; i++) data[i]= Integer.valueOf(tok[i]); return data; } boolean[] present= new boolean[201]; int[] setId= new int[201]; int[][] sets; public void solve() throws IOException { int i=0, j=0; N= getInt(); int noOfDouble= (N*(N-1))/2; sets= new int[201][N-1]; int K; for (i=0; i<noOfDouble; ++i) { tok= getTok(); for (j=0; j<Integer.valueOf(tok[0]); j++) { present[Integer.valueOf(tok[j+1])]= true; insert(Integer.valueOf(tok[j+1]), i+1); } } int curSet= 1, count; String res; if (N==2) { i= 1; while (!present[i]) ++i; System.out.printf("1 %d\n", i); ++i; while (!present[i]) ++i; count= 1; res= ""+ i; for (j=i+1; j<201; j++) { if (!present[j]) continue; res+= " "+j; ++count; } System.out.printf("%d %s\n", count, res); return; } // System.out.printf("\n"); for (i=1; i<201; i++) { if (!present[i]) continue; res= ""+i; count= 1; for (j=i+1; j<201; j++) { if (!present[j]) continue; if (equals(i,j)) { res+= " "+j; ++count; present[j]= false;} } System.out.printf("%d %s\n", count, res); } } private boolean equals(int v1, int v2) { int[] vv1= sets[v1], vv2= sets[v2]; for (int i=0; i<N-1; i++) if (vv1[i]!=vv2[i]) return false; return true; } private void insert(int v, int s) { int[] set= sets[v]; int i= 0; while (set[i]>0) ++i; set[i]= s; } public static void main(String[] args) throws IOException { new Sets().solve(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
6242dc2e6f9b163c2458b4c189bded2e
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.InputMismatchException; import java.io.*; import java.util.Vector; import java.util.HashSet; /** * Generated by Contest helper plug-in * Actual solution is at the bottom */ public class Main { public static void main(String[] args) { InputReader in = new StreamInputReader(System.in); PrintWriter out = new PrintWriter(System.out); run(in, out); } public static void run(InputReader in, PrintWriter out) { Solver solver = new taskB(); solver.solve(1, in, out); Exit.exit(in, out); } } abstract class InputReader { private boolean finished = false; public abstract int read(); public int nextInt() { return Integer.parseInt(nextToken()); } public String readString() { return readLine(); } public String readLine() { StringBuffer res = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') res.appendCodePoint(c); c = read(); } return res.toString(); } public String nextToken() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public void setFinished(boolean finished) { this.finished = finished; } public abstract void close(); } class StreamInputReader extends InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public StreamInputReader(InputStream stream) { this.stream = stream; curChar = 0; } 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++]; } @Override public void close() { try { stream.close(); } catch (IOException ignored) { } } } class Exit { private Exit() { } public static void exit(InputReader in, PrintWriter out) { in.setFinished(true); in.close(); out.close(); } } interface Solver { public void solve(int testNumber, InputReader in, PrintWriter out); } class taskB implements Solver { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int cnt[][] = new int[201][201]; HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < (n * (n - 1) / 2); ++i) { int k = in.nextInt(); int a[] = new int[k]; for (int j = 0; j < k; ++j) { a[j] = in.nextInt(); set.add(a[j]); } for (int j = 0; j < k; ++j) for (int u = j + 1; u < k; ++u) { cnt[a[j]][a[u]] ++ ; cnt[a[u]][a[j]] ++ ; } } if (n == 2) { for (int i =0 ; i <= 200; ++i) { if (set.contains(i)) { out.println("1 " + i); set.remove(i); break; } } out.print(set.size() + " "); for (int x : set) { out.print(x + " "); } return; } int p[] = new int[201]; for (int i = 0; i < p.length; ++i) p[i] = i; for (int i = 0; i <= 200; ++i) for (int j = 0; j <= 200; ++j) if (cnt[i][j] == n - 1) { union(i, j, p); } Vector<Integer>[] g = new Vector[201]; for (int i =0 ; i < g.length; ++i) g[i] = new Vector<Integer>(0); for (int i = 0; i <= 200; ++i) { if (!set.contains(i)) continue; g[getp(i, p)].add(i); } for (int i =0 ; i <= 200; ++i) { if (g[i].size() > 0) { out.print(g[i].size() + " "); for (int x : g[i]) out.print(x + " "); out.println(); } } } private void union(int i, int j, int[] p) { i = getp(i, p); j = getp(j, p); p[i] = j; } private int getp(int i, int[] p) { if (p[i] != i) p[i] = getp(p[i], p); return p[i]; } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
d0e46f8ebda35f4925fb31c754f43fd1
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class B{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int nn = Integer.parseInt(in.readLine()); int n = (nn*(nn-1))/2; boolean[][] matrix = new boolean[201][n]; ArrayList<Integer> set = new ArrayList<Integer>(); for(int i = 0; i < n; ++i){ StringTokenizer st = new StringTokenizer(in.readLine()); int m = Integer.parseInt(st.nextToken()); for(int j = 0; j < m; ++j){ int temp = Integer.parseInt(st.nextToken()); matrix[temp][i] = true; if(!set.contains(temp)) set.add(temp); } } ArrayList<ArrayList> ans = new ArrayList<ArrayList>(); boolean[] done = new boolean[201]; for(int i = 0; i < set.size(); ++i){ if(done[set.get(i)]) continue; done[set.get(i)] = true; ArrayList<Integer> temp = new ArrayList<Integer>(); temp.add(set.get(i)); for(int j = i+1; j < set.size(); ++j){ if(!done[set.get(j)] && check(matrix[set.get(i)], matrix[set.get(j)])){ temp.add(set.get(j)); done[set.get(j)] = true; } } ans.add(temp); } while(ans.size()!=nn){ for(int i = 0; i < ans.size(); ++i){ if(ans.get(i).size() > 1){ ArrayList<Integer> temp = new ArrayList<Integer>(); temp.add((Integer)ans.get(i).remove(0)); ans.add(temp); break; } } } for(ArrayList i : ans){ out.print(i.size()+" "); for(Object j : i){ out.print(j+" "); } out.println(); } out.close(); } public static boolean check(boolean[] a, boolean[] b){ for(int i = 0; i < a.length; ++i){ if(a[i]!=b[i]) return false; } return true; } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
351ddb6c258382f6ac0087c3e693cd1b
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class P082B { public static void main(String[] args) { Scanner inScanner = new Scanner(System.in); int n = inScanner.nextInt(); Map<Integer, Number> numbers = new HashMap<Integer, Number>(); Set<Integer> seenNumbers = new HashSet<Integer>(); for (int i = 0; i < n * (n - 1) / 2; i++) { int m = inScanner.nextInt(); List<Integer> lineNumbers = new ArrayList<Integer>(); for (int j = 0; j < m; j++) { int number = inScanner.nextInt(); seenNumbers.add(number); if (!numbers.containsKey(number)) numbers.put(number, new Number(number)); Number currentNumber = numbers.get(number); for (Integer integer : lineNumbers) { currentNumber.add(integer); numbers.get(integer).add(number); } lineNumbers.add(number); } } if (n == 2) { int alone = (Integer) seenNumbers.toArray()[0]; System.out.println("1 " + alone); seenNumbers.remove(alone); System.out.print(seenNumbers.size() + " "); for (Integer number : seenNumbers) System.out.print(number + " "); return; } Set<Integer> seenAgainNumbers = new HashSet<Integer>(); for (Integer number : seenNumbers) { if (seenAgainNumbers.contains(number)) continue; Number currentNumber = numbers.get(number); System.out.print(currentNumber.connected.size() + " "); for (Integer relation : currentNumber.connected) { System.out.print(relation + " "); seenAgainNumbers.add(relation); } System.out.println(); } } private static class Number { int number; Set<Integer> seen; Set<Integer> connected; Number(int number) { this.number = number; seen = new HashSet<Integer>(); connected = new HashSet<Integer>(); connected.add(this.number); } void add(int number) { if (seen.contains(number)) connected.add(number); seen.add(number); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
abdae88cabf9abe60f695ed8786ab2fc
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; public class B82 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); if(n == 2) { StringTokenizer st = new StringTokenizer(br.readLine()); st.nextToken(); System.out.println("1 " + st.nextToken()); ArrayList<String> list = new ArrayList<String>(); while(st.hasMoreTokens()) list.add(st.nextToken()); System.out.print(list.size() + " "); for(int q = 0; q < list.size()-1; q++) System.out.print(list.get(q) + " "); System.out.println(list.get(list.size()-1)); return; } Set<Integer>[] sets = new HashSet[(n*n-n)>>1]; boolean[] has = new boolean[201]; for(int i = 0; i < sets.length; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); sets[i] = new HashSet<Integer>(); st.nextToken(); while(st.hasMoreTokens()) { int curr = Integer.parseInt(st.nextToken()); sets[i].add(curr); has[curr] = true; } } for(int i = 0; i < has.length; i++) { if(has[i]) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(Set<Integer> set: sets) { if(set.contains(i)) { for(int curr: set) { if(!map.containsKey(curr)) map.put(curr, 1); else map.put(curr, 1+map.get(curr)); } } } ArrayList<Integer> list = new ArrayList<Integer>(); for(int key: map.keySet()) { if(map.get(key) == n-1) list.add(key); } System.out.print(list.size() + " "); for(int q = 0; q < list.size()-1; q++) System.out.print(list.get(q) + " "); System.out.println(list.get(list.size()-1)); for(int out: list) has[out] = false; } } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
d2a9b49292394aa1fb9b9069844a6ef3
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Sets { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); String s=r.readLine(); int n=new Integer(s); int tot=n*(n-1)/2; int[][] count=new int[202][202]; boolean[] con=new boolean[202]; while(tot-->0){ s=r.readLine(); String[] sp=s.split(" "); int[] arr=new int[sp.length-1]; for (int i = 0; i < arr.length; i++) { arr[i]=new Integer(sp[i+1]); } Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { con[arr[i]]=true; for (int j = i+1; j < arr.length; j++) { count[arr[i]][arr[j]]++; count[arr[j]][arr[i]]++; } } if(n==2){ System.out.println(1+" "+arr[0]); System.out.print(arr.length-1); for (int i = 1; i < arr.length; i++) { System.out.println(" "+arr[i]); } return; } } boolean[] v=new boolean[202]; for (int i = 0; i < count.length; i++) { if(v[i]||!con[i])continue; v[i]=true; int element=1; for (int j = 0; j < count.length; j++) { if(count[i][j]==n-1&&!v[j]&&con[j]) element++; } System.out.print(element+" "+i); for (int j = 0; j < count.length; j++) { if(count[i][j]==n-1&&!v[j]&&con[j]){ v[j]=true; System.out.print(" "+j); } } System.out.println(); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
0f1b0a8d4b4f7d99e10975f9a0704088
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; public class A { private void work() { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader( System.in))); int[] t = new int[202]; while (sc.hasNextInt()) { int n = sc.nextInt(); if (n == 2) { int k = sc.nextInt(); System.out.println("1 " + sc.nextInt()); System.out.print(--k); while (k-- > 0) { System.out.printf(" %d", sc.nextInt()); } System.out.println(); continue; } int m = n * (n - 1) / 2; int[][] a = new int[202][202]; boolean[] s = new boolean[202]; for (int i = 0; i < m; i++) { int k = sc.nextInt(); for (int j = 0; j < k; j++) { t[j] = sc.nextInt(); s[t[j]] = true; for (int jj = 0; jj < j; jj++) { a[t[j]][t[jj]]++; a[t[jj]][t[j]]++; } } } boolean[] used = new boolean[202]; for (int i = 0; i < used.length; i++) { if (s[i] && !used[i]) { int k = 1; for (int j = 0; j < used.length; j++) { if (a[i][j] == n - 1) k++; } System.out.print(k); System.out.printf(" %d", i); used[i] = true; for (int j = 0; j < used.length; j++) { if (a[i][j] == n - 1) { System.out.printf(" %d", j); used[j] = true; } } System.out.println(); } } } } public static void main(String[] args) { new A().work(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
ff5de84d4b09a0b665df7fd9df236ba4
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; @SuppressWarnings("unchecked") public class Main { static ArrayList<Integer> res = new ArrayList<Integer>(); static int n; public static void main(String[] arqs) { Scanner scan = new Scanner(System.in); n = scan.nextInt(); if(n == 2) { int now = scan.nextInt(); System.out.println("1 " + scan.nextInt()); System.out.print(now-1); boolean first = true; for(int i = 1;i < now;i++) System.out.print(" " + scan.nextInt()); System.out.println(); return; } int[][] lines = new int[n*(n-1)/2][]; boolean[][] have = new boolean[n*(n-1)/2][201]; boolean[] came = new boolean[201]; for(int i = 0;i < n*(n-1)/2;i++) { int k = scan.nextInt(); lines[i] = new int[k]; for(int j = 0;j < k;j++) { lines[i][j] = scan.nextInt(); came[lines[i][j]] = true; have[i][lines[i][j]] = true; } Arrays.sort(lines[i]); } boolean[][] connected = new boolean[201][201]; for(int i = 1;i < 201;i++) for(int j = i+1;j < 201;j++) { connected[i][j] = connected[j][i] = true; } for(boolean[] i : have) { int[] all = new int[201]; int[] bads = new int[201]; int ind = 0, bind = 0; for(int j = 0;j < 201;j++) if(i[j]) all[ind++] = j; else bads[bind++] = j; for(int p = 0;p < ind;p++) for(int q = 0;q < bind;q++) connected[all[p]][bads[q]] = connected[bads[q]][all[p]] = false; } boolean[] visited = new boolean[201]; res = new ArrayList<Integer>(); for(int i = 1;i < 201;i++) if(came[i] && !visited[i]) { res.clear(); dfs(i, connected, visited); System.out.print(res.size()); for(int j : res) System.out.print(" " + j); System.out.println(); } } static void dfs(int at, boolean[][] connected, boolean[] visited) { if(visited[at]) return; res.add(at); visited[at] = true; for(int i = 0;i < 201;i++) if(connected[at][i] && !visited[i]) dfs(i, connected, visited); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
f1907c8dc1e1185c54836b1d58764d97
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class B_as { FastScanner in; PrintWriter out; class D implements Comparable<D> { int[] t; int v; public int compareTo(D o) { for (int j = 0; j < t.length; j++) { if (t[j] < o.t[j]) { return -1; } if (t[j] > o.t[j]) { return 1; } } return 0; } D(int v, int n) { this.v = v; t = new int[n]; Arrays.fill(t, -1); } } void solve2() { int k = in.nextInt(); out.print(1); int l = in.nextInt(); out.println(" " + l); out.print(k - 1); for (int i = 1; i < k; i++) { int j = in.nextInt(); out.print(" " + j); } return; } public void solve() throws IOException { int n = in.nextInt(); if (n == 2) { solve2(); return; } int m = n * (n - 1) / 2; int c = 200; D[] t = new D[c]; for (int i = 0; i < c; i++) { t[i] = new D(i + 1, n); } int[] tn = new int[c]; for (int i = 0; i < m; i++) { int k = in.nextInt(); for (int j = 0; j < k; j++) { int z = in.nextInt() - 1; t[z].t[tn[z]] = i; tn[z]++; } } Arrays.sort(t); int first = 0; while (t[first].t[0] == -1) { first++; } while (first < c) { int last = first; while (last < c && Arrays.equals(t[first].t, t[last].t)) { last++; } out.print(last - first); for (int i = first; i < last; i++) { out.print(" " + t[i].v); } out.println(); first = last; } } public void run() { try { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } 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()); } } public static void main(String[] arg) { new B_as().run(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
0553f92bb036ee75fb371ab3d7c881c8
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.ArrayList; import java.util.Scanner; import java.util.TreeSet; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = n*(n-1) / 2; TreeSet<Integer>[] sets = new TreeSet[m+1]; int p = 0; for (int i = 1; i <= m; i++) { int k = sc.nextInt(); sets[i] = new TreeSet(); for (int j = 1; j <= k; j++) { int t = sc.nextInt(); sets[i].add(t); p = t; } } if (n==2) { System.out.print(1+" "); System.out.println(sets[1].last()); System.out.print(sets[1].size()-1+" "); int cnt = 0; for (int i : sets[1]) { if (cnt==sets[1].size()-1) break; cnt++; System.out.print(i+" "); } return; } ArrayList<Integer> first = new ArrayList<Integer>(); for (int i = 1; i <= m; i++) { if (sets[i].contains(p)) { first.add(i); } } ArrayList<Integer> ans1 = new ArrayList<Integer>(); ans1.addAll(sets[first.get(0)]); for (int i : first) { ans1.retainAll(sets[i]); } System.out.print(ans1.size()+" "); for (int i : ans1) { System.out.print(i+" "); } System.out.println(); for (int i = 0; i < n-1; i++) { sets[first.get(i)].removeAll(ans1); System.out.print(sets[first.get(i)].size()+" "); for (int j : sets[first.get(i)]) { System.out.print(j+" "); } System.out.println(); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
3f4f63000111bd9f2f833f6e4ec98ae6
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; import java.io.*; import java.math.*; import java.util.*; @SuppressWarnings("unused") public class B { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int t = n * (n - 1) / 2; int one = Integer.MAX_VALUE; ArrayList<ArrayList<Integer>> input = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> index = new ArrayList<Integer>(); for (int i = 0; i < t; i++) { int k = in.nextInt(); ArrayList<Integer> set = new ArrayList<Integer>(); for (int j = 0; j < k; j++) { int a = in.nextInt(); one = min(one, a); set.add(a); } sort(set); input.add(set); } for (int i = 0; i < t; i++) { if (binarySearch(input.get(i), one) >= 0) index.add(i); } TreeSet<ArrayList<Integer>> output = new TreeSet<ArrayList<Integer>>(new Comparator<ArrayList<Integer>>() { public int compare(ArrayList<Integer> a1, ArrayList<Integer> a2) { for (int i = 0; i < min(a1.size(), a2.size()); i++) { if (a1.get(i).compareTo(a2.get(i)) != 0) return a1.get(i).compareTo(a2.get(i)); } return a1.size() - a2.size(); } }); if (n == 2) { ArrayList<Integer> A = input.get(0); int B = A.get(0); A.remove(0); System.out.println(1 + " " + B); System.out.print(A.size()); for (int i : A) System.out.print(" " + i); System.out.println(); } else { ArrayList<Integer> A = intersection(input.get(index.get(0)), input.get(index.get(1))); output.add(A); for (int i : index) { ArrayList<Integer> AB = input.get(i); AB.removeAll(A); if (AB.size() > 0) output.add(AB); } for (ArrayList<Integer> I : output) { System.out.print(I.size()); for (int i : I) System.out.print(" " + i); System.out.println(); } } } static ArrayList<Integer> intersection(final ArrayList<Integer> A, final ArrayList<Integer> B) { ArrayList<Integer> result = new ArrayList<Integer>(); for (int i = 0; i < A.size(); i++) if (binarySearch(B, A.get(i)) >= 0) result.add(A.get(i)); return result; } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
8a25b4063a9a7027864653153a8a043e
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; public class B { private static Scanner in; public void run() { int n = in.nextInt(); int max = 201; int[] count = new int[max]; int[][] together = new int[max][max]; int[] a = new int[max]; int k = -1; for (int s = 0; s < n * (n - 1) / 2; s++) { k = in.nextInt(); for (int i = 0; i < k; i++) { a[i] = in.nextInt(); count[a[i]]++; for (int j = 0; j < i; j++) { together[a[i]][a[j]]++; together[a[j]][a[i]]++; } } } if (n == 2) { System.out.println(1 + " " + a[0]); System.out.print(k - 1); for (int i = 1; i < k; i++) { System.out.print(" " + a[i]); } System.out.println(); return; } for (int i = 0; i < max; i++) { if (count[i] == 0) { continue; } Set<Integer> ans = new HashSet<Integer>(); ans.add(i); for (int j = i + 1; j < max; j++) { if (together[i][j] == count[i]) { count[j] = 0; ans.add(j); } } count[i] = 0; System.out.print(ans.size()); for (int x : ans) { System.out.print(" " + x); } System.out.println(); } } public static void main(String[] args) { Locale.setDefault(Locale.US); in = new Scanner(System.in); new B().run(); in.close(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
063572539beec321ca5bc7792d69be5f
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.awt.Point; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.Polygon; import java.io.*; import java.math.BigInteger; import static java.math.BigInteger.*; import java.util.*; import java.util.Map.Entry; public class B{ void solve()throws Exception { int n=nextInt(); int cntSet=n; n=n*(n-1)/2; HashSet<Integer>[]all=new HashSet[n]; for(int i=0;i<n;i++) { all[i]=new HashSet<Integer>(); int cnt=nextInt(); for(int j=0;j<cnt;j++) all[i].add(nextInt()); } HashSet<Integer>[]res=new HashSet[cntSet]; if(cntSet==2) { res[0]=new HashSet<Integer>(); res[1]=new HashSet<Integer>(); for(int x: all[0]) { if(res[0].size()==0) res[0].add(x); else res[1].add(x); } } else { HashSet<Integer>x=null; for(int i=1;i<n;i++) { HashSet<Integer>cur=new HashSet<Integer>(); for(int z: all[i]) if(all[0].contains(z)) cur.add(z); if(cur.size()>0) { x=cur; break; } } if(x==null) throw new RuntimeException(); res[0]=x; int at=1; for(int i=0;i<n;i++) { boolean haveAll=true; for(int z: x) if(!all[i].contains(z)) haveAll=false; if(haveAll) { HashSet<Integer>cur=new HashSet<Integer>(); for(int z: all[i]) if(!x.contains(z)) cur.add(z); res[at++]=cur; } } if(at!=cntSet) throw new RuntimeException(); } for(int i=0;i<cntSet;i++) { System.out.print(res[i].size()); for(int x: res[i]) { System.out.print(" "+x); } System.out.println(); } } BufferedReader reader; PrintWriter writer; StringTokenizer stk; void run()throws Exception { reader=new BufferedReader(new InputStreamReader(System.in)); stk=null; writer=new PrintWriter(System.out); solve(); reader.close(); writer.close(); } int nextInt()throws Exception { return Integer.parseInt(nextToken()); } long nextLong()throws Exception { return Long.parseLong(nextToken()); } double nextDouble()throws Exception { return Double.parseDouble(nextToken()); } String nextString()throws Exception { return nextToken(); } String nextLine()throws Exception { return reader.readLine(); } String nextToken()throws Exception { if(stk==null || !stk.hasMoreTokens()) { stk=new StringTokenizer(nextLine()); return nextToken(); } return stk.nextToken(); } public static void main(String[]a) throws Exception { new B().run(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
ebe80ad233b8f40e7b22550f03b1c2c9
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
/** * Created by IntelliJ IDEA. * User: X * Date: 5/10/11 * Time: 6:48 PM * To change this template use File | Settings | File Templates. */ import java.util.*; public class B { public static void main(String[]args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int all=n*(n-1)/2; HashSet<Integer>[]sets=new HashSet[all]; for(int i=0;i<all;i++) { int cnt=in.nextInt(); sets[i]=new HashSet<Integer>(); for(int j=0;j<cnt;j++) { sets[i].add(in.nextInt()); } } HashSet<Integer>[]res=new HashSet[n]; if(all==1) { res[0]=new HashSet<Integer>(); res[1]=new HashSet<Integer>(); for(int x: sets[0]) { if(res[0].size()==0) res[0].add(x); else res[1].add(x); } } else { HashSet<Integer>anyOne=new HashSet<Integer>(); for(int i=1;i<all;i++) { HashSet<Integer>intersect=new HashSet<Integer>(sets[i]); intersect.retainAll(sets[0]); if(intersect.size()>0) { anyOne=intersect; break; } } if(anyOne.size()==0) throw new RuntimeException(); res[0]=anyOne; int at=1; for(int i=0;i<all;i++) { if(sets[i].containsAll(anyOne)) { sets[i].removeAll(anyOne); res[at++]=sets[i]; } } if(at!=n) throw new RuntimeException(); } for(int i=0;i<n;i++) { System.out.print(res[i].size()); for(int x: res[i]) System.out.print(" "+x); System.out.println(); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
c6f06d5dfe69ddd28e81b1ffd624ccc5
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.io.*; public class Main { public static void main(String args[]) throws Exception { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out,false); String line=in.readLine(); int cases=Integer.parseInt(line); int number=cases; cases=((cases)*(cases-1))/2; int arr[][]=new int[201][201]; for(int i=0;i<201;i++) Arrays.fill(arr[i],0); int temp[]=new int[201]; Arrays.fill(temp,0); if(number==2) { String lin=in.readLine(); String parts[]=lin.split(" "); out.println("1 "+parts[1]); String next=(Integer.parseInt(parts[0])-1)+""; for(int i=2;i<parts.length;i++) next+=(" "+parts[i]); out.println(next); out.close(); return; } for(int i=0;i<cases;i++) { String lin=in.readLine(); String parts[]=lin.split(" "); int num=0; for(int j=1;j<parts.length;j++) { if(arr[Integer.parseInt(parts[j])][0]==0) { for(int k=1;k<parts.length;k++) arr[Integer.parseInt(parts[j])][Integer.parseInt(parts[k])]=1; arr[Integer.parseInt(parts[j])][0]=1; } else { Arrays.fill(temp,0); for(int k=1;k<parts.length;k++) { if(arr[Integer.parseInt(parts[j])][Integer.parseInt(parts[k])]==1) temp[Integer.parseInt(parts[k])]=1; } arr[Integer.parseInt(parts[j])][0]=1; for(int l=1;l<201;l++) arr[Integer.parseInt(parts[j])][l]=temp[l]; } } } for(int i=0;i<201;i++) { if(arr[i][0]==1) { int num=0; String output=""; for(int j=1;j<201;j++) { if(arr[i][j]==1) { arr[j][0]=0; num++; output+=(" "+j); } } output=num+output; out.println(output); } } out.close(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
50b6ff96a9b59bd14e430fb256c309d3
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.lang.*; import java.math.BigInteger; import java.io.*; import java.util.*; public class Solution implements Runnable{ public static BufferedReader br; public static PrintWriter out; public static StringTokenizer stk; public static boolean isStream = true; public static void main(String[] args) throws IOException { if (isStream) { br = new BufferedReader(new InputStreamReader(System.in)); } else { br = new BufferedReader(new FileReader("in.txt")); } out = new PrintWriter(System.out); new Thread(new Solution()).start(); } public void loadLine() { try { stk = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } public String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } } public String nextWord() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return stk.nextToken(); } public Integer nextInt() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Integer.valueOf(stk.nextToken()); } public Long nextLong() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Long.valueOf(stk.nextToken()); } public Double nextDouble() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Double.valueOf(stk.nextToken()); } public Float nextFloat() { while (stk==null||!stk.hasMoreTokens()) loadLine(); return Float.valueOf(stk.nextToken()); } public void run() { int n = nextInt(); int m = n*(n-1)/2; int[][] arr = new int[m][]; for (int i = 0; i < m; i++) { int cnt = nextInt(); arr[i] = new int[cnt]; for (int j = 0; j < cnt; j++) { arr[i][j] = nextInt(); } } ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>(); int[] res = new int[210]; Arrays.fill(res, -1); int comp = 1; for (int a = 0; a < 210; a++) { if (res[a] == -1) { boolean gafnd = false; int[] isGood = new int[210]; Arrays.fill(isGood, -1); for (int i = 0; i < m; i++) { boolean afnd = false; for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] == a) { afnd = true; } } gafnd |= afnd; boolean used[] = new boolean[210]; for (int j = 0; j < arr[i].length; j++) { used[arr[i][j]] = true; if (afnd) { if (isGood[arr[i][j]] == -1) { isGood[arr[i][j]] = 0; } } else { isGood[arr[i][j]] = 1; // Bad } } for (int b = 0; b < 210; b++) { if (isGood[b] == 0 && afnd && !used[b]) { isGood[b] = 1; } } } if (gafnd) { ArrayList<Integer> list = new ArrayList<Integer>(); ans.add(list); list.add(a); for (int b = 0; b < 210; b++) { if (b != a && isGood[b] == 0 && res[b] == -1) { list.add(b); res[b] = comp; } } comp++; } } } if (n == 2) { boolean first = true; for (Integer i : ans.get(0)) { if (first) { out.println("1 " + i); first = false; out.print(ans.get(0).size()-1 + " "); } else { out.print(i + " "); } } } else { for (ArrayList<Integer> list : ans) { out.print(list.size() + " "); for (Integer i : list) { out.print(i + " "); } out.println(); } } out.flush(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
30053ad9e6aff3e0cb8c984ee73fde6e
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.Locale; import java.util.Queue; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Solution implements Runnable { public static void main(String[] args) { (new Thread(new Solution())).start(); } BufferedReader in; PrintWriter out; StringTokenizer st; String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String r = in.readLine(); if (r == null) return null; st = new StringTokenizer(r); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } void solve() throws Exception { int n = nextInt(); if (n == 2) { int k = nextInt(); out.println("1 " + nextInt()); out.print(k - 1 + " "); for (int i = 0; i < k - 1; i++) out.print(nextInt() + " "); } else { BitSet[] a = new BitSet[201]; for (int i = 0; i < 201; i++) a[i] = new BitSet(n * (n - 1) / 2); for (int i = 0; i < n * (n - 1) / 2; i++) { for (int x = nextInt(); x > 0; x--) { a[nextInt()].set(i); } } boolean[] w = new boolean[201]; int j = 1; Stack<Integer> q = new Stack<Integer>(); for (int i = 0; i < n; i++) { while (w[j] || a[j].equals(new BitSet())) j++; w[j] = true; q.add(j); for (int jj = j + 1; jj < 201; jj++) { if (a[jj].equals(a[j])) { w[jj] = true; q.add(jj); } } out.print(q.size() + " "); while (!q.isEmpty()) out.print(q.pop() + " "); out.println(); } } } public void run() { Locale.setDefault(Locale.UK); try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { out.flush(); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
ef315471cee12ff8bcdbb292d89a02d5
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(r.readLine()); int[][] a = new int[N * (N - 1) / 2][]; for(int i = 0; i < N * (N - 1) / 2; i++){ String[] line = r.readLine().split("[ ]+"); int k = Integer.parseInt(line[0]); a[i] = new int[k]; for(int j = 0; j < k; j++) a[i][j] = Integer.parseInt(line[j + 1]); } HashSet<Integer>[] set = new HashSet[N * (N - 1) / 2]; for(int i = 0; i < N * (N - 1) / 2; i++) set[i] = new HashSet<Integer>(); for(int i = 0; i < N * (N - 1) / 2; i++) for(int j = 0; j < a[i].length; j++) set[i].add(a[i][j]); HashSet<Integer> v = new HashSet<Integer>(); for(int i = 0; i < N * (N - 1) / 2; i++) if(set[i].contains(a[0][0]))v.add(i); HashSet<Integer> one = new HashSet<Integer>(); for(int i : v){ boolean all = true; for(int j = 0; j < a[i].length; j++){ if(all && j == a[i].length - 1)break; int cnt = 0; for(int x : v){ if(set[x].contains(a[i][j]))cnt++; } if(cnt == N - 1)one.add(a[i][j]); else all = false; } break; } print(one); for(int i : v){ HashSet<Integer> p = new HashSet<Integer>(); for(int j = 0; j < a[i].length; j++) if(!one.contains(a[i][j]))p.add(a[i][j]); print(p); } } private static void print(HashSet<Integer> x) { System.out.print(x.size()); for(int i : x) System.out.print(" " + i); System.out.println(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
74b95879657e3b2a95d5455cc40a10f0
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Sets { public static void main(String args[]) { new Sets(); } Set<Integer> intersection(Set<Integer> a, Set<Integer> b) { Set<Integer> ans = new TreeSet<Integer>(); for (Integer i : a) if (b.contains(i)) ans.add(i); return ans; } Set<Integer> reduce(Set<Integer> a, Set<Integer> b) { Set<Integer> ans = new TreeSet<Integer>(); for (Integer i : a) if (!b.contains(i)) ans.add(i); return ans; } void printSet(Set<Integer> set) { System.out.print(set.size()); for (Integer s : set) { System.out.print(" " + s); } System.out.println(); } public Sets() { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int count = (N * (N - 1)) / 2; TreeSet<Integer> sets[] = new TreeSet[count]; for (int i = 0; i < count; i++) { sets[i] = new TreeSet<Integer>(); int c = scanner.nextInt(); for (int j = 0; j < c; j++) { int t = scanner.nextInt(); sets[i].add(t); } } if (N == 2) { Integer first = sets[0].first(); sets[0].remove(first); System.out.println(1 + " " + first); printSet(sets[0]); return; } int someNumber = sets[0].first(); int i1 = 0, i2 = -1; for (int i = 1; i < count; i++) if (sets[i].contains(someNumber)) { i2 = i; break; } Set<Integer> set = intersection(sets[i1], sets[i2]); printSet(set); for (int i = 0; i < count; i++) { if (sets[i].contains(someNumber)) printSet(reduce(sets[i], set)); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
cf6cee347b456770047d1e3020b5bc89
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import static java.util.Arrays.*; import static java.lang.Math.*; import static java.math.BigInteger.*; import java.util.*; import java.math.*; import java.io.*; public class B implements Runnable { String file = "input"; boolean TEST = false; int MAX = 200; void solve() throws IOException { int[][] deg = new int[201][201]; int n = nextInt(); boolean[] has = new boolean[201]; for(int i = 0; i < n * (n - 1) / 2; i++) { int k = nextInt(); int[] val = new int[k]; for(int j = 0; j < k; j++) { val[j] = nextInt(); has[val[j]] = true; } for(int j = 0; j < k; j++) for(int h = j + 1; h < k; h++) { deg[val[j]][val[h]]++; deg[val[h]][val[j]]++; } } if(n == 2) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 1; i <= 200; i++) if(has[i]) list.add(i); out.print((list.size() - 1) + " "); for(int i = 0; i < list.size() - 1; i++) out.print(list.get(i) + " "); out.println(); out.println(1 + " " + list.get(list.size() - 1)); return; } boolean[] done = new boolean[201]; for(int i = 1; i <= 200; i++) if(has[i] && !done[i]) { LinkedList<Integer> list = new LinkedList<Integer>(); list.add(i); done[i] = true; for(int j = i + 1; j <= 200; j++) if(has[j] && !done[j] && deg[i][j] == n - 1) { done[j] = true; list.add(j); } out.print(list.size() + " "); for(int x : list) out.print(x + " "); out.println(); } } String next() throws IOException { while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } void print(Object... o) { System.out.println(deepToString(o)); } void gcj(Object o) { String s = String.valueOf(o); out.println("Case #" + test + ": " + s); System.out.println("Case #" + test + ": " + s); } BufferedReader input; PrintWriter out; StringTokenizer st; int test; void init() throws IOException { if(TEST) input = new BufferedReader(new FileReader(file + ".in")); else input = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedOutputStream(System.out)); } public static void main(String[] args) throws IOException { new Thread(null, new B(), "", 1 << 20).start(); } public void run() { try { init(); if(TEST) { int runs = nextInt(); for(int i = 0; i < runs; i++) solve(); } else solve(); out.close(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
2a7604fe6001fd482c57985e7cec8608
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class LOL { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[][] vals = new int[N*(N-1)/2][]; int[] map = new int[201]; Arrays.fill(map,-1); int[] mapBack = new int[201]; Arrays.fill(mapBack,-1); int c=0; for (int i=0; i<vals.length; i++) { vals[i] = new int[in.nextInt()]; for (int j=0; j<vals[i].length; j++) { int tmp = in.nextInt(); if (map[tmp] == -1) { map[tmp] = c; mapBack[c] = tmp; c++; } vals[i][j] = map[tmp]; } } int[][] isInSet = new int[c][c]; for (int i=0; i<vals.length; i++) { boolean[] seen = new boolean[c]; for (int j=0; j<vals[i].length; j++) seen[vals[i][j]] = true; for (int j=0; j<vals[i].length; j++) for (int k=0; k<c; k++) if (seen[k] && isInSet[vals[i][j]][k] == 0) isInSet[vals[i][j]][k] = 1; else if (!seen[k]) isInSet[vals[i][j]][k] = -1; } boolean[] done = new boolean[c]; int used = 0; int ansCount = 0; for (int i=0; i<c; i++) { if (done[i]) continue; ArrayList<Integer> list = new ArrayList<Integer>(); for (int j=i; j<c && c-used >= N-ansCount; j++) if (isInSet[i][j] == 1) { list.add(mapBack[j]); done[j] = true; used++; } Collections.sort(list); System.out.print(list.size()); for (int j=0; j<list.size(); j++) System.out.printf(" %d",list.get(j)); System.out.println(); ansCount++; } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
50b9366ac0ab7d7bc0964b9251199ea4
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); if(N==2){ int x = sc.nextInt(); System.out.println("1 "+sc.nextInt()); System.out.print(x-1); StringBuilder SB = new StringBuilder(); for(int qq=0;qq<x-1;qq++){ System.out.print(" "+ sc.nextInt()); } System.exit(0); } int lines = N*(N-1)/2; boolean[][] massive = new boolean[lines][200]; boolean[][] ans = new boolean[N][200]; boolean[] did = new boolean[200]; for(int i=0;i<lines;i++){ int set=sc.nextInt(); for(int s=0;s<set;s++){ massive[i][sc.nextInt()-1]=true; } } int done=0; int line=0; while(done<N){ // System.out.println(done); for(int i=0;i<200;i++){ if(massive[line][i]&&!did[i]){ for(int j=0;j<200;j++){ if(massive[line][j])ans[done][j]=true; } for(int j=0;j<lines;j++){ if(massive[j][i]){ for(int k=0;k<200;k++){ if(ans[done][k]&&!massive[j][k]){ans[done][k]=false;} } } } for(int k=0;k<200;k++){ if(ans[done][k]){did[k]=true; // System.out.print(k+ " "); } } done++; line=-1; break; } } line=(line+1)%lines; } for(int i=0;i<N;i++){ int things=0; StringBuilder SB = new StringBuilder(); for(int j=0;j<200;j++){ if(ans[i][j]){ SB.append(" "+(j+1)); things++;} } System.out.println(things+SB.toString()); } System.exit(0); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
29a18c63a59c61a4710ea1e45ab1f466
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class problem82B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<ArrayList<Integer>> pages = new ArrayList<ArrayList<Integer>>(); int i; int j; int k; int m = n * (n - 1) / 2; for (i = 0; i < m; i++) { ArrayList<Integer> page = new ArrayList<Integer>(); k = in.nextInt(); for (j = 0; j < k; j++) page.add(in.nextInt()); pages.add(page); } in.close(); if (n == 2) { System.out.println("1 " + pages.get(0).get(0).toString()); System.out.print(pages.get(0).size() - 1); for (i = 1; i < pages.get(0).size(); i++) System.out.print(" " + pages.get(0).get(i).toString()); return; } k = pages.get(0).get(0); //System.out.println(pages.size()); Iterator iter = pages.iterator(); while (iter.hasNext()) if (! ((ArrayList<Integer>) iter.next()).contains(k)) iter.remove(); //System.out.println(pages.size()); HashSet<Integer> firstSet = new HashSet<Integer>(pages.get(0)); firstSet.retainAll(pages.get(1)); ArrayList<ArrayList<Integer>> sets = new ArrayList<ArrayList<Integer>>(); sets.add(new ArrayList<Integer>()); iter = firstSet.iterator(); while (iter.hasNext()) sets.get(0).add((Integer) iter.next()); iter = pages.iterator(); while (iter.hasNext()) { ArrayList<Integer> array = new ArrayList<Integer>(); for (Integer setItem : ((ArrayList<Integer>) iter.next())) if (!sets.get(0).contains(setItem.intValue())) array.add(setItem); sets.add(array); } for (ArrayList<Integer> set : sets) { System.out.print(set.size()); for (Integer setItem : set) System.out.print(" " + setItem.toString()); System.out.println(); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
b03c644c7e9d5c0d2a63b3fe21a46927
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
/* * Hello! You are trying to hack my solution, are you? =) * Don't be afraid of the size, it's just a dump of useful methods like gcd, or n-th Fib number. * And I'm just too lazy to create a new .java for every task. * And if you were successful to hack my solution, please, send me this test as a message or to Abrackadabraa@gmail.com. * It can help me improve my skills and i'd be very grateful for that. * Sorry for time you spent reading this message. =) * Good luck, unknown rival. =) * */ import java.io.*; import java.math.*; import java.util.*; public class Abra { // double d = 2.2250738585072012e-308; void solve() throws IOException { int n = nextInt(); if (n == 2) { int t = nextInt(); int[] s = new int[t]; Vector<Integer> a = new Vector<Integer>(); Vector<Integer> b = new Vector<Integer>(); for (int i = 0; i < t; i++) { s[i] = nextInt(); } Arrays.sort(s); int p = -1; boolean aa = false; for (int i = 0; i < t; i++) { if (s[i] != p) { p = s[i]; aa = !aa; } if (aa) a.add(s[i]); else b.add(s[i]); } out.print(a.size() + " "); for (Integer w : a) out.print(w + " "); out.println(); out.print(b.size() + " "); for (Integer w : b) out.print(w + " "); out.println(); return; } int[] a = new int[201]; int[] rep = new int[201]; TreeSet<Integer>[] tr = new TreeSet[n * (n + 1) / 2]; TreeSet<Integer>[] r = new TreeSet[n * (n + 1) / 2]; int ri = 0; for (int i = 0; i < n * (n - 1) / 2; i++) { int t = nextInt(); tr[i] = new TreeSet<Integer>(); for (int j = 0; j < t; j++) { int q = nextInt(); tr[i].add(q); if (rep[q] <= 0) rep[q]--; } for (int j = 0; j <= 200; j++) rep[j] = Math.abs(rep[j]); } Arrays.fill(a, -1); for (int i = 1; i <= 200; i++) { if (a[i] != -1) continue; TreeSet<Integer> f = null, s = null; for (int j = 0; j < n * (n - 1) / 2; j++) { if (tr[j].contains(i)) { if (f == null) f = tr[j]; else { s = tr[j]; break; } } } if (f == null) continue; TreeSet<Integer> re = new TreeSet<Integer>(); for (Integer t : f) { if (s.contains(t)) { re.add(t); a[t] = ri; } } r[ri] = re; ri++; } for (int i = 0; i < ri; i++) { out.print(r[i].size() + " "); for (Integer t : r[i]) { for (int j = 0; j < rep[t]; j++) out.print(t + " "); } out.println(); } } public static void main(String[] args) throws IOException { new Abra().run(); } StreamTokenizer in; PrintWriter out; boolean oj; BufferedReader br; void init() throws IOException { oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); br = new BufferedReader(reader); in = new StreamTokenizer(br); out = new PrintWriter(writer); } long beginTime; void run() throws IOException { beginTime = System.currentTimeMillis(); long beginMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); init(); solve(); long endMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long endTime = System.currentTimeMillis(); if (!oj) { System.out.println("Memory used = " + (endMem - beginMem)); System.out.println("Total memory = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); System.out.println("Running time = " + (endTime - beginTime)); } out.flush(); } int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } long nextLong() throws IOException { in.nextToken(); return (long) in.nval; } String nextString() throws IOException { in.nextToken(); return in.sval; } double nextDouble() throws IOException { in.nextToken(); return in.nval; } myLib lib = new myLib(); void time() { System.out.print("It's "); System.out.println(System.currentTimeMillis() - beginTime); } static class myLib { long fact(long x) { long a = 1; for (long i = 2; i <= x; i++) { a *= i; } return a; } long digitSum(String x) { long a = 0; for (int i = 0; i < x.length(); i++) { a += x.charAt(i) - '0'; } return a; } long digitSum(long x) { long a = 0; while (x > 0) { a += x % 10; x /= 10; } return a; } long digitMul(long x) { long a = 1; while (x > 0) { a *= x % 10; x /= 10; } return a; } int digitCubesSum(int x) { int a = 0; while (x > 0) { a += (x % 10) * (x % 10) * (x % 10); x /= 10; } return a; } double pif(double ax, double ay, double bx, double by) { return Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); } double pif3D(double ax, double ay, double az, double bx, double by, double bz) { return Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by) + (az - bz) * (az - bz)); } double pif3D(double[] a, double[] b) { return Math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2])); } long gcd(long a, long b) { if (a == 0 || b == 0) return 1; if (a < b) { long c = b; b = a; a = c; } while (a % b != 0) { a = a % b; if (a < b) { long c = b; b = a; a = c; } } return b; } int gcd(int a, int b) { if (a == 0 || b == 0) return 1; if (a < b) { int c = b; b = a; a = c; } while (a % b != 0) { a = a % b; if (a < b) { int c = b; b = a; a = c; } } return b; } long lcm(long a, long b) { return a * b / gcd(a, b); } int lcm(int a, int b) { return a * b / gcd(a, b); } int countOccurences(String x, String y) { int a = 0, i = 0; while (true) { i = y.indexOf(x); if (i == -1) break; a++; y = y.substring(i + 1); } return a; } int[] findPrimes(int x) { boolean[] forErato = new boolean[x - 1]; List<Integer> t = new Vector<Integer>(); int l = 0, j = 0; for (int i = 2; i < x; i++) { if (forErato[i - 2]) continue; t.add(i); l++; j = i * 2; while (j < x) { forErato[j - 2] = true; j += i; } } int[] primes = new int[l]; Iterator<Integer> iterator = t.iterator(); for (int i = 0; iterator.hasNext(); i++) { primes[i] = iterator.next().intValue(); } return primes; } int rev(int x) { int a = 0; while (x > 0) { a = a * 10 + x % 10; x /= 10; } return a; } class myDate { int d, m, y; int[] ml = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public myDate(int da, int ma, int ya) { d = da; m = ma; y = ya; if ((ma > 12 || ma < 1 || da > ml[ma - 1] || da < 1) && !(d == 29 && m == 2 && y % 4 == 0)) { d = 1; m = 1; y = 9999999; } } void incYear(int x) { for (int i = 0; i < x; i++) { y++; if (m == 2 && d == 29) { m = 3; d = 1; return; } if (m == 3 && d == 1) { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { m = 2; d = 29; } return; } } } boolean less(myDate x) { if (y < x.y) return true; if (y > x.y) return false; if (m < x.m) return true; if (m > x.m) return false; if (d < x.d) return true; if (d > x.d) return false; return true; } void inc() { if ((d == 31) && (m == 12)) { y++; d = 1; m = 1; } else { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { ml[1] = 29; } if (d == ml[m - 1]) { m++; d = 1; } else d++; } } } int partition(int n, int l, int m) {// n - sum, l - length, m - every // part // <= m if (n < l) return 0; if (n < l + 2) return 1; if (l == 1) return 1; int c = 0; for (int i = Math.min(n - l + 1, m); i >= (n + l - 1) / l; i--) { c += partition(n - i, l - 1, i); } return c; } int rifmQuality(String a, String b) { if (a.length() > b.length()) { String c = a; a = b; b = c; } int c = 0, d = b.length() - a.length(); for (int i = a.length() - 1; i >= 0; i--) { if (a.charAt(i) == b.charAt(i + d)) c++; else break; } return c; } String numSym = "0123456789ABCDEF"; String ZFromXToYNotation(int x, int y, String z) { if (z.equals("0")) return "0"; String a = ""; // long q = 0, t = 1; BigInteger q = BigInteger.ZERO, t = BigInteger.ONE; for (int i = z.length() - 1; i >= 0; i--) { q = q.add(t.multiply(BigInteger.valueOf(z.charAt(i) - 48))); t = t.multiply(BigInteger.valueOf(x)); } while (q.compareTo(BigInteger.ZERO) == 1) { a = numSym.charAt((int) (q.mod(BigInteger.valueOf(y)).intValue())) + a; q = q.divide(BigInteger.valueOf(y)); } return a; } double angleFromXY(int x, int y) { if ((x == 0) && (y > 0)) return Math.PI / 2; if ((x == 0) && (y < 0)) return -Math.PI / 2; if ((y == 0) && (x > 0)) return 0; if ((y == 0) && (x < 0)) return Math.PI; if (x > 0) return Math.atan((double) y / x); else { if (y > 0) return Math.atan((double) y / x) + Math.PI; else return Math.atan((double) y / x) - Math.PI; } } static boolean isNumber(String x) { try { Integer.parseInt(x); } catch (NumberFormatException ex) { return false; } return true; } static boolean stringContainsOf(String x, String c) { for (int i = 0; i < x.length(); i++) { if (c.indexOf(x.charAt(i)) == -1) return false; } return true; } long pow(long a, long n) { // b > 0 if (n == 0) return 1; long k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } int pow(int a, int n) { // b > 0 if (n == 0) return 1; int k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } double pow(double a, int n) { // b > 0 if (n == 0) return 1; double k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } double log2(double x) { return Math.log(x) / Math.log(2); } int lpd(int[] primes, int x) {// least prime divisor int i; for (i = 0; primes[i] <= x / 2; i++) { if (x % primes[i] == 0) { return primes[i]; } } ; return x; } int np(int[] primes, int x) {// number of prime number for (int i = 0; true; i++) { if (primes[i] == x) return i; } } int[] dijkstra(int[][] map, int n, int s) { int[] p = new int[n]; boolean[] b = new boolean[n]; Arrays.fill(p, Integer.MAX_VALUE); p[s] = 0; b[s] = true; for (int i = 0; i < n; i++) { if (i != s) p[i] = map[s][i]; } while (true) { int m = Integer.MAX_VALUE, mi = -1; for (int i = 0; i < n; i++) { if (!b[i] && (p[i] < m)) { mi = i; m = p[i]; } } if (mi == -1) break; b[mi] = true; for (int i = 0; i < n; i++) if (p[mi] + map[mi][i] < p[i]) p[i] = p[mi] + map[mi][i]; } return p; } boolean isLatinChar(char x) { if (((x >= 'a') && (x <= 'z')) || ((x >= 'A') && (x <= 'Z'))) return true; else return false; } boolean isBigLatinChar(char x) { if (x >= 'A' && x <= 'Z') return true; else return false; } boolean isSmallLatinChar(char x) { if (x >= 'a' && x <= 'z') return true; else return false; } boolean isDigitChar(char x) { if (x >= '0' && x <= '9') return true; else return false; } class NotANumberException extends Exception { private static final long serialVersionUID = 1L; String mistake; NotANumberException() { mistake = "Unknown."; } NotANumberException(String message) { mistake = message; } } class Real { String num = "0"; long exp = 0; boolean pos = true; long length() { return num.length(); } void check(String x) throws NotANumberException { if (!stringContainsOf(x, "0123456789+-.eE")) throw new NotANumberException("Illegal character."); long j = 0; for (long i = 0; i < x.length(); i++) { if ((x.charAt((int) i) == '-') || (x.charAt((int) i) == '+')) { if (j == 0) j = 1; else if (j == 5) j = 6; else throw new NotANumberException("Unexpected sign."); } else if ("0123456789".indexOf(x.charAt((int) i)) != -1) { if (j == 0) j = 2; else if (j == 1) j = 2; else if (j == 2) ; else if (j == 3) j = 4; else if (j == 4) ; else if (j == 5) j = 6; else if (j == 6) ; else throw new NotANumberException("Unexpected digit."); } else if (x.charAt((int) i) == '.') { if (j == 0) j = 3; else if (j == 1) j = 3; else if (j == 2) j = 3; else throw new NotANumberException("Unexpected dot."); } else if ((x.charAt((int) i) == 'e') || (x.charAt((int) i) == 'E')) { if (j == 2) j = 5; else if (j == 4) j = 5; else throw new NotANumberException("Unexpected exponent."); } else throw new NotANumberException("O_o."); } if ((j == 0) || (j == 1) || (j == 3) || (j == 5)) throw new NotANumberException("Unexpected end."); } public Real(String x) throws NotANumberException { check(x); if (x.charAt(0) == '-') pos = false; long j = 0; String e = ""; boolean epos = true; for (long i = 0; i < x.length(); i++) { if ("0123456789".indexOf(x.charAt((int) i)) != -1) { if (j == 0) num += x.charAt((int) i); if (j == 1) { num += x.charAt((int) i); exp--; } if (j == 2) e += x.charAt((int) i); } if (x.charAt((int) i) == '.') { if (j == 0) j = 1; } if ((x.charAt((int) i) == 'e') || (x.charAt((int) i) == 'E')) { j = 2; if (x.charAt((int) (i + 1)) == '-') epos = false; } } while ((num.length() > 1) && (num.charAt(0) == '0')) num = num.substring(1); while ((num.length() > 1) && (num.charAt(num.length() - 1) == '0')) { num = num.substring(0, num.length() - 1); exp++; } if (num.equals("0")) { exp = 0; pos = true; return; } while ((e.length() > 1) && (e.charAt(0) == '0')) e = e.substring(1); try { if (e != "") if (epos) exp += Long.parseLong(e); else exp -= Long.parseLong(e); } catch (NumberFormatException exc) { if (!epos) { num = "0"; exp = 0; pos = true; } else { throw new NotANumberException("Too long exponent"); } } } public Real() { } String toString(long mantissa) { String a = "", b = ""; if (exp >= 0) { a = num; if (!pos) a = '-' + a; for (long i = 0; i < exp; i++) a += '0'; for (long i = 0; i < mantissa; i++) b += '0'; if (mantissa == 0) return a; else return a + "." + b; } else { if (exp + length() <= 0) { a = "0"; if (mantissa == 0) { return a; } if (mantissa < -(exp + length() - 1)) { for (long i = 0; i < mantissa; i++) b += '0'; return a + "." + b; } else { if (!pos) a = '-' + a; for (long i = 0; i < mantissa; i++) if (i < -(exp + length())) b += '0'; else if (i + exp >= 0) b += '0'; else b += num.charAt((int) (i + exp + length())); return a + "." + b; } } else { if (!pos) a = "-"; for (long i = 0; i < exp + length(); i++) a += num.charAt((int) i); if (mantissa == 0) return a; for (long i = exp + length(); i < exp + length() + mantissa; i++) if (i < length()) b += num.charAt((int) i); else b += '0'; return a + "." + b; } } } } boolean containsRepeats(int... num) { Set<Integer> s = new TreeSet<Integer>(); for (int d : num) if (!s.contains(d)) s.add(d); else return true; return false; } int[] rotateDice(int[] a, int n) { int[] c = new int[6]; if (n == 0) { c[0] = a[1]; c[1] = a[5]; c[2] = a[2]; c[3] = a[0]; c[4] = a[4]; c[5] = a[3]; } if (n == 1) { c[0] = a[2]; c[1] = a[1]; c[2] = a[5]; c[3] = a[3]; c[4] = a[0]; c[5] = a[4]; } if (n == 2) { c[0] = a[3]; c[1] = a[0]; c[2] = a[2]; c[3] = a[5]; c[4] = a[4]; c[5] = a[1]; } if (n == 3) { c[0] = a[4]; c[1] = a[1]; c[2] = a[0]; c[3] = a[3]; c[4] = a[5]; c[5] = a[2]; } if (n == 4) { c[0] = a[0]; c[1] = a[2]; c[2] = a[3]; c[3] = a[4]; c[4] = a[1]; c[5] = a[5]; } if (n == 5) { c[0] = a[0]; c[1] = a[4]; c[2] = a[1]; c[3] = a[2]; c[4] = a[3]; c[5] = a[5]; } return c; } int min(int... a) { int c = Integer.MAX_VALUE; for (int d : a) if (d < c) c = d; return c; } int max(int... a) { int c = Integer.MIN_VALUE; for (int d : a) if (d > c) c = d; return c; } int pos(int x) { if (x > 0) return x; else return 0; } long pos(long x) { if (x > 0) return x; else return 0; } double maxD(double... a) { double c = Double.MIN_VALUE; for (double d : a) if (d > c) c = d; return c; } double minD(double... a) { double c = Double.MAX_VALUE; for (double d : a) if (d < c) c = d; return c; } int[] normalizeDice(int[] a) { int[] c = a.clone(); if (c[0] != 0) if (c[1] == 0) c = rotateDice(c, 0); else if (c[2] == 0) c = rotateDice(c, 1); else if (c[3] == 0) c = rotateDice(c, 2); else if (c[4] == 0) c = rotateDice(c, 3); else if (c[5] == 0) c = rotateDice(rotateDice(c, 0), 0); while (c[1] != min(c[1], c[2], c[3], c[4])) c = rotateDice(c, 4); return c; } boolean sameDice(int[] a, int[] b) { for (int i = 0; i < 6; i++) if (a[i] != b[i]) return false; return true; } final double goldenRatio = (1 + Math.sqrt(5)) / 2; final double aGoldenRatio = (1 - Math.sqrt(5)) / 2; long Fib(int n) { if (n < 0) if (n % 2 == 0) return -Math.round((pow(goldenRatio, -n) - pow(aGoldenRatio, -n)) / Math.sqrt(5)); else return -Math.round((pow(goldenRatio, -n) - pow(aGoldenRatio, -n)) / Math.sqrt(5)); return Math.round((pow(goldenRatio, n) - pow(aGoldenRatio, n)) / Math.sqrt(5)); } class japaneeseComparator implements Comparator<String> { @Override public int compare(String a, String b) { int ai = 0, bi = 0; boolean m = false, ns = false; if (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') { if (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') m = true; else return -1; } if (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') { if (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') m = true; else return 1; } a += "!"; b += "!"; int na = 0, nb = 0; while (true) { if (a.charAt(ai) == '!') { if (b.charAt(bi) == '!') break; return -1; } if (b.charAt(bi) == '!') { return 1; } if (m) { int ab = -1, bb = -1; while (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') { if (ab == -1) ab = ai; ai++; } while (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') { if (bb == -1) bb = bi; bi++; } m = !m; if (ab == -1) { if (bb == -1) continue; else return 1; } if (bb == -1) return -1; while (a.charAt(ab) == '0' && ab + 1 != ai) { ab++; if (!ns) na++; } while (b.charAt(bb) == '0' && bb + 1 != bi) { bb++; if (!ns) nb++; } if (na != nb) ns = true; if (ai - ab < bi - bb) return -1; if (ai - ab > bi - bb) return 1; for (int i = 0; i < ai - ab; i++) { if (a.charAt(ab + i) < b.charAt(bb + i)) return -1; if (a.charAt(ab + i) > b.charAt(bb + i)) return 1; } } else { m = !m; while (true) { if (a.charAt(ai) <= 'z' && a.charAt(ai) >= 'a' && b.charAt(bi) <= 'z' && b.charAt(bi) >= 'a') { if (a.charAt(ai) < b.charAt(bi)) return -1; if (a.charAt(ai) > b.charAt(bi)) return 1; ai++; bi++; } else if (a.charAt(ai) <= 'z' && a.charAt(ai) >= 'a') return 1; else if (b.charAt(bi) <= 'z' && b.charAt(bi) >= 'a') return -1; else break; } } } if (na < nb) return 1; if (na > nb) return -1; return 0; } } Random random = new Random(); } void readIntArray(int[] a) throws IOException { for (int i = 0; i < a.length; i++) a[i] = nextInt(); } String readChars(int l) throws IOException { String r = ""; for (int i = 0; i < l; i++) r += (char) br.read(); return r; } class myFraction { long num = 0, den = 1; void reduce() { long d = lib.gcd(num, den); num /= d; den /= d; } myFraction(long ch, long zn) { num = ch; den = zn; reduce(); } myFraction add(myFraction t) { long nd = lib.lcm(den, t.den); myFraction r = new myFraction(nd / den * num + nd / t.den * t.num, nd); r.reduce(); return r; } public String toString() { return num + "/" + den; } int lev(String s1, String s2) { int[][] distance = new int[s1.length() + 1][s2.length() + 1]; for (int i = 0; i <= s1.length(); i++) distance[i][0] = i; for (int j = 0; j <= s2.length(); j++) distance[0][j] = j; for (int i = 1; i <= s1.length(); i++) for (int j = 1; j <= s2.length(); j++) distance[i][j] = Math.min(Math.min(distance[i - 1][j] + 1, distance[i][j - 1] + 1), distance[i - 1][j - 1] + ((s1.charAt(i - 1) == s2.charAt(j - 1)) ? 0 : 1)); return distance[s1.length()][s2.length()]; } } class myPoint { myPoint(int a, int b) { x = a; y = b; } int x, y; boolean equals(myPoint a) { if (x == a.x && y == a.y) return true; else return false; } public String toString() { return x + ":" + y; } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
b726b5d21fac79c2d85a253bb0d915b5
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.awt.geom.Point2D; import java.text.*; import java.math.*; import java.util.*; public class Main implements Runnable { final String filename = ""; public void solve() throws Exception { int MAX = 201; int n = iread(); int m = n * (n - 1) / 2; int[][] a = new int[m][MAX]; int[] c = new int[m]; int[] w1 = new int[MAX]; int[] w2 = new int[MAX]; int[][] ans = new int[n][MAX]; int[] k = new int[n]; Arrays.fill(w1, -1); Arrays.fill(w2, -1); for (int i = 0; i < m; i++) { c[i] = iread(); for (int j = 0; j < c[i]; j++) { a[i][j] = iread(); if (w1[a[i][j]] == -1) w1[a[i][j]] = i; else if (w2[a[i][j]] == -1) w2[a[i][j]] = i; } } if (m == 1) { out.write(c[0] / 2 + ""); for (int i = 0; i < c[0] / 2; i++) out.write(" " + a[0][i]); out.write("\n"); out.write((c[0] - c[0] / 2) + ""); for (int i = c[0] / 2; i < c[0]; i++) out.write(" " + a[0][i]); out.write("\n"); return; } int s = 0; for (int r = 0; r < MAX; r++) { if (w1[r] == -1) continue; int t = w1[r], u = w2[r]; for (int i = 0; i < c[t]; i++) for (int j = 0; j < c[u]; j++) { if (a[t][i] == a[u][j]) { ans[s][k[s]++] = a[t][i]; w1[a[t][i]] = -1; w2[a[t][i]] = -1; break; } } s++; } for (int i = 0; i < n; i++) { out.write(k[i] + ""); for (int j = 0; j < k[i]; j++) { out.write(" " + ans[i][j]); } out.write("\n"); } } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); // in = new BufferedReader(new FileReader(filename+".in")); // out = new BufferedWriter(new FileWriter(filename+".out")); solve(); out.flush(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public int iread() throws Exception { return Integer.parseInt(readword()); } public double dread() throws Exception { return Double.parseDouble(readword()); } public long lread() throws Exception { return Long.parseLong(readword()); } BufferedReader in; BufferedWriter out; public String readword() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') c = in.read(); if (c < 0) return ""; while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); } public static void main(String[] args) { try { Locale.setDefault(Locale.US); } catch (Exception e) { } // new Thread(new Main()).start(); new Thread(null, new Main(), "1", 1 << 25).start(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
759ad29af81bf8249f35ec585e17a511
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Locale; import java.util.StringTokenizer; public class B { private static final String TASKNAME = "b"; private void solve() throws Exception { int n = nextInt(); int[][] sets = new int[n * (n - 1) / 2][]; ArrayList<Integer>[] ids = new ArrayList[210]; for (int i = 0; i < ids.length; ++i) { ids[i] = new ArrayList<Integer>(); } for (int i = 0; i < sets.length; ++i) { sets[i] = new int[nextInt()]; for (int j = 0; j < sets[i].length; ++j) { int x = nextInt(); sets[i][j] = x; ids[x].add(i); } } if (n == 2) { println("1 " + sets[0][0]); print((sets[0].length - 1)); for (int i = 1; i < sets[0].length; ++i) { print(" " + sets[0][i]); } println(""); return; } boolean[] found = new boolean[210]; for (int i = 0; i < ids.length; ++i) { if (found[i] || ids[i].isEmpty()) { continue; } ArrayList<Integer> set = cross(sets[ids[i].get(0)], sets[ids[i].get(1)]); print(set.size()); for (int j : set) { found[j] = true; print(" " + j); } println(""); } } int[] mark = new int[300]; int gen = 1; private ArrayList<Integer> cross(int[] is, int[] is2) { ArrayList<Integer> ans = new ArrayList<Integer>(); for (int i : is) { mark[i] = gen; } for (int i : is2) { if (mark[i] == gen) { ans.add(i); } } ++gen; return ans; } private void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(new OutputStreamWriter(System.out)); // reader = new BufferedReader(new FileReader(TASKNAME + ".in")); // writer = new PrintWriter(new File(TASKNAME + ".out")); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(13); } } public static void main(String[] args) { long time = System.currentTimeMillis(); Locale.setDefault(Locale.US); new B().run(); System.err.printf("%.3f\n", (System.currentTimeMillis() - time) * 1e-3); } private StringTokenizer tokenizer; private PrintWriter writer; private BufferedReader reader; private String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private void print(Object o) { writer.print(o); } private void println(Object o) { writer.println(o); } private void printf(String format, Object... args) { writer.printf(format, args); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
00afd6bc2af81c8cdd9a8e9995915fae
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; public class Main { // static Scanner in; static PrintWriter out; static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;} public static void main(String[] args) throws Exception { // in = new Scanner(System.in); out = new PrintWriter(System.out); in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int n = next(); int max = 201; int k = n*(n-1)/2; int[][] set = new int[k][]; for (int i = 0; i < k; i++) { int size = next(); set[i] = new int[size]; for (int j = 0; j < size; j++) set[i][j] = next(); } if (n == 2) { out.print("1 "); out.println(set[0][0]); out.print(set[0].length - 1); for (int i = 1; i < set[0].length; i++) out.print(" " + set[0][i]); out.println(); out.close(); return; } int a = set[0][0]; boolean[] good = new boolean[k]; for (int i = 0; i < k; i++) for (int t : set[i]) good[i] |= t == a; // for (int i = 0; i < k; i++) if (good[i]) out.println(": " + i); int[] kol = new int[max]; for (int i = 0; i < k; i++) if (good[i]) for (int t : set[i]) kol[t]++; int s = 0; boolean[] set1 = new boolean[max]; for (int i = 0; i < max; i++) if (kol[i] == n - 1) { set1[i] = true; s++; } out.print(s + " "); for (int i = 0; i < max; i++) if (set1[i]) out.print(i + " "); out.println(); for (int i = 0; i < k; i++) if (good[i]) { s = 0; for (int t : set[i]) if (!set1[t]) s++; out.print(s + " "); for (int t : set[i]) if (!set1[t]) out.print(t + " "); out.println(); } out.close(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
d68b3a6b1e94a2180d4428212eefc962
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @author Alexander Grigoryev * Created on 28.07.2011 */ public class Main { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); int[][] a = new int[201][201]; int[] v = new int[200]; int[] b = new int[201]; List<Integer> s = new ArrayList<Integer>(); for(int i = 1; i <= n * (n - 1) / 2; i++) { int k = in.nextInt(); for(int j = 1; j <= k; j++) { v[j] = in.nextInt(); for(int l = 1; l < j; l++) { a[v[j]][v[l]]++; a[v[l]][v[j]]++; } if(b[v[j]] == 0) b[v[j]]++; } } if(n != 2) { for(int i = 1; i <= 200; i++) { if(b[i] == 1) { s.clear(); s.add(i); for(int j = 1; j <= 200; j++) { if(a[i][j] == n - 1) { s.add(j); b[j] = 0; } } System.out.print(s.size() + " "); for(Integer val : s) System.out.print(val + " "); System.out.println(); } } } else { int i = 1; for(; i <= 200; i++) { if(b[i] == 1) { System.out.println("1 " + i); break; } } for(int j = i + 1; j <= 200; j++) if(b[j] == 1) s.add(j); System.out.print(s.size() + " "); for(i = 0; i < s.size(); i++) System.out.print(s.get(i) + " "); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
78a935b6a3553ef7a89940a183dec2b8
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
/** * Problem: * Source: ', Round * Link: * * @author Alexei Ostrovski */ import java.io.*; import java.util.Arrays; import java.util.Scanner; public class SetPairsSolver { public static final boolean DEBUG = false; public static final int MAX_NUMBER = 205; public static void main(String[] args) { //redirect input and output if (DEBUG) { try { System.setIn(new FileInputStream("input.txt")); //System.setOut(new PrintStream("input.txt")); } catch (IOException e) { //nothing } //generate(); //return; } Scanner sc = new Scanner(System.in); //read data & solve int n = sc.nextInt(); int mapCount = 0; int[] map = new int[MAX_NUMBER]; int[] set = new int[MAX_NUMBER]; //current set int ssCount = 0; int[][] savedSet = new int[MAX_NUMBER][MAX_NUMBER]; //saved sets int[] ssPtr = new int[MAX_NUMBER]; //saved set pointer if (n > 2) { for (int s = 0; s < (n * (n - 1))/2; s++) { //read current set int nSet = sc.nextInt(); int mapped = 0, saved = 0; for (int k = 0; k < nSet; k++) { set[k] = sc.nextInt(); if (map[set[k]] > 0) mapped++; if (ssPtr[set[k]] > 0) saved++; } //System.out.println("Set #" + s + ": mapped=" + mapped + ", saved=" + saved); if (mapped == nSet) { //do nothing } else if (mapped > 0) { //save unmapped elements as a new set mapCount++; for (int k = 0; k < nSet; k++) if (map[set[k]] == 0) map[set[k]] = mapCount; //System.out.println("Set #" + s + ": " + Arrays.toString(map)); } else if (saved == 0) //save set { for (int k = 0; k < nSet; k++) { savedSet[ssCount][k] = set[k]; ssPtr[set[k]] = ssCount + 1; } ssCount++; //System.out.println("Set #" + s + " saved."); } else if (saved < nSet) { mapCount += 2; for (int k = 0; k < nSet; k++) { if (ssPtr[set[k]] > 0) map[set[k]] = mapCount - 1; else map[set[k]] = mapCount; } //System.out.println("Set #" + s + ": " + Arrays.toString(map)); } else { //saved == nSet mapCount += 2; for (int k = 0; k < nSet; k++) { if (ssPtr[set[k]] == ssPtr[set[0]]) map[set[k]] = mapCount - 1; else map[set[k]] = mapCount; } //System.out.println("Set #" + s + ": " + Arrays.toString(map)); } } } else { //n == 2; sets can be chosen arbitrarily int nSet = sc.nextInt(); for (int k = 0; k < nSet; k++) { set[k] = sc.nextInt(); map[set[k]] = 2; } map[set[0]] = 1; } //output for (int s = 1; s <= n; s++) { int nSet = 0; for (int i = 1; i < MAX_NUMBER; i++) if (map[i] == s) nSet++; System.out.print(nSet + " "); for (int i = 1; i < MAX_NUMBER; i++) if (map[i] == s) System.out.print(i + " "); System.out.println(); } } public static void generate() { int n = 200; System.out.println(n); for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) System.out.println("2 " + i + " " + j); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
9b46f2d87f212becded0f5de8d21a8a8
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main implements Runnable { String file = "template"; @SuppressWarnings("unchecked") private void solve() throws IOException { int n = nextInt(); Set<Integer>[] sets = new HashSet[(n * (n - 1) / 2)]; boolean[] was = new boolean[201]; for (int i = 0; i < n * (n - 1) / 2; ++i) { sets[i] = new HashSet<Integer>(); int k = nextInt(); for (int j = 0; j < k; ++j) { sets[i].add(nextInt()); } } if (n > 2) { for (int i = 0; i < n * (n - 1) / 2; ++i) { for (int e : sets[i]) { if (!was[e]) { was[e] = true; for (int j = i + 1; j < n * (n - 1) / 2; ++j) { if (sets[j].contains(e)) { HashSet<Integer> as = new HashSet<Integer>( sets[i]); as.retainAll(sets[j]); out.print(as.size() + " "); for (Integer integer : as) { was[integer] = true; out.print(integer + " "); } out.println(); break; } } } } } } else { ArrayList<Integer> list = new ArrayList<Integer>(); list.addAll(sets[0]); out.println(1 + " " + list.get(0)); out.print(list.size() - 1 + " "); for (int i =1; i < list.size(); ++i) { out.print(list.get(i) + " "); } out.println(); } } public static void main(String[] args) { new Thread(new Main()).start(); } StringTokenizer tok; BufferedReader br; PrintWriter out; @Override public void run() { try { if (Boolean.getBoolean("SpbAU")) { br = new BufferedReader(new FileReader(file + ".in")); out = new PrintWriter(System.out); } else { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } tok = new StringTokenizer(""); while (hasNext()) { solve(); } out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } boolean hasNext() throws IOException { while (!tok.hasMoreElements()) { String line = br.readLine(); if (line == null) { return false; } tok = new StringTokenizer(line); } return true; } String next() throws IOException { if (hasNext()) { return tok.nextToken(); } throw new IOException("No more tokens"); } String nextLine() throws IOException { if (hasNext()) { return tok.nextToken("\n"); } throw new IOException("No more tokens"); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
ba3a6c5158296533813d7fd3abc4501c
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.*; import java.io.*; public class a { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(); int[][] adj = new int[201][201]; boolean[] has = new boolean[201]; for(int i = 0; i<n*(n-1)/2; i++) { int k = input.nextInt(); int[] data = new int[k]; for(int j = 0; j<k; j++) { data[j] = input.nextInt(); has[data[j]] = true; } for(int j = 0; j<k; j++) for(int m = j+1; m<k; m++) { adj[data[j]][data[m]]++; adj[data[m]][data[j]]++; } } ArrayList<Integer>[] res = new ArrayList[n]; for(int i = 0; i<n; i++) res[i] = new ArrayList<Integer>(); int at = 0; for(int i = 1; i<=200; i++) { if(!has[i]) continue; has[i] = false; res[at].add(i); for(int j = 1; j<=200; j++) { if(adj[i][j] > 1 && has[j]) { has[j] = false; res[at].add(j); } } if(at != n-1) at++; } for(int i =0; i<n; i++) { out.print(res[i].size()); for(int j = 0; j<res[i].size(); j++) out.print(" "+res[i].get(j)); out.println(); } out.close(); } static class Soldier implements Comparable<Soldier> { int b, i; public Soldier(int bb, int ii) { b = bb; i = ii; } @Override public int compareTo(Soldier that) { // TODO Auto-generated method stub return that.b - this.b; } } public static 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 int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } } static class IT { int[] left,right, val, a, b; IT(int n) { left = new int[4*n]; right = new int[4*n]; val = new int[4*n]; a = new int[4*n]; b = new int[4*n]; init(0,0, n); } int init(int at, int l, int r) { a[at] = l; b[at] = r; if(l==r) left[at] = right [at] = -1; else { int mid = (l+r)/2; left[at] = init(2*at+1,l,mid); right[at] = init(2*at+2,mid+1,r); } return at++; } //return the sum over [x,y] int get(int x, int y) { return go(x,y, 0); } int go(int x,int y, int at) { if(at==-1) return 0; if(x <= a[at] && y>= b[at]) return val[at]; if(y<a[at] || x>b[at]) return 0; return go(x, y, left[at]) + go(x, y, right[at]); } //add v to elements x through y void add(int x, int y, int v) { go3(x, y, v, 0); } void go3(int x, int y, int v, int at) { if(at==-1) return; if(y < a[at] || x > b[at]) return; val[at] += (Math.min(b[at], y) - Math.max(a[at], x) + 1)*v; go3(x, y, v, left[at]); go3(x, y, v, right[at]); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
eda5000dd56b63c7157124d59b0af14c
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; public class Test implements Runnable { private void solve() throws IOException { Vector<Vector<Integer>> v = new Vector<Vector<Integer>>(); for (int i = 0; i < 201; i++) { v.add(new Vector<Integer>()); } Vector<Integer> vi = new Vector<Integer>(); for (int i = 0; i < 201; i++) { vi.add(0); } Vector<Integer> used = new Vector<Integer>(); for (int i = 0; i < 201; i++) { used.add(0); } Vector<Vector<Integer>> res = new Vector<Vector<Integer>>(); Vector<Integer> set; int n; n = nextInt(); int num = n * (n - 1) / 2; int count, curr; if (n == 2) { int a; Vector<Integer> vect = new Vector<Integer>(); count = nextInt(); a=nextInt(); for (int j = 1; j < count; j++) { curr = nextInt(); vect.add(curr); } writer.print(1+" "); writer.println(a); writer.print(vect.size()+" "); for(int j=0;j<vect.size();j++) { writer.print(vect.get(j)+" "); } writer.println(); return; } for (int i = 0; i < num; i++) { count = nextInt(); for (int j = 0; j < count; j++) { curr = nextInt(); v.get(curr).add(i); } } // MT timer = new MT(); for (int i = 0; i < 201; i++) { Collections.sort(v.get(i)); } for (int i = 1; i < 201; i++) { vi.set(i, v.get(i).hashCode()); } /* * for (int i = 0; i < 201; i++) { writer.print(vi.get(i) + " "); for * (Integer integ : v.get(i)) { writer.print(integ + " "); } * writer.println(); } */ for (int i = 1; i < 201; i++) { if (v.get(i).size() != 0 && used.get(i) == 0) { set = new Vector<Integer>(); set.add(i); for (int j = i + 1; j < 201; j++) { if (vi.get(i).equals(vi.get(j))) { set.add(j); used.set(j, 1); } } res.add(set); } used.set(i, 1); } // writer.println(res.size()); for (int i = 0; i < res.size(); i++) { writer.print(res.get(i).size() + " "); for (int j = 0; j < res.get(i).size(); j++) { writer.print(res.get(i).get(j) + " "); } writer.println(); } // timer.showTime(); } public static void main(String[] args) { new Test().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public class MT { long start; MT() { start = System.currentTimeMillis(); } public void showTime() { System.out.println("Time = " + (System.currentTimeMillis() - start) + " ms"); } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
69a2fdd1e87f089ba774a0cdfbd3a92d
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.*; public class Main implements Runnable { String join(List<Integer> lst) { StringBuilder sb = new StringBuilder(); if (lst.size() > 0) { sb.append(lst.get(0)); for(int i = 1; i < lst.size(); ++i) { sb.append(" "); sb.append(lst.get(i)); } } return sb.toString(); } public void solve() throws IOException { int n = nextInt(); if (n == 2) { int q = nextInt(); if (q < 2) throw new RuntimeException("!"); int[] tmp = new int[q]; for(int i = 0; i < q; ++i) tmp[i] = nextInt(); pw.printf("%d %d\n%d", 1, tmp[0], tmp.length - 1); for(int i = 1; i < tmp.length; ++i) pw.printf(" %d", tmp[i]); pw.println(); return; } List<Integer>[] where = new List[205]; for(int i = 0; i < where.length; ++i) where[i] = new ArrayList<Integer>(); for(int t = 0; t < n * (n - 1) / 2; ++t) { int q = nextInt(); for(int j = 0; j < q; ++j) { int p = nextInt(); where[p].add(t); } } for(int i = 0; i < where.length; ++i) Collections.sort(where[i]); Map<String, List<Integer>> map = new HashMap<String, List<Integer>>(); for(int i = 0; i < where.length; ++i) if (where[i].size() > 0) { String temp = join(where[i]); if (!map.containsKey(temp)) map.put(temp, new ArrayList<Integer>()); map.get(temp).add(i); } int q = 0; for(List<Integer> lst : map.values()) { if (lst.size() > 0) { pw.printf("%d", lst.size()); for(int a : lst) { pw.printf(" %d", a); } q++; pw.println(); } } if (q != n) throw new RuntimeException("!"); } static final String filename = "A"; static final boolean fromConsole = true; public void run() { try { if (!fromConsole) { in = new BufferedReader(new FileReader(filename + ".in")); pw = new PrintWriter(filename + ".out"); } else { in = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); } st = new StringTokenizer(""); long st = System.currentTimeMillis(); solve(); //pw.printf("\nWorking time: %d ms\n", System.currentTimeMillis() - st); pw.close(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } private StringTokenizer st; private BufferedReader in; private PrintWriter pw; boolean hasNext() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return false; } st = new StringTokenizer(line); } return st.hasMoreTokens(); } String next() throws IOException { return hasNext() ? st.nextToken() : null; } int nextInt() throws IOException { return Integer.parseInt(next()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) { new Thread(new Main()).start(); } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
c5b0c3bd1748705c9d87da4b29666066
train_004.jsonl
1304694000
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on nΒ·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
256 megabytes
import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; public class b82 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int lines = (n*(n-1))/2; HashSet<Integer>[] setContents = new HashSet[lines]; ArrayList<Integer>[] sets = new ArrayList[lines]; HashSet<Integer> fs = new HashSet<Integer>(); ArrayList<Integer> bigSet = new ArrayList<Integer>(); for(int i = 0; i < sets.length; i++) { sets[i] = new ArrayList<Integer>(); setContents[i] = new HashSet<Integer>(); } for(int i = 0; i < lines; i++) { int q = in.nextInt(); for(int j = 0; j < q; j++) { int a = in.nextInt(); sets[i].add(a); setContents[i].add(a); if(!fs.contains(a)) { fs.add(a); bigSet.add(a); } } } HashSet<Integer> used = new HashSet<Integer>(); if(n == 2) { System.out.printf("%d %d\n",1,bigSet.get(0)); System.out.printf("%d ", bigSet.size()-1); for(int i = 1; i < bigSet.size(); i++) System.out.printf("%d ", bigSet.get(i)); System.out.println(); } else for(int i = 0; i < bigSet.size(); i++) { int t = bigSet.get(i); if(!used.contains(t)) { LinkedList<Integer> curSet = new LinkedList<Integer>(); int j,k; for(j = 0; j < sets.length; j++) if(setContents[j].contains(t)) break; for(k = j+1; k < sets.length; k++) if(setContents[k].contains(t)) break; for(int l = 0; l < sets[j].size(); l++) { int tt = sets[j].get(l); if(!used.contains(tt) && setContents[k].contains(tt)) { used.add(tt); curSet.add(tt); } } System.out.printf("%d ", curSet.size()); while(!curSet.isEmpty()) System.out.printf("%d ", curSet.poll()); System.out.println(); } } } }
Java
["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"]
2 seconds
["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"]
null
Java 6
standard input
[ "constructive algorithms", "implementation", "hashing" ]
bde894dc01c65da67d32c716981926f6
The first input file line contains a number n (2 ≀ n ≀ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on nΒ·(n - 1) / 2 lines. Each set starts with the number ki (2 ≀ ki ≀ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≀ aij ≀ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.
1,700
Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
standard output
PASSED
cd1c3bdc92b47e7f2093330f40b5dd9d
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.util.*; import java.math.*; public class CP { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); List<Integer> arr = new ArrayList<>(); Set<Integer> call = new HashSet<>(); Set<Integer> temp = new HashSet<>(); int ans = 0; for(int i = 0; i < n; i++) { arr.add(sc.nextInt()); } for(int i = 0; i < n; i++) { if(!call.contains(arr.get(i)) && arr.get(i) != 0) { call.add(arr.get(i)); } else if (temp.contains(arr.get(i)) && arr.get(i) != 0) { ans = -1; break; } else { if(arr.get(i) != 0) { ans++; temp.add(arr.get(i)); } } } System.out.println(ans); } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
1dd87c846b2e5b814642776d41433ee7
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.math.*; import java.io.*; import java.util.*; /* Jai Shree Ram */ public class JAVA { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String str[] = br.readLine().trim().split("\\s+"); int arr[] = Arrays.stream(str).mapToInt(Integer::parseInt).toArray(); HashMap<Integer , Integer> hm = new HashMap<>(); int count = 0; for(int i = 0; i < n; i++) { if(!hm.containsKey(arr[i])) { hm.put(arr[i] , 1); } else if(hm.get(arr[i]) == 1) { if(arr[i] != 0) { hm.put(arr[i] , 2); count++; } } else { System.out.println(-1); return; } } System.out.println(count); } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
8d1ce5f32ecb704404857b495a021389
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; public class a { /* * In a Programming contest, you are expected to print the output at the * end, so `output` variable will hold all the processed results till the * end */ public static String output = ""; // Program's starting point public static void main(String[] args) { /* * A Scanner class slows down Input/Output a LOT ,thereby increasing * your code execution time , Hence for best results that is Fast I/O * try to use BufferedReader */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); /* * Generally Code Chef, Hacker Rank gives X number of test cases so we * process the input for each. */ final int cases; try { cases = Integer.parseInt(br.readLine().trim()); /* * Logic of the program must be separated from the meta code to * increase readability and help debugging easier * Also note that Solver object is created inside for loop to * avoid multiple object creation that drastically increases * execution time and memory usage */ Solver solver = new Solver(); String[] ids = br.readLine().split(" "); int[] id = new int[cases]; for (int i = 0; i < cases; i++) { id[i] = Integer.parseInt(ids[i]); } solver.solve(cases, id); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Print the final output System.out.println(output); } } class Solver { /* * Logic goes here ... * Add to the global variables after processing the input * Maybe reverse a string or parse to an integer or , etc. */ public void solve(int n, int[] id) { HashMap<Integer, Integer> map = new HashMap<>(); for(int i : id){ if(!map.containsKey(i)) { map.put(i, 1); } else { map.put(i, map.get(i)+1); } } int bads = 0; for(int i : map.keySet()) { if(i == 0) continue; if(map.get(i) == 2) bads++; if(map.get(i) > 2) { System.out.println("-1"); return; } } a.output = a.output.concat(String.valueOf(bads)); } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
e966d6ba1f8fc1a1856876e54b31306a
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
/** * @author egaeus * @mail sebegaeusprogram@gmail.com * @veredict Accepted * @url <https://codeforces.com/problemset/problem/291/A * @category implementation * @date 29/10/2019 **/ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.TreeMap; import static java.lang.Integer.parseInt; public class CF291A { public static void main(String args[]) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (String ln; (ln = in.readLine()) != null; ) { int N = parseInt(ln); TreeMap<Integer, Integer> map = new TreeMap<>(); StringTokenizer st = new StringTokenizer(in.readLine()); boolean ws = true; int c=0; for (int i = 0; i < N; i++) { int a = parseInt(st.nextToken()); if(a>0) { Integer v = map.get(a); if(v==null)v=0; v++; map.put(a, v); if(v>2)ws = false; if(v==2)c++; } } System.out.println(ws?c:-1); } } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
11e0b4d0c496f61b3b59f6590e417c18
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.io.IOException; import java.util.*; public class d { public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); // BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); StringBuilder sb1=new StringBuilder(); int n=s.nextInt();int flag=0; HashMap<Long,Long> h=new HashMap<>();int ans=0; for(int i=0;i<n;i++){ long x=s.nextInt(); if(x==0L) continue; if(!h.containsKey(x)) h.put(x,1L); else { if(h.get(x)==2) flag=1; if(h.get(x)==1) { ans++; } h.put(x,h.get(x)+1); } }if(flag==1) System.out.println(-1); else System.out.println(ans); } static int[] vis; public static int countSetBits(int n) { return (BitsSetTable256[n & 0xff] + BitsSetTable256[(n >> 8) & 0xff] + BitsSetTable256[(n >> 16) & 0xff] + BitsSetTable256[n >> 24]); } static int[] BitsSetTable256 ; public static void initialize(int n) { BitsSetTable256[0] = 0; for (int i = 0; i <=Math.pow(2,n); i++) { BitsSetTable256[i] = (i & 1) + BitsSetTable256[i / 2]; } } static HashMap<Integer,Integer>[] val;//static int[] vis;static int y; static boolean dfs(int x ,int i,ArrayList<Integer>[] adj){ vis[i]=1; // if(x==1) System.out.print(i+" "); if(adj[i]==null) return false; for(int j:adj[i]){ // if(x==1) System.out.print(j+" "); if(j==x) return true; if(vis[j]==0) return dfs(x,j,adj); }return false; } static void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; i++; } } } } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long powerwithmod(long x, long y, int p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long powerwithoutmod(long x, int y) { long temp; if( y == 0) return 1; temp = powerwithoutmod(x, y/2); if (y%2 == 0) return temp*temp; else { if(y > 0) return x * temp * temp; else return (temp * temp) / x; } } static void fracion(double x) { String a = "" + x; String spilts[] = a.split("\\."); // split using decimal int b = spilts[1].length(); // find the decimal length int denominator = (int) Math.pow(10, b); // calculate the denominator int numerator = (int) (x * denominator); // calculate the nerumrator Ex // 1.2*10 = 12 int gcd = (int) gcd((long) numerator, denominator); // Find the greatest common // divisor bw them String fraction = "" + numerator / gcd + "/" + denominator / gcd; // System.out.println((denominator/gcd)); long x1 = modInverse(denominator / gcd, 998244353); // System.out.println(x1); System.out.println((((numerator / gcd) % 998244353 * (x1 % 998244353)) % 998244353)); } static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) { Queue<Integer> q = new LinkedList<Integer>(); q.add(i1);Queue<Integer> aq=new LinkedList<Integer>(); aq.add(0); while(!q.isEmpty()){ int i=q.poll(); int val=aq.poll(); if(i==n){ return val; } if(h[i]!=null){ for(Integer j:h[i]){ if(vis[j]==0){ q.add(j);vis[j]=1; aq.add(val+1);} } } }return -1; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long modInverse(long a, int m) { return (powerwithmod(a, m - 2, m)); } static int MAXN=100001; static int[] spf=new int[MAXN]; static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) spf[i] = i; for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { if (spf[i] == i) { for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i; } } } static Vector<Integer> getFactorizationUsingSeive(int x) { Vector<Integer> ret = new Vector<Integer>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } /* static long[] fac = new long[MAXN+1]; static void calculatefac(int mod){ for (int i = 1 ;i <= MAXN; i++) fac[i] = fac[i-1] * i % mod; } static long nCrModPFermat(int n, int r, int mod) { if (r == 0) return 1; fac[0] = 1; return (fac[n]* modInverse(fac[r], mod) % mod * modInverse(fac[n-r], mod) % mod) % mod; } */} class Student { int l;int r;int x; public Student(int l, int r,int x) { this.l = l;this.x=x; this.r = r; } public String toString() { return this.l+" "; } } class Sortbyroll implements Comparator<Student> { public int compare(Student a, Student b){ return b.x-a.x; } } class Sortbyroll1 implements Comparator<Student> { public int compare(Student a, Student b){ if(a.r==b.r) return a.l-b.l; return b.l-a.l; } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
290f4dd958620a0a4aed751b9e00ac35
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
//package codeforcesa2; import java.util.*; public class SpykeTalks { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); ArrayList al=new ArrayList(); HashSet hs=new HashSet(); for(int i=0;i<n;i++) { int x=in.nextInt(); al.add(x); hs.add(x); } Object arr[]=hs.toArray(); int count=0; int flag=0; for(int i=0;i<arr.length;i++) { int fre=Collections.frequency(al, arr[i]); if((int)arr[i]==0){ continue; } if(fre==2){ count++; } if(fre>2){ flag=1; System.out.println("-1"); break; } } if(flag==0){ System.out.println(count); } } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
f8e98e1f5e3599da9bb4e09d97a009eb
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; /** * * @author User */ public class JavaApplication71 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long a[]=new long[n]; ArrayList<Integer> arr = new ArrayList<Integer>(); int pair=0; for(int i=0;i<n;i++) { int p=sc.nextInt(); if(p>0) { int x=arr.indexOf(p); if(x<0) { arr.add(p); a[arr.indexOf(p)]++; } else{ a[x]++; if(a[x]==2) { pair++; } if(a[x]>2) { System.out.println(-1); System.exit(0); } } } } System.out.println(pair); } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
bcab74ce67d288f047ae73e2ed32e0ca
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; /** * * @author User */ public class JavaApplication71 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long a[]=new long[n]; ArrayList<Integer> arr = new ArrayList<Integer>(); int pair=0; for(int i=0;i<n;i++) { int p=sc.nextInt(); if(p>0) { int x=arr.indexOf(p); if(x<0) { arr.add(p); a[arr.indexOf(p)]++; } else{ a[x]++; if(a[x]==2) { pair++; } if(a[x]>2) { System.out.println(-1); System.exit(0); } } } } System.out.println(pair); } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
d7757721691f5bae8f7e39beec3c3bb3
train_004.jsonl
1365796800
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted β€” that is, one call connects exactly two people.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { static MyScanner in = new MyScanner(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static long H,W; static BitSet bs; static long mod = 1000000007; public static void main(String args[]) throws IOException { int n = in.nextInt(); int [] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = in.nextInt(); } Arrays.sort(arr); int prev = arr[0]; int cnt =1; int ans =0; for(int i=1;i<n;i++){ if(prev==arr[i]&&arr[i]!=0){ cnt++; }else{ if(cnt==2){ ans++; }else if(cnt>=3){ ans = Integer.MIN_VALUE/2; } prev = arr[i]; cnt = 1; } } if(cnt==2){ ans++; }else if(cnt>=3){ ans = Integer.MIN_VALUE/2; } out.println(ans<0?-1:ans); out.flush(); } static class IP implements Comparable<IP>{ public int first,second; IP(int first, int second){ this.first = first; this.second = second; } public int compareTo(IP ip){ if(first==ip.first) return second-ip.second; return first-ip.first; } } static long gcd(long a, long b){ return b!=0?gcd(b, a%b):a; } static boolean isEven(long a) { return (a&1)==0; } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"]
2 seconds
["2", "-1", "0"]
NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed.
Java 11
standard input
[ "sortings", "implementation", "*special" ]
45e51f3dee9a22cee86e1a98da519e2d
The first line contains integer n (1 ≀ n ≀ 103) β€” the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≀ idi ≀ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.
800
Print a single integer β€” the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.
standard output
PASSED
67af16ea79d134d430e845699a1e274f
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
/** * @author egaeus * @mail sebegaeusprogram@gmail.com * @veredict Not sended * @url <https://codeforces.com/problemset/problem/CFA> * @category ? * @date 9/06/2020 **/ import java.io.*; import java.util.*; import static java.lang.Integer.*; import static java.lang.Math.*; public class CFA { public static void main(String args[]) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int T = parseInt(in.readLine()); for (int t = 0; t < T; t++) { StringTokenizer st = new StringTokenizer(in.readLine()); int N = parseInt(st.nextToken()), S = parseInt(st.nextToken()), K = parseInt(st.nextToken()); int[] arr = new int[K]; st = new StringTokenizer(in.readLine()); for (int i = 0; i < K; i++) arr[i] = parseInt(st.nextToken()); Arrays.sort(arr); int min = -1; for (int i = S, j = S; i >= S - 2000 && min < 0; i--, j++) { if (j <= N && Arrays.binarySearch(arr, j) < 0) min = j - S; if (i > 0 && Arrays.binarySearch(arr, i) < 0) min = S - i; } System.out.println(min); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
b55424e15a027bbe37e76b015788756b
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; public class pudge{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int q = in.nextInt(); while(q-- > 0){ int n, s, k, r = (int)1e9, l = (int)1e9; n = in.nextInt(); s = in.nextInt(); k = in.nextInt(); HashSet<Integer> a = new HashSet<Integer>(); while(k-- > 0){ int x = in.nextInt(); a.add(x); } for (int i = s; i <= n; i++){ if (!a.contains(i)){ r = i - s; break; } } for (int i = s; i > 0; i--){ if (!a.contains(i)){ l = s - i; break; } } System.out.printf("%d\n", Math.min(l, r)); } in.close(); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
b4cbdf5312f75356c53c9acf19dd4327
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t>0){ int n = sc.nextInt(); int s = sc.nextInt(); int k = sc.nextInt(); boolean hasTheNumber = false; ArrayList<Integer> lower = new ArrayList<>(); ArrayList<Integer> greater = new ArrayList<>(); for(int i=0;i<k;i++){ int a = sc.nextInt(); if(a<s){ lower.add(a); }else if(a > s){ greater.add(a); }else{ hasTheNumber = true; } } if(hasTheNumber){ Collections.sort(lower);Collections.sort(greater); int ans = Integer.MAX_VALUE; int lLen = lower.size()-1;int gLen = 0; for(int i=s-1;i>0;i--){ if(lLen < 0 || i!=lower.get(lLen)){ ans = Math.min(ans,s-i); break; } lLen--; } for(int i=s+1;i<=n;i++){ if(gLen >= greater.size() || i!=greater.get(gLen)){ ans = Math.min(ans,i-s); break; } gLen++; } System.out.println(ans); }else{ System.out.println(0); } t--; } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
175d6b26dbe95fc05ac39a49d9006285
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ky112233 */ 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); TaskA solver = new TaskA(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int s = in.nextInt(); int k = in.nextInt(); Set<Integer> set = new HashSet<>(); for (int i = 0; i < k; i++) set.add(in.nextInt()); int i = 0; while (true) { if ((s + i <= n && !set.contains(s + i)) || (s - i >= 1 && !set.contains(s - i))) { out.println(i); return; } else { i++; } } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
466af0eb0483f076b742fdf9028e5cb1
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
//package pkg1293a; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { long n=sc.nextLong(); long s=sc.nextLong(); int k=sc.nextInt(); long array[]=new long[k]; for(int j=0;j<k;j++) { array[j]=sc.nextInt(); } long num1=0,num2=0; //int flage=0; long j; for( j=s;j<=n;j++) { int flage=0; for(int l=0;l<k;l++) { if(array[l]==j) { flage=1; break; } } if(flage==0) break; num1++; } if(j==n+1 ) num1=n+1; // System.out.println("num1:"+num1); for( j=s;j>0;j--) { int flage=0; for(int l=0;l<k;l++) { if(array[l]==j) { flage=1; break; } } if(flage==0) break; num2++; } if(j==0) { num2=n+1; } // System.out.println("num2:"+num2 + " j:"+j); if(num1>num2) { System.out.println(num2); } else { System.out.println(num1); } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
a81d17fe18159f24a700a32fe3cc52f8
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; public class ConnerAndMarkland { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(),s=sc.nextInt(),k=sc.nextInt();List<Integer> a=new ArrayList<>(); while(k--!=0) a.add(sc.nextInt());int d=0; for(int i=0;i<n;i++) { int x=s-i; int y=s+i; if(x>0 && !a.contains(x)) { d=i; break; } if(y<n+1 && !a.contains(y)) { d=i; break; } } System.out.println(d); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
ae7495d674e15c8958523ed7a45f28a9
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Scanner; public class Main { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int t = scanner.nextInt(); ArrayList<Integer> a1; ArrayList<Integer> a2; for (int l = 0; l < t; l++) { a1 = new ArrayList<>(); a2 = new ArrayList<>(); int n = scanner.nextInt(); int s = scanner.nextInt(); int k = scanner.nextInt(); int r = 0; for (int i = s - k; i <= s + k; i++) { a1.add(i); a2.add(i); r++; } // System.out.println(a1); for (int i = 1; i <= k; i++) { int kt = scanner.nextInt(); if (a1.contains(kt)) { a1.set(a1.indexOf(kt), Integer.MAX_VALUE); } } // System.out.println(); // System.out.println(a1); // System.out.println(a2); // System.out.println(a1); for (int i = 0; i < a1.size(); i++) { a1.set(i, Math.abs(a1.get(i) - s)); } int min = Integer.MAX_VALUE; for (int i = 0; i < a1.size(); i++) { if (a2.get(i) > 0 && a2.get(i) <= n && a1.get(i) < min) { min = a1.get(i); } } System.out.println(min); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
0f20b1d7fbdba72ad5bf690f3ba90b7f
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; public class CP { static long startTime; static long endTime; static Boolean [] prime ; static int M=(int)1e9+7; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static int SIZE = (int)1e9*2; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void printArray(int arr[]) { int n = arr.length; for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.println(); } static void printArray(long arr[]) { int n = arr.length; for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.println(); } static void printArray(Integer arr[]) { int n = arr.length; for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.println(); } static void printArray(Long arr[]) { int n = arr.length; for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.println(); } static void yes(){ System.out.println("YES"); } static void no(){ System.out.println("NO"); } public static int lowerBound (Integer[] array, int length, int value) { // Returns the index of the value or // if there is no value, returns the closest to the left. int low=-1; // arr[low]<=tar int hi=length;//arr[hi]>tar while (low+1<hi){ // while pointers are not neighboring(low+1!=hi) int mid=(low+hi)/2; if(array[mid]<=value){ low=mid; } else { hi=mid; } } return low;// if no such element, returns -1. } public static int upperBound(Integer[] array, int length, int value){ // returns the index of the value or // if the value does not exist, returns the closest to the right int low=-1; // arr[low]<tar int hi=length;// arr[hi]>=tar while (low+1<hi){// Eventually, pointers should be adjacent. int mid=(low+hi)>>1; if(array[mid]>=value){ hi=mid; } else low=mid; } return hi; } public static int binarySearch(Integer [] arr, int length, int value){ // returns the index of the targeted value or -1 if there is no value. int low=0; int hi=length-1; int ans=-1; while (low<=hi){ int mid=(low+hi)>>1; if(arr[mid]>value){ hi=mid-1; } else if(arr[mid]<value){ low=mid+1; } else { ans=mid; break; } } return ans; } public static long gcd(long a, long b){ if (b == 0) return a; else return gcd(b, a % b); } public static long countDivs(long n ){ int cn=0; long sqr = (long)Math.sqrt(n); for (long i = 1; i<=sqr ; ++i) { if(n%i==0){ ++cn; } } cn*=2; if(sqr*sqr==n) cn--; return cn; } static void prime(int x) { //sieve algorithm. nlog(log(n)). prime=new Boolean[ (x+1)]; Arrays.fill(prime,true); prime[0] = prime[1] = false; for (int i = 2; i * i <= x ; i++) if (prime[i]) for (int j = i * i; j <= x; j += i) prime[j] = false; } static int[] sum; static void cumulitiveSum( int [] arr){ sum = new int[arr.length]; sum[0]=arr[0]; for (int i = 1; i <arr.length; i++) { sum[i]=arr[i]+sum[i-1]; } } static boolean isEven(long x ){ return x % 2 == 0; } static boolean isPrime(long x ){ boolean flag = true; int sqr = (int)Math.sqrt(x); if(x<2) return false; for (int i = 2; i <=sqr; i++) { if(x%i==0){ flag=false; break; } } return flag; } static long factorial( long x) { long total = 1; for (int i = 2; i <= x; i++) total *= i; return total; } static long power(long x, long n) { if (n == 0) { return 1; } long pow = power(x, n / 2L); if ((n & 1) == 1) { return x * pow * pow; } return pow * pow; } public static void main(String [] args) { startTime = System.currentTimeMillis(); int T = sc.nextInt();while (T--!=0) { solve(); } endTime = System.currentTimeMillis(); long duration= endTime-startTime; //System.out.println(duration); } // static Scanner sc = new Scanner(System.in); static FastReader sc = new FastReader(); public static void solve() { ////////////////////////////////////////////////////////////////////// int n =sc.nextInt(),pos=sc.nextInt(),k=sc.nextInt(); Integer [] arr = new Integer[k]; for (int i = 0; i < k; i++) { arr[i]=sc.nextInt(); } Arrays.sort(arr); int x=pos; int temp=pos; int ans1=(int)1e9*2,ans2=(int)1e9*2; while (pos<=n||temp>=1){ if(pos<=n) if(binarySearch(arr,k,pos)==-1){ ans1=pos; break; } pos++; if(temp>=1) if(binarySearch(arr,k,temp)==-1){ ans2=temp; break; } temp--; } System.out.println(Math.min(Math.abs(ans1-x),Math.abs(x-ans2))); /////////////////////////////////////////////////////////////////////// } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
b91a0cc6fb60172c6604f98e65839243
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class ContestFormat614 { int memo[][]; final static int Infinity = Integer.MAX_VALUE; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); int n, s, k, a[]; HashSet<Integer> hs; for (int i = 0; i < t; i++) { n = sc.nextInt(); s = sc.nextInt(); k = sc.nextInt(); hs = new HashSet<Integer>(); for (int j = 0; j < k; j++) hs.add(sc.nextInt()); if (!hs.contains(s)) { pw.println(0); continue; } int up; int down; for (up = s + 1; up <= n && hs.contains(up); up++) ; if (up == n + 1) up = Infinity; for (down = s - 1; down > 0 && hs.contains(down); down--) ; if (down == 0) down = Infinity; if (up == Infinity && up == down) { pw.println(0); continue; } // pw.println("asd :" + up + " , " + down + " , s:" + s); int abs1 = up - s; abs1 = Math.abs(abs1); // pw.println(abs1); int abs2 = s - down; abs2 = Math.abs(abs2); int ans = Math.min(abs1, abs2); pw.println(ans); } pw.flush(); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
a179309b306f7959a2ae448ce6b1637f
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import java.text.DecimalFormat; import static java.lang.Math.*; public class Cf182 implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Cf182(),"Main",1<<27).start(); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } // array sorting by colm public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } // gcd public static long findGCD(long arr[], int n) { long result = arr[0]; for (int i = 1; i < n; i++) result = gcd(arr[i], result); return result; } // fibonaci static int fib(int n) { int a = 0, b = 1, c; if (n == 0) return a; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } // sort a string public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return new String(tempArray); } // pair function // list.add(new Pair<>(sc.nextInt(), i + 1)); // Collections.sort(list, (a, b) -> Integer.compare(b.first, a.first)); private static class Pair<F, S> { private F first; private S second; public Pair() {} public Pair(F first, S second) { this.first = first; this.second = second; } } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) return false; i++; j--; } return true; } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t = sc.nextInt(); while(t--!=0) { int n = sc.nextInt(); int s = sc.nextInt(); HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); int k = sc.nextInt(); for(int x = 0;x<k;x++) map.put(sc.nextInt(),0); if(!map.containsKey(s)) System.out.println("0"); else { int i=s-1,j=s+1; int ans = 0; while(true) { ans++; if(i>=1) { if(!map.containsKey(i)) { System.out.println(ans); break; } i--; } if(j<=n) { if(!map.containsKey(j)) { System.out.println(ans); break; } j++; } } } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
fb9164f164331b86860f2bac2c029556
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Rextester{ public static void shuffle(int[] array){ Random rand = new Random(); for(int i=0;i<array.length;i++){ int x = rand.nextInt(array.length-i)+i; int temp = array[x]; array[x]=array[i]; array[i]=temp; } } public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = new Integer(br.readLine()); StringBuffer sb = new StringBuffer(); while(n-->0){ StringTokenizer st = new StringTokenizer(br.readLine()); StringTokenizer st1 = new StringTokenizer(br.readLine()); int floors = new Integer(st.nextToken()); int now = new Integer(st.nextToken()); int k = new Integer(st.nextToken()); int[] array = new int[k]; for(int i=0;i<k;i++){ array[i]=new Integer(st1.nextToken()); } shuffle(array); Arrays.sort(array); int t = Arrays.binarySearch(array,now); if(t<0){ sb.append("0\n"); } else{ int low=now,t1=t,t2=t,high=now,min=0; while(true){ if(t1>=0){ if(array[t1]!=low){ min=Math.min(high-now,now-low); break; } else{ low--; t1--; } } if(t2<k){ if(array[t2]!=high){ min = Math.min(high-now,now-low); break; } else{ high++; t2++; } } if(t1==-1&&t2==k){ break; } } min = Math.min(high-now,now-low); //System.out.println(high+" "+low); if(low==0){ min = high-now; } if(high==floors+1){ min = now-low; } sb.append(min).append("\n"); } } br.close(); System.out.println(sb); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
cbaf7078f9da68b551d4cde30914b8b7
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); int t=scanner.nextInt(); for (int i=0;i<t;i++) { int n=scanner.nextInt(); int s=scanner.nextInt(); int k=scanner.nextInt(); int[] a=new int[k]; for (int j=0;j<k;j++) { a[j]=scanner.nextInt(); } Arrays.sort(a); int j; for (j=0;j<k;j++) { if (a[j]>=s) break; } if (j==k||a[j]!=s) { System.out.println(0); } else { int down=s-1; int tj=j-1; while (tj>=0&&down==a[tj]) { down--; tj--; } int up=s+1; tj=j+1; while (tj<k&&up==a[tj]) { up++; tj++; } if (down>0&&up<=n) { System.out.println((int)Math.min(s-down, up-s)); } else if (down>0) { System.out.println(s-down); } else { System.out.println(up-s); } } } scanner.close(); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
8786dd1379ca112579983b0e642f88ef
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import com.sun.source.tree.Tree; import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class round { FastScanner in; PrintWriter out; private void solve() throws IOException { int t=in.nextInt(); for (int h = 0; h <t ; h++) { int n = in.nextInt(); int s = in.nextInt(); int k = in.nextInt(); int[] a = new int[k + 1]; a[0] = s; for (int i = 1; i <= k; i++) { a[i] = in.nextInt(); } Arrays.sort(a); int x = 0; for (int i = 0; i < a.length; i++) { if (a[i] == s) { x = i; break; } } if (x == a.length - 1) { out.println(0); } else if (a[x + 1] != s) { out.println(0); } else { int min1 = -1; int min2 = -1; for (int i = x + 1; i < a.length; i++) { if (a[i] != s + i - x - 1) { min1 = s + i - x - 1; break; } } if (min1 == -1) { if (s + k - x <= n) { min1 = s + k - x; } } for (int i = x - 1; i >= 0; i--) { if (a[i] != s - x + i) { min2 = s - x + i; break; } } if (min2 == -1) { if (s - x - 1 > 0) { min2 = s - x - 1; } } if (min1 == -1) { out.println(s - min2); } else if (min2 == -1) { out.println(min1 - s); } else { out.println(Math.min(s - min2, min1 - s)); } } } } class FastScanner { StringTokenizer st; BufferedReader br; FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } boolean hasNext() throws IOException { return br.ready() || (st != null && st.hasMoreTokens()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } boolean hasNextLine() throws IOException { return br.ready(); } } private void run() throws IOException { in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in")); out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out")); solve(); out.flush(); out.close(); } public static void main(String[] args) throws IOException { new round().run(); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
23dda91fdb234f6eb1f1a640135bdddb
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class A1293 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int j=0;j<t;j++) { int n=sc.nextInt(); int w=sc.nextInt(); int c=sc.nextInt(); int d=0; ArrayList<Integer> al = new ArrayList<Integer>(); for(int i=0;i<c;i++) { al.add(sc.nextInt()); } for(int i=0;i<n;i++) { int a=w-i; int b=w+i; if(a>0 && !al.contains(a)) { d=i; break; } if(b<n+1 && !al.contains(b)) { d=i; break; } } System.out.println(d);} } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
45ded23e1aabe0d2ce2105991e714d59
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); int x=0,y=0; int n = 0, s = 0,k = 0; while(t-->0) { x=Integer.MAX_VALUE;y=Integer.MAX_VALUE; n = scan.nextInt(); s = scan.nextInt(); k = scan.nextInt() ; ArrayList<Integer>v = new ArrayList<Integer>(); for(int i=0;i<k;i++)v.add(scan.nextInt()-1); // System.out.println(v); for(int i=s-1;i<n;i++) { if(!v.contains(i)) { x=i-(s-1); break; } } for(int i=s-2;i>=0;i--) { if(!v.contains(i)) { y=(s-1)-i; break; } } System.out.println(Math.min(x, y)); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
157149420b4b8d58947b3bd56431038f
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
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 sc=new Scanner(System.in); int n1=sc.nextInt(); for(int i=0;i<n1;i++) {int n=0;int s=0;int k=0; int flag=0;int ans=0; int flag1=0; n=sc.nextInt(); s=sc.nextInt(); k=sc.nextInt(); HashSet<Integer> h=new HashSet<>(); for(int i1=0;i1<k;i1++) { h.add(sc.nextInt()); } if(h.contains(s)==false) {ans=0; } else { int p1=s-1;int p2=s+1; while(p1>=1&&p2<=n) { if(h.contains(p1)==false) { ans=s-p1; flag=1; break; } if(h.contains(p2)==false) { ans=p2-s; flag=1; break; } p1--;p2++; } while(p1>=1&&flag==0) { if(h.contains(p1)==false) { ans=s-p1; flag=1; break; } p1--; } while(p2<=n&&flag==0) { if(h.contains(p2)==false) { ans=p2-s; flag=1; break; } ++p2; } } System.out.println(ans); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
85dcc88dbbfe65f55a27377cd2c7aba9
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while (t-->0){ //ζ₯Όε±‚ζ•° int n=scan.nextInt(); //ζ‰€εœ¨ηš„ζ₯Όε±‚ int s=scan.nextInt(); //ε…³ι—­ηš„ι€ι¦†ζ•° int k=scan.nextInt(); int x; ArrayList<Integer> list=new ArrayList<>(); for(int i=1;i<=k;i++){ x=scan.nextInt(); list.add(x); } int ans=0; while (true){ if(s+ans<=n&&!list.contains(s+ans)){ break; } if(s-ans>=1&&!list.contains(s-ans)){ break; } ans++; } System.out.println(ans); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
2df778c1e87b42796b39fc645bd8c222
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastReader scan = new FastReader(); int t = scan.nextInt(); while(t--> 0) { int n = scan.nextInt(); int s = scan.nextInt(); int k = scan.nextInt(); List<Integer> kf = new ArrayList<>(); for(int i=0; i<k; i++) kf.add(scan.nextInt()); int count = s; int turn = 1; int i = 1, j = 1; boolean one = true; boolean two = true; while(true) { if(s > 0 && s <= n && !kf.contains(s)) { break; } else if(turn % 2 == 1) { s = count + i; i++; } else if(turn % 2 == 0) { s = count - j; j++; } turn++; } System.out.println(abs(count, s)); } } public static int abs(int a, int b) { return Math.abs(a - b); } // sieveOfEratosthenes public static boolean[] sieve(int n) { boolean[] prime = new boolean[n+1]; Arrays.fill(prime, true); prime[0] = false; prime[1] = false; for(int i=2; i*i<n; i++) { if(prime[i] == true) { for(int j=i*2; j<=n; j+=i) { prime[j] = false; } } } return prime; } //smallestFactor function public static long smallestFactor(long n) { int i; for(i=2; i*i<n; i++) { if(n%i == 0) break; } return i; } //FastReader Class 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()); } char[] nextChar() { return next().toCharArray(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
f8b248385e5ae31241fe6dfe489a123b
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; public class Main{ public static void main(String[] args) throws IOException { final BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); final StringBuilder outputStringBuilder=new StringBuilder(); int testcases=Integer.parseInt(bufferedReader.readLine()); while (testcases-->0){ String[] input=bufferedReader.readLine().split(" "); int n=Integer.parseInt(input[0]); int s=Integer.parseInt(input[1]); int k=Integer.parseInt(input[2]); String[] input2=bufferedReader.readLine().split(" "); TreeSet<Integer> treeSet=new TreeSet<>(); for (int i = 0; i <k ; i++) { treeSet.add(Integer.valueOf(input2[i])); } int first = -1,second =-1; for (int i = s; i <=n ; i++) { if (!treeSet.contains(i)) { first=Math.abs(s-i); break; } } for (int i = s; i >=1 ; i--) { if (!treeSet.contains(i)) { second=Math.abs(s-i); break; } } if (first==-1) outputStringBuilder.append(second).append("\n"); else if(second==-1) outputStringBuilder.append(first).append("\n"); else outputStringBuilder.append(Math.min(first,second)).append("\n"); } System.out.println(outputStringBuilder); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
c001b151e52de24ff5abd06d72bd0dc3
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Solution { static Integer[][] dp; public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while (t-- > 0) { int n = scn.nextInt(); int s = scn.nextInt(); int k = scn.nextInt(); int a[] = new int[k]; for (int i = 0; i < k; i++) { a[i] = scn.nextInt(); } Arrays.sort(a); int i = Arrays.binarySearch(a, s); if (i <= -1) System.out.println(0); else { int b = 1; boolean flag = true; while (true) { if (i - b >= 0 && a[i - b] != a[i] - b) { flag = true; break; } if (i + b < a.length && a[i + b] != a[i] + b) { flag = true; break; } if(i + b >= a.length && a[a.length - 1] < n) break; if(i - b < 0 && a[0] > 1) break; if(i + b >= a.length && i - b < 0) break; b++; } System.out.println(b); } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
97283bffd5ecd25018a8ec2d15af812f
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int x=0;x<t;x++) { int n=sc.nextInt(); int s=sc.nextInt(); int k=sc.nextInt(); ArrayList<Integer> ll=new ArrayList<>(); for(int i=0;i<k;i++) ll.add(sc.nextInt()); if(!ll.contains(s)) System.out.println("0"); else { int min1=0,min2=0; for(int i=s-1;i>=1;i--) { if(!ll.contains(i)) { min1=(s-i); break; } } for(int i=s+1;i<=n;i++) { if(!ll.contains(i)) { min2=(i-s); break; } } if(min1==0) System.out.println(min2); else if(min2==0) System.out.println(min1); else System.out.println((int)Math.min(min1,min2)); } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
a532484acf1467d0879abcb87af3cf1c
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.BufferedInputStream; import java.util.*; public class A { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int s = in.nextInt(); int k = in.nextInt(); int[] pos = new int[k]; for (int j = 0; j < k; j++) { int temp = in.nextInt(); pos[j] = Math.abs(temp-s); } Arrays.sort(pos); int len1 = s - 1; int len2 = n - s; int start = 0; for (int j = 0; j <= n; j++){ if (start < k && j == pos[start]){ if (j == 0){ start++; } else if (j <=len1 && j<=len2) { if (start +1 <k && pos[start] == pos[start+1]) { start += 2; } else { System.out.println(j); break; } } else { start++; } } else { System.out.println(j); break; } } } in.close(); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
2c5e2e72515a5fed099e7ca39a9e6a9b
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; import java.io.*; public class GFG{ public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ int n=sc.nextInt(); int s=sc.nextInt(); int k=sc.nextInt(); int arr[]=new int[k]; HashSet<Integer> h=new HashSet<Integer>(); for(int i=0;i<k;i++){ arr[i]=sc.nextInt();h.add(arr[i]);} int ans=Integer.MAX_VALUE; int tmp=s; while(s>0) { if(!h.contains(s)){ ans=tmp-s;break;} s--; } int ans1=Integer.MAX_VALUE; s=tmp; while(s<=n) { if(!h.contains(s)){ ans1=s-tmp;break;} s++; } if(ans>0&&ans1>0){ System.out.println((int)Math.min(ans1,ans));continue;} System.out.println((int)Math.max(ans1,ans)); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
0a55c44d4c47b01a63fc8a824fee9ff0
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; public class TaskB { static int x, y, n; static Integer memo[]; static int[] arr; static int OO = (int) (1e9); static TreeSet<Integer> set; static int mod = (int) 1e9 + 7; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int s = sc.nextInt(); int k = sc.nextInt(); HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < k; i++) { set.add(sc.nextInt()); } int min = Integer.MAX_VALUE; for (int i = s, l = 0; l < 1010 && i <= n; i++, l++) { if (!set.contains(i)) { min = Math.min(min, Math.abs(s - i)); } } for (int i = s, l = 0; l < 1010 && i > 0; i--, l++) { if (!set.contains(i)) { min = Math.min(min, Math.abs(s - i)); } } pw.println(min); } pw.close(); } static class Pair { long total; int diff; public Pair(long total, int diff) { this.total = total; this.diff = diff; } public String toString() { return total + " " + diff; } } static class Pair2 implements Comparable<Pair2> { int x, y; public Pair2(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair2 o) { if (x == o.x) return y - o.y; return x - o.x; } public String toString() { return x + " " + y; } } static class Edge implements Comparable<Edge> { int node, cost; Edge(int a, int b) { node = a; cost = b; } public int compareTo(Edge e) { return cost - e.cost; } } 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\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
c1312832369f604f5670c54b95a8cd81
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.Scanner; import java.lang.Math; public class rank{ public static void main(String []args){ Scanner in=new Scanner(System.in); int l=in.nextInt(); while(l-->0){ long a=in.nextInt(); long b=in.nextInt(); int c=in.nextInt(); int ab[]=new int[c]; long q=0; for(int i=0;i<c;i++) ab[i]=in.nextInt(); if(dec(b,a,c,ab)!=-1 && inc(b,a,c,ab)!=-1) q=Math.min((b-dec(b,a,c,ab)),(inc(b,a,c,ab)-b)); if(inc(b,a,c,ab)==-1) q=b-dec(b,a,c,ab); if(dec(b,a,c,ab)==-1) q=inc(b,a,c,ab)-b; System.out.println(q); } } public static long inc(long b,long a,int c,int ab[]){ int d=0; for(long i=b;i<a+1;i++){ for(int j=0;j<c;j++){ if(ab[j]>=i && i==ab[j]){ d++; } } if(d==0) return i; d=0; } return -1; } public static long dec(long b,long a,int c,int ab[]){ int d=0; for(long i=b;i>0;i--){ for(int j=0;j<c;j++){ if(ab[j]<=i && i==ab[j]){ d++; } } if(d==0) return i; d=0; } return -1; } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
dcf151f197a749e615b2af949f156c92
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.util.*; public class Akisolution { public static Scanner sc = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out, true); public static int n, s, k; public static int[] a; public static boolean exist(int[] arr, int x) { for (int i=0; i<arr.length; i++) { if (arr[i] == x) return true; } return false; } public static void Input() { n = sc.nextInt(); s = sc.nextInt(); k = sc.nextInt(); a = new int[k]; for (int i=0; i<k; i++) a[i] = sc.nextInt(); } public static void Solve() { for (int i=0; i<=k; i++) { if (s-i >= 1 && !exist(a, s-i)) {out.println(i); return;} if (s+i <= n && !exist(a, s+i)) {out.println(i); return;} } assert false; // if reached this line, the solution failed to find a free floor } public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) {Input(); Solve();} } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
8447c702007f4fca6ec6c72d02ee3c7e
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
//package com.company; import java.io.*; import java.util.*; public class Main { public static class Task { public void solve(Scanner sc, PrintWriter pw) throws IOException { int T = sc.nextInt(); while (T-- > 0) { int n = sc.nextInt(); int s = sc.nextInt(); int k = sc.nextInt(); Set<Integer> ss = new HashSet<>(); for (int i = 0; i < k; i++) { ss.add(sc.nextInt()); } int min = k + 1; for (int i = -k - 5; i <= k + 5; i++) if (s + i > 0 && s + i <= n){ if (!ss.contains(s + i)) { min = Math.min(min, Math.abs(i)); } } pw.println(min); } } } static long TIME_START, TIME_END; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new FileInputStream("input")); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out)); // PrintWriter pw = new PrintWriter(new FileOutputStream("output")); Runtime runtime = Runtime.getRuntime(); long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory(); TIME_START = System.currentTimeMillis(); Task t = new Task(); t.solve(sc, pw); TIME_END = System.currentTimeMillis(); long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory(); pw.close(); System.err.println("Memory increased: " + (usedMemoryAfter - usedMemoryBefore) / 1000000); System.err.println("Time used: " + (TIME_END - TIME_START) + "."); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader s) throws FileNotFoundException { 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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
667f90330f6b7ce81339b345c8eac9c1
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; public class Code { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int s=sc.nextInt(); int k=sc.nextInt(); HashSet<Integer> numSet=new HashSet<>(); while(k!=0){ numSet.add(sc.nextInt()); k--; } int min=Integer.MAX_VALUE; for(int i=s;i>0;i--){ if(numSet.contains(i)) continue; min=s-i; break; } for(int i=s+1;i<=n;i++){ if(numSet.contains(i)) continue; min=Math.min(min,i-s); break; } System.out.println(min); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
9f34bd44ddc87005ec3cf6f4450b5345
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; import java.io.*; //Captain on duty! public class Main { static void compare(Main.pair a[] , int n) { Arrays.sort(a, new Comparator<Main.pair>() { @Override public int compare(Main.pair p1, Main.pair p2) { return p1.f - p2.f; } }); } public static boolean checkPalindrome(String s) { // reverse the given String String reverse = new StringBuffer(s).reverse().toString(); // check whether the string is palindrome or not if (s.equals(reverse)) return true; else return false; } static class pair implements Comparable { int f; int s; pair(int fi, int se) { f=fi; s=se; } public int compareTo(Object o)//desc order { pair pr=(pair)o; if(s>pr.s) return -1; if(s==pr.s) { if(f>pr.f) return 1; else return -1; } else return 1; } public boolean equals(Object o) { pair ob=(pair)o; if(o!=null) { if((ob.f==this.f)&&(ob.s==this.s)) return true; } return false; } public int hashCode() { return (this.f+" "+this.s).hashCode(); } } public static boolean palin(int l, int r, char[] c) { while (l <= r) { if (c[l] != c[r]) return false; l++; r--; } return true; } public static long gcd(long a, long b) { if(b==0) return a; return gcd(b, a%b); } public static long hcf(long a, long b) { long t; while(b != 0) { t = b; b = a%b; a = t; } return a; } public static boolean isPrime(int n) { if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } public static String reverse(String str) { String str1=""; for(int i=0;i<str.length();i++) { str1 = str1 + str.charAt(str.length()-i-1); } return str1; } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } } public static void main(String[] args) { FastReader s = new FastReader(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int s1=s.nextInt(); int k=s.nextInt(); ArrayList l=new ArrayList(); for(int i=0;i<k;i++) l.add(s.nextInt()); if(!l.contains(s1)) System.out.println(0); else { int min1=Integer.MAX_VALUE; int i=s1; int temp=s1; while(i-->0) { if(!l.contains(i)) { temp=i; break; } } if(temp==0) temp =Integer.MAX_VALUE; else temp = s1-temp; i=s1; int temp1=s1; while(i++<n) { if(!l.contains(i)) { temp1 = i; break; } } if(temp1==s1) temp1=Integer.MAX_VALUE; else temp1 = temp1-s1; //System.out.println(temp+" "+temp1); System.out.println(Math.min(temp,temp1)); } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
b14a798dc946324caab101067b2bd4c6
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.io.*; import java.util.*; public class test { public static int helper(int arr[], int mid, int s, int n) { int ul=s+mid; int ll=s-mid; ul=Math.min(n, ul); ll=Math.max(0, ll); return 0; } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Reader.init(System.in); int t=Reader.nextInt(); while(t-->0) { int n=Reader.nextInt(); int s=Reader.nextInt(); int k=Reader.nextInt(); Map<Integer,Boolean> mp=new HashMap<>(); int arr[]=new int[k]; for(int i=0;i<k;i++) { arr[i]=Reader.nextInt(); mp.put(arr[i], true); } for(int i=0;i<=k;i++) { int ul=s+i; int ll=s-i; ul=Math.min(n, ul); ll=Math.max(1, ll); if(mp.containsKey(ul) && mp.containsKey(ll)) continue; else { System.out.println(i); break; } } } } } 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()); } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
04298f9a55b59468531c57c1bbb5cad3
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
//package codeforces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Findlo { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); // InputStreamReader r=new InputStreamReader(System.in); //BufferedReader bf=new BufferedReader(r); int t = in.nextInt(); for (int x = 0; x < t; x++) { long n = in.nextLong(); long s = in.nextLong(); long k = in.nextLong(); ArrayList<Long> list = new ArrayList<Long>(); //ar[0]=s; for (int i = 0; i < k; i++) { long temp = in.nextLong(); list.add(temp); } long back = 0; long front = 0; int i; boolean flag = false; long p=s; long q=s; while(true) { if(p>=1) { if(!list.contains(p)) { System.out.println(back); break; } back++; p--; } if(q<=n) { if(!list.contains(q)) { System.out.println(front); break; } front++; q++; } } } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
737ffbe4955dd3ceb100e45c085072b2
train_004.jsonl
1579440900
Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!
256 megabytes
import java.util.*; import java.io.*; public class uu { static TreeSet<Long>ts; public static void main(String[] args) throws IOException, InterruptedException { Scanner sc =new Scanner(System.in); PrintWriter pw =new PrintWriter(System.out); int t =sc.nextInt(); while (t-->0) { long n=sc.nextLong(),s=sc.nextLong(),k=sc.nextLong(); ts =new TreeSet<Long>(); while (k-->0) ts.add(sc.nextLong()); long min =Integer.MAX_VALUE; if (!ts.contains(s)) pw.println(0); else { long i=s+1,j=s-1,cou=1020; while (cou-->0) { if (i<=n&&!ts.contains(i)) { min=Math.abs(i-s); break; } if (j>=1&&!ts.contains(j)) { min=Math.abs(j-s); break; } i++;j--; } pw.println(min); } } pw.close(); } static int mod(int x,int y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class Scanner { StringTokenizer stringTokenizer; BufferedReader bfBufferedReader; public Scanner(InputStream system) { bfBufferedReader=new BufferedReader(new InputStreamReader( system)); } public Scanner(String file) throws Exception { bfBufferedReader = new BufferedReader(new FileReader( file)); } public String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) stringTokenizer = new StringTokenizer( bfBufferedReader.readLine()); return stringTokenizer.nextToken(); } public String nextLine() throws IOException { return bfBufferedReader.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return bfBufferedReader.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } public static class InfixEvaluation { public int evaluate(String expression){ //Stack for Numbers Stack<Integer> numbers = new Stack<>(); //Stack for operators Stack<Character> operations = new Stack<>(); for(int i=0; i<expression.length();i++) { char c = expression.charAt(i); //check if it is number if(Character.isDigit(c)){ //Entry is Digit, it could be greater than one digit number int num = 0; while (Character.isDigit(c)) { num = num*10 + (c-'0'); i++; if(i < expression.length()) c = expression.charAt(i); else break; } i--; //push it into stack numbers.push(num); }else if(c=='('){ //push it to operators stack operations.push(c); } //Closed brace, evaluate the entire brace else if(c==')') { while(operations.peek()!='('){ int output = performOperation(numbers, operations); //push it back to stack numbers.push(output); } operations.pop(); } // current character is operator else if(isOperator(c)){ //1. If current operator has higher precedence than operator on top of the stack, //the current operator can be placed in stack // 2. else keep popping operator from stack and perform the operation in numbers stack till //either stack is not empty or current operator has higher precedence than operator on top of the stack while(!operations.isEmpty() && precedence(c)<precedence(operations.peek())){ int output = performOperation(numbers, operations); //push it back to stack numbers.push(output); } //now push the current operator to stack operations.push(c); } } //If here means entire expression has been processed, //Perform the remaining operations in stack to the numbers stack while(!operations.isEmpty()){ int output = performOperation(numbers, operations); //push it back to stack numbers.push(output); } return numbers.pop(); } static int precedence(char c){ switch (c){ case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; } return -1; } public int performOperation(Stack<Integer> numbers, Stack<Character> operations) { int a = numbers.pop(); int b = numbers.pop(); char operation = operations.pop(); switch (operation) { case '+': return a + b; case '-': return b - a; case '*': return a * b; case '/': if (a == 0) throw new UnsupportedOperationException("Cannot divide by zero"); return b / a; } return 0; } public boolean isOperator(char c){ return (c=='+'||c=='-'||c=='/'||c=='*'||c=='^'); } } }
Java
["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"]
1 second
["2\n0\n4\n0\n2"]
NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.
Java 11
standard input
[ "binary search", "implementation", "brute force" ]
faae9c0868b92b2355947c9adcaefb43
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)Β β€” the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$)Β β€” respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$)Β β€” the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
1,100
For each test case print a single integerΒ β€” the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.
standard output
PASSED
2fd4842adc666033c84c30aac2d84970
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class Solution { //static ArrayList<Integer> user=new ArrayList<>(); public static void dfs(int arr[][],int[] v,int n,int h ,int r){ v[h]=r; boolean child=false; for(int i=0;i<n;i++){ if(v[i]==-1 && arr[h][i]==1){ dfs(arr,v,n,i,r); child=true; } } if(child==false) return ; } public static void ans(Graph g,int n){ int visited[]=new int[n]; int ans[]=new int[n]; Arrays.fill(visited,-1); for(int j=0;j<n;j++){ if(visited[j]==-1){ g.user.clear(); g.DFSUtil(j,visited,j); int size=g.user.size(); for(int item: g.user) ans[item]=size; } } StringBuffer out=new StringBuffer(); for(int i=0; i<n; i++) out.append(ans[i]+" "); System.out.println(out); // HashMap<Integer,Integer>h=new HashMap<>(); // for(int i=0;i<n;i++){ // if(h.containsKey(visited[i])) // h.put(visited[i],h.get(visited[i])+1); // else // h.put(visited[i],1); // } // for(int i=0;i<n;i++){ // System.out.print(h.get(visited[i])+" "); // } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); StringTokenizer s=new StringTokenizer(br.readLine()); int n=Integer.parseInt(s.nextToken()); int m=Integer.parseInt(s.nextToken()); Graph g = new Graph(n); for(int i=0;i<m;i++){ StringTokenizer sa=new StringTokenizer(br.readLine()); int k=Integer.parseInt(sa.nextToken()); int u=-1; if(k!=0) u=Integer.parseInt(sa.nextToken()); for(int j=1;j<k;j++){ int v=Integer.parseInt(sa.nextToken()); g.addEdge(u-1, v-1); } } ans(g,n); } } class Graph { private int V; // No. of vertices // Array of lists for Adjacency List Representation private LinkedList<Integer> adj[]; ArrayList<Integer> user=new ArrayList<>(); // Constructor Graph(int v) { V = v; adj = new LinkedList[v]; for (int i=0; i<v; ++i) adj[i] = new LinkedList(); } //Function to add an edge into the graph void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v);// Add w to v's list. } // A function used by DFS void DFSUtil(int v,int visited[],int k) { // Mark the current node as visited and print it visited[v] = k; user.add(v); //System.out.print(v+" "); // Recur for all the vertices adjacent to this vertex Iterator<Integer> i = adj[v].listIterator(); while (i.hasNext()) { int n = i.next(); if (visited[n]==-1) DFSUtil(n, visited,k); } } // The function to do DFS traversal. It uses recursive DFSUtil() }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
a460579df2d0168b9f4bc67b8f14a54d
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class Solution { public static void dfs(int arr[][],int[] v,int n,int h ,int r){ v[h]=r; boolean child=false; for(int i=0;i<n;i++){ if(v[i]==-1 && arr[h][i]==1){ dfs(arr,v,n,i,r); child=true; } } if(child==false) return ; } public static void ans(Graph g,int n){ int visited[]=new int[n]; //int ans[]=new int[n]; Arrays.fill(visited,-1); for(int j=0;j<n;j++){ if(visited[j]==-1){ g.DFSUtil(j,visited,j); } } HashMap<Integer,Integer>h=new HashMap<>(); for(int i=0;i<n;i++){ if(h.containsKey(visited[i])) h.put(visited[i],h.get(visited[i])+1); else h.put(visited[i],1); } StringBuffer out=new StringBuffer(); for(int i=0; i<n; i++) out.append(h.get(visited[i])+" "); System.out.println(out); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); StringTokenizer s=new StringTokenizer(br.readLine()); int n=Integer.parseInt(s.nextToken()); int m=Integer.parseInt(s.nextToken()); Graph g = new Graph(n); for(int i=0;i<m;i++){ StringTokenizer sa=new StringTokenizer(br.readLine()); int k=Integer.parseInt(sa.nextToken()); int u=-1; if(k!=0) u=Integer.parseInt(sa.nextToken()); for(int j=1;j<k;j++){ int v=Integer.parseInt(sa.nextToken()); g.addEdge(u-1, v-1); } } ans(g,n); } } class Graph { private int V; // No. of vertices // Array of lists for Adjacency List Representation private LinkedList<Integer> adj[]; // Constructor Graph(int v) { V = v; adj = new LinkedList[v]; for (int i=0; i<v; ++i) adj[i] = new LinkedList(); } //Function to add an edge into the graph void addEdge(int v, int w) { adj[v].add(w); adj[w].add(v);// Add w to v's list. } // A function used by DFS void DFSUtil(int v,int visited[],int k) { // Mark the current node as visited and print it visited[v] = k; //System.out.print(v+" "); // Recur for all the vertices adjacent to this vertex Iterator<Integer> i = adj[v].listIterator(); while (i.hasNext()) { int n = i.next(); if (visited[n]==-1) DFSUtil(n, visited,k); } } // The function to do DFS traversal. It uses recursive DFSUtil() }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
3cb9033da4b548a9aa1110752b20ff1d
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.util.*; import java.io.*; public class unionfind{ public static int[] parent; public static int[] size; public static void union(int a, int b){ int a_parent = find(a); int b_parent = find(b); parent[b_parent] = a_parent; // if (size[a_parent] > size[b_parent]){ //size of component to which a belongs is big // parent[b_parent] = a_parent; // size[a_parent] += size[b_parent]; // } // else{ // parent[a_parent] = b_parent; // size[b_parent] += size[a_parent]; // } } public static int find(int a){ if (parent[a] != a){ parent[a] = find(parent[a]); } return parent[a]; } public static void main(String[] args) throws IOException{ Reader.init(System.in); // Scanner sc = new Scanner(System.in); int n = Reader.nextInt(); int m = Reader.nextInt(); parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++){ parent[i] = i; size[i] = 1; } for (int i = 0; i < m; i++){ int k = Reader.nextInt(); int u = 0; if (k > 0){ u = Reader.nextInt() - 1; } for (int j = 0; j < k - 1; j++){ int v = Reader.nextInt() - 1; union(u, v); } } // for (int i = 0; i < n; i++){ // System.out.print(parent[size[i]] + " "); // } // System.out.println(); int[] ans = new int[n]; for (int i = 0; i < n; i++){ int f = find(i); ans[f]+=1; // System.out.print( + " "); } for (int i = 0; i < n; i++){ System.out.print(ans[find(i)] + " "); } // System.out.println(); } } 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 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() ); } }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
bae7b9cab110b054b75f0a67424c5913
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main1 { static PrintWriter out = new PrintWriter(System.out, true); public static class disjointset { int p[], r[], setSize[]; disjointset(int n) { p = new int[n]; r = new int[n]; setSize = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; setSize[i] = 1; } } boolean sameSet(int x, int y) { return findSet(x) == findSet(y); } void connect(int x, int y) { if (!sameSet(x, y)) { int i = findSet(x); int j = findSet(y); if (r[i] > r[j]) { p[j] = i; setSize[i] += setSize[j]; } else { p[i] = j; setSize[j] += setSize[i]; if (r[j] == r[i]) { r[j]++; } } } } int findSet(int x) { return p[x] = p[x] == x ? x : findSet(p[x]); } } public static void main(String[] args) throws IOException { FastReader input = new FastReader(); int n = input.nextInt(); int m = input.nextInt(); disjointset set = new disjointset(n); for (int i = 0; i < m; i++) { int k = input.nextInt(); int p = 0; if (k > 0) { p = input.nextInt() - 1; k--; } while (k-- > 0) { set.connect(p, input.nextInt() - 1); } } for (int i = 0; i < n; i++) { out.print(set.setSize[set.findSet(i)] + " "); } out.flush(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { // try { // br = new BufferedReader(new FileReader(new File("army.in"))); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; str = br.readLine(); return str; } boolean hasNext() throws IOException { return br.ready(); } } static class con { static int IINF = (int) 1e9; static int _IINF = (int) -1e9; static long LINF = (long) 1e15; static long _LINF = (long) -1e15; static double EPS = 1e-9; } static class Triple implements Comparable<Triple> { int x; int y; int z; Triple(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public int compareTo(Triple o) { if (x == o.x && y == o.y) return z - o.z; if (x == o.x) return y - o.y; return x - o.x; } @Override public String toString() { return "(" + x + ", " + y + ", " + z + ")"; } } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (x == o.x) return y - o.y; return x - o.x; } @Override public String toString() { return "(" + x + ", " + y + ")"; } } static void shuffle(long[] a) { for (int i = 0; i < a.length; i++) { int r = i + (int) (Math.random() * (a.length - i)); long tmp = a[r]; a[r] = a[i]; a[i] = tmp; } } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static class DSU { int[] p, rank, setSize; int numSets; DSU(int n) { p = new int[n]; rank = new int[n]; setSize = new int[n]; numSets = n; for (int i = 0; i < n; i++) { p[i] = i; setSize[i] = 1; } } int findSet(int n) { return p[n] = p[n] == n ? n : findSet(p[n]); } boolean isSameSet(int n, int m) { return findSet(n) == findSet(m); } void mergeSet(int n, int m) { if (!isSameSet(n, m)) { numSets--; int p1 = findSet(n); int p2 = findSet(m); if (rank[p1] > rank[p2]) { p[p2] = p1; setSize[p1] += setSize[p2]; } else { p[p1] = p2; setSize[p2] += setSize[p1]; if (rank[p1] == rank[p2]) rank[p1]++; } } } int size() { return numSets; } int setSize(int n) { return setSize[findSet(n)]; } } 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[1024]; 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
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
3e7430110b84b5d9069f37ee7045da73
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class Solution { static int parent[]; static int size[]; public static int root(int ele) { while(parent[ele]!=ele) { ele=parent[ele]; } return ele; } public static void main(String[] args)throws IOException { FastReader in=new FastReader(System.in); StringBuilder sb=new StringBuilder(); int i,j=0,a,b,x,f,f1=0,maxsize,root,mroot; int n=in.nextInt(); int m=in.nextInt(); int arr[][]=new int[m][]; int len[]=new int[m]; parent=new int[n+1]; size=new int[n+1]; for(i=1;i<=n;i++) { parent[i] = i; size[i]=1; } for(i=0;i<m;i++) { len[i]=in.nextInt(); arr[i]=new int[len[i]]; if(len[i]==0) continue; maxsize=0; mroot=-1; f=0; for(j=0;j<len[i];j++) { arr[i][j] = in.nextInt(); root=root(arr[i][j]); if(size[root]!=1) { f = 1; if(maxsize<size[root]) { maxsize=size[root]; mroot=root; } } } if(f==1) { for(j=0;j<len[i];j++) { root=root(arr[i][j]); if(root!=mroot) { parent[root] = mroot; size[mroot]+=size[root]; } } } else { for(j=1;j<len[i];j++) parent[arr[i][j]]=arr[i][0]; size[arr[i][0]]=len[i]; } //System.out.println(Arrays.toString(size)); } for(i=1;i<=n;i++) { System.out.print(size[root(i)]+" "); } } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c !=10 && c!=13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException{ int c; for (c = scan(); c <= 32; c = scan()); return (char)c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
d599389e26a144f1912f6196d545203e
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public final class Main extends Thread { boolean[] prime; FastScanner sc; PrintWriter pw; MathF mf; long startTime = System.currentTimeMillis(); final class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } public long nlo() { return Long.parseLong(next()); } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } return st.nextToken(); } public int ni() { return Integer.parseInt(next()); } public String nli() { String line = ""; if (st.hasMoreTokens()) line = st.nextToken(); else try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } while (st.hasMoreTokens()) line += " " + st.nextToken(); return line; } public double nd() { return Double.parseDouble(next()); } } final class MathF { public long gcd(long a, long b) { return (b == 0 ? a : gcd(b, a % b)); } public long exponent(long a,long b) { if(b==0) return 1; else { long x=exponent(a,b/2); x*=x; if(b%2==1) x*=a; return x; } } public int nod(long x) { int i = 0; while (x != 0) { i++; x = x / 10; } return i; } public int nob(long x) { if(x==0) return 1; return (int) Math.floor(Math.log(x) / Math.log(2)) + 1; } public int[] FastradixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } public void Primesieve(int n) { for (int i = 0; i < n; i++) prime[i] = true; for (int p = 2; p * p < n; p++) { if (prime[p] == true) { for (int i = p * p; i < n; i += p) prime[i] = false; } } } }; public Main(ThreadGroup t,Runnable r,String s,long d ) { super(t,r,s,d); } public void run() { sc=new FastScanner(); pw=new PrintWriter(System.out); mf=new MathF(); solve(); pw.flush(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } public static void main(String[] args) { new Main(null,null,"",1<<30).start(); } /////////////------------------------------------////////////// ////////////------------------Main-Logic--------////////////// ///////////-------------------------------------////////////// LinkedList<Integer>[] adj; int[] visited; public void solve() { int n,i,a=0,b=0,j,k,m; n=sc.ni(); visited=new int[n]; adj=new LinkedList[n]; Arrays.fill(visited,0); int[] ans=new int[n]; Arrays.fill(ans,1); for(i=0;i<n;i++) { adj[i]=new LinkedList(); } m=sc.ni(); for(i=0;i<m;i++) { k=sc.ni(); if(k!=0) { a=sc.ni()-1; for(j=1;j<k;j++) { b=sc.ni()-1; adj[a].add(b); adj[b].add(a); } } } for(i=0;i<n;i++) { if(visited[i]==0) { ArrayList<Integer> list = new ArrayList(); dfs(i, list); k = list.size(); for (int y : list) ans[y] = k; } } for(int y:ans) pw.print(y+" "); } public void dfs(int x,ArrayList<Integer> list) { visited[x]=1; list.add(x); for(int y:adj[x]) { if(visited[y]==0) { dfs(y,list); } } } }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
cb2b90e5a0d6cf0fb795f86bd6058578
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class TaskC implements Runnable { PrintWriter w; InputReader c; public void run() { c = new InputReader(System.in); w = new PrintWriter(System.out); int n = c.nextInt(); int m = c.nextInt(); DJSet d = new DJSet(n); for(int i=0;i<m;i++){ int k = c.nextInt(); if(k==0) continue; int a = c.nextInt()-1; for(int j=0;j<k-1;j++){ int v = c.nextInt()-1; d.union(a,v); } } HashMap<Integer,Integer> hs = new HashMap<>(); for(int i=0;i<n;i++){ int r = d.root(i); if(hs.containsKey(r)) hs.put(r,hs.get(r)+1); else hs.put(r,1); } for(int i=0;i<n;i++){ int et = d.root(i); w.print(hs.get(et)+" "); } w.println(); w.close(); } public static void sortbyColumn(int arr[][], int col){ Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } public void printArray(int[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } public int[] scanArrayI(int n){ int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = c.nextInt(); return a; } public long[] scanArrayL(int n){ long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = c.nextLong(); return a; } public void printArray(long[] a){ for(int i=0;i<a.length;i++) w.print(a[i]+" "); w.println(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new TaskC(),"TaskC",1<<26).start(); } }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
0de5eaba0ee8357c3c07b0c3ff13a4cb
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
//import com.sun.org.apache.xpath.internal.operations.String; import java.io.*; import java.util.*; public class scratch_25 { //static long count=0; static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } public static class Graph{ public static class Vertex{ HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex } public static HashMap<Integer,Vertex> vt; // for vertices(all) public Graph(){ vt= new HashMap<>(); } public static int numVer(){ return vt.size(); } public static boolean contVer(int ver){ return vt.containsKey(ver); } public static void addVer(int ver){ Vertex v= new Vertex(); vt.put(ver,v); } public static int numEdg(){ int count=0; ArrayList<Integer> vrtc= new ArrayList<>(vt.keySet()); for (int i = 0; i <vrtc.size() ; i++) { count+=(vt.get(vrtc.get(i))).nb.size(); } return count/2; } public static boolean contEdg(int ver1, int ver2){ if(vt.get(ver1)==null || vt.get(ver2)==null){ return false; } Vertex v= vt.get(ver1); if(v.nb.containsKey(ver2)){ return true; } return false; } public static void addEdge(int ver1, int ver2, int weight){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } Vertex v1= vt.get(ver1); Vertex v2= vt.get(ver2); v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge v2.nb.put(ver1,weight); } public static void delEdge(int ver1, int ver2){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } vt.get(ver1).nb.remove(ver2); vt.get(ver2).nb.remove(ver1); } public static void delVer(int ver){ if(!vt.containsKey(ver)){ return; } Vertex v1= vt.get(ver); ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); vt.get(s).nb.remove(ver); } vt.remove(ver); } public class Pair{ int vname; ArrayList<Integer> psf= new ArrayList<>(); // path so far int dis; int col; } public HashMap<Integer,Integer> bfs(int src){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue Pair strtp= new Pair(); strtp.vname= src; ArrayList<Integer> ar= new ArrayList<>(); ar.add(src); strtp.psf= ar; strtp.dis=0; queue.addLast(strtp); while(!queue.isEmpty()){ Pair rp = queue.removeFirst(); if(prcd.containsKey(rp.vname)){ continue; } prcd.put(rp.vname,true); ans.put(rp.vname,rp.dis); // if(contEdg(rp.vname,dst)){ // return true; // } Vertex a= vt.get(rp.vname); ArrayList<Integer> arr= new ArrayList<>(a.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); if(!prcd.containsKey(s)){ Pair np = new Pair(); np.vname= s; np.dis+=rp.dis+a.nb.get(s); np.psf.addAll(rp.psf); np.psf.add(s); // np.psf.add(s); // np.psf= rp.psf+" "+s; queue.addLast(np); } } } return ans; // return false; } public HashMap<Integer,Integer> dfs(int src){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> stack= new LinkedList<>(); // for bfs queue Pair strtp= new Pair(); strtp.vname= src; ArrayList<Integer> ar= new ArrayList<>(); ar.add(src); strtp.psf= ar; strtp.dis=0; stack.addFirst(strtp); while(!stack.isEmpty()){ Pair rp = stack.removeFirst(); if(prcd.containsKey(rp.vname)){ continue; } prcd.put(rp.vname,true); ans.put(rp.vname,rp.dis); // if(contEdg(rp.vname,dst)){ // return true; // } Vertex a= vt.get(rp.vname); ArrayList<Integer> arr= new ArrayList<>(a.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); if(!prcd.containsKey(s)){ Pair np = new Pair(); np.vname= s; np.dis+=rp.dis+a.nb.get(s); np.psf.addAll(rp.psf); np.psf.add(s); // np.psf.add(s); // np.psf= rp.psf+" "+s; stack.addFirst(np); } } } return ans; // return false; } public boolean isCycle(){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye // HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue ArrayList<Integer> keys = new ArrayList<>(vt.keySet()); for (int i = 0; i <keys.size(); i++) { int cur= keys.get(i); if(prcd.containsKey(cur)){ continue; } Pair sp = new Pair(); sp.vname= cur; ArrayList<Integer> as= new ArrayList<>(); as.add(cur); sp.psf= as; queue.addLast(sp); while(!queue.isEmpty()){ Pair rp= queue.removeFirst(); if(prcd.containsKey(rp.vname)){ return true; } prcd.put(rp.vname,true); Vertex v1= vt.get(rp.vname); ArrayList<Integer> nbrs= new ArrayList<>(v1.nb.keySet()); for (int j = 0; j <nbrs.size() ; j++) { int u= nbrs.get(j); Pair np= new Pair(); np.vname= u; queue.addLast(np); } } } return false; } public ArrayList<ArrayList<Integer>> genConnctdComp(){ HashMap<Integer, Boolean> prcd= new HashMap<>(); // jo jo ho chuke hain unke liye ArrayList<ArrayList<Integer>> ans= new ArrayList<>(); // HashMap<Integer, Integer> ans= new HashMap<>(); LinkedList<Pair> queue= new LinkedList<>(); // for bfs queue // int con=-1; ArrayList<Integer> keys = new ArrayList<>(vt.keySet()); for (int i = 0; i <keys.size(); i++) { int cur= keys.get(i); if(prcd.containsKey(cur)){ //return true; continue; } ArrayList<Integer> fu= new ArrayList<>(); fu.add(cur); Pair sp = new Pair(); sp.vname= cur; ArrayList<Integer> as= new ArrayList<>(); as.add(cur); sp.psf= as; queue.addLast(sp); while(!queue.isEmpty()){ Pair rp= queue.removeFirst(); if(prcd.containsKey(rp.vname)){ //return true; continue; } prcd.put(rp.vname,true); fu.add(rp.vname); Vertex v1= vt.get(rp.vname); ArrayList<Integer> nbrs= new ArrayList<>(v1.nb.keySet()); for (int j = 0; j <nbrs.size() ; j++) { int u= nbrs.get(j); if(!prcd.containsKey(u)){ Pair np= new Pair(); np.vname= u; queue.addLast(np); } }} ans.add(fu); } //return false; return ans; } public boolean isBip(int src){ // only for connected graph HashMap<Integer,Integer> clr= new HashMap<>(); // colors are 1 and -1 LinkedList<Integer>q = new LinkedList<Integer>(); clr.put(src,1); q.add(src); while(!q.isEmpty()){ int u = q.getFirst(); q.pop(); ArrayList<Integer> arr= new ArrayList<>(vt.keySet()); for (int i = 0; i <arr.size() ; ++i) { int x= arr.get(i); if(vt.get(u).nb.containsKey(x) && !clr.containsKey(x)){ if(clr.get(u)==1){ clr.put(x,-1); } else{ clr.put(x,1); } q.push(x); } else if(vt.get(u).nb.containsKey(x) && (clr.get(x)==clr.get(u))){ return false; } } } return true; } public static void printGr() { ArrayList<Integer> arr= new ArrayList<>(vt.keySet()); for (int i = 0; i <arr.size() ; i++) { int ver= arr.get(i); Vertex v1= vt.get(ver); ArrayList<Integer> arr1= new ArrayList<>(v1.nb.keySet()); for (int j = 0; j <arr1.size() ; j++) { System.out.println(ver+"-"+arr1.get(j)+":"+v1.nb.get(arr1.get(j))); } } } } static class Cus implements Comparable<Cus>{ int size; int mon; int num; public Cus(int size,int mon,int num){ this.size=size; this.mon=mon; this.num=num; } @Override public int compareTo(Cus o){ if(this.mon!=o.mon){ return this.mon-o.mon;} else{ return o.size-this.size; } } } static class Table implements Comparable<Table>{ int go; int cum; int cost; public Table(int go,int cum,int cost){ this.go=go; this.cum=cum; this.cost=cost; } @Override public int compareTo(Table o){ if(this.go==o.go){ return this.cum-o.cum; } return this.go-o.go; } } static class Table1 implements Comparable<Table1>{ int go; int cum; int cost; public Table1(int go,int cum,int cost){ this.go=go; this.cum=cum; this.cost=cost; } @Override public int compareTo(Table1 o){ return this.cost-o.cost; }} public static class DisjointSet{ HashMap<Integer,Node> mp= new HashMap<>(); public static class Node{ int data; Node parent; int rank; } public void create(int val){ Node nn= new Node(); nn.data=val; nn.parent=nn; nn.rank=0; mp.put(val,nn); } public int find(int val){ return findn(mp.get(val)).data; } public Node findn(Node n){ if(n==n.parent){ return n; } Node rr= findn(n.parent); n.parent=rr; return rr; } public void union(int val1, int val2){ // can also be used to check cycles Node n1= findn(mp.get(val1)); Node n2= findn(mp.get(val2)); if(n1.data==n2.data) { return; } if(n1.rank<n2.rank){ n1.parent=n2; } else if(n2.rank<n1.rank){ n2.parent=n1; } else { n2.parent=n1; n1.rank++; } } } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n= Reader.nextInt(); int k= Reader.nextInt(); DisjointSet set= new DisjointSet(); for (int i = 0; i <n ; i++) { set.create(i+1); } for (int i = 0; i <k ; i++) { int m= Reader.nextInt(); int arr[]= new int[m]; for (int j = 0; j <m ; j++) { arr[j]= Reader.nextInt(); } // System.out.println(Arrays.toString(arr)); for (int j = 0; j <m-1 ; j++) { set.union(arr[j],arr[j+1]); } } // int ans[]= new int[n]; // HashMap<Integer, Long> map= new HashMap<>(); int map[]= new int[n+1]; for (int i = 1; i <=n ; i++) { int x= set.find(i); map[x]++; } for (int i = 1; i <=n ; i++) { int x=set.find(i); out.append(map[x]+" "); } out.flush(); out.close(); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd1(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater //count++; if (a > b){ return gcd(a-b, b); } return gcd(a, b-a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a*b)/gcd(a, b); } // Driver method static int partition(long arr[],long arr1[], int low, int high) { long pivot = arr[high]; int i = (low-1); // index of smaller element for (int j=low; j<high; j++) { // If current element is smaller than the pivot if (arr[j] < pivot) { i++; // swap arr[i] and arr[j] long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; long temp1=arr1[i]; arr1[i]=arr1[j]; arr1[j]=temp1; } } // swap arr[i+1] and arr[high] (or pivot) long temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; long temp1= arr1[i+1]; arr1[i+1]=arr1[high]; arr1[high]= temp1; return i+1; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, low --> Starting index, high --> Ending index */ static void sort(long arr[],long arr1[], int low, int high) { if (low < high) { /* pi is partitioning index, arr[pi] is now at right place */ int pi = partition(arr,arr1, low, high); // Recursively sort elements before // partition and after partition sort(arr,arr1, low, pi-1); sort(arr,arr1, pi+1, high); } } public static ArrayList<Integer> Sieve(int n) { boolean arr[]= new boolean [n+1]; Arrays.fill(arr,true); arr[0]=false; arr[1]=false; for (int i = 2; i*i <=n ; i++) { if(arr[i]){ for (int j = 2; j <=n/i ; j++) { int u= i*j; arr[u]=false; }} } ArrayList<Integer> ans= new ArrayList<>(); for (int i = 0; i <n+1 ; i++) { if(arr[i]){ ans.add(i); } } return ans; } }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output
PASSED
4c4ce9b5e347b7d3c9e9ee76b4641dd5
train_004.jsonl
1557930900
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users.Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class scratch_39 { public static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) { // Initially, all elements are in // their own set. parent[i] = i; } } // Returns representative of x's set int find(int x) { // Finds the representative of the set // that x is an element of if (parent[x] != x) { // if x is not the parent of itself // Then x is not the representative of // his set, parent[x] = find(parent[x]); // so we recursively call Find on its parent // and move i's node directly under the // representative of this set } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { // Find representatives of two sets int xRoot = find(x), yRoot = find(y); // Elements are in the same set, no need // to unite anything. if (xRoot == yRoot) return; // If x's rank is less than y's rank if (rank[xRoot] < rank[yRoot]) // Then move x under y so that depth // of tree remains less parent[xRoot] = yRoot; // Else if y's rank is less than x's rank else if (rank[yRoot] < rank[xRoot]) // Then move y under x so that depth of // tree remains less parent[yRoot] = xRoot; else // if ranks are the same { // Then move y under x (doesn't matter // which one goes where) parent[yRoot] = xRoot; // And increment the result tree's // rank by 1 rank[xRoot] = rank[xRoot] + 1; } } } static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int n= Reader.nextInt(); DisjointUnionSets set= new DisjointUnionSets(n); int k= Reader.nextInt(); for (int i = 0; i <k ; i++) { int m= Reader.nextInt(); int arr[]= new int[m]; for (int j = 0; j <m ; j++) { arr[j]= Reader.nextInt(); } for (int j = 0; j <m-1 ; j++) { set.union(arr[j]-1,arr[j+1]-1); }} int ans[]= new int[n+1]; for (int j = 0; j <n ; j++) { int x= set.find(j); ans[x]++; } for (int j = 0; j <n ; j++) { out.append(ans[set.find(j)]+" "); } out.flush(); out.close(); } }
Java
["7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7"]
2 seconds
["4 4 1 4 4 2 2"]
null
Java 8
standard input
[ "dsu", "dfs and similar", "graphs" ]
a7e75ff150d300b2a8494dca076a3075
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5 \cdot 10^5$$$) β€” the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 \le k_i \le n$$$) β€” the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$\sum \limits_{i = 1}^{m} k_i \le 5 \cdot 10^5$$$.
1,400
Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it.
standard output