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
b4ec2e46c55ec7997e7e75ef11c3c4ad
train_002.jsonl
1560090900
Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any.
256 megabytes
import java.io.*; import java.util.*; import java.util.Stack; import java.util.regex.Pattern; public class ROUGH { public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static long mod = (long)(1e9+7); static boolean[] primes; static int MAX = (int) 1e7; static Map<Integer,Integer> index = new HashMap<>(); public static void main(String[] args) { FastReader sc = new FastReader(); int n = sc.nextInt(); int[] b = new int[2*n]; Map<Integer,Integer> map = new HashMap<>(); index = new HashMap<>(); primes = new boolean[MAX+1]; int max = 0; for(int i=0;i<2*n;++i) { b[i] = sc.nextInt(); max = Math.max(max, b[i]); add(map,b[i]); } List<Integer> p = sieve(MAX); List<Integer> list = new ArrayList<>(); b = radixSort(b); for(int i=2*n-1;i>=0;--i) { if(!primes[b[i]] && map.containsKey(b[i])) { int val = greatestDiv(b[i],p); if(map.containsKey(val)) { list.add(b[i]); delete(map,val); delete(map,b[i]); } } } for(int i=0;i<2*n && map.size()>0;++i) { if(b[i] > 200000) break; if(map.containsKey(b[i])) { int idx = p.get(b[i]-1); list.add(b[i]); delete(map,b[i]); delete(map,idx); } } for(int x : list) out.print(x+" "); out.close(); } private static int greatestDiv(int num, List<Integer> p) { for(int x : p) { if(x > num/2) break; if(num%x == 0) return Math.max(x,num/x); } return 1; } private static void add(Map<Integer, Integer> map, int i) { // TODO Auto-generated method stub if(map.containsKey(i)) map.put(i, map.get(i)+1); else map.put(i, 1); } private static void delete(Map<Integer, Integer> map, int i) { if(map.containsKey(i)) { if(map.get(i) == 1) map.remove(i); else map.put(i, map.get(i)-1); } } private static List<Integer> sieve(int n) { // TIME COMPLEXITY: O(nLogLogn) primes = new boolean[n+1]; for(int i=2;i<=n;++i) primes[i] = true; for(int i=2;i*i<=n;++i) { if(primes[i]) { for(int j=i*i;j<=n;j+=i) { primes[j] = false; } } } List<Integer> temp = new ArrayList<>(); for(int i=2;i< primes.length;++i) if(primes[i]) { temp.add(i); // index.put(i,temp.size()); } return temp; } public static int[] radixSort(int[] f){ return radixSort(f, f.length); } public static int[] radixSort(int[] f, int n) { int[] to = new int[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } }
Java
["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"]
4 seconds
["3 4 2", "199999", "6"]
null
Java 8
standard input
[ "greedy", "graphs", "number theory", "sortings", "dfs and similar" ]
07484b6a6915c5cb5fdf1921355f2a6a
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number.
1,800
In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.
standard output
PASSED
d8afdc95846b38c66762fea9aa51346b
train_002.jsonl
1560090900
Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package here; /** * * @author sokumar */ import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class TryB { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static long mod=1000000007; static BigInteger bigInteger = new BigInteger("1000000007"); static int n = (int)1e7; static boolean[] prime; static ArrayList<Integer> as; static ArrayList<Integer> []as1; static void sieve() { Arrays.fill(prime , true); prime[0] = prime[1] = false; for(int i = 2 ; i * i <= n ; ++i) { if(prime[i]) { for(int k = i * i; k< n ; k+=i) { prime[k] = false; } } } } static PrintWriter w = new PrintWriter(System.out); static int p = 0; static char [][]sol; static int x1; static int y1; static int x2; static int y2; public static void main(String[] args) { InputReader sc = new InputReader(System.in); //PrintWriter w = new PrintWriter(System.out); prime = new boolean[n + 1]; sieve(); prime[1] = false; /* as = new ArrayList<>(); for(int i=2;i<=1000000;i++) { if(prime[i]) as.add(i); } */ /* long a = sc.nl(); BigInteger ans = new BigInteger("1"); for (long i = 1; i < Math.sqrt(a); i++) { if (a % i == 0) { if (a / i == i) { ans = ans.multiply(BigInteger.valueOf(phi(i))); } else { ans = ans.multiply(BigInteger.valueOf(phi(i))); ans = ans.multiply(BigInteger.valueOf(phi(a / i))); } } } w.println(ans.mod(bigInteger)); */ // MergeSort ob = new MergeSort(); // ob.sort(arr, 0, arr.length-1); // Student []st = new Student[x]; // st[i] = new Student(i,d[i]); //Arrays.sort(st,(p,q)->p.diff-q.diff); //BigDecimal x = new BigDecimal(b[i]).multiply(new BigDecimal("-1")).divide(new BigDecimal(a[i]),100,RoundingMode.HALF_UP); // PriorityQueue<Integer> pq=new PriorityQueue<Integer>(new multipliers()); int x = sc.ni(); int []a = sc.nia(2*x); ArrayList<Integer> a1 = new ArrayList<>(); ArrayList<Integer> p = new ArrayList<>(); ArrayList<Integer> a2 = new ArrayList<>(); for(int i=2;i<10000000;i++) { if(prime[i]) { p.add(i); } } ArrayList<Integer> res = new ArrayList<>(); HashMap<Integer,Integer> hm = new HashMap<>(); for(int i=1;i<=2750131;i++) { hm.put(i, 0); } for(int i=0;i<2*x;i++) { a2.add(a[i]); if(!hm.containsKey(a[i])) hm.put(a[i], 1); else hm.put(a[i], hm.get(a[i])+1); } int []primei = new int[2750132]; // w.println(p.get(2)); Collections.sort(a2); for(int i=2;i<=2750131;i++) { if(prime[i]) { for(int j = 2;i*j<=2750131;j++) { if(primei[i*j] == 0) primei[i*j] = i; } } } int []prime2 = new int[2750132]; int index = 1; for(int i=2;i<=2750131;i++) { if(prime[i]) { prime2[i] = index++; } } //w.println(primei[6]); for(int i = 2750131; i >= 2; i--) { if(hm.get(i) > 0) { if(prime[i]) { for(int j = 0; j < hm.get(i); j++) res.add(prime2[i]); hm.put(prime2[i],hm.get(prime2[i])-hm.get(i)); hm.put(i, 0); } else { for(int j = 0; j < hm.get(i); j++) res.add(i); hm.put(i/primei[i],hm.get(i/primei[i])-hm.get(i)); hm.put(i, 0); } } } for(int i=0;i<res.size();i++) { w.print(res.get(i) + " "); } w.close(); } static int []a; static long a1,b; public static int searchlow(int x) { int lo=0, hi=a.length-1; int res=Integer.MIN_VALUE; while(lo<=hi){ int mid = (lo+hi)/2; if(a[mid]==x){ res = mid; hi = mid - 1; } else if(a[mid]>x){ hi = mid - 1; } else{ lo = mid + 1; } } return res==Integer.MIN_VALUE?lo:res; } public static int searchhigh(int x) { int lo=0, hi=a.length-1; int res=Integer.MIN_VALUE; while(lo<=hi){ int mid = (lo+hi)/2; if(a[mid]==x){ res = mid; lo=mid+1; } else if(a[mid]>x){ hi = mid - 1; } else{ lo = mid + 1; } } return res==Integer.MIN_VALUE?hi:res; } public static long ct(int l, int r) { int lo=searchlow(l), hi=searchhigh(r); return hi-lo+1; } static long log2(long value) { return Long.SIZE-Long.numberOfLeadingZeros(value); } static class Student { int id; //int x; int y; int z; Student(int id,int y,int z) { this.id = id; //this.x = x; //this.s = s; this.y = y; this.z = z; } } public static long modMultiply(long one, long two) { return (one % mod * two % mod) % mod; } static long fx(int m) { long re = 0; for(int i=1;i<=m;i++) { re += (long) (i / gcd(i,m)); } return re; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static long phi(long nx) { // Initialize result as n double result = nx; // Consider all prime factors of n and for // every prime factor p, multiply result // with (1 - 1/p) for (int p = 0; as.get(p) * as.get(p) <= nx; ++p) { // Check if p is a prime factor. if (nx % as.get(p) == 0) { // If yes, then update n and result while (nx % as.get(p) == 0) nx /= as.get(p); result *= (1.0 - (1.0 / (double) as.get(p))); } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (nx > 1) result *= (1.0 - (1.0 / (double) nx)); return (long)result; //return(phi((long)result,k-1)); } public static void primeFactors(int n,int x) { as = new ArrayList<>(); // Print the number of 2s that divide n while (n%2==0) { as.add(2); //System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { // System.out.print(i + " "); as.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n >= 2) { as.add(n); } } static int digitsum(int x) { int sum = 0; while(x > 0) { int temp = x % 10; sum += temp; x /= 10; } return sum; } static int countDivisors(int n) { int cnt = 0; for (int i = 1; i*i <=n; i++) { if (n % i == 0 && i<=1000000) { // If divisors are equal, // count only one if (n / i == i) cnt++; else // Otherwise count both cnt = cnt + 2; } } return cnt; } static boolean isprime(long n) { if(n == 2) return true; if(n == 3) return true; if(n % 2 == 0) return false; if(n % 3 == 0) return false; long i = 5; long w = 2; while(i * i <= n) { if(n % i == 0) return false; i += w; w = 6 - w; } return true; } static boolean binarysearch(int []arr,int p,int n) { //ArrayList<Integer> as = new ArrayList<>(); //as.addAll(0,at); //Collections.sort(as); boolean re = false; int st = 0; int end = n-1; while(st <= end) { int mid = (st+end)/2; if(p > arr[mid]) { st = mid+1; } else if(p < arr[mid]) { end = mid-1; } else if(p == arr[mid]) { re = true; break; } } return re; } /* Java program for Merge Sort */ static class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } /* A utility function to print array of size n */ } public static int ip(String s){ return Integer.parseInt(s); } public static String ips(int s){ return Integer.toString(s); } // Java program to print all permutations using // Heap's algorithm static class HeapAlgo { //static ArrayList<Integer> []ad; //Prints the array void printArr(int a[], int n) { for (int i=0; i<n; i++) { System.out.print(a[i] + " "); as1[p].add(a[i]); } p++; //System.out.println(); } //Generating permutation using Heap Algorithm void heapPermutation(int a[], int size, int n) { // if size becomes 1 then prints the obtained // permutation if (size == 1) { printArr(a,n); } for (int i=0; i<size; i++) { heapPermutation(a, size-1, n); p++; // if size is odd, swap first and last // element if (size % 2 == 1) { int temp = a[0]; a[0] = a[size-1]; a[size-1] = temp; } // If size is even, swap ith and last // element else { int temp = a[i]; a[i] = a[size-1]; a[size-1] = temp; } } } public static int lowerBound(int[] array, int length, int value) { int low = 0; int high = length-1; while (low < high) { final int mid = (low + high) / 2; if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(int[] array, int length, int value) { int low = 0; int high = length-1; while (low < high) { final int mid = (low + high) / 2; if (value >= array[mid]) { low = mid + 1; } else { high = mid; } } return low; } // Driver code } // This code has been contributed by Amit Khandelwal. // JAVA program for implementation of KMP pattern // searching algorithm static class KMP_String_Matching { boolean KMPSearch(String pat, String txt) { int f = 0; int M = pat.length(); int N = txt.length(); // create lps[] that will hold the longest // prefix suffix values for pattern int lps[] = new int[M]; int j = 0; // index for pat[] // Preprocess the pattern (calculate lps[] // array) computeLPSArray(pat, M, lps); int i = 0; // index for txt[] while (i < N) { if (pat.charAt(j) == txt.charAt(i)) { j++; i++; } if (j == M) { /* System.out.println("Found pattern " + "at index " + (i - j)); */ f = 1; // return true; j = lps[j - 1]; } // mismatch after j matches else if (i < N && pat.charAt(j) != txt.charAt(i)) { // Do not match lps[0..lps[j-1]] characters, // they will match anyway if (j != 0) j = lps[j - 1]; else i = i + 1; } } if(f==0) return false; else return true; } void computeLPSArray(String pat, int M, int lps[]) { // length of the previous longest prefix suffix int len = 0; int i = 1; lps[0] = 0; // lps[0] is always 0 // the loop calculates lps[i] for i = 1 to M-1 while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { // This is tricky. Consider the example. // AAACAAAA and i = 7. The idea is similar // to search step. if (len != 0) { len = lps[len - 1]; // Also, note that we do not increment // i here } else // if (len == 0) { lps[i] = len; i++; } } } } // Driver program to test above function /* public static void main(String args[]) { String txt = "ABABDABACDABABCABAB"; String pat = "ABABCABAB"; new KMP_String_Matching().KMPSearch(pat, txt); } */ } // This code has been contributed by Amit Khandelwal. static class multipliers implements Comparator<Integer>{ public int compare(Integer a,Integer b) { if(a<b) return -1; else if(b<a) return 1; else return 0; } } static class multipliers1 implements Comparator<Student>{ public int compare(Student a, Student b) { if(a.y < b.y) return -1; else if(a.y > b.y) return 1; else { if(a.z < b.z) return -1; else return 1; } } } static void checkCollision(int a, int b, int c, int x, int y, int radius) { // Finding the distance of line from center. double dist = (Math.abs(a * x + b * y + c)) / Math.sqrt(a * a + b * b); // Checking if the distance is less than, // greater than or equal to radius. if (radius == dist) { double dis = (double)Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))); DecimalFormat df = new DecimalFormat("0.000000"); w.println(df.format(dis)); //System.out.println(df.format(364565.1454)); } else if (radius > dist) { double x11 = (double)x1; double y11 = (double)y1; double x22 = (double)x2; double y22 = (double)y2; double up = 2*x11*y11/(((x11*x11) - ((double)radius*(double)radius))); up *= up; double denm = ((y11*y11) - ((double)radius*(double)radius))/((x11*x11) - ((double)radius*(double)radius)); up -= (4*denm); up = Math.sqrt(up); double theta = 0.0; denm += 1.0; if(denm!=0.00000000) theta = Math.atan(up/denm); else theta = Math.PI/(double)2; //w.println(up); //w.println(denm); up = 2*x22*y22/(((x22*x22) - ((double)radius*(double)radius))); up *= up; denm = ((y22*y22) - ((double)radius*(double)radius))/((x22*x22) - ((double)radius*(double)radius)); up -= (4*denm); up = Math.sqrt(up); denm += 1.0; double theta1 = 0.0; // denm += 1.0; if(denm!=0.00000000) theta1 = Math.atan(up/denm); else theta1 = Math.PI/(double)2; theta /= 2; theta1 /= 2; double re = theta +theta1; re = re * (double) radius; double eq1 = (x11*x11) + (y11*y11) - ((double)radius*(double)radius); double eq2 = (x22*x22) + (y22*y22) - ((double)radius*(double)radius); double t1 = Math.sqrt(eq1); double t2 = Math.sqrt(eq2); double res = re + t1 + t2; // w.println(theta1); //w.println(theta); DecimalFormat df = new DecimalFormat("0.000000"); w.println(df.format(res)); } else { double x11 = (double)x1; double y11 = (double)y1; double x22 = (double)x2; double y22 = (double)y2; double midx = (x11+x22)/4; double midy = (y11+y22)/4; double dis = (double)Math.sqrt(((x22-midx)*(x22-midx)) + ((y22-midy)*(y22-midy))); double dis1 = (double)Math.sqrt(((x11-midx)*(x11-midx)) + ((y11-midy)*(y11-midy))); dis += dis1; DecimalFormat df = new DecimalFormat("0.000000"); w.println(df.format(dis)); } } }
Java
["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"]
4 seconds
["3 4 2", "199999", "6"]
null
Java 8
standard input
[ "greedy", "graphs", "number theory", "sortings", "dfs and similar" ]
07484b6a6915c5cb5fdf1921355f2a6a
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number.
1,800
In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.
standard output
PASSED
647fdb50a2e6cebaec372b50c9b070dc
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner s=new Scanner(System.in); int n=s.nextInt(); int minprob=1; for(int i=0;i<n;i++) {int a=s.nextInt(); int b=s.nextInt(); for(int j=1;j<=a;j++) if(j*b > a) {minprob= j-1; break;} String str=""; for(int z=1;z<=b;z++) for(int k=1;k<=a/b;k++) str+=(char)(z+96); int off=a%b; for(int p=1;p<=off;p++) str+=(char)(b+96); System.out.println(str); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
1cdf7aa298038d816c40a7718106f0c8
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; /** * @Created by sbhowmik on 18/12/18 */ public class UniformString { public static void main(String []args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while ( n > 0) { int k = scanner.nextInt(); int t = scanner.nextInt(); int l = k/t; int p = 0; while (k > 0) { for(int i = 0; i < l && k> 0; i++) { System.out.print((char)('a' + p)); k--; } p++; if(p == t) { p = 0; } } System.out.println(); n--; } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
99076223d1de3d51ba8aa4dbc8b96f6b
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class uniformString { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); short t=Short.parseShort(br.readLine()); int a,i,j; char b; while(t>0) { String cc=br.readLine(); String z[]; z=cc.split(" "); int n=Integer.parseInt(z[0]); int k=Integer.parseInt(z[1]); char s[]=new char[n]; i=1;j=0; while(j<n) { if(i>k) i=1; a=96+i; b=(char)a; s[j]=b; i++; j++; } System.out.println(String.valueOf(s)); t--; } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
597b874f143480b3bd818bc1972efb65
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class test { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int testNumber = in.nextInt(); TaskA solver = new TaskA(); solver.solve(testNumber, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { for (int t = 1; t <= testNumber; ++t) { String ans = ""; int n = in.nextInt(); int k = in.nextInt(); for (int i = 0; i < n; i++) ans += (char) ('a' + i%k); out.println(ans); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
68bf0a3e74fe9aa964c3882d33c99e56
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); String a = "abcdefghijklmnopqrstuvwxyz"; while(t-->0){ int n=s.nextInt(); int k=s.nextInt(); int i=0; int count=0; for(i=0;i<k;i++){ for(int j=1;j<=(n/k);j++){ System.out.print(a.charAt(i)); count++; } } for(int l=1;l<=(n%k);l++){ System.out.print(a.charAt(i-1)); } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
aff53b7468c803f790f3f755c9f03b59
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String [] args){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int z = 0; z < t; z++) { int n = scanner.nextInt(); int k = scanner.nextInt(); int rem = n%k; int div = n/k; char []chars = {'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's','t', 'u', 'v', 'w', 'x', 'y', 'z'}; String res = ""; for(int i = 0; i <k; i++) { for(int j = 0; j <div; j++) { res+= chars[i]; } } for(int j = 0; j <rem; j++) { res+= chars[k - 1]; } System.out.println(res); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
fe4c2dcb39fe4cd7910a54fd87cae6bb
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int j=0;j<t;j++) { int n,i,k; String str=""; n=sc.nextInt(); k=sc.nextInt(); for(i=0;i<k;i++) str=str+(char)(i+97); String st1=str; for(i=0;i<n/k-1;i++) str=str+st1; str=str+str.substring(0,n%k); System.out.println(str); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
6f8e0890abcbfc24e3874e7f26af9bab
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter pw = new PrintWriter(System.out); char[] alps = "abcdefghijklmnopqrstuvwxyz".toCharArray(); StringBuilder sb = new StringBuilder(); int T = sc.nextInt(); for(int ti = 0; ti < T; ti++){ int n = sc.nextInt(); int alp = sc.nextInt(); for(int i = 0; i < alp; i++){ int max = n/alp; if(n%alp > i) max++; for(int j = 0; j < max; j++){ sb.append(alps[i]); } } pw.println(sb.toString()); sb.setLength(0); } pw.flush(); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
b90c5703d22f828b344fd933c7413252
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Test11 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int cases = in.nextInt(); for (int i = 0; i < cases; i++) { int length = in.nextInt(); int alpha = in.nextInt(); int l = 0; char letter = 96; String s = ""; for (int j = 0; j < length; j++) { if (l >= alpha) { letter = 96; l =0; } l++; letter += 1; s += letter; } System.out.println(s); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
51bd686e750e632a5695f1a1e91e4fac
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner s=new Scanner(System.in); int n=s.nextInt(); int minprob=1; for(int i=0;i<n;i++) {int a=s.nextInt(); int b=s.nextInt(); String str=""; for(int z=1;z<=b;z++) for(int k=1;k<=a/b;k++) str+=(char)(z+96); for(int p=1;p<=a%b;p++) str+=(char)(b+96); System.out.println(str); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
7de8e7ae21b788591972847f0cf9a312
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner s=new Scanner(System.in); int n=s.nextInt(); int minprob=1; for(int i=0;i<n;i++) {int a=s.nextInt(); int b=s.nextInt(); String str=""; for(int z=1;z<=b;z++) for(int k=1;k<=a/b;k++) str+=(char)(z+96); for(int p=1;p<=a%b;p++) str+=(char)(b+96); System.out.println(str); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
4b392beece0082a759f6f8a854ad01e8
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc =new Scanner(System.in); int i = sc.nextInt(); while(i-->0) { int d = sc.nextInt(); int tt = sc.nextInt(); int v = d/tt; for(int y=0;y<tt;y++) for(int ii=0;ii<v;ii++){ System.out.print((char)(y+97)); } if(d%tt!=0){ for(int y=0;y<d%tt;y++){System.out.print((char)(97));} } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
23cddb0402213e8251b2ff7b4323da77
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Uniform_Str { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt();int ch=97; String s=""; int f=n/k; int d=n%k; for(int i=1;i<=k;i++) { for(int j=1;j<=f;j++) { s=s+(char)ch; } ch=ch+1; } if(d>=1) { for(int z=1;z<=d;z++) { s=s+'a'; } } System.out.println(s); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
39ec93243e6bf01fe8daf139a8a51cd6
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int o =sc.nextInt(); while(o-->0) { int a=sc.nextInt(),b=sc.nextInt(); String s="abcdefghijklmnopqrstuvwxyz"; int x = a/b; int y = a%b; int m = 0; for(int i=0;i<b;i++) { for(int j=0;j<x;j++) { System.out.print(s.charAt(m)); } m++; } for(int i=0;i<y;i++) { System.out.print(s.charAt(i)); } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
332157558e3653b28a16981ef101ad09
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Main{ public static void main(String[] args){ @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-- > 0) { int n = scanner.nextInt(); int k = scanner.nextInt(); StringBuilder stringBuilder = new StringBuilder(); int f = n / k; for(int i = 0; i < k ; ++i) { for(int j = 0; j < f; ++j) stringBuilder.append((char)('a' + i)); } int r = n % k; for(int i = 0; i < r ; ++i) stringBuilder.append((char)('a' + i)); System.out.println(stringBuilder); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
53dc8d31d4cbc8ca76dc208001b0e724
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class g { public static void main(String[] args) { Scanner s=new Scanner(System.in); String line=s.nextLine(); int n=Integer.parseInt(line); String alph="abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < n; i++) { line=s.nextLine(); String[]t=line.split("\\ "); int len=Integer.parseInt(t[0]); int num=Integer.parseInt(t[1]); String part=alph.substring(0,num); for (int j = 0; j <len/num; j++) { System.out.print(part); } if(len%num!=0) System.out.println(part.substring(0, len%num)); else System.out.println(); } }}
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
b9a1545aad12e32717c7c42b5f3f76f1
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class string { public static void main(String[] args) { Scanner line = new Scanner(System.in); int t = line.nextInt(); int n=0 ,k=0 ,m=0, l=0, i , j, h; char c; for( i=0; i<t ;i++) { StringBuffer S = new StringBuffer(); n = line.nextInt(); k = line.nextInt(); m= n/k; l = n%k; for (j =0 ;j<k ;j++) { for(h =0 ;h<m ;h++) { int a = 'a'+j; c = (char) a; S.append(c); } } if(l!=0) { for(j =0; j<l ;j++) { S.append('a'); } } System.out.println(S.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
1bbde1b7e09b92b23cbf84694c82b20a
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Codeforces1088A { public static void main(String[] args) throws java.lang.Exception { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); for (int i = 0; i < n; i++) { int x = scan.nextInt(); int y = scan.nextInt(); int count = x / y; char letter = 'a'; if(x%y!=0){ for(int j =0;j<x%y;j++){ System.out.print(letter); } } for (int k = 0; k < y; k++) { for (int j = 0; j < count; j++) { System.out.print(letter); } letter++; } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
141aa665aa4094e6b6b7577e2939dbb2
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class uniform { public static void main(String args[]) { Scanner s= new Scanner(System.in); int t=s.nextInt(); int q; for(q=0; q<t; q++) { int n= s.nextInt(); int k= s.nextInt(); int j=0, x=0, i; char c; while(j==0) { for(i=97; i<97+k; i++) { c= (char)(i); System.out.print(c); x++; if(x==n) break; } if(x<n) j=0; else j=1; } System.out.println(" "); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
afb6e4954f21bd5542f71907aeb45064
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author dhairya */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); A solver = new A(); solver.solve(1, in, out); out.close(); } static class A { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); int f = Math.floorDiv(n, k); String s = ""; int cnt = 0; while (cnt < f) { char c = 'a'; for (int i = 0; i < k; i++) { s += c; c++; } cnt++; } char c = 'a'; int sl = s.length(); while (sl < n) { s += c; c++; sl++; } out.println(s); } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
530309db538ce8a8e4e1d1b8d7f93334
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Demo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int k=sc.nextInt(); int f=n/k; int r=n-(f*k); for(int j=0;j<r;j++) System.out.print("a"); //int k=65; for(int j=0;j<k;j++) { for(int i1=0;i1<f;i1++) { char ch=(char)(j+97); System.out.print(ch); } } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
7a56d4dad5e266e68538f3b274948635
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Demo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int k=sc.nextInt(); int f=n/k; int r=n-(f*k); for(int j=0;j<r;j++) System.out.print("a"); //int k=65; for(int j=0;j<k;j++) { for(int i1=0;i1<f;i1++) { char ch=(char)(j+97); System.out.print(ch); } } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
169fed163efc3de5288f091a4d1f7988
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); String str = ""; int p = 1; for (int i = 0; i < t; ++i) { int n = in.nextInt(); int k = in.nextInt(); for (int j = 1; j <= n; ++j) { if (p > k) { p = 1; } str = str + (char)(p + 96); ++p; } out.println(str); str = ""; p = 1; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
054d1b23eb3ef767a919d6b388a129dd
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.StreamTokenizer; import java.io.Writer; public class MaxMinFreqInString { private static Reader reader = new InputStreamReader(System.in); private static Writer writer = new OutputStreamWriter(System.out); private static StreamTokenizer in = new StreamTokenizer(new BufferedReader(reader)); private static PrintWriter out = new PrintWriter(writer); private static char[] al = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; private static char[] res = new char[100]; private static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { int qs = nextInt(); int n, k; for (int i = 0; i < qs; i++) { n = nextInt(); k = nextInt(); out.println(solve(n, k)); out.flush(); } } public static String solve(int n, int k) { int rem = n % k; int t = n / k; StringBuilder res = new StringBuilder(); for (int i = 0; i < k; i++) { if (rem > 0) { res.append(al[i]); rem--; } for (int j = 0; j < t; j++) { res.append(al[i]); } } return res.toString(); } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
7cf7bcb763e3a669fb7d93d1c3038ad0
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { Reader in = new Reader(); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int k = in.nextInt(); String s = ""; int idx = 0; for (int i = 0; i < n; i++) { s += (char)('a'+idx); idx++; idx %= k; } System.out.println(s); } } static class Reader { static BufferedReader br; static StringTokenizer st; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } ; return Integer.parseInt(st.nextToken()); } public double nextDouble() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } ; return Double.parseDouble(st.nextToken()); } public Long nextLong() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } ; return Long.parseLong(st.nextToken()); } public String next() { if (st == null || !st.hasMoreTokens()) try { readLine(); } catch (Exception e) { } ; return st.nextToken(); } public static void readLine() { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } ; } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
4e1e852796c372c894b1de679ee81c70
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer stk =new StringTokenizer(br.readLine()); int t = Integer.parseInt(stk.nextToken()); StringBuilder sb= new StringBuilder(); for(int T=0;T<t;T++){ stk = new StringTokenizer(br.readLine()); int n = Integer.parseInt(stk.nextToken()); int k = Integer.parseInt(stk.nextToken()); int div = n/k; int i=0; if(n%k!=0){ for(i=0;i<n%k;i++){ sb.append('a'); } } char a = 'a'; for(;i<n;i+=div){ for(int j=0;j<div&&j+i<n;j++){ sb.append(a); } a+=1; } sb.append('\n'); } System.out.print(sb); } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
b37396f4cc0ee4103767a25abb587b1e
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.BufferedInputStream; import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { int t,n,k; Scanner sc = new Scanner(new BufferedInputStream(System.in)); t = Integer.parseInt(sc.next()); StringBuilder s = new StringBuilder(""); int idx = 0; while(t>0) { idx = 0; s.delete(0, s.length()); n = Integer.parseInt(sc.next()); k = Integer.parseInt(sc.next()); for (int i = 0; i < n; i++) { char p = (char)('a'+idx); s.append(p); idx += 1; if (idx == k) idx = 0; } t--; System.out.println(s.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
f17f68bc4341a55e30803ed259487c55
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main(String[] args) { int t,n,k; FastScanner in = new FastScanner(System.in); t = in.nextInt(); StringBuilder s = new StringBuilder(""); int idx = 0; while(t-->0) { idx = 0; s.delete(0, s.length()); n = in.nextInt(); k = in.nextInt(); for (int i = 0; i < n; i++) { char p = (char) ('a' + idx); s.append(p); idx += 1; if (idx == k) idx = 0; } System.out.println(s.toString()); } } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream ins){ try{ br = new BufferedReader(new InputStreamReader(ins)); } catch (Exception e){ e.printStackTrace();; } } String next(){ while(st == null || !st.hasMoreTokens()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
e09eb6561621abee0625699d4150303e
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { int t,n,k; Scanner sc = new Scanner(System.in); t = Integer.parseInt(sc.next()); while(t>0) { StringBuilder s = new StringBuilder(""); int idx = 0; n = Integer.parseInt(sc.next()); k = Integer.parseInt(sc.next()); for (int i = 0; i < n; i++) { char p = (char)('a'+idx); s.append(p); idx += 1; if (idx == k) idx = 0; } t--; System.out.println(s.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
654d14b9c1fe74809297743fe8567c04
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { int t,n,k; Scanner sc = new Scanner(System.in); t = sc.nextInt(); while(t>0) { StringBuilder s = new StringBuilder(""); int idx = 0; n = sc.nextInt(); k = sc.nextInt(); for (int i = 0; i < n; i++) { char p = (char)('a'+idx); s.append(p); idx += 1; if (idx == k) idx = 0; } t--; System.out.println(s.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
b419d5be2dc257512365977ed14c6b43
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.io.BufferedInputStream; import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) { int t,n,k; Scanner sc = new Scanner(new BufferedInputStream(System.in)); t = Integer.parseInt(sc.next()); while(t>0) { StringBuilder s = new StringBuilder(""); int idx = 0; n = Integer.parseInt(sc.next()); k = Integer.parseInt(sc.next()); for (int i = 0; i < n; i++) { char p = (char)('a'+idx); s.append(p); idx += 1; if (idx == k) idx = 0; } t--; System.out.println(s.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
e0c038518a2b53dc45e01a84e9b085f2
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner in=new Scanner(System.in); int t,n,k; char a[]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; t=in.nextInt(); for(int i=0;i<t;i++) { n=in.nextInt(); k=in.nextInt(); char ans[]=new char[n]; char b[]=new char[k]; for(int j=0;j<k;j++) { b[j]=a[j]; } for(int m=0,x=0;m<n;m++,x++) { if(x==k) { x=0; } ans[m]=b[x]; } String A=new String(ans); System.out.println(A); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
43823443dde0e332765a139058dc204b
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.Vector; import javafx.util.Pair; import java.*; public class test { static Scanner s= new Scanner(System.in); public static void main(String[] args) { int n =s.nextInt(); char[] letters= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; while(n>0) { StringBuilder sb = new StringBuilder(); int len=s.nextInt(); int k=s.nextInt(); if(len==k) { for(int i=0;i<len;i++) { sb.append(letters[i]); } } else { int x=len%k; if(x==0) { while(sb.length()<len) { for(int i=0;i<k;i++) sb.append(letters[i]); } } else { int j=0; while(sb.length()<len) { if(j>k-1) j=0; sb.append(letters[j]); j++; } } } n--; System.out.println(sb.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
8df6ce6f38bbb81c48e4ca0b264a30c8
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
//package Contest527; import java.io.PrintWriter; import java.util.Scanner; public class mainA { public static Scanner enter = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t=enter.nextInt(); for (int i = 0; i <t ; i++) { int n=enter.nextInt(); int k=enter.nextInt(); String tmp=""; for (int l = 0; l <n ;) { for (int j = 0; j <k && l<n; j++, l++) { char s=(char)('a'+j); tmp+=s; } } System.out.println(tmp); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
44f853c26ad51fd04c045a10a798fa9f
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String letters = "abcdefghijklmnopqrstuvwxyz"; int tCase = in.nextInt(); for (int i = 0; i < tCase; i++) { int n = in.nextInt(); int k = in.nextInt(); int add = n%k; int fre = n/k; String ans = ""; for (int j = 0; j < k; j++) { if(add > 0){ char[] chars = new char[fre+1]; Arrays.fill(chars, letters.charAt(j)); ans += new String(chars); add--; } else{ char[] chars = new char[fre]; Arrays.fill(chars, letters.charAt(j)); ans += new String(chars); } } System.out.println(ans); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
07695d00d3c8f91dbc2e451e36c9183a
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String letters = "abcdefghijklmnopqrstuvwxyz"; int tCase = in.nextInt(); for (int i = 0; i < tCase; i++) { int n = in.nextInt(); int k = in.nextInt(); int add = n%k; int fre = n/k; String ans = ""; for (int j = 0; j < k; j++) { if(add > 0){ for (int l = 0; l < fre+1; l++) { ans += letters.charAt(j); } add--; } else{ for (int l = 0; l < fre; l++) { ans += letters.charAt(j); } } } System.out.println(ans); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
e519c73f098a93e10554a78b690d855b
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; /* Name of the class has to be "Main" only if the class is public. */ public final class Codechef { public static void main(String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int T = s.nextInt(); while (T-- > 0) { int N = s.nextInt(); int K = s.nextInt(); int freq = N / K; char ch = 'a'; int c = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < N; i++) { ans.append(ch); c++; if (c == freq) { c = 0; ch++; if(ch > ('a' + K-1)) { ch = 'a'; } } } System.out.println(ans.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
909f0a7e9d14ad66de8026313e803b08
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); byte t = sc.nextByte(); short c = 0; byte n, k; for (byte i = 0; i < t; i++){ n = sc.nextByte(); k = sc.nextByte(); for (byte j = (int)'a'; ; j++) { if (j > (int)'a'+k-1) j = (int)'a'; System.out.print((char)j); c++; if (c == n) { c = 0; System.out.println(); break; } } } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
25868f55b002b144b0be905e31264f37
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; import java.math.*; public class Main { public static void main(String []args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int a = -1; int b = -1; Character [] ar = new Character[26]; int [] frequency = new int[26]; for(int i=0;i<26;i++){ ar[i]=(char)(i+97); frequency[i]=0; //System.out.print(ar[i]); } int maxFrequency = 0; StringBuilder sb = new StringBuilder(); while(n>0){ --n; sb.setLength(0); a = scanner.nextInt(); b = scanner.nextInt(); maxFrequency = (int)(a/b); for(int i=0;i<26;i++){ frequency[i]=0; } for(int j=0;j<a;j++){ if(frequency[j%b]<=maxFrequency){ frequency[j%b]+=1; sb.append(ar[j%b]); } } System.out.println(sb.toString()); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
f5de77026db7751ea2d18cf04f5054b1
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { 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 re=sc.nextInt(); String s=""; int i; for(i=0;i<re;i++) { s+=(char)('a'+i); } if(s.length()==n) System.out.println(s); else if(n%s.length()==0) { int div=n/s.length(); String s1=s; for(i=0;i<div-1;i++) s+=s1; System.out.println(s); } else { int div=n/s.length(); String s1=s; for(i=0;i<div-1;i++) s+=s1; int left=n-(re*div); //System.out.println(left); char ch=s.charAt(s.length()-1); int c=(ch-'a')+1; //System.out.println(c); for(i=0;i<left;i++) { if(c>=re) { c=0; } s+=(char)('a'+c); c+=1; } System.out.println(s); } } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
c792beb1832c2a7a6d5119be1a4c60cf
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int T = input.nextInt(); for (int i = 0; i < T; i++) { int n = input.nextInt(); int k = input.nextInt(); uniformString(n, k); } } private static void uniformString(int n, int k) { char c= (char)97; while(n>0) { System.out.print(c); c=(char) (c+1); if(c>96+k) c=97; n--; } System.out.println(); } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
4ff1bff4e9e3424f0ccbf3abf096c865
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int m=sc.nextInt(); int min=n/m; int mini=n%m; int l=n-mini; int s=0; int count=0; // System.out.println(min+" "+mini); for(char c='a';c<='z';c++) { if(count<l) { s=min; while(s>0) { System.out.print(c); s--; count++; } } } while(mini>0) { System.out.print("a"); mini--; } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
bd4562e397c12ee514f4be8093ab8b1d
train_002.jsonl
1545143700
You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.
256 megabytes
import java.util.*; public class UniformString{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int ti=0;ti<t;ti++){ int n = sc.nextInt(); int k = sc.nextInt(); int f = n/k; for(int i=0;i<n;i++){ System.out.print((char)(i%k+97)); } System.out.println(); } } }
Java
["3\n7 3\n4 4\n6 2"]
1 second
["cbcacab\nabcd\nbaabab"]
NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).
Java 8
standard input
[ "implementation" ]
fa253aabcccd487d09142ea882abbc1b
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.
800
Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.
standard output
PASSED
28ec1197c6da03709f4d9f4d2f2c99b3
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; public class d1 extends PrintWriter { static BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); // static Scanner s=new Scanner(System.in); d1 () { super(System.out); } public static void main(String[] args) throws IOException{ d1 d1=new d1 ();d1.main();d1.flush(); } void main() throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); StringBuffer sb = new StringBuffer(); StringBuffer sb1 = new StringBuffer(); PrintWriter out = new PrintWriter(System.out); // Scanner s=new Scanner(System.in); // System.out.println(sb.length()); String[] s1=s(); int n=i(s1[0]);int m=i(s1[1]);int k=i(s1[2]); ArrayList<Pair> []adj=new ArrayList[n+1]; for(int i=0;i<m;i++){ String[] s2=s(); int u=i(s2[0]);int v=i(s2[1]);long val=i(s2[2]); if(adj[u]==null) adj[u]=new ArrayList<>(); if(adj[v]==null) adj[v]=new ArrayList<>(); adj[u].add(new Pair(v,val,0)); adj[v].add(new Pair(u,val,0)); } PriorityQueue<Pair> pq=new PriorityQueue(new Comparator<Pair>(){ public int compare(Pair a,Pair b){ if(a.val<b.val) return -1; if(a.val==b.val) {return a.train-b.train; }return 1; } }); int k1=k; while(k1-->0){ String[] s3=s(); int u=i(s3[0]);long dist=i(s3[1]); if(adj[1]==null) adj[1]=new ArrayList<>(); if(adj[u]==null) adj[u]=new ArrayList<>(); adj[u].add(new Pair(1,dist,1)); adj[1].add(new Pair(u,dist,1)); } long[] dis=new long[n+1]; Arrays.fill(dis,Long.MAX_VALUE); dis[1]=0; int[] vis=new int[n+1];int count=0; pq.add(new Pair(1,0,0)); while(!pq.isEmpty()){ Pair p=pq.poll(); if(vis[p.a]==1) continue; vis[p.a]=1; dis[p.a]=p.val; if(p.train==1) count++; for(Pair j:adj[p.a]){ if(dis[p.a]+j.val<dis[j.a]&&vis[j.a]==0){ pq.add(new Pair(j.a,dis[p.a]+j.val,j.train)); } } } System.out.println(k-count); } public void sort(int[] a,int l,int h){ if(l==h) return; int mid=(l+h)/2; sort(a,l,mid); sort(a,mid+1,h); merge(a,l,(l+h)/2,h); } void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); } static int i(String ss) { return Integer.parseInt(ss); } static long l(String ss) { return Long.parseLong(ss); } } class Student { int l;long r; public Student(int l, long r) { this.l = l; this.r = r; } public String toString() { return this.l+" "; } } class Pair { int a,b;long val;int edgeVal;int train; public Pair(int a,long val,int train){ this.a=a;this.val=val;this.train=train;} } class Sortbyroll implements Comparator<Student> { public int compare(Student a, Student b){ if(a.r<b.r) return -1; else if(a.r==b.r){ if(a.r==b.r){ return 0; } if(a.r<b.r) return -1; return 1;} return 1; } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
7c6bf3740358ca356e9878301c18e308
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
//package a2oj; import java.util.*; import java.io.*; public class JzzhuAndCities { private static ArrayList<LinkedList<Pair>>graph = new ArrayList<LinkedList<Pair>>(); private static long[] dist; private static boolean[] trainAvail; private static TreeMap<Long,TreeSet<Integer>>queue=new TreeMap<Long,TreeSet<Integer>>(); public static void main(String[]args){ MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n=sc.nextInt(), m=sc.nextInt(), k=sc.nextInt(); for(int i=0;i<n;i++) graph.add(new LinkedList<Pair>()); for(int i=0;i<m;i++){ int a=sc.nextInt()-1, b=sc.nextInt()-1; long d=sc.nextLong(); graph.get(a).add(new Pair(d,b)); graph.get(b).add(new Pair(d,a)); } dist=new long[n]; trainAvail=new boolean[n]; for(int i=1;i<n;i++) dist[i]=Long.MAX_VALUE; for(int i=0;i<k;i++){ int c=sc.nextInt()-1; add(dist[c],sc.nextLong(),c,true); } queue.put(0L,new TreeSet<Integer>()); queue.get(0L).add(0); while(!queue.isEmpty()) dijkstra(pollFirst()); int ct=0; for(int i=0;i<n;i++) if(trainAvail[i]) ct++; System.out.println(k-ct); out.close(); } private static void add(long oldD, long d, int c, boolean train){ if(d<=oldD){ if(oldD != Long.MAX_VALUE){ queue.get(oldD).remove(c); if(queue.get(oldD).isEmpty()) queue.remove(oldD); } if(!queue.containsKey(d)) queue.put(d,new TreeSet<Integer>()); queue.get(d).add(c); dist[c] = d; trainAvail[c]=train; } } private static int pollFirst(){ int x=queue.firstEntry().getValue().pollFirst(); if(queue.firstEntry().getValue().isEmpty()) queue.pollFirstEntry(); return x; } private static void dijkstra(int a){ for(Pair p:graph.get(a)) add(dist[p.city], dist[a]+p.dist,p.city,false); } public static class Pair{ public long dist; public int city; public Pair(long d,int c){ dist=d; city=c; } } public static PrintWriter out; 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
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
c9d15ea5690babed47dd290f86b1391f
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.BufferedReader; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[] u = new int[m]; int[] v = new int[m]; int[] x = new int[m]; for (int i = 0; i < m; i++) { u[i] = in.nextInt(); v[i] = in.nextInt(); x[i] = in.nextInt(); } int[] s = new int[k]; int[] y = new int[k]; for (int i = 0; i < k; i++) { s[i] = in.nextInt(); y[i] = in.nextInt(); } int[] from = new int[2 * (m + k)]; int[] to = new int[2 * (m + k)]; int[] cost = new int[2 * (m + k)]; for (int i = 0; i < m; i++) { from[2 * i] = u[i]; to[2 * i] = v[i]; cost[2 * i] = x[i]; from[2 * i + 1] = v[i]; to[2 * i + 1] = u[i]; cost[2 * i + 1] = x[i]; } for (int i = 0; i < k; i++) { from[2 * m + 2 * i] = 1; to[2 * m + 2 * i] = s[i]; cost[2 * m + 2 * i] = y[i]; from[2 * m + 2 * i + 1] = s[i]; to[2 * m + 2 * i + 1] = 1; cost[2 * m + 2 * i + 1] = y[i]; } int[] next = new int[2 * (m + k)]; int[] first = new int[n + 1]; for (int i = 0; i < first.length; i++) { first[i] = -1; } for (int i = 0; i < from.length; i++) { next[i] = first[from[i]]; first[from[i]] = i; } long[] shortest = new long[n + 1]; for (int i = 2; i <= n; i++) { shortest[i] = Long.MAX_VALUE / 2; } TreeSet<Integer> visited = new TreeSet<Integer>((Integer fi, Integer se) -> shortest[fi] == shortest[se] ? Integer.compare(fi, se) : Long.compare(shortest[fi], shortest[se])); visited.add(1); while (!visited.isEmpty()) { Integer cur = visited.first(); visited.remove(cur); for (int curEdge = first[cur]; curEdge >= 0; curEdge = next[curEdge]) { if (shortest[from[curEdge]] + cost[curEdge] < shortest[to[curEdge]]) { visited.remove(to[curEdge]); shortest[to[curEdge]] = shortest[from[curEdge]] + cost[curEdge]; visited.add(to[curEdge]); } } } int[] adj = new int[n + 1]; for (int i = 2; i <= n; i++) { adj[i] = Integer.MAX_VALUE; } for (int node = 2; node <= n; node++) { for (int curEdge = first[node]; curEdge >= 0; curEdge = next[curEdge]) { if (shortest[to[curEdge]] + cost[curEdge] == shortest[from[curEdge]]) { adj[node] = Math.min(adj[node], curEdge); } } } int ans = 0; for (int i = 0; i < k; i++) { if (adj[s[i]] < 2 * m || adj[s[i]] != 2 * (m + i) + 1) ans++; } out.println(ans); } } static class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String next() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
dbd7e0cb9d77eee65c28f0565f7ab1f1
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedWriter; import java.util.*; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { class Edge implements Comparable<Edge>{ int to; long weight; Edge(int to, long weight) { this.to=to; this.weight=weight; } public int compareTo(Edge other) { if (weight!=other.weight) return weight<other.weight?-1:1; return 0; } } List<Edge>[] graph; long[] dist; boolean[] arr; public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(),m=in.readInt(), k=in.readInt(); graph=new List[n]; dist=new long[n]; Arrays.fill(dist,Long.MAX_VALUE); arr=new boolean[n]; for (int i=0; i<n; i++) graph[i]=new ArrayList<>(); while (m-->0) { int u=in.readInt()-1, v=in.readInt()-1, w=in.readInt(); graph[u].add(new Edge(v, w)); graph[v].add(new Edge(u, w)); } int ret=0; for(int i=0;i<k;i++){ int u=in.readInt()-1, w=in.readInt(); if(w<dist[u]){ dist[u]=w; arr[u]=true; } } dijkstra(0); for(int i=0;i<n;i++)if(arr[i])ret++; out.printLine(k-ret); } void dijkstra(int start) { dist[start]=0; PriorityQueue<Edge> pq=new PriorityQueue<>(); pq.add(new Edge(start, 0)); for(int i=0;i<graph.length;i++) if(arr[i])pq.add(new Edge(i,dist[i])); //System.out.println(arr); while (!pq.isEmpty()) { Edge edge=pq.poll(); int u=edge.to; long d=edge.weight; if (d>dist[u]) continue; for (Edge i: graph[u]) { int v=i.to; long w=i.weight; if (d+w<=dist[v]) { dist[v]=d+w; pq.add(new Edge(v, dist[v])); arr[v]=false; } } graph[u].clear(); } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
9975c40939110535a6add32b76ca1bf2
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; import java.math.BigInteger; public class Main { static MyScanner scan; static PrintWriter pw; static long MOD = 1_000_000_007; static long INF = 1_000_000_000_000_000_000L; static long inf = 2_000_000_000; public static void main(String[] args) { new Thread(null,null,"BaZ",1<<25) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static void solve() throws IOException { //initIo(true); initIo(false); StringBuilder sb = new StringBuilder(); int n = ni(), m = ni(), k = ni(); ArrayList<Pair> adj[] = new ArrayList[n+1]; for(int i=1;i<=n;++i) { adj[i] = new ArrayList<>(); } for(int i=0;i<m;++i) { int u = ni(), v = ni(), w = ni(); adj[u].add(new Pair(v,w)); adj[v].add(new Pair(u,w)); } int count = 0; PriorityQueue<Tuple> pq = new PriorityQueue<>(); for(int i=0;i<k;++i) { pq.add(new Tuple(ni(), ni(), 1)); } pq.add(new Tuple(1,0,0)); boolean vis[] = new boolean[n+1]; long min[] = new long[n+1]; Arrays.fill(min, Long.MAX_VALUE); min[1] = 0; while(!pq.isEmpty()) { Tuple curr = pq.poll(); if(vis[curr.x]) { if(curr.type==1) { count++; } continue; } vis[curr.x] = true; for(Pair p : adj[curr.x]) { if(min[p.x] > curr.y + p.y) { min[p.x] = curr.y + p.y; pq.add(new Tuple(p.x, min[p.x], 0)); } } } pl(count); pw.flush(); pw.close(); } static class Pair { int x,y; Pair(int x, int y) { this.x = x; this.y = y; } } static class Tuple implements Comparable<Tuple> { int x, type; long y; Tuple(int x, long y, int type) { this.x = x; this.y = y; this.type = type; } public int compareTo(Tuple other) { if(this.y > other.y) { return 1; } else if(this.y < other.y) { return -1; } else return this.type - other.type; } } static void initIo(boolean isFileIO) throws IOException { scan = new MyScanner(isFileIO); if(isFileIO) { pw = new PrintWriter("/Users/amandeep/Desktop/output2.txt"); } else { pw = new PrintWriter(System.out, true); } } static int ni() throws IOException { return scan.nextInt(); } static long nl() throws IOException { return scan.nextLong(); } static double nd() throws IOException { return scan.nextDouble(); } static String ne() throws IOException { return scan.next(); } static String nel() throws IOException { return scan.nextLine(); } static void pl() { pw.println(); } static void p(Object o) { pw.print(o+" "); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(String arrayName, Object arr[]) { pl(arrayName+" : "); for(Object o : arr) p(o); pl(); } static void pa(String arrayName, int arr[]) { pl(arrayName+" : "); for(int o : arr) p(o); pl(); } static void pa(String arrayName, long arr[]) { pl(arrayName+" : "); for(long o : arr) p(o); pl(); } static void pa(String arrayName, double arr[]) { pl(arrayName+" : "); for(double o : arr) p(o); pl(); } static void pa(String arrayName, char arr[]) { pl(arrayName+" : "); for(char o : arr) p(o); pl(); } static void pa(String listName, List list) { pl(listName+" : "); for(Object o : list) p(o); pl(); } static void pa(String arrayName, Object[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(String arrayName, int[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(String arrayName, long[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(String arrayName, char[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(String arrayName, double[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner(boolean readingFromFile) throws IOException { if(readingFromFile) { br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input2.txt")); } else { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextLine()throws IOException { return br.readLine(); } String next() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.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()); } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
611cb84846e8bb3e657b100a90f8bef9
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; public class p0449B { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line; line = br.readLine().split(" "); int N = Integer.parseInt(line[0]); int M = Integer.parseInt(line[1]); int K = Integer.parseInt(line[2]); ArrayList<Edge>[] nexts = new ArrayList[N]; for(int n=0;n<N;n++){ nexts[n] = new ArrayList<>(); } for(int m=0;m<M;m++){ line = br.readLine().split(" "); int U = Integer.parseInt(line[0])-1; int V = Integer.parseInt(line[1])-1; long X = Integer.parseInt(line[2]); Edge e = new Edge(U, V, X, false); nexts[U].add(e); nexts[V].add(e); } for(int k = 0; k < K; k++){ line = br.readLine().split(" "); int S = Integer.parseInt(line[0])-1; long L = Integer.parseInt(line[1]); Edge e = new Edge(0, S, L, true); nexts[0].add(e); nexts[S].add(e); } long[] dist = new long[N]; Arrays.fill(dist, 1_000_000_000_000_000_000L); PriorityQueue<Edge> pq = new PriorityQueue<>(new Comparator<Edge>(){ public int compare(Edge o1, Edge o2) { if(o1.buildDist < o2.buildDist) return -1; if(o1.buildDist > o2.buildDist) return 1; if(!o1.isTrainEdge && o2.isTrainEdge) return -1; if(o1.isTrainEdge && !o2.isTrainEdge) return 1; return 0; } }); pq.offer(new Edge(-1, 0, 0, false)); Edge[] prevs = new Edge[N]; int ans = K; int countMK = 0, countPoll = 0; while(!pq.isEmpty()){ Edge poll = pq.poll(); int u = poll.forwards? poll.b: poll.a; countMK++; if(dist[u] != 1_000_000_000_000_000_000L) continue; prevs[u] = poll; dist[u] = poll.buildDist; if(poll.isTrainEdge){ ans--; } countPoll++; for(Edge e : nexts[u]){ if(e.isQueued) continue; int v = e.other(u); e.forwards = (u == e.a? true: false); e.buildDist = dist[u] + e.dist; e.isQueued = true; pq.offer(e); } } // System.out.println(">>"+countMK+" "+countPoll); System.out.println(ans); } static class Edge{ int a, b; long dist, buildDist = 0; boolean isTrainEdge, isQueued = false, forwards = true; Edge(int aa, int bb, long distt, boolean isTrainEdgee){ a = aa; b = bb; if(a > b){ int s = a; a = b; b = s; } dist = distt; isTrainEdge = isTrainEdgee; } int other(int i){ return a == i? b: a; } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
b6b06a5e80e4606f0ea48d49091e591f
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codeforce_449_B { static int n; static int m; static int k; static class Edge { int u, v, index; long w; boolean train; public Edge(int u, int v, long w, boolean train, int index) { this.u = u; this.v = v; this.w = w; this.train = train; this.index = index; } } static class Node implements Comparable<Node> { int at; long dist; int normal; int train; public Node(int at, long dist) { this.at = at; this.dist = dist; } @Override public int compareTo(Node o) { int diff = Long.compare(this.dist, o.dist); if (diff != 0) return diff; return Integer.compare(this.at, o.at); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return at == node.at; } @Override public int hashCode() { return at; } void incident(Edge edge) { if (edge.train) { train ++; } else { normal ++; } } void clearState() { train = 0; normal = 0; } boolean requiredTrain() { return train > 0 && normal <= 0; } } static HashMap<Integer, ArrayList<Edge>> ady; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); ady = new HashMap<>(); for (int i = 0; i < m; i++) { st = new StringTokenizer(in.readLine()); int u = Integer.parseInt(st.nextToken()); int v = Integer.parseInt(st.nextToken()); long w = Long.parseLong(st.nextToken()); addEdge(new Edge(u, v, w, false, i), ady); addEdge(new Edge(v, u, w, false, i), ady); } for (int i = 0; i < k; i++) { st = new StringTokenizer(in.readLine()); int v = Integer.parseInt(st.nextToken()); long w = Long.parseLong(st.nextToken()); Edge trainEdge = new Edge(1, v, w, true, i); Edge trainEdgeRev = new Edge(1, v, w, true, i); addEdge(trainEdge, ady); addEdge(trainEdgeRev, ady); } long usedTrains = CountUsedTrains(1, ady); System.out.println(k - usedTrains); } static long CountUsedTrains(int source, HashMap<Integer, ArrayList<Edge>> ady) { TreeSet<Node> set = new TreeSet<>(); // Init dist values long[] dist = new long[n + 1]; Arrays.fill(dist, Long.MAX_VALUE); // Add source node dist[source] = 0; Node start = new Node(source, 0); set.add(start); HashMap<Integer, Node> nodes = new HashMap<>(); nodes.put(source, start); // Dijsktra main loop while (!set.isEmpty()) { Node top = set.pollFirst(); int u = top.at; for (Edge edge : ady.get(u)) { int v = edge.v; long w = edge.w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; Node next; if (nodes.containsKey(v)) { next = nodes.get(v); next.dist = dist[v]; } else { next = new Node(v, dist[v]); nodes.put(v, next); } next.clearState(); next.incident(edge); set.remove(next); set.add(next); } else if (dist[v] == dist[u] + w) { nodes.get(v).incident(edge); } } } long ans = 0; for (Node node : nodes.values()) { if (node.requiredTrain()) { ans ++; } } return ans; } static void addEdge(Edge edge, HashMap<Integer, ArrayList<Edge>> ady) { if (!ady.containsKey(edge.u)) { ady.put(edge.u, new ArrayList<>()); } ady.get(edge.u).add(edge); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
540844b18f1106064006ed4cae5b4ee1
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.*; import java.util.*; public class CF449B { static final boolean _DEBUG = true; static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner(BufferedReader _br) { br = _br; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); return ""; } } 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 MyScanner scan; static PrintWriter out; static int debugCount = 0; static void debug(String msg) { if (_DEBUG && debugCount < 200) { out.println(msg); out.flush(); debugCount++; } } public static void main (String args[]) throws IOException { // scan = new MyScanner(new BufferedReader(new FileReader("test.in"))); scan = new MyScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); CF449B inst = new CF449B(); inst.execute(); out.close(); } Node[] nodes; int[][] edges; int[][] trains; class Node { int id; ArrayList<int[]> nbs = new ArrayList<int[]>(); public Node(int _id) { id = _id; } int getOther(int[] e) { return (e[1] == id) ? e[0] : e[1]; } int dist = Integer.MAX_VALUE; boolean visited = false; } class MyCmp implements Comparator<int[]>{ @Override public int compare(int[] o1, int[] o2) { if (o1[2] != o2[2]) { return o1[2] - o2[2]; } else { return o1[0] - o2[0]; } } } void execute() throws IOException { int n = scan.nextInt(); int m = scan.nextInt(); int k = scan.nextInt(); nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(i); } edges = new int[m][3]; for (int i = 0; i < m; i++) { edges[i][0] = scan.nextInt()-1; edges[i][1] = scan.nextInt()-1; edges[i][2] = scan.nextInt(); nodes[edges[i][0]].nbs.add(edges[i]); nodes[edges[i][1]].nbs.add(edges[i]); } trains = new int[k][3]; for (int i = 0; i < k; i++) { trains[i][0] = 1; trains[i][1] = scan.nextInt() - 1; trains[i][2] = scan.nextInt(); } PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new MyCmp()); Node root = nodes[0]; root.dist = 0; root.visited = true; for (int[] e : root.nbs) { int[] t = new int[3]; t[0] = 0; t[1] = root.getOther(e); t[2] = e[2]; queue.add(t); } for (int[] e : trains) { queue.add(e); } int ans = 0; while(!queue.isEmpty()) { int[] top = queue.poll(); Node cur = nodes[top[1]]; if (cur.visited) continue; cur.visited = true; if (top[0] == 1) { ans++; } int wt = top[2]; for (int[] e : cur.nbs) { Node next = nodes[cur.getOther(e)]; if (!next.visited) { int nwt = wt + e[2]; if (nwt < next.dist) { next.dist = nwt; queue.add(new int[] {0, next.id, nwt}); } } } } out.println(k - ans); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
8902027eee2e0b0abaf178d688fb4a59
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
//package practice; import java.util.*; import java.io.*; public class Practice { static ArrayList<edge> list[]; static boolean[] vis; static class edge implements Comparable<edge>{ int v; long w; boolean t; edge(int v, long w,boolean t) { this.v=v; this.w=w; this.t=t; } public int compareTo(edge e){ if(Long.compare(w, e.w)==0){ if(t!=e.t) { if(t) return 1; else return -1; } else return 0; } return Long.compare(w,e.w); } } public static void main(String args[]) { InputReader in=new InputReader(System.in); int n=in.nextInt(); int m=in.nextInt(); int k=in.nextInt(); list=new ArrayList[n+1]; for(int i=0;i<=n;i++) list[i]=new ArrayList<>(); vis=new boolean[n+1]; while(m-->0) { int a=in.nextInt(); int b=in.nextInt(); long c=in.nextLong(); list[a].add(new edge(b,c,false)); list[b].add(new edge(a,c,false)); } PriorityQueue<edge> pq=new PriorityQueue<>(); while(k-->0) { pq.add(new edge(in.nextInt(),in.nextLong(),true)); } pq.offer(new edge(1,0,false)); int ans=0; while(!pq.isEmpty()) { edge temp=pq.poll(); if(vis[temp.v]) { if(temp.t) ans++; continue; } vis[temp.v]=true; for(edge now:list[temp.v]) { pq.add(new edge(now.v,now.w+temp.w,false)); } } System.out.println(ans); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c & 15; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) array[i] = nextInt(); return array; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sign = 1; if (c == '-') { sign = -1; c = read(); } long result = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); result *= 10; result += c & 15; c = read(); } while (!isSpaceChar(c)); return result * sign; } public long[] nextLongArray(int arraySize) { long array[] = new long[arraySize]; for (int i = 0; i < arraySize; i++) array[i] = nextLong(); return array; } public float nextFloat() // problematic { float result, div; byte c; result = 0; div = 1; c = (byte) read(); while (c <= ' ') c = (byte) read(); boolean isNegative = (c == '-'); if (isNegative) c = (byte) read(); do { result = result * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') while ((c = (byte) read()) >= '0' && c <= '9') result += (c - '0') / (div *= 10); if (isNegative) return -result; return result; } public double nextDouble() // not completely accurate { double ret = 0, div = 1; byte c = (byte) read(); while (c <= ' ') c = (byte) read(); boolean neg = (c == '-'); if (neg) c = (byte) read(); do { ret = ret * 10 + c - '0'; } while ((c = (byte) read()) >= '0' && c <= '9'); if (c == '.') while ((c = (byte) read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String nextLine() { int c = read(); StringBuilder result = new StringBuilder(); do { result.appendCodePoint(c); c = read(); } while (!isNewLine(c)); return result.toString(); } public boolean isNewLine(int c) { return c == '\n'; } public void close() { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
71ba7b2638bc7c8fe6a744f455e25042
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Locale; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public void run() { try { long prevTime = System.currentTimeMillis(); new Main().run(); System.err.println("Total time: " + (System.currentTimeMillis() - prevTime) + " ms"); System.err.println("Memory status: " + memoryStatus()); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Object o = solve(); if (o != null) out.println(o); out.close(); in.close(); } private Object solve() throws IOException { int n = ni(); int m = ni(); int k = ni(); List<Edge>[] g = (List<Edge>[]) new List[n]; for (int i = 0; i < n; i++) g[i] = new ArrayList<Edge>(); for (int i = 0; i < m; i++) { int u = ni() - 1; int v = ni() - 1; long d = ni(); g[u].add(new Edge(v, d, false)); g[v].add(new Edge(u, d, false)); } for (int i = 0; i < k; i++) { int u = 0; int v = ni() - 1; long d = ni(); g[u].add(new Edge(v, d, true)); g[v].add(new Edge(u, d, true)); } boolean[] ok = new boolean[n]; PriorityQueue<State> pq = new PriorityQueue<State>(); pq.add(new State(0, 0, false)); int count=0; while(!pq.isEmpty()){ State s = pq.poll(); if(ok[s.node]) continue; ok[s.node]=true; if(s.train) count++; int u = s.node; for(Edge e : g[u]) if(!ok[e.v]) pq.add(new State(e.v,e.d+s.value, e.isTrain)); } return k-count; } class State implements Comparable<State> { int node; long value; boolean train; public State(int node, long value, boolean train) { this.node = node; this.value = value; this.train = train; } @Override public int compareTo(State o) { if (this.value != o.value) return Long.compare(this.value ,o.value); if (this.train == o.train) return 0; if (this.train) return 1; return -1; } } class Edge { int v; long d; boolean isTrain; public Edge(int v, long d, boolean isTrain) { this.v = v; this.d = d; this.isTrain = isTrain; } } BufferedReader in; PrintWriter out; StringTokenizer strTok = new StringTokenizer(""); String nextToken() throws IOException { while (!strTok.hasMoreTokens()) strTok = new StringTokenizer(in.readLine()); return strTok.nextToken(); } int ni() throws IOException { return Integer.parseInt(nextToken()); } long nl() throws IOException { return Long.parseLong(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } int[] nia(int size) throws IOException { int[] ret = new int[size]; for (int i = 0; i < size; i++) ret[i] = ni(); return ret; } long[] nla(int size) throws IOException { long[] ret = new long[size]; for (int i = 0; i < size; i++) ret[i] = nl(); return ret; } double[] nda(int size) throws IOException { double[] ret = new double[size]; for (int i = 0; i < size; i++) ret[i] = nd(); return ret; } String nextLine() throws IOException { strTok = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!strTok.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; strTok = new StringTokenizer(s); } return false; } void printRepeat(String s, int count) { for (int i = 0; i < count; i++) out.print(s); } void printArray(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(long[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array, String spec) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.printf(Locale.US, spec, array[i]); } out.println(); } void printArray(Object[] array) { boolean blank = false; for (Object x : array) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } @SuppressWarnings("rawtypes") void printCollection(Collection collection) { boolean blank = false; for (Object x : collection) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } static String memoryStatus() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB"; } public void pln() { out.println(); } public void pln(int arg) { out.println(arg); } public void pln(long arg) { out.println(arg); } public void pln(double arg) { out.println(arg); } public void pln(String arg) { out.println(arg); } public void pln(boolean arg) { out.println(arg); } public void pln(char arg) { out.println(arg); } public void pln(float arg) { out.println(arg); } public void pln(Object arg) { out.println(arg); } public void p(int arg) { out.print(arg); } public void p(long arg) { out.print(arg); } public void p(double arg) { out.print(arg); } public void p(String arg) { out.print(arg); } public void p(boolean arg) { out.print(arg); } public void p(char arg) { out.print(arg); } public void p(float arg) { out.print(arg); } public void p(Object arg) { out.print(arg); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
b7ff41cbff2df1a1672f5ab0586762b6
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
/** * Created by Aminul on 8/21/2018. */ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.min; public class CF449B_2 { public static void main(String[] args)throws Exception { FastReader in = new FastReader(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); List<Edge> g[] = genList(n+1); for (int i = 0; i < m; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); g[u].add(new Edge(v, w)); g[v].add(new Edge(u, w)); } int train[] = new int[n+1]; for (int i = 0; i < k; i++) { int u = 1, v = in.nextInt(), w = in.nextInt(); g[u].add(new Edge(v, w, 1)); g[v].add(new Edge(u, w, 1)); train[v]++; } long dist[] = dijkstra(1, n+1, g); int cnt = 0; boolean found[] = new boolean[n+1]; for(int i = 1; i <= n; i++){ for(Edge e : g[i]){ if(dist[i] + e.w == dist[e.v]){ found[e.v] |= (e.type == 0); } else if(e.type == 1 && e.v != 1){ train[e.v]--; cnt++; } } } //debug(train, found, cnt); for (int i = 2; i <= n; i++) { if(found[i]) cnt += train[i]; else cnt += Math.max(0, train[i]-1); } pw.println(cnt); pw.close(); } static <T>List<T>[] genList(int n){ List<T> list[] = new List[n]; for(int i = 0; i < n; i++) list[i] = new ArrayList<T>(); return list; } static long inf = (long)1e15; static long[] dijkstra(int source, int n, List<Edge> g[]){ PriorityQueue<Edge> pq = new PriorityQueue<>(); long vis[] = new long[n]; boolean done[] = new boolean[n]; Arrays.fill(vis, inf); pq.add(new Edge(source, 0)); vis[source] = 0; while (!pq.isEmpty()){ Edge pop = pq.poll(); int u = pop.v; long w = pop.w; if(done[u]) continue; done[u] = true; for(Edge e : g[u]){ if(!done[e.v] && vis[e.v] > w + e.w){ vis[e.v] = w + e.w; pq.add(new Edge(e.v, w+e.w)); } } } return vis; } static class Edge implements Comparable<Edge>{ int v, type; long w; Edge(int vv, long ww){ v = vv; w = ww; } Edge(int vv, long ww, int Type){ v = vv; w = ww; type = Type; } public int compareTo(Edge o) { return Long.compare(w , o.w); } } static void debug(Object...obj) { System.err.println(Arrays.deepToString(obj)); } static class FastReader { InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; static final int ints[] = new int[128]; public FastReader(InputStream is){ for(int i='0';i<='9';i++) ints[i]=i-'0'; this.is = is; } public int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } public String next(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = (num<<3) + (num<<1) + ints[b]; }else{ return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = (num<<3) + (num<<1) + ints[b]; }else{ return minus ? -num : num; } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } /* public char nextChar() { return (char)skip(); }*/ public char[] next(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } /*private char buff[] = new char[1005]; public char[] nextCharArray(){ int b = skip(), p = 0; while(!(isSpaceChar(b))){ buff[p++] = (char)b; b = readByte(); } return Arrays.copyOf(buff, p); }*/ } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
7ead579b82be0b495b986d0f27853c3c
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
/* / フフ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ム / )\⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Y (⠀⠀| ( ͡° ͜ʖ ͡°)⠀⌒(⠀ ノ (⠀ ノ⌒ Y ⌒ヽ-く __/ | _⠀。ノ| ノ。 |/ (⠀ー '_人`ー ノ ⠀|\  ̄ _人'彡ノ ⠀ )\⠀⠀ 。⠀⠀ / ⠀⠀(\⠀ #⠀ / ⠀/⠀⠀⠀/ὣ====================D- /⠀⠀⠀/⠀ \ \⠀⠀\ ( (⠀)⠀⠀⠀⠀ ) ).⠀) (⠀⠀)⠀⠀⠀⠀⠀( | / |⠀ /⠀⠀⠀⠀⠀⠀ | / [_] ⠀⠀⠀⠀⠀[___] */ // Main Code at the Bottom import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; public class Main { //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=1000000000+7; //Euclidean Algorithm static long gcd(long A,long B){ return (B==0)?A:gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //Modular Inverse static long inverse(long x) { return fastExpo(x,MOD-2); } //Prime Number Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static void reverse(int arr[],int l,int r){ while(l<r) { int tmp=arr[l]; arr[l++]=arr[r]; arr[r--]=tmp; } } //Print array static void print1d(int arr[]) { out.println(Arrays.toString(arr)); } static void print2d(int arr[][]) { for(int a[]: arr) out.println(Arrays.toString(a)); } // Pair static class pair{ long x,y; pair(long a,long b){ this.x=a; this.y=b; } public boolean equals(Object obj) { if(obj == null || obj.getClass()!= this.getClass()) return false; pair p = (pair) obj; return (this.x==p.x && this.y==p.y); } public int hashCode() { return Objects.hash(x,y); } } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Main function(The main code starts from here static class node{ int dest, weight,type; node(int d,int w,int t){ dest=d; weight=w; type=t; } } static int V; static long inf=1000000000000000L; static ArrayList<node> graph[]; static void init(int n){ V=n; graph=new ArrayList[V]; for(int i=0;i<V;i++) graph[i]=new ArrayList<>(); } static void addEdge(int src,int dest,int weight,int type){ graph[src].add(new node(dest,weight,type)); graph[dest].add(new node(src,weight,type)); } static int dijkstra() { long dist[]=new long[V],visited[]=new long[V]; HashSet<Integer> set=new HashSet<>(); Arrays.fill(dist, inf); dist[0]=0; PriorityQueue<pair> q=new PriorityQueue<>((p1,p2)->(int)(p1.y==p2.y?p2.x-p1.x:p1.y-p2.y)); q.add(new pair(0,0)); while(!q.isEmpty()) { int s=(int)q.poll().x; if(visited[Math.abs(s)]==1) continue; if(s<0) { s=-s; set.add(s); } visited[s]=1; for(node x: graph[s]) { if(dist[s]+x.weight<=dist[x.dest]) { dist[x.dest]=dist[s]+x.weight; if(x.type==1) q.add(new pair(-x.dest,dist[x.dest])); else q.add(new pair(x.dest,dist[x.dest])); } } // for(pair p: q) out.print("("+p.x+","+p.y+")"); // out.println(); } //out.println(Arrays.toString(dist)); return set.size(); } public static void main (String[] args) throws java.lang.Exception { int test; test=1; //test=sc.nextInt(); while(test-->0){ int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(); init(n); while(m-->0) addEdge(sc.nextInt()-1,sc.nextInt()-1,sc.nextInt(),0); for(int i=0;i<k;i++) addEdge(0,sc.nextInt()-1,sc.nextInt(),1); out.println(k-dijkstra()); } out.flush(); out.close(); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
def56ae6193890dee622235aadd77632
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.util.*; import java.io.*; public class cf_449B //Jzzhu and Cities { public static class Edge implements Comparable<Edge> { public int loc, id; //type 0 is road, type>=1 is train public long weight; public Edge(int l, int i, long w) { loc = l; id = i; weight = w; } public int compareTo(Edge o) { if (Long.compare(weight, o.weight) == 0) return id - o.id; return Long.compare(weight, o.weight); } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); String[] line = in.readLine().split(" "); int numCities = Integer.parseInt(line[0]); int numRoads = Integer.parseInt(line[1]); int numTrains = Integer.parseInt(line[2]); ArrayList<Edge>[] adj = new ArrayList[numCities]; for (int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Edge>(); for (int i = 0; i < numRoads; i++) { line = in.readLine().split(" "); int from = Integer.parseInt(line[0]) - 1; int to = Integer.parseInt(line[1]) - 1; long weight = Long.parseLong(line[2]); adj[from].add(new Edge(to, 0, weight)); adj[to].add(new Edge(from, 0, weight)); } int routeid = 1; for (int i = 0; i < numTrains; i++) { line = in.readLine().split(" "); int to = Integer.parseInt(line[0]) - 1; long weight = Long.parseLong(line[1]); adj[0].add(new Edge(to, routeid, weight)); adj[to].add(new Edge(0, routeid, weight)); routeid++; } boolean[] visited = new boolean[numCities]; Arrays.fill(visited, false); ArrayList<Integer> allowed = new ArrayList<Integer>(); int ok = 0; PriorityQueue<Edge> pq = new PriorityQueue<Edge>(); pq.add(new Edge(0, 0, 0)); while (pq.size() > 0) { Edge now = pq.poll(); if (visited[now.loc]) continue; visited[now.loc] = true; if (now.id != 0) ok++; for (int i = 0; i < adj[now.loc].size(); i++) { Edge to = adj[now.loc].get(i); pq.add(new Edge(to.loc, to.id, now.weight + to.weight)); } } long output = numTrains - ok; out.write("" + output); out.flush(); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
4e18047a9185250c3be818aeaa17461b
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.util.*; import java.io.*; public class cf_449B //Jzzhu and Cities { public static class Edge implements Comparable<Edge> { public int loc, id; //type 0 is road, type 1 is train public long weight; public Edge(int l, int i, long w) { loc = l; id = i; weight = w; } public int compareTo(Edge o) { if (Long.compare(weight, o.weight) == 0) return id - o.id; return Long.compare(weight, o.weight); } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); String[] line = in.readLine().split(" "); int numCities = Integer.parseInt(line[0]); int numRoads = Integer.parseInt(line[1]); int numTrains = Integer.parseInt(line[2]); ArrayList<Edge>[] adj = new ArrayList[numCities]; for (int i = 0; i < adj.length; i++) adj[i] = new ArrayList<Edge>(); for (int i = 0; i < numRoads; i++) { line = in.readLine().split(" "); int from = Integer.parseInt(line[0]) - 1; int to = Integer.parseInt(line[1]) - 1; long weight = Long.parseLong(line[2]); adj[from].add(new Edge(to, 0, weight)); adj[to].add(new Edge(from, 0, weight)); } for (int i = 0; i < numTrains; i++) { line = in.readLine().split(" "); int to = Integer.parseInt(line[0]) - 1; long weight = Long.parseLong(line[1]); adj[0].add(new Edge(to, 1, weight)); adj[to].add(new Edge(0, 1, weight)); } boolean[] visited = new boolean[numCities]; Arrays.fill(visited, false); int ok = 0; PriorityQueue<Edge> pq = new PriorityQueue<Edge>(); pq.add(new Edge(0, 0, 0)); while (pq.size() > 0) { Edge now = pq.poll(); if (visited[now.loc]) continue; visited[now.loc] = true; ok += now.id; for (int i = 0; i < adj[now.loc].size(); i++) { Edge to = adj[now.loc].get(i); pq.add(new Edge(to.loc, to.id, now.weight + to.weight)); } } long output = numTrains - ok; out.write("" + output); out.flush(); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
079f1e8a3761308988d54fcd8b1f23ee
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.*; import java.util.*; public class Main { static final long INF = (long) 1e18; // don't increase, avoid overflow static ArrayList<Edge>[] adjList; static int V, parent[]; static class Edge implements Comparable<Edge> { int node; long cost; int f; Edge(int a, long b,int x) { node = a; cost = b; f=x; } public int compareTo(Edge e) { if(cost!=e.cost) return Long.compare(cost, e.cost); return f-e.f; } } static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException, Exception { Scanner sc = new Scanner(System.in); V = sc.nextInt(); int m = sc.nextInt(); int k=sc.nextInt(); adjList = new ArrayList[V + 1]; for (int i = 0; i <= V; i++) adjList[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int w = sc.nextInt(); adjList[a].add(new Edge(b, w,0)); adjList[b].add(new Edge(a, w,0)); } int res=k; for(int i=0;i<k;i++) { int a = sc.nextInt(); int b = sc.nextInt(); adjList[1].add(new Edge(a, b,1)); adjList[a].add(new Edge(1, b,1)); } PriorityQueue<Edge>que=new PriorityQueue<>(); que.add(new Edge(1,0,0)); boolean sel[]=new boolean[V+1]; long[]dist=new long[V+1]; Arrays.fill(dist, INF); while(!que.isEmpty()) { Edge x=que.poll(); if(sel[x.node])continue; sel[x.node]=true; if(x.f==1)res--; for(Edge to:adjList[x.node]) { if(x.cost+to.cost<=dist[to.node]) { que.add(new Edge(to.node,x.cost+to.cost,to.f)); } } } pw.println(res); pw.close(); } static void print(int n, int[] p) { if (n != 1) print(p[n], p); pw.print(n + " "); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
e2a186f74f395a7060cb3840c48d39d8
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.*; import java.util.*; public class Main { static final long INF = (long) 1e18; // don't increase, avoid overflow static ArrayList<Edge>[] adjList; static int V, parent[]; static class Edge implements Comparable<Edge> { int node; long cost; boolean f; Edge(int a, long b,boolean x) { node = a; cost = b; f=x; } public int compareTo(Edge e) { if(cost!=e.cost) return Long.compare(cost, e.cost); int t1=0,t2=0; if(!f)t1=1; if(!e.f)t2=1;return t1-t2; } } static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException, Exception { Scanner sc = new Scanner(System.in); V = sc.nextInt(); int m = sc.nextInt(); int k=sc.nextInt(); adjList = new ArrayList[V + 1]; for (int i = 0; i <= V; i++) adjList[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int w = sc.nextInt(); adjList[a].add(new Edge(b, w,true)); adjList[b].add(new Edge(a, w,true)); } int res=k; for(int i=0;i<k;i++) { int a = sc.nextInt(); int b = sc.nextInt(); adjList[1].add(new Edge(a, b,false)); adjList[a].add(new Edge(1, b,false)); } PriorityQueue<Edge>que=new PriorityQueue<>(); que.add(new Edge(1,0,true)); boolean sel[]=new boolean[V+1]; long[]dist=new long[V+1]; Arrays.fill(dist, INF); dist[1]=0; while(!que.isEmpty()) { Edge x=que.poll(); if(sel[x.node])continue; sel[x.node]=true; if(!x.f)res--; for(Edge to:adjList[x.node]) { if(dist[x.node]+to.cost<=dist[to.node]) { dist[to.node]=dist[x.node]+to.cost; que.add(new Edge(to.node,x.cost+to.cost,to.f)); } } } pw.println(res); pw.close(); } static void print(int n, int[] p) { if (n != 1) print(p[n], p); pw.print(n + " "); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
4121e57c779d7ed25a145fd9000fb1e4
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.*; import java.util.*; public class CF0449B { /** * If the length of the railroad between city 1 and i is longer than the minimal dist between 1 and i, delete * If the length of the railroad between city 1 and i is equal to the minimal dist between 1 and i, delete iff path is not unique * * This works because we can let u be the shortest path between 1 and i that doesn't use the train from 1 to u, * then the train path can be deleted iff * The length of u is longer than the train path * * The first case is easy to check * How do we deal with the second case? * * If I use the train, I add a boolean; * Let f[i]=true iff to go from 1 to i Jzzhu need a train * Initially f[i] is always true * If we find a path that doesn't use the train, turn it false * The answer is # of false entries */ public static class Edge implements Comparable<Edge>{ int v; int w; boolean train; public Edge(int a, int b, boolean c){ this.v=a; this.w=b; this.train=c; } public int compareTo(Edge other){ if(this.w<other.w)return -1; if(this.w>other.w)return 1; if(!this.train && other.train)return -1; if(this.train && !other.train)return 1; return 0; } } public static class Pair implements Comparable<Pair>{ int v; long dist; boolean train; public Pair(int a, long b, boolean c){ this.v=a; this.dist=b; this.train=c; } public int compareTo(Pair other){ if(this.dist<other.dist)return -1; if(this.dist>other.dist)return 1; if(!this.train && other.train)return -1; if(this.train && !other.train)return 1; return 0; //I don't want zeroes } } public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int N=Integer.parseInt(st.nextToken()); int M=Integer.parseInt(st.nextToken()); int K=Integer.parseInt(st.nextToken()); ArrayList<Edge> arl[]=new ArrayList[N]; for (int i = 0; i < N; i++) { arl[i]=new ArrayList<>(); } int a,b,c=0; for (int i = 0; i < M; i++) { st=new StringTokenizer(br.readLine()); a=Integer.parseInt(st.nextToken())-1; b=Integer.parseInt(st.nextToken())-1; c=Integer.parseInt(st.nextToken()); arl[a].add(new Edge(b,c,false)); arl[b].add(new Edge(a,c,false)); } int[] tt=new int[N];//tt[a]=0 means it's not used int dup=0;//Clear duplicate train roads, only keep min for (int i = 0; i < K; i++) { st=new StringTokenizer(br.readLine()); a=Integer.parseInt(st.nextToken())-1; c=Integer.parseInt(st.nextToken()); if(tt[a]==0){ tt[a]=c; }else{ dup++; tt[a]=Math.min(tt[a],c); } } for (int i = 1; i < N; i++) { if(tt[i]>0){ arl[0].add(new Edge(i,tt[i],true)); arl[i].add(new Edge(0,tt[i],true)); } } boolean[] train=new boolean[N]; long[] dist=new long[N]; Arrays.fill(dist,Long.MAX_VALUE); dist[0]=0; boolean[] visited=new boolean[N]; PriorityQueue<Pair> pq=new PriorityQueue<>(); pq.add(new Pair(0,0,false)); int done=0; while(done<N){ Pair p=pq.poll(); if(!visited[p.v]){ for (Edge e : arl[p.v]) { if(!visited[e.v]){ if(dist[e.v]>dist[p.v]+e.w){ dist[e.v]=dist[p.v]+e.w; pq.add(new Pair(e.v,dist[e.v],e.train)); train[e.v]=e.train; }else if(dist[e.v]==dist[p.v]+e.w){ train[e.v]&=e.train; } //System.out.println(done+" "+p.v+" "+e.v+" "+dist[e.v]+" "+e.train); } } visited[p.v]=true; done++; } } //System.out.println(Arrays.toString(train)); for (int i = 1; i < N; i++) { if(tt[i]>0 && !train[i]){ dup++; } } System.out.println(dup); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
bd1126c370818cb730b4a863af7907b2
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; public class Jzzhu { public static class Edge implements Comparable<Edge>{ public int loc; public long weight; public boolean train; public Edge(int l, long w, boolean t) { loc = l; weight = w; train = t; } public int compareTo(Edge o) { if(Long.compare(weight, o.weight)==0) { if(train!=o.train) { if(train) { return 1; } else { return -1; } } else { return 0; } } return Long.compare(weight, o.weight); } } public static void main(String[] args) throws IOException { // Scanner in = new Scanner(System.in); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] line = in.readLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); int k = Integer.parseInt(line[2]); ArrayList<Edge>[] adj = new ArrayList[n]; for(int i = 0; i<n; i++) { adj[i] = new ArrayList<Edge>(); } for(int i = 0; i<m; i++) { line = in.readLine().split(" "); int s = Integer.parseInt(line[0])-1; int e = Integer.parseInt(line[1])-1; int w = Integer.parseInt(line[2]); //this is undirected adj[s].add(new Edge(e,w,false)); adj[e].add(new Edge(s,w,false)); } boolean[] visited = new boolean[n]; long[] dist = new long[n]; Arrays.fill(visited, false); //path from 0 to n-1 PriorityQueue<Edge> pq = new PriorityQueue<Edge>(); pq.add(new Edge(0,0,false)); for(int i = 0; i<k; i++) { line = in.readLine().split(" "); int loc = Integer.parseInt(line[0])-1; int weight = Integer.parseInt(line[1]); pq.add(new Edge(loc,weight,true)); } int ans = 0; while(pq.size()>0) { Edge now = pq.poll(); if(visited[now.loc]) { if(now.train) { ans++; } continue; } dist[now.loc] = now.weight; visited[now.loc] = true; for(int i = 0; i<adj[now.loc].size(); i++) { Edge to = adj[now.loc].get(i); pq.add(new Edge(to.loc,to.weight+now.weight,false)); } } System.out.println(ans); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
3040fa2490460ea181807aa582b81177
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class JzzhuandCities { static class edge implements Comparable<edge> { int s; long d; boolean f; edge(int s, long d, boolean f) { this.s = s; this.d = d; this.f = f; } @Override public int compareTo(edge o) { if (this.d == o.d) return this.f == false ? -1 : 1; return this.d < o.d ? -1 : 1; } public String toString() { return "(" + this.s + " " + this.d + " " + (this.f ? "T" : "F") + ")"; } } static ArrayList<edge>[] al; static int n, k; static int dijkstra() throws IOException { long[] dist = new long[n]; Arrays.fill(dist, 999999999999999L); dist[0] = 0; PriorityQueue<edge> pq = new PriorityQueue<>(); pq.add(new edge(0, 0, false)); while (!pq.isEmpty()) { edge e = pq.poll(); if (dist[e.s] != e.d) continue; for (edge x : al[e.s]) { if (dist[e.s] + x.d < dist[x.s]) { dist[x.s] = dist[e.s] + x.d; pq.add(new edge(x.s, dist[x.s], false)); } } } int cnt = 0; while (k-- > 0) { int s = nxtInt() - 1; long d = nxtLng(); if (d < dist[s]) { dist[s] = d; pq.add(new edge(s, d, true)); } else { cnt++; } } boolean[] v = new boolean[n]; while (!pq.isEmpty()) { edge e = pq.poll(); if (dist[e.s] != e.d) { cnt += e.f ? 1 : 0; continue; } if (e.f && v[e.s]) cnt++; for (edge x : al[e.s]) { if (dist[e.s] + x.d < dist[x.s]) { dist[x.s] = dist[e.s] + x.d; pq.add(new edge(x.s, dist[x.s], false)); } else if (dist[e.s] + x.d == dist[x.s]) { v[x.s] = true; } } } return cnt; } @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new StringTokenizer(""); n = nxtInt(); int m = nxtInt(); k = nxtInt(); al = new ArrayList[n]; for (int i = 0; i < n; i++) al[i] = new ArrayList<>(); while (m-- > 0) { int u = nxtInt() - 1; int v = nxtInt() - 1; long d = nxtLng(); al[u].add(new edge(v, d, false)); al[v].add(new edge(u, d, false)); } out.println(dijkstra()); br.close(); out.close(); } static BufferedReader br; static StringTokenizer sc; static PrintWriter out; static String nxtTok() throws IOException { while (!sc.hasMoreTokens()) { String s = br.readLine(); if (s == null) return null; sc = new StringTokenizer(s.trim()); } return sc.nextToken(); } static int nxtInt() throws IOException { return Integer.parseInt(nxtTok()); } static long nxtLng() throws IOException { return Long.parseLong(nxtTok()); } static double nxtDbl() throws IOException { return Double.parseDouble(nxtTok()); } static int[] nxtIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nxtInt(); return a; } static long[] nxtLngArr(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nxtLng(); return a; } static char[] nxtCharArr() throws IOException { return nxtTok().toCharArray(); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
01881a6d63926fc6dedb38f03b07415d
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { solve(); } final static int MAXN = 5 * 1000000 + 5; final static int MAXK = 505; static int N = 0; static int M = 0; static int K = 0; static int W = 0; static int H = 0; static int[][] A; static long[][] dp; static int[][] pre; static int[][] g; static int curAns = Integer.MAX_VALUE; static long[] HASH = new long[MAXN]; static long MOD = (1L << 50) + 7; static void solve() { FastReader scanner = new FastReader(); PrintWriter out = new PrintWriter(System.out); N = scanner.nextInt(); M = scanner.nextInt(); K = scanner.nextInt(); Long[] directDist = new Long[N+1]; Long[] trainDist = new Long[N+1]; Arrays.fill(directDist, Long.MAX_VALUE); Arrays.fill(trainDist, Long.MAX_VALUE); Map<Pair<Integer, Integer>, Long> dist = new HashMap<>(); Map<Integer, List<Pair<Integer, Long>>> g = new HashMap<>(); for (int i = 0; i < M; i++) { int u = scanner.nextInt(); int v = scanner.nextInt(); int w = scanner.nextInt(); if (u == 1) { directDist[v] = Math.min(directDist[v], w); } else if (v == 1) { directDist[u] = Math.min(directDist[u], w); } else { Pair<Integer, Integer> k = u < v ? new Pair<>(u, v) : new Pair<>(v, u); dist.put(k, Math.min(dist.getOrDefault(k, Long.MAX_VALUE), w)); } } for (Map.Entry<Pair<Integer, Integer>, Long> entry: dist.entrySet()) { Pair<Integer, Integer> p = entry.getKey(); int u = p.getFirst(); int v = p.getSecond(); Long w = entry.getValue(); if (!g.containsKey(u)) { g.put(u, new ArrayList<>()); } g.get(u).add(new Pair<>(v, w)); if (!g.containsKey(v)) { g.put(v, new ArrayList<>()); } g.get(v).add(new Pair<>(u, w)); } Set<Integer> train = new HashSet<>(); for (int i = 0; i < K; i++) { int v = scanner.nextInt(); long w = scanner.nextLong(); if (w < directDist[v]) { directDist[v] = w; train.add(v); } trainDist[v] = Math.min(trainDist[v], w); } if (!g.containsKey(1)) { g.put(1, new ArrayList<>()); } for (int v = 2; v <= N; v++) { if (directDist[v] < Long.MAX_VALUE) { g.get(1).add(new Pair<>(v, directDist[v])); if (!g.containsKey(v)) { g.put(v, new ArrayList<>()); } g.get(v).add(new Pair<>(1, directDist[v])); } } Long[] distFromCap = new Long[N+1]; Arrays.fill(distFromCap, Long.MAX_VALUE); distFromCap[1] = 0L; PriorityQueue<Pair<Long, Integer>> q = new PriorityQueue<>(); int[] indegree = new int[N+1]; for (int u = 1; u <= N; u++) { if (directDist[u] < Long.MAX_VALUE) { distFromCap[u] = directDist[u]; q.add(new Pair<>(directDist[u], u)); indegree[u] = 1; } } while (!q.isEmpty()) { Pair<Long, Integer> p = q.poll(); long d = p.getFirst(); int u = p.getSecond(); if (d > distFromCap[u]) continue; for (Pair<Integer, Long> vw : g.getOrDefault(u, new ArrayList<>())) { int v = vw.getFirst(); long w = vw.getSecond(); Long nd = distFromCap[u] + w; if (nd < distFromCap[v]) { distFromCap[v] = nd; indegree[v] = 1; q.add(new Pair<>(nd, v)); } else if(nd.equals(distFromCap[v])) { indegree[v] += 1; } } } int ans = K - train.size(); for (int v: train) { if (distFromCap[v] < trainDist[v]) { ans += 1; } else if (distFromCap[v].equals(trainDist[v]) && indegree[v] > 1) { indegree[v] -= 1; ans += 1; } } out.println(ans); out.close(); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } } class UnioinSet { private Map<Long, Long> parents = new HashMap<>(); public Long find(Long u) { if (parents.getOrDefault(u, u).equals(u)) { return u; } return find(parents.get(u)); } public void union(Long u, Long v) { Long pu = find(u); Long pv = find(v); parents.put(pu, pv); } } class Pair<A, B> implements Comparable<Pair<A, B>>{ private A first; private B second; public Pair(A first, B second) { super(); this.first = first; this.second = second; } public String toString() { return "(" + first + ", " + second + ")"; } public A getFirst() { return first; } public void setFirst(A first) { this.first = first; } public B getSecond() { return second; } public void setSecond(B second) { this.second = second; } @Override public int compareTo(Pair<A, B> o) { int cmp = compare(first, o.first); return cmp == 0 ? compare(second, o.second) : cmp; } private static int compare(Object o1, Object o2) { return o1 == null ? o2 == null ? 0 : -1 : o2 == null ? +1 : ((Comparable) o1).compareTo(o2); } @Override public int hashCode() { return 31 * hashcode(first) + hashcode(second); } private static int hashcode(Object o) { return o == null ? 0 : o.hashCode(); } public boolean equals(Object other) { if (other instanceof Pair) { Pair<A, B> otherPair = (Pair<A, B>) other; return (( this.first == otherPair.first || ( this.first != null && otherPair.first != null && this.first.equals(otherPair.first))) && ( this.second == otherPair.second || ( this.second != null && otherPair.second != null && this.second.equals(otherPair.second))) ); } return false; } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
31c8c99017ec28731c8406d011a39ee7
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; import java.lang.Long; public class Test { static long mod = 1000000007; static int[][][] dp; public static void main(String[] args) throws FileNotFoundException { PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); ArrayList<Pair>[] map = new ArrayList[n]; for (int i = 0; i < n; i++) { map[i] = new ArrayList(); } for (int i = 0; i < m; i++) { int a = in.nextInt() - 1; int b = in.nextInt() - 1; long v = in.nextLong(); map[a].add(new Pair(b, v)); map[b].add(new Pair(a, v)); } Pair[] train = new Pair[k]; for (int i = 0; i < k; i++) { train[i] = new Pair(in.nextInt() - 1, i, in.nextLong()); } long[] dist = new long[n]; Arrays.fill(dist, -1); dist[0] = 0; PriorityQueue<Pair> q = new PriorityQueue(); q.add(new Pair(0, 0)); while (!q.isEmpty()) { Pair p = q.poll(); if (dist[p.x] == p.y) { for (Pair nxt : map[p.x]) { if (dist[nxt.x] == -1 || dist[nxt.x] > dist[p.x] + nxt.y) { dist[nxt.x] = dist[p.x] + nxt.y; q.add(new Pair(nxt.x, dist[nxt.x])); } } } } Arrays.sort(train); int result = k; for (Pair p : train) { map[0].add(new Pair(p.x, p.index, p.y)); map[p.x].add(new Pair(0, p.index, p.y)); } int[] used = new int[n]; Arrays.fill(used, -1); q = new PriorityQueue(); q.add(new Pair(0, 0)); while (!q.isEmpty()) { Pair p = q.poll(); if (dist[p.x] == p.y) { for (Pair nxt : map[p.x]) { if (dist[nxt.x] == -1 || dist[nxt.x] > dist[p.x] + nxt.y) { dist[nxt.x] = dist[p.x] + nxt.y; if (nxt.index != -1) { used[nxt.x] = nxt.index; } else { used[nxt.x] = -1; } q.add(new Pair(nxt.x, dist[nxt.x])); } else if (dist[nxt.x] == dist[p.x] + nxt.y) { if (nxt.index == -1) { used[nxt.x] = -1; } } } } } boolean[] check = new boolean[k]; for (int i = 0; i < n; i++) { if (used[i] != -1) { if (!check[used[i]]) { check[used[i]] = true; result--; } } } out.println(result); out.close(); } static class Pair implements Comparable<Pair> { int x, index = -1; long y; public Pair(int x, long y) { this.x = x; this.y = y; } public Pair(int x, int index, long y) { super(); this.x = x; this.index = index; this.y = y; } public int compareTo(Pair o) { if (y < o.y) { return -1; } else if (y == o.y) { return 0; } return 1; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new // BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new // FileInputStream(new File("B-large.in")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
f72a06f9bc535dd90fff304e81ae0fb1
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; /* * GRAPH IS GIVEN BELOW. YOU NEED TO PRINT THE SHORTEST PATH FROM NODE 1 TO LAST NODE * EXAMPLE INPUT 5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1 * OUTPUT = 1 4 3 5 */ public class _449B_JzzhuAndCities { static FastReader in; static PrintWriter out; final static long INFINITY = (long)(1e16); private static class Node implements Comparable<Node>{ int v; long w; public Node(int v,long w) { this.v = v; this.w = w; } @Override public int compareTo(Node o) { if(this.w > o.w) return 1; else if(this.w < o.w) return -1; return 0; } } public static void Solve(){ String[] line = in.nextLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); int k = Integer.parseInt(line[2]); @SuppressWarnings("unchecked") ArrayList<Node> g[] = new ArrayList[n]; for(int i = 0; i < n; i++) { g[i] = new ArrayList<Node>(); } for(int i = 0; i < m; i++) { line = in.nextLine().split(" "); int u = Integer.parseInt(line[0]) - 1; int v = Integer.parseInt(line[1]) - 1; int x = Integer.parseInt(line[2]); g[u].add(new Node(v,x)); g[v].add(new Node(u,x)); } boolean need[] = new boolean[n]; int train[] = new int[n]; for(int i = 0; i < k; i++) { line = in.nextLine().split(" "); int s = Integer.parseInt(line[0]) - 1; int y = Integer.parseInt(line[1]); need[s] = true; if(train[s] == 0) train[s] = y; else train[s] = Math.min(train[s], y); } long dist[] = new long[n]; boolean[] outOfQueue = new boolean[g.length]; Arrays.fill(dist, INFINITY); dist[0] = 0; PriorityQueue<Node> q = new PriorityQueue<Node>(2*g.length); q.add(new Node(0,0)); for(int i = 1; i < n; i++) { if(need[i]) { q.add(new Node(i,train[i])); dist[i] = train[i]; } } while(!q.isEmpty()) { Node now = q.poll(); int curr = now.v; if(!outOfQueue[curr]) { outOfQueue[curr] = true; int s = g[curr].size(); for(int i = 0; i < s; i++) { int next = g[curr].get(i).v; long edge = g[curr].get(i).w; if(dist[curr] + edge < dist[next]) { dist[next] = dist[curr] + edge; need[next] = false; q.add(new Node(next,dist[next])); } else if(dist[curr] + edge == dist[next]) need[next] = false; } } } int der = 0; for(int i = 1; i<n; i++) { if(need[i]) der++; } out.println(k-der); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); //long s = System.currentTimeMillis(); Solve(); out.flush(); //System.out.println(System.currentTimeMillis()-s +"ms"); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
1380e967b16c21ba53d872cfb9a72257
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class _449B_JzzhuAndCities { static FastReader in; static PrintWriter out; final static long INFINITY = (long)(1e16); private static class Node implements Comparable<Node>{ int v; long w; public Node(int v,long w) { this.v = v; this.w = w; } @Override public int compareTo(Node o) { if(this.w > o.w) return 1; else if(this.w < o.w) return -1; return 0; } } public static void Solve(){ String[] line = in.nextLine().split(" "); int n = Integer.parseInt(line[0]); int m = Integer.parseInt(line[1]); int k = Integer.parseInt(line[2]); @SuppressWarnings("unchecked") ArrayList<Node> g[] = new ArrayList[n]; for(int i = 0; i < n; i++) { g[i] = new ArrayList<Node>(); } for(int i = 0; i < m; i++) { line = in.nextLine().split(" "); int u = Integer.parseInt(line[0]) - 1; int v = Integer.parseInt(line[1]) - 1; int x = Integer.parseInt(line[2]); g[u].add(new Node(v,x)); g[v].add(new Node(u,x)); } boolean need[] = new boolean[n]; int train[] = new int[n]; for(int i = 0; i < k; i++) { line = in.nextLine().split(" "); int s = Integer.parseInt(line[0]) - 1; int y = Integer.parseInt(line[1]); need[s] = true; if(train[s] == 0) train[s] = y; else train[s] = Math.min(train[s], y); } long dist[] = new long[n]; boolean[] outOfQueue = new boolean[g.length]; Arrays.fill(dist, INFINITY); dist[0] = 0; PriorityQueue<Node> q = new PriorityQueue<Node>(2*g.length); q.add(new Node(0,0)); for(int i = 1; i < n; i++) { if(need[i]) { q.add(new Node(i,train[i])); dist[i] = train[i]; } } while(!q.isEmpty()) { Node now = q.poll(); int curr = now.v; if(!outOfQueue[curr]) { outOfQueue[curr] = true; int s = g[curr].size(); for(int i = 0; i < s; i++) { int next = g[curr].get(i).v; long edge = g[curr].get(i).w; if(dist[curr] + edge < dist[next]) { dist[next] = dist[curr] + edge; need[next] = false; q.add(new Node(next,dist[next])); } else if(dist[curr] + edge == dist[next]) need[next] = false; } } } int der = 0; for(int i = 1; i<n; i++) { if(need[i]) der++; } out.println(k-der); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); //long s = System.currentTimeMillis(); Solve(); out.flush(); //System.out.println(System.currentTimeMillis()-s +"ms"); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
231313ad55d12c0708d0cc27b21094f0
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import org.omg.PortableServer.POA; import java.awt.*; import java.io.*; import java.util.*; public class Abc { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); long inf=(long)1e18; int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(); ArrayList<Edge> adj[]=new ArrayList[n]; for (int i=0;i<n;i++)adj[i]=new ArrayList<>(); for (int i=0;i<m;i++){ int a=sc.nextInt()-1,b=sc.nextInt()-1,w=sc.nextInt(); adj[a].add(new Edge(b,w,false));adj[b].add(new Edge(a,w,false)); } for (int i=0;i<k;i++){ int s=sc.nextInt()-1,w=sc.nextInt(); adj[0].add(new Edge(s,w,true)); adj[s].add(new Edge(0,w,true)); } long dis[]=new long[n]; PriorityQueue<P> pq=new PriorityQueue<>(new P()); pq.add(new P(0,0)); Arrays.fill(dis,inf); dis[0]=0; boolean isNeeded[]=new boolean[n]; while (!pq.isEmpty()){ P u=pq.poll(); if (dis[u.u]!=u.w)continue; for (Edge v:adj[u.u]){ long cost=dis[u.u]+v.w; if (cost>dis[v.u])continue; if (cost==dis[v.u] && v.tr)continue; isNeeded[v.u]=v.tr; if (dis[v.u]>cost){ dis[v.u]=cost; pq.add(new P(v.u,cost)); } } } int x=0; for (int i=0;i<n;i++)if (isNeeded[i])x++; System.out.println(k-x); } static class P implements Comparator<P>{ int u;long w; P(){} P(int u,long w){ this.u=u; this.w=w; } public int compare(P o1,P o2){ long x=o1.w-o2.w; if (x>0)return 1; return -1; } } static class Edge{ boolean tr; int u,w; Edge(int u,int w,boolean tr){ this.u=u; this.w=w; this.tr=tr; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
7bb4cef1aac6780ee93a2593231019c7
train_002.jsonl
1405774800
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(in, out); out.close(); } } class Edge { int to; int cost; public Edge(int to, int cost) { this.to = to; this.cost = cost; } } class TaskB { private List<Edge> graph[]; private List<Edge> trainRoutes; private long distances[]; private int count[]; public void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); init(n); for (int i = 0; i < m; i++) { int from = in.nextInt() - 1; int to = in.nextInt() - 1; int cost = in.nextInt(); addEdge(from, to, cost); } trainRoutes = new ArrayList<>(k); for (int i = 0; i < k; i++) { int to = in.nextInt() - 1; int cost = in.nextInt(); addEdge(0, to, cost); trainRoutes.add(new Edge(to, cost)); } dijkstra(0); trainRoutes.sort((Edge e1, Edge e2) -> - (Integer.compare(e1.cost, e2.cost))); int result = 0; for(Edge edge : trainRoutes) { if(distances[edge.to] < edge.cost) result++; else if(distances[edge.to] == edge.cost && count[edge.to] > 1) { result++; count[edge.to]--; } } out.printLine(result); } private void dijkstra(int vertex) { boolean[] relaxed = new boolean[distances.length]; distances[vertex] = 0; PriorityQueue<Integer> queue = new PriorityQueue<>((Integer i1, Integer i2 ) -> Long.compare(distances[i1], distances[i2])); queue.add(vertex); while(!queue.isEmpty()) { vertex = queue.poll(); if (relaxed[vertex]) continue; relaxed[vertex] = true; for(Edge edge : graph[vertex]) { if(distances[edge.to] > distances[vertex] + edge.cost) { distances[edge.to] = distances[vertex] + edge.cost; queue.add(edge.to); relaxed[edge.to] = false; count[edge.to] = 1; } else if(distances[edge.to] == distances[vertex] + edge.cost) { count[edge.to]++; } } } } private void addEdge(int from, int to, int cost) { graph[from].add(new Edge(to, cost)); graph[to].add(new Edge(from, cost)); } private void init(int n) { distances = new long[n]; count = new int[n]; Arrays.fill(distances, Long.MAX_VALUE); graph = new List[n]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<>(); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) { return false; } tokenizer = new StringTokenizer(line); } catch (IOException e) { throw new RuntimeException(e); } } return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
Java
["5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3"]
2 seconds
["2", "2"]
null
Java 8
standard input
[ "graphs", "greedy", "shortest paths" ]
03d6b61be6ca0ac9dd8259458f41da59
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
2,000
Output a single integer representing the maximum number of the train routes which can be closed.
standard output
PASSED
75d3bcb69b7f10710ca603f7bdcf3443
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; // New Year Snowmen // 2012/1/5 public class P140C{ Scanner sc=new Scanner(System.in); int n; int[] rs; void run(){ n=sc.nextInt(); rs=new int[n]; for(int i=0; i<n; i++){ rs[i]=sc.nextInt(); } solve(); } void solve(){ HashMap<Integer, Integer> map=new HashMap<Integer, Integer>(); for(int i=0; i<n; i++){ if(!map.containsKey(rs[i])){ map.put(rs[i], 0); } map.put(rs[i], map.get(rs[i])+1); } TreeSet<R> set=new TreeSet<R>(); for(Entry<Integer, Integer> entry : map.entrySet()){ set.add(new R(entry.getKey(), entry.getValue())); } LinkedList<Answer> list=new LinkedList<Answer>(); R[] array=new R[3]; for(; set.size()>=3;){ for(int i=0; i<3; i++){ array[i]=set.pollFirst(); } list.add(new Answer(array[0].r, array[1].r, array[2].r)); for(int i=0; i<3; i++){ array[i].s--; if(array[i].s>0){ set.add(array[i]); } } } println(""+list.size()); for(Answer ans : list){ StringBuffer sb=new StringBuffer(); sb.append(ans.i1); sb.append(" "); sb.append(ans.i2); sb.append(" "); sb.append(ans.i3); println(sb.toString()); } } class Answer{ int i1, i2, i3; Answer(int i1, int i2, int i3){ int[] is=new int[]{-i1, -i2, -i3}; sort(is); this.i1=-is[0]; this.i2=-is[1]; this.i3=-is[2]; } } class R implements Comparable<R>{ int r, s; R(int r, int s){ this.r=r; this.s=s; } @Override public int compareTo(R r){ if(this.s!=r.s){ return r.s-this.s; }else{ return r.r-this.r; } } } void println(String s){ System.out.println(s); } public static void main(String[] args){ new P140C().run(); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
07bb1f9742d1ef9f4468e20035245074
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class C{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-9; int n; int[] rs; void run(){ n=sc.nextInt(); rs=new int[n]; for(int i=0; i<n; i++){ rs[i]=sc.nextInt(); } solve(); } void solve(){ HashMap<Integer, Integer> map=new HashMap<Integer, Integer>(); for(int i=0; i<n; i++){ if(!map.containsKey(rs[i])){ map.put(rs[i], 0); } map.put(rs[i], map.get(rs[i])+1); } TreeSet<R> set=new TreeSet<R>(); for(Entry<Integer, Integer> entry : map.entrySet()){ set.add(new R(entry.getKey(), entry.getValue())); } LinkedList<Answer> list=new LinkedList<Answer>(); R[] array=new R[3]; for(; set.size()>=3;){ for(int i=0; i<3; i++){ array[i]=set.pollFirst(); } list.add(new Answer(array[0].r, array[1].r, array[2].r)); for(int i=0; i<3; i++){ array[i].s--; if(array[i].s>0){ set.add(array[i]); } } } println(""+list.size()); for(Answer ans : list){ StringBuffer sb=new StringBuffer(); sb.append(ans.i1); sb.append(" "); sb.append(ans.i2); sb.append(" "); sb.append(ans.i3); println(sb.toString()); } } class Answer{ int i1, i2, i3; Answer(int i1, int i2, int i3){ int[] is=new int[]{-i1, -i2, -i3}; sort(is); this.i1=-is[0]; this.i2=-is[1]; this.i3=-is[2]; } } class R implements Comparable<R>{ int r, s; R(int r, int s){ this.r=r; this.s=s; } @Override public int compareTo(R r){ if(this.s!=r.s){ return r.s-this.s; }else{ return r.r-this.r; } } } void println(String s){ System.out.println(s); } void print(String s){ System.out.print(s); } void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } public static void main(String[] args){ new C().run(); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
3742a05b9aa54cdf44148c06bf6b0f64
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class C implements Runnable { public void solve() throws IOException { int n = nextInt(); int[] a = new int[n]; for ( int i = 0; i < n; i ++ ) { a[i] = nextInt(); } Arrays.sort( a ); int l = 0; int r = a.length / 3; while ( l < r ) { int m = ( l + r + 1 ) / 2; if ( can( m, a ) != null ) { l = m; } else { r = m - 1; } } int[][] res = can( l, a ); out.println( l ); if ( res != null ) { for ( int[] s : res ) { out.println( s[2] + " " + s[1] + " " + s[0] ); } } } private int[][] can( int m, int[] a ) { int[][] r = new int[m][3]; int last = 0; int cnt = 0; int row = 0; int pos = 0; for ( int x : a ) { if ( x == last ) { cnt ++; } else { last = x; cnt = 1; } if ( cnt > m ) { continue; } r[pos][row] = x; pos ++; if ( pos >= m ) { row ++; pos = 0; } if ( row >= 3 ) { break; } } if ( row >= 3 ) { return r; } else { return null; } } public StreamTokenizer in; public PrintWriter out; C() throws IOException { in = new StreamTokenizer( new InputStreamReader( System.in ) ); out = new PrintWriter(System.out); } int nextInt() throws IOException { in.nextToken(); return ( int ) in.nval; } void check(boolean f, String msg) { if (!f) { out.close(); throw new RuntimeException(msg); } } void close() throws IOException { out.close(); } public void run() { try { solve(); close(); } catch (Exception e) { e.printStackTrace(out); out.flush(); throw new RuntimeException(e); } } public static void main(String[] args) throws IOException { new Thread(new C()).start(); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
b213a9b79c0945d182c11ff648012afb
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.*; import java.util.*; public class b { BufferedReader in; PrintWriter out; StringTokenizer st; boolean can_read() throws Exception { return in.ready() || st.hasMoreTokens(); } void init(boolean file, String name) throws Exception { st = new StringTokenizer(" "); if (file) { in = new BufferedReader(new FileReader(name+".in")); out = new PrintWriter(new File(name + ".out")); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } String _token() throws Exception { while (!st.hasMoreTokens() && in.ready()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } int _int() throws Exception { return Integer.parseInt(_token()); } double _double() throws Exception { return Double.parseDouble(_token()); } long _long()throws Exception { return Long.parseLong(_token()); } class Pair implements Comparable<Pair> { int x,y; Pair(){} Pair(int X, int Y) { x = X; y = Y; } public int compareTo(Pair b) { if (x != b.x) return b.x - x; return b.y - y; } } void do_solve() throws Exception { int n = _int(); ArrayList<int[]>ans = new ArrayList<int[]>(0); TreeMap<Integer, Integer> m = new TreeMap<Integer, Integer>(); for(int i=0;i<n;i++) { int x = _int(); if (m.containsKey(x)) { int w = m.get(x); m.remove(x); m.put(x,w+1); } else { m.put(x,1); } } TreeSet<Pair> m2 = new TreeSet<Pair>(); for(int q = m.firstKey(); ; q=m.higherKey(q)) { m2.add(new Pair(m.get(q),q)); if (q == m.lastKey()) break; } while (m2.size() >= 3) { Pair a = m2.pollFirst(); Pair b = m2.pollFirst(); Pair c = m2.pollFirst(); int t [] = new int[]{a.y,b.y,c.y}; Arrays.sort(t); ans.add(t); if (a.x > 1) { m2.add(new Pair(a.x-1,a.y)); } if (b.x > 1) { m2.add(new Pair(b.x-1,b.y)); } if (c.x > 1) { m2.add(new Pair(c.x-1,c.y)); } } out.println(ans.size()); for(int i=0;i<ans.size();i++) { out.println(ans.get(i)[2] +" "+ans.get(i)[1] +" "+ans.get(i)[0]); } } public void mainProgram()throws Exception { init(false, "b"); while(can_read()) { do_solve(); } out.close(); } public static void main(String[] args) throws Exception { long T = System.currentTimeMillis(); new b().mainProgram(); System.err.println((System.currentTimeMillis() - T) + " ms"); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
8fc5ae65e95fdad91295ba0582a7d9aa
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.Arrays; import java.util.PriorityQueue; import java.util.Scanner; public class Prob140C { public static void main( String[] Args ) { Scanner scan = new Scanner( System.in ); int snowballs = scan.nextInt(); int[] ar = new int[snowballs]; for ( int x = 0; x < snowballs; x++ ) ar[x] = scan.nextInt(); Arrays.sort( ar ); int count = 1; for ( int x = 1; x < snowballs; x++ ) if ( ar[x] != ar[x - 1] ) count++; Snowball[] quantities = new Snowball[count]; count = 0; quantities[0] = new Snowball( ar[0] ); for ( int x = 1; x < snowballs; x++ ) if ( ar[x] != ar[x - 1] ) quantities[++count] = new Snowball( ar[x] ); else quantities[count].amt++; PriorityQueue<Snowball> pq = new PriorityQueue<Snowball>(); for ( int i = 0; i <= count; i++ ) pq.add( quantities[i] ); int[][] ans = new int[snowballs / 3][3]; Snowball[] temp = new Snowball[3]; count = 0; while ( pq.size() >= 3 ) { for ( int x = 0; x < 3; x++ ) { temp[x] = pq.poll(); ans[count][x] = temp[x].size; } for ( int x = 0; x < 3; x++ ) if ( --temp[x].amt > 0 ) pq.add( temp[x] ); count++; } System.out.println( count ); for ( int x = 0; x < count; x++ ) { Arrays.sort( ans[x] ); System.out.println( ans[x][2] + " " + ans[x][1] + " " + ans[x][0] ); } } } class Snowball implements Comparable<Snowball> { int size; int amt; public Snowball( int s ) { size = s; amt = 1; } @Override public int compareTo( Snowball arg0 ) { return arg0.amt - amt; } public boolean equals( Object o ) { if ( o instanceof Snowball ) { Snowball s = (Snowball)o; if ( size == s.size ) return true; } return false; } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
5019fa554e69eba4f36caec8b6455327
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.Map; import java.util.List; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.PriorityQueue; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author niyaznigmatul */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); Map<Integer, Integer> count = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { int x = in.nextInt(); if (!count.containsKey(x)) { count.put(x, 0); } count.put(x, count.get(x) + 1); } PriorityQueue<Element> pq = new PriorityQueue<Element>(); for (Map.Entry<Integer, Integer> e : count.entrySet()) { pq.add(new Element(e.getKey(), e.getValue())); } List<String> output = new ArrayList<String>(); while (pq.size() >= 3) { Element e1 = pq.poll(); Element e2 = pq.poll(); Element e3 = pq.poll(); int[] a = new int[]{e1.val, e2.val, e3.val}; Arrays.sort(a); output.add(a[2] + " " + a[1] + " " + a[0]); e1.count--; e2.count--; e3.count--; if (e1.count != 0) { pq.add(e1); } if (e2.count != 0) { pq.add(e2); } if (e3.count != 0) { pq.add(e3); } } out.println(output.size()); for (String e : output) { out.println(e); } } static class Element implements Comparable<Element> { int val; int count; Element(int val, int count) { this.val = val; this.count = count; } public int compareTo(Element o) { return o.count - count; } } } class FastScanner { BufferedReader br; StringTokenizer st; IOException happened; public FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { String line = br.readLine(); st = new StringTokenizer(line); } catch (IOException e) { happened = e; return null; } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
15f940e7712df85f6ff13098a2472e29
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.*; import java.util.*; public class C { final String filename = new String("C").toLowerCase(); class Pair implements Comparable<Pair> { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } @Override public int compareTo(Pair arg0) { if (x == arg0.x) { return y - arg0.y; } return x - arg0.x; } } void solve() throws Exception { int n = nextInt(); int[] a = new int[n]; TreeSet<Pair> ts = new TreeSet<Pair>(); HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { a[i] = nextInt(); if (!hm.containsKey(a[i])) { hm.put(a[i], 0); } hm.put(a[i], hm.get(a[i]) + 1); } for (int g : hm.keySet()) { ts.add(new Pair(hm.get(g), g)); } ArrayList<int[]> ans = new ArrayList<int[]>(); if (true) { while (!ts.isEmpty()) { Pair max = ts.pollLast(); Pair max2 = ts.pollLast(); Pair max3 = ts.pollLast(); if (max == null || max2 == null || max3 == null) { break; } int[] res = new int[] { max.y, max2.y, max3.y }; Arrays.sort(res); ans.add(res); if (max.x - 1 > 0) ts.add(new Pair(max.x - 1, max.y)); if (max2.x - 1 > 0) ts.add(new Pair(max2.x - 1, max2.y)); if (max3.x - 1 > 0) ts.add(new Pair(max3.x - 1, max3.y)); } } else { Arrays.sort(a); ArrayDeque<Integer> ones = new ArrayDeque<Integer>(); ArrayDeque<Pair> pairs = new ArrayDeque<Pair>(); for (int j = 0; j < n; j++) { if (!pairs.isEmpty() && pairs.peekFirst().y < a[j]) { Pair p = pairs.pollFirst(); ans.add(new int[] { a[j], p.y, p.x }); } else if (!ones.isEmpty() && ones.peekFirst() < a[j]) { Pair p = new Pair(ones.pollFirst(), a[j]); pairs.addLast(p); } else { ones.addFirst(a[j]); } } } out.println(ans.size()); for (int i = 0; i < ans.size(); i++) { for (int j = 0; j < 3; j++) { out.print(ans.get(i)[2 - j] + " "); } out.println(); } } void run() { 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(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } BufferedReader in; StringTokenizer st; PrintWriter out; String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } long nextLong() throws Exception { return Long.parseLong(nextToken()); } double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } public static void main(String[] args) { new C().run(); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
b4ad06c8a82e1c3f6f8593b7db481913
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; public class Main implements Runnable { private BufferedReader in; private PrintWriter out; private StringTokenizer st; private Random rnd; final String filename = "codeforces"; class Pair implements Comparable<Pair> { int val, howMuch; Pair(int val, int howMuch) { this.val = val; this.howMuch = howMuch; } public int compareTo(Pair another) { int howMuchDiff = this.howMuch - another.howMuch; if(howMuchDiff != 0) return -howMuchDiff; return (this.val - another.val); } } public void solve() throws IOException { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int n = nextInt(); for(int i = 0; i < n; i++) { int val = nextInt(); int curVal = 0; if(map.containsKey(val)) curVal = map.get(val); map.put(val, curVal + 1); } PriorityQueue<Pair> t = new PriorityQueue<Pair>(); for(int val : map.keySet()) { t.add(new Pair(val, map.get(val))); } int res = 0; int[][] resArray = new int[n][3]; while(t.size() >= 3) { Pair first = t.poll(); Pair second = t.poll(); Pair third = t.poll(); resArray[res][0] = first.val; resArray[res][1] = second.val; resArray[res][2] = third.val; if(first.howMuch > 1) { first.howMuch -= 1; t.add(first); } if(second.howMuch > 1) { second.howMuch -= 1; t.add(second); } if(third.howMuch > 1) { third.howMuch -= 1; t.add(third); } ++res; } out.println(res); for(int i = 0; i < res; i++) { Arrays.sort(resArray[i]); out.println(resArray[i][2] + " " + resArray[i][1] + " " + resArray[i][0]); } } public static void main(String[] args) { new Main().run(); } public void run() { try { try { in = new BufferedReader(new FileReader(filename + ".in")); out = new PrintWriter(new FileWriter(filename + ".out")); } catch(FileNotFoundException e) { in = new BufferedReader(new InputStreamReader((System.in))); out = new PrintWriter(System.out); } st = null; rnd = new Random(); solve(); out.close(); } catch(IOException e) { e.printStackTrace(); } } private String nextToken() throws IOException, NullPointerException { while(st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.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()); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
995ff89a7d4690524182fbb6224c7f99
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class CR100A { // static Scanner in; static StreamTokenizer st; static int n; static int[] c; static PrintWriter out; public static void main(String[] args) throws IOException { // in = new Scanner(System.in); st = new StreamTokenizer(new InputStreamReader(System.in)); out = new PrintWriter(System.out); n = nextInt(); c = new int[n]; tmp = new int[n]; for (int i = 0; i < n; ++i) c[i] = nextInt(); Arrays.sort(c); int l = 0, r = n; while (r - l > 1) { int m = (l + r) >> 1; if (may(m)) l = m; else r = m; } out.println(l); if (l == 0) { out.close(); return; } int[] x, y, z; x = new int[l]; y = new int[l]; z = new int[l]; for (int i = 0; i < l; ++i) x[i] = c[i]; int cc = 0; int i = 0; for (i = l; i < n; ++i) { if (c[i] > x[cc]) { y[cc++] = c[i]; } if (cc == l) { ++i; break; } } cc = 0; for (; i < n; ++i) { if (c[i] > y[cc]) { z[cc++] = c[i]; } if (cc == l) break; } for (i = 0; i < l; ++i) { out.println(z[i] + " " + y[i] + " " + x[i]); } out.close(); } static int[] tmp; private static boolean may(int m) { int i = 0; for (i = 0; i < m; ++i) tmp[i] = c[i]; int j, l = 0; for (j = i; j < n; ++j) { if (c[j] > tmp[l]) { tmp[l++] = c[j]; } if (l == m) break; } ++j; if (l != m) return false; l = 0; for (i = j; i < n; ++i) { if (c[i] > tmp[l]) { ++l; } if (l == m) break; } if (l != m) return false; return true; } private static int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
0dd47148425efab98cff9eb974f896f3
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.PriorityQueue; public class C { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String[] S = in.readLine().split(" "); int[] A = new int[n]; for (int i = 0; i < n; i++) A[i] = Integer.parseInt(S[i]); Arrays.sort(A); PriorityQueue<ball> P = new PriorityQueue<ball>(); int[][] ans = new int[n][3]; int c = 0; for (int i = 0; i < n;) { int j = i; while (j < n && A[j] == A[i]) j++; P.add(new ball(A[i], j - i)); i = j; } while (true) { if (P.size() < 3) break; ball[] T = new ball[3]; for (int i = 0; i < 3; i++) { T[i] = P.poll(); T[i].count--; ans[c][i] = T[i].v; } for (int i = 0; i < 3; i++) { if (T[i].count > 0) P.add(T[i]); } c++; } System.out.println(c); for (int i = 0; i < c; i++) { Arrays.sort(ans[i]); System.out.println(ans[i][2] + " " + ans[i][1] + " " + ans[i][0]); } } } class ball implements Comparable<ball> { int v; int count; public ball(int i, int j) { v = i; count = j; } public int compareTo(ball o) { return o.count - count; } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
55637cf3c4eed6bf8d197f475a6ece84
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.PriorityQueue; public class C { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String[] S = in.readLine().split(" "); int[] A = new int[n]; for (int i = 0; i < n; i++) A[i] = Integer.parseInt(S[i]); Arrays.sort(A); PriorityQueue<ball> P = new PriorityQueue<ball>(); int[][] ans = new int[n][3]; int c = 0; for (int i = 0; i < n;) { int j = i; while (j < n && A[j] == A[i]) j++; P.add(new ball(A[i], j - i)); i = j; } while (true) { if (P.size() < 3) break; ball temp1 = P.poll(); ball temp2 = P.poll(); ball temp3 = P.poll(); ans[c][0] = temp1.v; ans[c][1] = temp2.v; ans[c][2] = temp3.v; c++; temp1.count--; temp2.count--; temp3.count--; if (temp1.count > 0) P.add(temp1); if (temp2.count > 0) P.add(temp2); if (temp3.count > 0) P.add(temp3); } System.out.println(c); for (int i = 0; i < c; i++) { Arrays.sort(ans[i]); System.out.println(ans[i][2] + " " + ans[i][1] + " " + ans[i][0]); } } } class ball implements Comparable<ball> { int v; int count; public ball(int i, int j) { v = i; count = j; } public int compareTo(ball o) { return o.count - count; } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
ff7b1ab73f9246bebb03a0ae29db80fc
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.*; import java.util.*; public class C { private static void solve() throws IOException { int n = nextInt(); int[] r = new int[n]; for (int i = 0; i < n; i++) { r[i] = nextInt(); } Arrays.sort(r); List<Ball> balls = new ArrayList<Ball>(); int last = -1; for (int i = 0; i < n; i++) { if (i == n - 1 || r[i] != r[i + 1]) { balls.add(new Ball(r[i], i - last)); last = i; } } PriorityQueue<Ball> queue = new PriorityQueue<Ball>(); queue.addAll(balls); List<Snowman> answer = new ArrayList<Snowman>(); while (queue.size() >= 3) { Ball b1 = queue.poll(); Ball b2 = queue.poll(); Ball b3 = queue.poll(); answer.add(new Snowman(b1.r, b2.r, b3.r)); for (Ball b : new Ball[] { b1, b2, b3 }) { --b.count; if (b.count > 0) { queue.add(b); } } } out.println(answer.size()); for (Snowman s : answer) { out.println(s.r3 + " " + s.r2 + " " + s.r1); } } static class Ball implements Comparable<Ball> { int r, count; public Ball(int r, int count) { this.r = r; this.count = count; } @Override public int compareTo(Ball o) { return o.count - count; } } static class Snowman { final int r1, r2, r3; public Snowman(int r1, int r2, int r3) { if (r1 > r2) { int t = r1; r1 = r2; r2 = t; } if (r1 > r3) { int t = r1; r1 = r3; r3 = t; } if (r2 > r3) { int t = r2; r2 = r3; r3 = t; } this.r1 = r1; this.r2 = r2; this.r3 = r3; } } public static void main(String[] args) { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(239); } } static BufferedReader br; static StringTokenizer st; static PrintWriter out; static String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
988d2fda711868464d8fb09bcf19e98c
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; /** * 111118315581 * * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3 * * @author pttrung */ public class C { public static long x, y, gcd; public static void main(String[] args) { Scanner in = new Scanner(); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); } Arrays.sort(data); int count = 1; PriorityQueue<Pair> pq = new PriorityQueue(); for (int i = 1; i < n; i++) { if (data[i] == data[i - 1]) { count++; } else { pq.add(new Pair(data[i - 1], count)); // System.out.println(data[i - 1] + " " + count); count = 1; } } if (count > 0) { pq.add(new Pair(data[n - 1], count)); //System.out.println(data[n - 1] + " " + count); } int result = 0; ArrayList<Triple> list = new ArrayList(); while (pq.size() >= 3) { Pair first = pq.poll(); Pair second = pq.poll(); Pair third = pq.poll(); int min = 1; Triple trip = new Triple(first.value, second.value, third.value); list.add(trip); result += 1; if (first.num > min) { pq.add(new Pair(first.value, first.num - min)); } if (second.num > min) { pq.add(new Pair(second.value, second.num - min)); } if (third.num > min) { pq.add(new Pair(third.value, third.num - min)); } } out.println(result); for (Triple trip : list) { out.println(trip.x + " " + trip.y + " " + trip.z); } out.close(); } public static class Triple { int x, y, z; public Triple(int x, int y, int z) { this.x = Math.max(x, Math.max(y, z)); this.z = Math.min(x, Math.min(y, z)); this.y = (x + y + z) - this.x - this.z; } } public static class Pair implements Comparable<Pair> { int value, num; public Pair(int value, int num) { this.value = value; this.num = num; } @Override public int compareTo(Pair o) { return o.num - num; } } public static void extendEuclid(long a, long b) { if (b == 0) { x = 1; y = 0; gcd = a; return; } extendEuclid(b, a % b); long x1 = y; long y1 = x - (a / b) * y; x = x1; y = y1; } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
df625e3a8de3c6b268eb6d95a04e732c
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import static java.lang.Math.*; import java.util.*; import java.io.*; public class C { class El implements Comparable<El> { int cnt; int what; public El(int a, int b) { cnt = a; what = b; } public int compareTo(El o) { if (o.cnt == cnt) return o.what-what; return o.cnt - cnt; } } public void solve() throws Exception { int n = nextInt(); int[] p = new int[n+1]; p[0] = INF; for (int i=1; i<=n; ++i) p[i] = nextInt(); Arrays.sort(p); TreeSet<El> set = new TreeSet<El>(); int cnt = 1; for (int i=1; i<p.length; ++i) { if (p[i]==p[i-1]) { cnt++; } else { set.add(new El(cnt, p[i-1])); cnt=1; } } LinkedList<int[]> res = new LinkedList<int[]>(); while (true) { if (set.size()<3) break; El[] e = new El[3]; e[0] = set.first(); e[1] = set.higher(e[0]); e[2] = set.higher(e[1]); for (int i=0; i<3; ++i) { set.remove(e[i]); e[i].cnt--; if (e[i].cnt>0) set.add(e[i]); } int[] x = {e[0].what, e[1].what, e[2].what}; Arrays.sort(x); res.addLast(x); } println(res.size()); for (int[] x: res) println(x[2], x[1], x[0]); } //////////////////////////////////////////////////////////////////////////// boolean showDebug = true; static boolean useFiles = false; static String inFile = "input.txt"; static String outFile = "output.txt"; double EPS = 1e-7; int INF = Integer.MAX_VALUE; long INFL = Long.MAX_VALUE; double INFD = Double.MAX_VALUE; int absPos(int num) { return num<0 ? 0:num; } long absPos(long num) { return num<0 ? 0:num; } double absPos(double num) { return num<0 ? 0:num; } int min(int... nums) { int r = Integer.MAX_VALUE; for (int i: nums) if (i<r) r=i; return r; } int max(int... nums) { int r = Integer.MIN_VALUE; for (int i: nums) if (i>r) r=i; return r; } long minL(long... nums) { long r = Long.MAX_VALUE; for (long i: nums) if (i<r) r=i; return r; } long maxL(long... nums) { long r = Long.MIN_VALUE; for (long i: nums) if (i>r) r=i; return r; } double minD(double... nums) { double r = Double.MAX_VALUE; for (double i: nums) if (i<r) r=i; return r; } double maxD(double... nums) { double r = Double.MIN_VALUE; for (double i: nums) if (i>r) r=i; return r; } long sumArr(int[] arr) { long res = 0; for (int i: arr) res+=i; return res; } long sumArr(long[] arr) { long res = 0; for (long i: arr) res+=i; return res; } double sumArr(double[] arr) { double res = 0; for (double i: arr) res+=i; return res; } long partsFitCnt(long partSize, long wholeSize) { return (partSize+wholeSize-1)/partSize; } boolean odd(long num) { return (num&1)==1; } boolean hasBit(int num, int pos) { return (num&(1<<pos))>0; } long binpow(long a, int n) { long r = 1; while (n>0) { if ((n&1)!=0) r*=a; a*=a; n>>=1; } return r; } boolean isLetter(char c) { return (c>='a' && c<='z') || (c>='A' && c<='Z'); } boolean isLowercase(char c) { return (c>='a' && c<='z'); } boolean isUppercase(char c) { return (c>='A' && c<='Z'); } boolean isDigit(char c) { return (c>='0' && c<='9'); } boolean charIn(char ch, String s) { if (s==null) return false; for (int i=0; i<s.length(); ++i) if (ch==s.charAt(i)) return true; return false; } String stringn(String s, int n) { if (n<1 || s==null) return ""; StringBuilder sb = new StringBuilder(s.length()*n); for (int i=0; i<n; ++i) sb.append(s); return sb.toString(); } String str(Object o) { if (o==null) return ""; return o.toString(); } long timer = System.currentTimeMillis(); void startTimer() { timer = System.currentTimeMillis(); } void stopTimer() { System.err.println("time: "+(System.currentTimeMillis()-timer)/1000.0); } static class InputReader { private byte[] buf; private int bufPos = 0, bufLim = -1; private InputStream stream; public InputReader(InputStream stream, int size) { buf = new byte[size]; this.stream = stream; } private void fillBuf() throws IOException { bufLim = stream.read(buf); bufPos = 0; } char read() throws IOException { if (bufPos>=bufLim) fillBuf(); return (char)buf[bufPos++]; } boolean hasInput() throws IOException { if (bufPos>=bufLim) fillBuf(); return bufPos<bufLim; } } static InputReader inputReader; static BufferedWriter outputWriter; char nextChar() throws IOException { return inputReader.read(); } char nextNonWhitespaceChar() throws IOException { char c = inputReader.read(); while (c<=' ') c=inputReader.read(); return c; } String nextWord() throws IOException { StringBuilder sb = new StringBuilder(); char c = inputReader.read(); while (c<=' ') c=inputReader.read(); while (c>' ') { sb.append(c); c = inputReader.read(); } return new String(sb); } String nextLine() throws IOException { StringBuilder sb = new StringBuilder(); char c = inputReader.read(); while (c<=' ') c=inputReader.read(); while (c!='\n' && c!='\r') { sb.append(c); c = inputReader.read(); } return new String(sb); } int nextInt() throws IOException { int r = 0; char c = nextNonWhitespaceChar(); boolean neg = false; if (c=='-') neg=true; else r=c-48; c = nextChar(); while (c>='0' && c<='9') { r*=10; r+=c-48; c=nextChar(); } return neg ? -r:r; } long nextLong() throws IOException { long r = 0; char c = nextNonWhitespaceChar(); boolean neg = false; if (c=='-') neg=true; else r = c-48; c = nextChar(); while (c>='0' && c<='9') { r*=10L; r+=c-48L; c=nextChar(); } return neg ? -r:r; } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextWord()); } int[] nextArr(int size) throws NumberFormatException, IOException { int[] arr = new int[size]; for (int i=0; i<size; ++i) arr[i] = nextInt(); return arr; } long[] nextArrL(int size) throws NumberFormatException, IOException { long[] arr = new long[size]; for (int i=0; i<size; ++i) arr[i] = nextLong(); return arr; } double[] nextArrD(int size) throws NumberFormatException, IOException { double[] arr = new double[size]; for (int i=0; i<size; ++i) arr[i] = nextDouble(); return arr; } String[] nextArrS(int size) throws NumberFormatException, IOException { String[] arr = new String[size]; for (int i=0; i<size; ++i) arr[i] = nextWord(); return arr; } char[] nextArrCh(int size) throws IOException { char[] arr = new char[size]; for (int i=0; i<size; ++i) arr[i] = nextNonWhitespaceChar(); return arr; } char[][] nextArrCh(int rows, int columns) throws IOException { char[][] arr = new char[rows][columns]; for (int i=0; i<rows; ++i) for (int j=0; j<columns; ++j) arr[i][j] = nextNonWhitespaceChar(); return arr; } char[][] nextArrChBorders(int rows, int columns, char border) throws IOException { char[][] arr = new char[rows+2][columns+2]; for (int i=1; i<=rows; ++i) for (int j=1; j<=columns; ++j) arr[i][j] = nextNonWhitespaceChar(); for (int i=0; i<columns+2; ++i) { arr[0][i] = border; arr[rows+1][i] = border; } for (int i=0; i<rows+2; ++i) { arr[i][0] = border; arr[i][columns+1] = border; } return arr; } void printf(String format, Object... args) throws IOException { outputWriter.write(String.format(format, args)); } void print(Object o) throws IOException { outputWriter.write(o.toString()); } void println(Object o) throws IOException { outputWriter.write(o.toString()); outputWriter.newLine(); } void print(Object... o) throws IOException { for (int i=0; i<o.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(o[i].toString()); } } void println(Object... o) throws IOException { print(o); outputWriter.newLine(); } void printn(Object o, int n) throws IOException { String s = o.toString(); for (int i=0; i<n; ++i) { outputWriter.write(s); if (i!=n-1) outputWriter.write(' '); } } void printnln(Object o, int n) throws IOException { printn(o, n); outputWriter.newLine(); } void printArr(int[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Integer.toString(arr[i])); } } void printArr(long[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Long.toString(arr[i])); } } void printArr(double[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(Double.toString(arr[i])); } } void printArr(String[] arr) throws IOException { for (int i=0; i<arr.length; ++i) { if (i!=0) outputWriter.write(' '); outputWriter.write(arr[i]); } } void printArr(char[] arr) throws IOException { for (char c: arr) outputWriter.write(c); } void printlnArr(int[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(long[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(double[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(String[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void printlnArr(char[] arr) throws IOException { printArr(arr); outputWriter.newLine(); } void halt(Object... o) throws IOException { if (o.length!=0) println(o); outputWriter.flush(); outputWriter.close(); System.exit(0); } void debug(Object... o) { if (showDebug) System.err.println(Arrays.deepToString(o)); } public static void main(String[] args) throws Exception { Locale.setDefault(Locale.US); if (!useFiles) { inputReader = new InputReader(System.in, 1<<16); outputWriter = new BufferedWriter(new OutputStreamWriter(System.out), 1<<16); } else { inputReader = new InputReader(new FileInputStream(new File(inFile)), 1<<16); outputWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outFile))), 1<<16); } new C().solve(); outputWriter.flush(); outputWriter.close(); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
dc4e1acc110858ca9cb41f863a24a3e5
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; public class c { public static void main(String[] args){ Scanner br = new Scanner(System.in); int n = br.nextInt(); int[] nums = new int[n]; for(int i = 0;i<n;i++){ nums[i] = br.nextInt(); } Arrays.sort(nums); TreeSet<num> ns = new TreeSet<num>(); for(int i = 0;i<n;i++){ num cur = new num(nums[i]); int index = i+1; while(index < n && nums[index] == nums[i]){ cur.count++; index++; } ns.add(cur); i = index-1; } ArrayList<snowman> sn = new ArrayList<snowman>(); while(ns.size() > 2){ num sb1 = ns.pollFirst(); num sb2 = ns.pollFirst(); num sb3 = ns.pollFirst(); sn.add(new snowman(sb1.num, sb2.num, sb3.num)); sb1.count--; sb2.count--; sb3.count--; if(sb1.count > 0){ ns.add(sb1); } if(sb2.count > 0){ ns.add(sb2); } if(sb3.count > 0){ ns.add(sb3); } } System.out.println(sn.size()); for(int i = 0;i<sn.size();i++){ System.out.println(sn.get(i).bs[2]+" "+sn.get(i).bs[1]+" "+sn.get(i).bs[0]); } } public static class num implements Comparable<num>{ int num, count; public num(int a){ num = a; count = 1; } public int compareTo(num o){ if(count == o.count){ return num-o.num; } return o.count-count; } } public static class snowman{ int[] bs; public snowman(int o, int t, int th){ bs = new int[3]; bs[0] = o; bs[1] = t; bs[2] = th; Arrays.sort(bs); } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
4c34ef0667beb2b8bf112ea33b407559
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; import java.io.*; public class Main { static class Number implements Comparable<Number> { int num,count; public Number(int n,int c) { num=n; count=c; } public int compareTo(Number o) { return o.count-count; } } public static void main(String[] args) { try { Parserdoubt pd=new Parserdoubt(System.in); StringBuffer sb=new StringBuffer(); int n=pd.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=pd.nextInt(); Arrays.sort(arr); PriorityQueue<Number> q=new PriorityQueue<Number>(); int ans=0; int c=1; for(int i=1;i<arr.length;i++) { if(arr[i]!=arr[i-1]) { q.add(new Number(arr[i-1],c)); c=1; } else c++; } q.add(new Number(arr[arr.length-1],c)); while(q.size()>=3) { Number k1=q.poll(); Number k2=q.poll(); Number k3=q.poll(); int temp[]=new int[3]; temp[0]=k1.num; temp[1]=k2.num; temp[2]=k3.num; Arrays.sort(temp); sb.append(temp[2]+" "+temp[1]+" "+temp[0]+"\n"); ans++; k1.count--; k2.count--; k3.count--; if(k1.count>0)q.add(k1); if(k2.count>0)q.add(k2); if(k3.count>0)q.add(k3); } System.out.println(ans); System.out.println(sb); } catch(Exception e) { e.printStackTrace(); } } } class Parserdoubt { final private int BUFFER_SIZE = 1 << 17; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parserdoubt(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextString() throws Exception { StringBuffer sb=new StringBuffer(""); byte c = read(); while (c <= ' ') c = read(); do { sb.append((char)c); c=read(); }while(c>' '); return sb.toString(); } public char nextChar() throws Exception { byte c=read(); while(c<=' ') c= read(); return (char)c; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } public long nextLong() throws Exception { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws Exception { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
d164199d87a163eb32ee4a56bdd82313
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; import java.math.*; public class main { static Comparator<snowball> comp=new Comparator<snowball>() { public int compare(snowball e1,snowball e2) { if(e1.occ<e2.occ) return 1; return -1; } }; public static void main(String args[]) { Scanner c = new Scanner(System.in); int n=c.nextInt(); int A[]=new int[n]; for(int i=0;i<n;i++) A[i]=c.nextInt(); Arrays.sort(A); int count=1; for(int i=0;i<n-1;i++) if(A[i]!=A[i+1]) count++; TreeSet<snowball> T=new TreeSet<snowball>(comp); TreeSet<snowball> T1=new TreeSet<snowball>(comp); //System.out.println(count); if(count<=2) { System.out.println(0); return; } snowball hist[]=new snowball[count]; snowball hist1[]=new snowball[count]; hist[0]=new snowball(); hist1[0]=new snowball(); hist[0].occ=1; hist1[0].occ=1; hist[0].radius=A[0]; hist1[0].radius=A[0]; int index=0; for(int i=1;i<=n-1;i++) { if(A[i]==A[i-1]) { hist[index].occ++; hist1[index].occ++; } else { index++; hist[index]=new snowball(); hist1[index]=new snowball(); hist[index].radius=A[i]; hist1[index].radius=A[i]; hist[index].occ=1; hist1[index].occ=1; } } /*for(int i=0;i<count;i++) System.out.println("("+hist[i].radius+","+hist[i].occ+")");*/ for(int i=0;i<count;i++) { T.add(hist[i]); T1.add(hist1[i]); } //System.out.println(T.size()+" "+T1.size()); int num=0; String s=""; while(!T.isEmpty()) { snowball t1,t2,t3; t1=T.pollFirst(); t2=T.pollFirst(); t3=T.pollFirst(); if(t1.occ==0||t2.occ==0||t3.occ==0) break; int temp[]={t1.radius,t2.radius,t3.radius}; Arrays.sort(temp); t1.occ--; T.add(t1); t2.occ--; T.add(t2); t3.occ--; T.add(t3); num++; } System.out.println(num); while(!T1.isEmpty()) { //System.out.println("here"); snowball t1,t2,t3; t1=T1.pollFirst(); t2=T1.pollFirst(); t3=T1.pollFirst(); if(t1.occ==0||t2.occ==0||t3.occ==0) break; int temp[]={t1.radius,t2.radius,t3.radius}; Arrays.sort(temp); System.out.println(temp[2]+" "+temp[1]+" "+temp[0]); t1.occ--; T1.add(t1); t2.occ--; T1.add(t2); t3.occ--; T1.add(t3); } } } class snowball { int radius; int occ; }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
ac19576bf3a7e2ebcc99111570c39757
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; import java.math.*; public class main { static Comparator<snowball> comp=new Comparator<snowball>() { public int compare(snowball e1,snowball e2) { if(e1.occ<e2.occ) return 1; return -1; } }; public static void main(String args[]) { Scanner c = new Scanner(System.in); int n=c.nextInt(); int A[]=new int[n]; for(int i=0;i<n;i++) A[i]=c.nextInt(); Arrays.sort(A); int count=1; for(int i=0;i<n-1;i++) if(A[i]!=A[i+1]) count++; //System.out.println(count); if(count<=2) { System.out.println(0); return; } snowball hist[]=new snowball[count]; hist[0]=new snowball(); hist[0].occ=1; hist[0].radius=A[0]; int index=0; for(int i=1;i<=n-1;i++) { if(A[i]==A[i-1]) { hist[index].occ++; } else { index++; hist[index]=new snowball(); hist[index].radius=A[i]; hist[index].occ=1; } } /*for(int i=0;i<count;i++) System.out.println("("+hist[i].radius+","+hist[i].occ+")");*/ TreeSet<snowball> T=new TreeSet<snowball>(comp); for(int i=0;i<count;i++) T.add(hist[i]); int num=0; StringBuffer s=new StringBuffer(); while(!T.isEmpty()) { //System.out.println("here"); snowball t1,t2,t3; t1=T.pollFirst(); t2=T.pollFirst(); t3=T.pollFirst(); if(t1.occ==0||t2.occ==0||t3.occ==0) break; int temp[]={t1.radius,t2.radius,t3.radius}; Arrays.sort(temp); // System.out.println(temp[2]); s.append(Integer.toString(temp[2])); s.append(" "); s.append(Integer.toString(temp[1])); s.append(" "); s.append(Integer.toString(temp[0])); s.append("\n"); t1.occ--; T.add(t1); t2.occ--; T.add(t2); t3.occ--; T.add(t3); num++; } System.out.println(num+"\n"+s); } } class snowball { int radius; int occ; } //must declare new classes here
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
f33997d496e303d4710c9ed0163d9959
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; import java.math.*; public class main { static Comparator<snowball> comp=new Comparator<snowball>() { public int compare(snowball e1,snowball e2) { if(e1.occ<e2.occ) return 1; return -1; } }; public static void main(String args[]) { Scanner c = new Scanner(System.in); int n=c.nextInt(); int A[]=new int[n]; for(int i=0;i<n;i++) A[i]=c.nextInt(); Arrays.sort(A); int count=1; for(int i=0;i<n-1;i++) if(A[i]!=A[i+1]) count++; PriorityQueue<snowball> T=new PriorityQueue<snowball>(n, comp); PriorityQueue<snowball> T1=new PriorityQueue<snowball>(n, comp); //System.out.println(count); if(count<=2) { System.out.println(0); return; } snowball hist[]=new snowball[count]; snowball hist1[]=new snowball[count]; hist[0]=new snowball(); hist1[0]=new snowball(); hist[0].occ=1; hist1[0].occ=1; hist[0].radius=A[0]; hist1[0].radius=A[0]; int index=0; for(int i=1;i<=n-1;i++) { if(A[i]==A[i-1]) { hist[index].occ++; hist1[index].occ++; } else { index++; hist[index]=new snowball(); hist1[index]=new snowball(); hist[index].radius=A[i]; hist1[index].radius=A[i]; hist[index].occ=1; hist1[index].occ=1; } } /*for(int i=0;i<count;i++) System.out.println("("+hist[i].radius+","+hist[i].occ+")");*/ for(int i=0;i<count;i++) { T.add(hist[i]); T1.add(hist1[i]); } //System.out.println(T.size()+" "+T1.size()); int num=0; String s=""; while(!T.isEmpty()) { snowball t1,t2,t3; t1=T.poll(); t2=T.poll(); t3=T.poll(); if(t1.occ==0||t2.occ==0||t3.occ==0) break; int temp[]={t1.radius,t2.radius,t3.radius}; Arrays.sort(temp); t1.occ--; T.add(t1); t2.occ--; T.add(t2); t3.occ--; T.add(t3); num++; } System.out.println(num); while(!T1.isEmpty()) { //System.out.println("here"); snowball t1,t2,t3; t1=T1.poll(); t2=T1.poll(); t3=T1.poll(); if(t1.occ==0||t2.occ==0||t3.occ==0) break; int temp[]={t1.radius,t2.radius,t3.radius}; Arrays.sort(temp); System.out.println(temp[2]+" "+temp[1]+" "+temp[0]); t1.occ--; T1.add(t1); t2.occ--; T1.add(t2); t3.occ--; T1.add(t3); } } } class snowball { int radius; int occ; } //must declare new classes here
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
7f1390d8915ea832bce80e2d1db02243
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @SuppressWarnings("unchecked") public class C_New_Year_Snowmen { static class Snow implements Comparable{ private int r; private int cnt; public int getR() { return r; } public void setR(int r) { this.r = r; } public int getCnt() { return cnt; } public void setCnt(int cnt) { this.cnt = cnt; } public Snow(int r){ this.r=r; this.cnt=1; } public int compareTo(Object o) { // TODO Auto-generated method stub Snow s = (Snow)o; int res; if(this.cnt==s.getCnt()){ res = s.r-this.r; }else{ res = s.cnt-this.cnt; } return res; } } /** * 2012-1-8 * @author froest * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int i; int a[][] = new int[40000][3]; TreeSet<Snow> set = new TreeSet<Snow>(); TreeMap<Integer,Snow> tree = new TreeMap<Integer,Snow>(); for(i = 0; i < n; i++){ int temp = scan.nextInt(); if(!tree.containsKey(temp)){ tree.put(temp, new Snow(temp)); }else{ Snow ns = tree.get(temp); ns.setCnt(ns.getCnt()+1); tree.put(temp, ns); } } Set se = tree.keySet(); for (Object object : se) { Integer ii = (Integer)object; set.add(tree.get(ii)); } //long start = System.currentTimeMillis(); int k = 0; List<Integer> b = new ArrayList<Integer>(); Snow snow[] = new Snow[3]; while(set.size()>=3){ for(int j = 0; j < 3; j++){ snow[j] = set.pollFirst(); b.add(snow[j].r); snow[j].setCnt(snow[j].getCnt()-1); } for(int j = 0; j < 3; j++){ if(snow[j].getCnt()==0){ continue; } set.add(snow[j]); } Collections.sort(b); for(int j = 0; j < 3; j++){ a[k][j] = b.get(2-j); } b.clear(); k++; } System.out.println(k); for (int j = 0; j < k; j++) { System.out.println(a[j][0]+" "+a[j][1]+" "+a[j][2]); } //System.out.println(System.currentTimeMillis()-start); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
0f5b72b139fb2d257443b30da070da90
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.*; import java.util.*; public class c{ public static void main(String[] args){ boolean ans; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] r = new int[n]; for(int i = 0; i < n; i++){ r[i] = sc.nextInt(); } Arrays.sort(r); new c(n,r); } public c(int n, int[] r){ HashMap<Integer,Snowball> map = new HashMap<Integer,Snowball>(); for(int i = 0; i < r.length; i++){ if(map.containsKey(r[i])){ map.get(r[i]).add(); } else{ map.put(r[i],new Snowball(r[i],i)); } } PriorityQueue<Snowball> sb = new PriorityQueue<Snowball>(); for(Snowball s:map.values()){ sb.offer(s); } Snowball[] using = new Snowball[3]; int[][] res = new int[(n/3)+1][3]; int row = 0; mainLp:while((!sb.isEmpty()||(using[0]!=null&&using[1]!=null&&using[2]!=null))){ for(int i = 0; i < 3; i++){ if(using[i] == null){ using[i] = sb.poll(); continue mainLp; } } Arrays.sort(using); if(sb.peek()!=null && using[2].n < sb.peek().n){ sb.offer(using[2]); using[2] = sb.poll(); } for(int i = 0; i < 3; i++){ res[row][i] = using[i].get(); if(using[i].n == 0){ using[i] = null; } } //System.out.println(res[row][0] + " " + res[row][1] + " " + res[row][2]); Arrays.sort(res[row++]); } System.out.println(row); for(int i = 0; i < row; i++){ System.out.println(res[i][2] + " " + res[i][1] + " " + res[i][0]); } } public class Snowball implements Comparable<Snowball>{ int r, n, ind; public Snowball(int R, int Ind){ ind = Ind; r = R; n = 1; } public void add(){ n++; } public int get(){ n--; return r; } public int compareTo(Snowball s){ if(s.n > n){ return 1; } else if(s.n < n){ return -1; } return 0; } public String toString(){ return "Snowball(" + r +"): " + n; } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
2d9883fbe54c18be7db4154c4f1cba4f
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; public class C140 { private static class Pair implements Comparable<Pair> { int num, ct; Pair(int num, int ct) { this.num = num; this.ct = ct; } public int compareTo(Pair p) { int k = Integer.signum(ct - p.ct); if (k != 0) return k; return Integer.signum(num - p.num); } // public boolean equals(Pair p) { // return (num==p.num && ct == p.ct); // } public String toString() { return "("+num+","+ct+")"; } } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); HashMap<Integer, Integer> map =new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { int k = s.nextInt(); if (map.containsKey(k)) map.put(k, map.get(k)+1); else map.put(k, 1); } TreeSet<Pair> t = new TreeSet<Pair>(); for (int k : map.keySet()) { t.add(new Pair(k, map.get(k))); } int ct = 0; StringBuffer buf = new StringBuffer(); while (t.size() >= 3) { // System.out.println(t); Pair[] toAdd = new Pair[3]; int[] aa = new int[3]; for (int i = 0; i < 3; i++) { Pair a = t.last(); t.remove(a); aa[i] = a.num; if (a.ct > 1) toAdd[i] = new Pair(a.num, a.ct-1); } ct++; Arrays.sort(aa); for (int i = 0; i < 3; i++) { if (toAdd[i] != null) { t.add(toAdd[i]); } } buf.append(aa[2]).append(' ').append(aa[1]).append(' ').append(aa[0]).append('\n'); } System.out.println(ct); System.out.print(buf.toString()); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
c6e58c5b8c3364d28513c86eab72a9a4
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
//package round100; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; public class C { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); int[] imap = shrinkX(a); int[][] ct = new int[n][2]; int im = imap.length; for(int i = 0;i < n;i++){ ct[a[i]][0]++; } for(int i = 0;i < im;i++){ ct[i][1] = imap[i]; } Queue<int[]> q = new PriorityQueue<int[]>(n, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return -(a[0] - b[0]); } }); for(int i = 0;i < im;i++)q.add(ct[i]); StringBuilder sb = new StringBuilder(); int c = 0; while(q.size() >= 3){ int[][] x = {q.poll(), q.poll(), q.poll()}; Arrays.sort(x, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return -(a[1] - b[1]); } }); sb.append(x[0][1] + " " + x[1][1] + " " + x[2][1] + "\n"); c++; for(int i = 0;i < 3;i++){ x[i][0]--; if(x[i][0] > 0)q.add(x[i]); } } out.println(c); out.print(sb); } public static int[] shrinkX(int[] a) { int n = a.length; long[] b = new long[n]; for(int i = 0;i < n;i++)b[i] = (long)a[i]<<32|i; Arrays.sort(b); int[] ret = new int[n]; int p = 0; ret[0] = (int)(b[0]>>32); for(int i = 0;i < n;i++) { if(i>0 && (b[i]^b[i-1])>>32!=0) { p++; ret[p] = (int)(b[i]>>32); } a[(int)b[i]] = p; } return Arrays.copyOf(ret, p+1); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C().run(); } public int ni() { try { int num = 0; boolean minus = false; while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')); if(num == '-'){ num = 0; minus = true; }else{ num -= '0'; } while(true){ int b = is.read(); if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } } } catch (IOException e) { } return -1; } public long nl() { try { long num = 0; boolean minus = false; while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')); if(num == '-'){ num = 0; minus = true; }else{ num -= '0'; } while(true){ int b = is.read(); if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } } } catch (IOException e) { } return -1; } public String ns() { try{ int b = 0; StringBuilder sb = new StringBuilder(); while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' ')); if(b == -1)return ""; sb.append((char)b); while(true){ b = is.read(); if(b == -1)return sb.toString(); if(b == '\r' || b == '\n' || b == ' ')return sb.toString(); sb.append((char)b); } } catch (IOException e) { } return ""; } public char[] ns(int n) { char[] buf = new char[n]; try{ int b = 0, p = 0; while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n')); if(b == -1)return null; buf[p++] = (char)b; while(p < n){ b = is.read(); if(b == -1 || b == ' ' || b == '\r' || b == '\n')break; buf[p++] = (char)b; } return Arrays.copyOf(buf, p); } catch (IOException e) { } return null; } double nd() { return Double.parseDouble(ns()); } boolean oj = System.getProperty("ONLINE_JUDGE") != null; void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
86b9ab6875192c5650fe24bcc7d08fa3
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; public class newyearssnowman { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] balls = new int[N]; for (int n = 0; n < N; ++n) balls[n] = in.nextInt(); Arrays.sort(balls); PriorityQueue<Ball> next = new PriorityQueue<Ball>(); int last = 0; int count = 0; for (int n = 0; n < N; ++n) { if (last == balls[n]) ++count; else { if (last != 0) next.add(new Ball(last, count)); last = balls[n]; count = 1; } } next.add(new Ball(last, count)); ArrayList<Integer> ans = new ArrayList<Integer>(); int k = 0; while (true) { if (next.size() < 3) break; int[] out = new int[3]; int[] cout = new int[3]; for (int i = 0; i < 3; ++i) { Ball cur = next.remove(); out[i] = cur.v; cout[i] = cur.c; } int[] foo = new int[3]; for (int i = 0; i < 3; ++i) foo[i] = out[i]; Arrays.sort(foo); ++k; for (int i = 0; i < 3; ++i) --cout[i]; for (int j = 2; j >= 0; --j) ans.add(foo[j]); for (int i = 0; i < 3; ++i) if (cout[i] > 0) {next.add(new Ball(out[i], cout[i]));} } System.out.println(k); for (int i = 0; i < k; ++i) System.out.printf("%d %d %d\n", ans.get(i*3), ans.get(i*3+1), ans.get(i*3+2)); } static class Ball implements Comparable<Ball> { int v; int c; public Ball(int vv, int cc) { v = vv; c = cc; } public int compareTo(Ball o) { return o.c - c; } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
d12b5ce578f96c56202af0890bbfdbb7
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; public class C { static class Struct { int node, count; public Struct(int n, int c) { node = n; count = c; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = sc.nextInt(); Arrays.sort(ar); TreeSet<Struct> tree = new TreeSet<Struct>(new Comparator<Struct>() { @Override public int compare(Struct o1, Struct o2) { if (o2.count> o1.count) return 1; return -1; } }); int prev = -1; int count = 0; for (int i = 0; i < n; i++) { if (ar[i] != prev) { if (prev != -1) { tree.add(new Struct(prev, count)); } prev = ar[i]; count = 1; } else { count++; } } tree.add(new Struct(prev, count)); int res=0; ArrayList<Integer> fir=new ArrayList<Integer>(),sec=new ArrayList<Integer>(),thir=new ArrayList<Integer>(); while (tree.size() > 2) { Struct f = tree.pollFirst(); Struct s = tree.pollFirst(); Struct t = tree.pollFirst(); int[] tmp={f.node,s.node,t.node}; Arrays.sort(tmp); res++; fir.add(tmp[2]); sec.add(tmp[1]); thir.add(tmp[0]); f.count--; s.count--; t.count--; if (f.count > 0) { tree.add(f); } if (s.count > 0) { tree.add(s); } if (t.count > 0) { tree.add(t); } } System.out.println(res); for(int i=0;i<fir.size();i++) System.out.println(fir.get(i)+" "+sec.get(i)+" "+thir.get(i)); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
75968e878137c8c35c1292b14d54fe84
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.util.*; public class Snowmen { public static void main(String[] args){ Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int[] r = new int[n]; for(int i = 0; i < n; i++) r[i] = reader.nextInt(); Arrays.sort(r); PriorityQueue<Pair> q = new PriorityQueue<Pair>(); for(int i = 0; i < n; i++){ int k = i+1; while(k < n && r[i]==r[k]) k++; q.add(new Pair(r[i], k-i)); i = k-1; } int cnt = 0; LinkedList<Integer> out = new LinkedList<Integer>(); while(q.size() > 2){ Pair a = q.remove(); Pair b = q.remove(); Pair c = q.remove(); int min = 1; // int min = Math.min(a.f, Math.min(b.f, c.f)); for(int i = 0; i < min; i++) add(a,b,c,out); insert(a,min,q); insert(b,min,q); insert(c,min,q); cnt += min; } System.out.println(cnt); for(int i = 0; i < cnt; i++){ for(int j = 0; j < 3; j++) System.out.print(out.remove() + " "); System.out.println(); } } public static void add(Pair a, Pair b, Pair c, LinkedList<Integer> sol){ int[] ret = new int[]{a.v, b.v, c.v}; Arrays.sort(ret); sol.add(ret[2]); sol.add(ret[1]); sol.add(ret[0]); } public static void insert(Pair p, int d, PriorityQueue<Pair> q){ p.f-=d; if(p.f == 0) return; q.add(p); } public static class Pair implements Comparable<Pair>{ int v,f; public Pair(int _v, int _f){ v = _v; f = _f; } public int compareTo(Pair p){ return p.f-f; } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
a458f85de14013bea47b461d9468cfae
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.PriorityQueue; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.AbstractCollection; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nova */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { class Ball implements Comparable<Ball> { int radius = 0; int count = 0; Ball(int radius, int count) { this.radius = radius; this.count = count; } public int compareTo(Ball o) { return -(this.count - o.count); } } class Result { int a, b, c; Result(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public String toString() { int[] d = new int[3]; d[0] = a; d[1] = b; d[2] = c; Arrays.sort(d); return d[2] + " " + d[1] + " " + d[0]; } } public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); Arrays.sort(a); int i = 0; PriorityQueue<Ball> balls = new PriorityQueue<Ball>(); while (i < n) { int radius = a[i]; int j = i + 1; while ((j < n) && (a[j] == a[i])) j++; int count = j - i; Ball ball = new Ball(radius, count); balls.add(ball); i = j; } ArrayList<Result> results = new ArrayList<Result>(); int res = 0; while (!balls.isEmpty()) { Ball x = balls.poll(); if (balls.isEmpty()) break; Ball y = balls.poll(); if (balls.isEmpty()) break; Ball z = balls.poll(); res++; results.add(new Result(x.radius, y.radius, z.radius)); x.count--; y.count--; z.count--; if (x.count != 0) balls.add(x); if (y.count != 0) balls.add(y); if (z.count != 0) balls.add(z); } out.println(res); for (Result r : results) out.println(r); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(outputStream); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { //if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
79f09859c8f9fa67a17e9f4154ed54dd
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
//package round100; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; import java.util.StringTokenizer; public class C { static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static String nextToken() throws IOException{ while (st==null || !st.hasMoreTokens()){ String s = bf.readLine(); if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } static int nextInt() throws IOException{ return Integer.parseInt(nextToken()); } static long nextLong() throws IOException{ return Long.parseLong(nextToken()); } static String nextStr() throws IOException{ return nextToken(); } static double nextDouble() throws IOException{ return Double.parseDouble(nextToken()); } static BigInteger nextBigInt() throws IOException{ return new BigInteger(nextToken()); } public static void main(String[] args) throws IOException{ int n = nextInt(); Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { int a = nextInt(); Integer c = map.get(a); if (c == null) { map.put(a, 1); } else { map.put(a, c + 1); } } PriorityQueue<Pair> pq = new PriorityQueue<Pair>(); ArrayList<Triple> res = new ArrayList<Triple>(); for (int a: map.keySet()) { pq.add(new Pair(a, map.get(a))); } while (pq.size() >= 3) { Pair p1 = pq.poll(); Pair p2 = pq.poll(); Pair p3 = pq.poll(); res.add(new Triple(p1.val, p2.val, p3.val)); p1.num--; if (p1.num > 0) { pq.add(p1); } p2.num--; if (p2.num > 0) { pq.add(p2); } p3.num--; if (p3.num > 0) { pq.add(p3); } } out.println(res.size()); for (Triple t: res) { out.println(t.a + " " + t.b + " " + t.c); } out.flush(); } static class Pair implements Comparable<Pair>{ int val; int num; Pair(int v, int n) { val = v; num = n; } @Override public int compareTo(Pair p) { return p.num - num; } } static class Triple { int a; int b; int c; Triple(int p, int q, int r) { a = Math.max(Math.max(p, q), r); c = Math.min(Math.min(p, q), r); if (a == p) { b = Math.max(q, r); } else if (a == q) { b = Math.max(p, r); } else { b = Math.max(p, q); } } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
727b7e88e2a9d5596f0b539731b0fcab
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; import static java.lang.Math.*; public class C implements Runnable{ final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ try{ tok = new StringTokenizer(in.readLine()); }catch (Exception e){ return null; } } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Thread(null, new C(), "", 256 * (1L << 20)).start(); } long timeBegin, timeEnd; void timePrint(){ timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memoryPrint(){ memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } boolean DEBUG = false; void debugPrint(Object... objects){ if (DEBUG){ for (Object o: objects){ System.err.println(o.toString()); } } } public void run(){ try{ timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().totalMemory(); init(); solve(); out.close(); timePrint(); memoryPrint(); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } class CmpX implements Comparator<Point>{ @Override public int compare(Point p1, Point p2) { return p2.x - p1.x; } } class CmpY implements Comparator<Point>{ @Override public int compare(Point p1, Point p2) { return p2.y - p1.y; } } void solve() throws IOException{ int n = readInt(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++){ int r = readInt(); if (!map.containsKey(r)){ map.put(r, 1); }else{ map.put(r, map.get(r) + 1); } } CmpX cmpX = new CmpX(); CmpY cmpY = new CmpY(); PriorityQueue<Point> q = new PriorityQueue<Point>(map.size(), cmpY); for (Entry e: map.entrySet()){ q.add(new Point((Integer) e.getKey(), (Integer) e.getValue())); } int res = 0; Point[] p = new Point[3]; int[][] a = new int[n][3]; while (q.size() >= 3){ for (int i = 0; i < 3; i++){ p[i] = q.poll(); } Arrays.sort(p, cmpX); for (int i = 0; i < 3; i++){ a[res][i] = p[i].x; p[i].y--; if (p[i].y > 0) q.add(p[i]); } res++; } out.println(res); for (int i = 0; i < res; i++){ for (int r: a[i]){ out.print(r + " "); } out.println(); } } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output
PASSED
951d0ba13af5653976e86d60d33681e0
train_002.jsonl
1325689200
As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.
256 megabytes
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class C { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } int[] readArr(int n) throws IOException{ int[] res = new int[n]; for(int i = 0; i < n; i++){ res[i] = readInt(); } return res; } long[] readArrL(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++){ res[i] = readLong(); } return res; } public static void main(String[] args){ new C().run(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } class Kom implements Comparable<Kom>{ @Override public int compareTo(Kom o) { return -this.c + o.c; } public int w; public int c; public Kom(int w, int c){ this.w = w; this.c = c; } } void solve() throws IOException{ int n = readInt(); int[] a = readArr(n); Utils.mergeSort(a); PriorityQueue<Kom> k = new PriorityQueue<Kom>(); Kom current = new Kom(0,0); int countCur = 1; for(int i = 1; i < n; i++){ if(a[i] == a[i-1]) countCur++; else{ current = new Kom(a[i-1], countCur); countCur = 1; k.add(current); } } k.add(new Kom(a[n-1], countCur)); int i = 0; ArrayList<Integer> res = new ArrayList<Integer>(); Kom m1, m2, m3; while(true){ if(k.isEmpty()) break; m1 = k.poll(); if(k.isEmpty()) break; m2 = k.poll(); if(k.isEmpty()) break; m3 = k.poll(); i++; res.add(m1.w); res.add(m2.w); res.add(m3.w); m1.c--; m2.c--; m3.c--; if(m3.c > 0) k.add(m3); if(m2.c > 0) k.add(m2); if(m1.c > 0) k.add(m1); } out.println(i); int[] r = new int[3]; for(int j = 0; j < i; j++){ r[0] = res.get(j*3); r[1] = res.get(j*3+1); r[2] = res.get(j*3+2); Arrays.sort(r); out.print(r[2] + " "); out.print(r[1] + " "); out.println(r[0] + " "); } } void maxHepify(int[] a, int i, int length){ int l = (i<<1) + 1; int r = (i<<1) + 2; int largest = i; if(l < length && a[l] > a[largest]) largest = l; if(r < length && a[r] > a[largest]) largest = r; if(largest != i){ a[largest] += a[i]; a[i] = a[largest] - a[i]; a[largest] -= a[i]; maxHepify(a, largest, length); } } void buildMaxHeap(int[] a){ for(int i = a.length/2 - 1; i >= 0; i--){ maxHepify(a, i, a.length); } } void heapSort(int[] a){ buildMaxHeap(a); for(int i = a.length - 1; i > 0; i--){ a[i] += a[0]; a[0] = a[i] - a[0]; a[i] -= a[0]; maxHepify(a, 0, i); } } int[] zFunction(char[] s){ int[] z = new int[s.length]; z[0] = 0; for (int i=1, l=0, r=0; i<s.length; ++i) { if (i <= r) z[i] = min (r-i+1, z[i-l]); while (i+z[i] < s.length && s[z[i]] == s[i+z[i]]) ++z[i]; if (i+z[i]-1 > r){ l = i; r = i+z[i]-1; } } return z; } int[] prefixFunction(char[] s){ int[] pr = new int[s.length]; for (int i = 1; i< s.length; ++i) { int j = pr[i-1]; while (j > 0 && s[i] != s[j]) j = pr[j-1]; if (s[i] == s[j]) ++j; pr[i] = j; } return pr; } int ModExp(int a, int n, int mod){ int res = 1; while (n!=0) if ((n & 1) != 0) { res = (res*a)%mod; --n; } else { a = (a*a)%mod; n >>= 1; } return res; } static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } boolean isPrime(int a){ for(int i = 2; i <= sqrt(a); i++) if(a % i == 0) return false; return true; } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ if(min(a,b) == 0) return max(a,b); return gcd(max(a, b) % min(a,b), min(a,b)); } static long lcm(long a, long b){ return a * b /gcd(a, b); } }
Java
["7\n1 2 3 4 5 6 7", "3\n2 2 3"]
2 seconds
["2\n3 2 1\n6 5 4", "0"]
null
Java 6
standard input
[ "data structures", "binary search", "greedy" ]
551e66a4b3da71682652d84313adb8ab
The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.
1,800
Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.
standard output