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
sequencelengths
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
cdc037eaa1b5267fccb4f8734b4a18c0
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*;import java.lang.*;import java.util.*; //* --> number of prime numbers less then or equal to x are --> x/ln(x) //* --> String concatenation using the + operator within a loop should be avoided. Since the String object is immutable, each call for concatenation will // result in a new String object being created. // THE SIEVE USED HERE WILL RETURN A LIST CONTAINING ALL THE PRIME NUMBERS TILL N public class codechef {static FastScanner sc;static PrintWriter pw;static class FastScanner {InputStreamReader is;BufferedReader br;StringTokenizer st; public FastScanner() {is = new InputStreamReader(System.in);br = new BufferedReader(is);} String next() throws Exception {while (st == null || !st.hasMoreElements())st = new StringTokenizer(br.readLine()); return st.nextToken();}int nextInt() throws Exception {return Integer.parseInt(next());}long nextLong() throws Exception { return Long.parseLong(next());}int[] readArray(int num) throws Exception {int arr[]=new int[num]; for(int i=0;i<num;i++)arr[i]=nextInt();return arr;}String nextLine() throws Exception {return br.readLine(); }} public static boolean power_of_two(int a){if((a&(a-1))==0){ return true;}return false;} static boolean PS(double x){if (x >= 0) {double i= Math.sqrt(x);if(i%1!=0){ return false;}return ((i * i) == x);}return false;}public static int[] ia(int n){int ar[]=new int[n]; return ar;}public static long[] la(int n){long ar[]=new long[n];return ar;} public static void print(int ans,int t){System.out.println("Case"+" "+"#"+t+":"+" "+ans);} static long mod=1000000007;static int max=Integer.MIN_VALUE;static int min=Integer.MAX_VALUE; public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Long> ls = new ArrayList<Long>();for(long x: arr)ls.add(x);Collections.sort(ls); for(int i=0; i < arr.length; i++)arr[i] = ls.get(i);}public static long fciel(long a, long b) {if (a == 0) return 0;return (a - 1) / b + 1;} static boolean[] is_prime = new boolean[1000001];static ArrayList<Integer> list = new ArrayList<>(); static long n = 1000000;public static void sieve() {Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false;for (int i = 2; i * i <= n; i++) { if (is_prime[i]) {for (int j = i * i; j <= n; j += i)is_prime[j] = false;}}for (int i = 2; i <= n; i++) { if (is_prime[i]) {list.add(i);}}} // ---------- NCR ---------- \ static int NC=100005; static long inv[]=new long[NC]; static long fac_inv[]=new long[NC]; static long fac[]=new long[NC];public static void initialize() { long MOD=mod; int i; inv[1]=1; for(i=2;i<=NC-2;i++) inv[i]=(MOD-MOD/i)*inv[(int)MOD%i]%MOD; fac[0]=fac[1]=1; for(i=2;i<=NC-2;i++) fac[i]=i*fac[i-1]%MOD; fac_inv[0]=fac_inv[1]=1; for(i=2;i<=NC-2;i++) fac_inv[i]=inv[i]*fac_inv[i-1]%MOD; } public static long ncr(int n,int r) { long MOD=mod; if(n<r) return 0; return (fac[n]*fac_inv[r]%MOD)*fac_inv[n-r]%MOD; } // ---------- NCR ---------- \ // ---------- FACTORS -------- \ static int div[][] = new int[1000001][]; public static void factors() { int divCnt[] = new int[1000001]; for(int i = 1000000; i >= 1; --i) { for(int j = i; j <= 1000000; j += i) divCnt[j]++; } for(int i = 1; i <= 1000000; ++i) div[i] = new int[divCnt[i]]; int ptr[] = new int[1000001]; for(int i = 1000000; i >= 1; --i) { for(int j = i; j <= 1000000; j += i) div[j][ptr[j]++] = i; } } // ---------- FACTORS -------- \ // ------------- DSU ---------------\ static int par[]=new int[1000001];static int size[]=new int[1000001]; public static void make(int v){par[v]=v;size[v]++;} public static void union(int a,int b){a=find(a);b=find(b); if(a!=b){if(size[a]<size[b]){int temp=a;a=b;b=temp;}par[b]=a; size[a]++;}}public static int find(int v) {if(v==par[v]){return v;}return par[v]=find(par[v]);} // ------------- DSU ---------------\ public static void main(String args[]) throws java.lang.Exception { sc = new FastScanner();pw = new PrintWriter(System.out);StringBuilder s = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long a[]=new long[n]; long b[]=new long[n]; for(int i=0;i<n;i++) { a[i]=sc.nextLong(); } for(int i=0;i<n;i++) { b[i]=sc.nextLong(); } long dif[]=new long[n]; for(int i=0;i<n;i++) { dif[i]=b[i]-a[i]; } sort(dif); int i=0; int j=n-1; int count=0; while(i<j) { if(dif[i]+dif[j]<0) {i++;} else{ j--; i++; count++; } } s.append(count); if(t>0) { s.append("\n"); }} pw.print(s);pw.close();}}
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
011da1097943ea2c93d67e63b17b78df
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static PrintWriter pw = new PrintWriter(System.out); static FastReader fr = new FastReader(System.in); private static long MOD = 1_000_000_007; public static void main(String[] args) throws Exception{ FastReader fr = new FastReader(System.in); int t = fr.nextInt(); while(t-->0){ int n = fr.nextInt(); int[] a =new int[n]; int[] b =new int[n]; for(int i = 0;i<n;i++){ a[i] = fr.nextInt(); } for(int i = 0;i<n;i++){ b[i]=fr.nextInt(); } PriorityQueue<Integer> pos = new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<Integer> neg = new PriorityQueue<>(Collections.reverseOrder()); for (int i = 0; i < n; i++) { int res = b[i]-a[i]; if(res>=0){ pos.add(res); }else{ neg.add(Math.abs(res)); } } int pairCreated = 0; while(!pos.isEmpty() && !neg.isEmpty()){ if(pos.peek()>=neg.peek()){ pos.remove(); neg.remove(); pairCreated++; }else{ neg.remove(); } } pw.println(pairCreated+(pos.size()/2)); } pw.close(); } public static boolean possible(int row,int col,long diff,int r,int c){ if(row+diff>=r||col-diff<0||col+diff>=c){ return false; } return true; } static boolean canPut(int n,int tempR,int tempC){ if(tempR<0||tempC<0||tempR>=n||tempC>=n){ return false; } return true; } static class Pair{ int value; boolean pairFormed = false; public Pair(int value,boolean pairFormed){ this.value = value; this.pairFormed = pairFormed; } } public static long opNeeded(long c,long[] vals){ long tempResult = 0; for(int j = 0;j<vals.length;j++){ tempResult=tempResult+Math.abs((long)(vals[j]-Math.pow(c,j))); } if(tempResult<0){ tempResult=Long.MAX_VALUE; } return tempResult; } static int isPerfectSquare(int vals){ int lastPow=1; while(lastPow*lastPow<vals){ lastPow++; } if(lastPow*lastPow==vals){ return lastPow*lastPow; }else{ return -1; } } public static int[] sort(int[] vals){ ArrayList<Integer> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static long[] sort(long[] vals){ ArrayList<Long> values = new ArrayList<>(); for(int i = 0;i<vals.length;i++){ values.add(vals[i]); } Collections.sort(values); for(int i =0;i<values.size();i++){ vals[i] = values.get(i); } return vals; } public static void reverseArray(long[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ long temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } public static void reverseArray(int[] vals){ int startIndex = 0; int endIndex = vals.length-1; while(startIndex<=endIndex){ int temp = vals[startIndex]; vals[startIndex] = vals[endIndex]; vals[endIndex] = temp; startIndex++; endIndex--; } } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } public static int GCD(int numA, int numB){ if(numA==0){ return numB; }else if(numB==0){ return numA; }else{ if(numA>numB){ return GCD(numA%numB,numB); }else{ return GCD(numA,numB%numA); } } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9c3dd4d718b4ced6fa87ec16796af593
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static final PrintWriter out =new PrintWriter(System.out); static final FastReader sc = new FastReader(); //I invented a new word!Plagiarism! //Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them //What do Alexander the Great and Winnie the Pooh have in common? Same middle name. //I finally decided to sell my vacuum cleaner. All it was doing was gathering dust! //ArrayList<Integer> a=new ArrayList <Integer>(); //PriorityQueue<Integer> pq=new PriorityQueue<>(); //char[] a = s.toCharArray(); // char s[]=sc.next().toCharArray(); public static boolean sorted(int a[]) { int n=a.length,i; int b[]=new int[n]; for(i=0;i<n;i++) b[i]=a[i]; Arrays.sort(b); for(i=0;i<n;i++) { if(a[i]!=b[i]) return false; } return true; } public static void main (String[] args) throws java.lang.Exception { int tes=sc.nextInt(); while(tes-->0) { int n=sc.nextInt(); int x[]=new int[n]; int y[]=new int[n]; int i,start=0,end=n-1,ans=0; for(i=0;i<n;i++) x[i]=sc.nextInt(); for(i=0;i<n;i++) y[i]=sc.nextInt(); int diff[]=new int[n]; for(i=0;i<n;i++) diff[i]=y[i]-x[i]; Arrays.sort(diff); while(end>start) { if(diff[end]+diff[start]>=0) { start++; end--; ans++; continue; } start++; } System.out.println(ans); } } public static int first(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x) return mid; else if (x > arr.get(mid)) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid - 1), x, n); } return -1; } public static int last(ArrayList<Integer> arr, int low, int high, int x, int n) { if (high >= low) { int mid = low + (high - low) / 2; if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x) return mid; else if (x < arr.get(mid)) return last(arr, low, (mid - 1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1; } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get((int)mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { Collections.sort(ar); int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } 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=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long mod=1000000007; long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } 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
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
a0ab482f98555ef7298dd5b9678decf9
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; // static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; while(t-->0) { int n = sc.nextInt(); int arr[] = sc.readArray(n); int brr[] = sc.readArray(n); ArrayList<Integer> diff = new ArrayList<>(); for(int i=0;i<n;i++) { diff.add(brr[i] - arr[i]); } Collections.sort(diff); int l = 0 , r = n - 1; int ans = 0; while(l < r) { int sum = diff.get(l) + diff.get(r); if(sum >= 0) { l++; r--; ans++; } else { l++; } } System.out.println(ans); } } static int bs(Pair[] arr , int start , boolean[] visited) { int l = start , r = arr.length - 1; int curr1 = arr[l].first; int curr2 = arr[l].second; int ind = -1; while(l <= r) { int mid = (l + r) >> 1; long possible1 = arr[mid].first + curr1; long possible2 = arr[mid].second + curr2; if(possible2 >= possible1) { if(!visited[mid]) { ind = mid; } r = mid - 1; } else { l = mid + 1; } } return ind; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } static class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } private static long mergeAndCount(int[] arr, int l, int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(int[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static void my_sort(long[] arr) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static void reverse_sorted(int[] arr) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<arr.length;i++) { list.add(arr[i]); } Collections.sort(list , Collections.reverseOrder()); for(int i=0;i<arr.length;i++) { arr[i] = list.get(i); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value int l=-1,r=list.size(); while(l+1<r) { int m=(l+r)>>>1; if(list.get(m)<=x) l=m; else r=m; } return l+1; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class Queue_Pair implements Comparable<Queue_Pair> { int first , second; public Queue_Pair(int first, int second) { this.first=first; this.second=second; } public int compareTo(Queue_Pair o) { return Integer.compare(o.first, first); } } static void leftRotate(int arr[], int d, int n) { for (int i = 0; i < d; i++) leftRotatebyOne(arr, n); } static void leftRotatebyOne(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[n-1] = temp; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(char arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (long i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static long getSum(long n) { long sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if (b == 0) return a; return gcdLong(b, a % b); } static void swap(int i, int j) { int temp = i; i = j; j = temp; } static int countDigit(int n) { return (int)Math.floor(Math.log10(n) + 1); } } class Pair { int first; int second; Pair(int first , int second) { this.first = first; this.second = second; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
6050d6697d2497c3b749c8ac017b03c0
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t--!=0){ int n=sc.nextInt(); int arr[]=new int[n]; int var=0; for(int i=0;i<n;++i){ arr[i]=sc.nextInt(); } for(int i=0;i<n;++i) { var=sc.nextInt(); arr[i]=var-arr[i]; } Arrays.sort(arr); int ind=0,ind2=0; for(int i=n-1;i>=0;i--,ind2++,ind++) { ind2++; while(ind<n && -arr[i]>arr[ind]) ind++; if(ind>=i) break; } System.out.println(ind2-(ind2/2+1)); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
08e457c7f8f7800c1491b50b9b83b11c
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); while(t--!=0){ int n=sc.nextInt(); int arr[]=new int[n]; // int var=0; for(int i=0;i<n;++i){ arr[i]=sc.nextInt(); } for(int i=0,var;i<n;++i) { var=sc.nextInt(); arr[i]=var-arr[i]; } Arrays.sort(arr); // sort(a+1,a+1+n); int i=n-1,j=0,s=0; for(;;i--,s++,j++) { s++; for(;j<n && -arr[i]>arr[j];) j++; if(j>=i) break; } System.out.println(s-(s/2+1)); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
479d1bee23107a637e9208ba4c7418da
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class D{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); for(int i=0; i< t; i++){ int n = scanner.nextInt();scanner.nextLine(); int[] x = new int[n]; int[] y = new int[n]; for(int j=0; j<n;j++){ x[j] = scanner.nextInt(); } scanner.nextLine(); for(int j = 0; j < n; j++){ y[j]=scanner.nextInt(); } scanner.nextLine(); int[] diff = new int[n]; for(int j = 0; j < n; j++){ diff[j]=y[j]-x[j]; } Arrays.sort(diff); int res=0; int start =0;int end = n-1; for(int j=0; j< n; j++){ if(start>=end){ break; } if(diff[end]+diff[start]<0){ start++; }else{ res++; end--; start++; } } // for(int j=0; j< n; j++){ // System.out.print(diff[j]+" "); // } // System.out.println(); System.out.println(res); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
37de7de1d11e52aa92ea6480f83887c0
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.text.CollationElementIterator; import java.util.*; //changed import javax.lang.model.type.IntersectionType; import javax.print.attribute.standard.PrinterResolution; import javax.swing.text.html.CSS; import javax.swing.tree.TreeNode; public class codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static boolean isKthBitSet(int n, int k) { if (((n >> (k)) & 1) > 0) return true; return false; } static int fact(int a) { int f = 1; for (int i = 2; i <= a; i++) f *= i; return f; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static class TrieNode { HashMap<Character, TrieNode> children; String w; TrieNode() { children = new HashMap<>(); w = ""; } } static TrieNode root = new TrieNode(); public static void insert(String word) { TrieNode curr = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = curr.children.get(ch); if (node == null) { node = new TrieNode(); curr.children.put(ch, node); } curr = node; curr.w = word; } curr.w = word; } public static String prefix(String word) { TrieNode curr = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = curr.children.get(ch); if (node == null) return ""; curr = node; } return curr.w; } static class st { static int size; static long min[][]; static void init(int n) { size = 1; while (size < n) size *= 2; min = new long[2 * size][2]; } static void build(int arr[]) { build(arr, 0, 0, size); } static long merge(long l[], long r[]) { if (l[0] < r[0]) return l[1]; else if (r[0] < l[0]) return r[1]; else return l[1] + r[1]; } static void build(int arr[], int x, int lx, int rx) { if (rx - lx == 1) { if (lx < arr.length) { min[x][0] = arr[lx]; min[x][1] = 1; } return; } int m = (lx + rx) / 2; build(arr, 2 * x + 1, lx, m); build(arr, 2 * x + 2, m, rx); min[x][0] = Math.min(min[2 * x + 1][0], min[2 * x + 2][0]); min[x][1] = merge(min[2 * x + 1], min[2 * x + 2]); } static void set(int i, int v) { set(i, v, 0, 0, size); } static void set(int i, int v, int x, int lx, int rx) { if (rx - lx == 1) { min[x][0] = v; return; } int m = (lx + rx) / 2; if (i < m) set(i, v, 2 * x + 1, lx, m); else set(i, v, 2 * x + 2, m, rx); min[x][0] = Math.min(min[2 * x + 1][0], min[2 * x + 2][0]); min[x][1] = merge(min[2 * x + 1], min[2 * x + 2]); } static long[] min(int l, int r) { return min(l, r, 0, 0, size); } static long[] min(int l, int r, int x, int lx, int rx) { if (lx >= r || l >= rx) return new long[]{Long.MAX_VALUE, 0}; if (lx >= l && rx <= r) return new long[]{min[x][0], min[x][1]}; int m = (lx + rx) / 2; long s1[] = min(l, r, 2 * x + 1, lx, m); long s2[] = min(l, r, 2 * x + 2, m, rx); return new long[]{Math.min(s1[0], s2[0]), merge(s1, s2)}; } } public static void main(String[] args) throws IOException { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); InputReader sc = new InputReader(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int x[]=sc.nextIntArray(n); int y[]=sc.nextIntArray(n); List<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++) list.add(y[i]-x[i]); Collections.sort(list); int i=0,j=n-1; int ans=0; while(i<j && list.get(i)<0) { if(list.get(i)+list.get(j)>=0) { i++; j--; ans++; } else i++; } while(i>=0 && i<n && list.get(i)<0) i++; if(j>i) ans+=(j-i+1)/2; System.out.println(ans); } } public static List<Integer> solve(String s, int min, int max) { int n=s.length(); char ch[]=s.toCharArray(); HashMap<Character,List<Integer>> hm=new HashMap<>(); for(int i=1;i<n-1;i++) { if(!hm.containsKey(ch[i])) hm.put(ch[i],new ArrayList<>()); hm.get(ch[i]).add(i+1); } ArrayList<Integer> al=new ArrayList<>(); Arrays.sort(ch,1,ch.length-1); int count=0; al.add(1); for(int i=1;i<n-1;i++) { char c=ch[i]; if(c>=min && c<=max) { List<Integer> arr=hm.get(c); al.add(arr.get(arr.size()-1)); arr.remove(arr.size()-1); if(arr.size()==0) hm.remove(c); else hm.put(c,arr); count++; } } al.add(n); return al; } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } 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; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } } class bs{ public static boolean search(long arr[],long tar) { int i=0; int j=arr.length-1; while(j>=i) { int mid=i+(j-i)/2; if(arr[mid]==tar) return true; else if(arr[mid]>tar) j=mid-1; else i=mid+1; } return false; } public static int closestleft(long arr[],long tar) { int l=-1; int r=arr.length; while(r>l+1) { int m=l+(r-l)/2; if(arr[m]<=tar) l=m; else r=m; } return l; } public static int closestright(long arr[],long tar) { int l=-1; int r=arr.length; while(r>l+1) { int m=l+(r-l)/2; if(arr[m]<tar) l=m; else r=m; } return r; } } class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] static void merge(long 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 */ long L[] = new long[n1]; long R[] = new long[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(long 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); } } } // class st { // static int size; // static long[][] sums; // static long NEUTRAL=0; // public static void init(int n) // { // size=1; // while(size<n) // size*=2; // sums=new long[2*size][27]; // } // static int m; // public static long single(long v) // { // return 1; // } // public static long[] merge(long a[],long b[]) // { // if(a.length==0 && b.length==0) // return new long[0]; // if(a.length==0) // return b; // if(b.length==0) // return a; // for(int i=0;i<27;i++) // b[i]=b[i]+a[i]; // return b; // } // public static void build(long[] a,int x,int lx,int rx) // { // if(rx-lx==1) // { // long z=a[lx]; // sums[x][(int)z-96]=1; // return; // } // int mid=(lx+rx)/2; // build(a,2*x+1,lx,mid); // build(a,2*x+2,mid,rx); // sums[x]=merge(sums[2*x+1],sums[2*x+2]); // } // public static void build(long[] a) // { // build(a,0,0,size); // } // // public static void set(int i,long v,int x,int lx,int rx,long a[],char c) // { // if(rx-lx==1) // { // long z=a[i]; // sums[x][(int)z-96]=0; // sums[x][(int)c-96]=1; // return; // } // int m=(lx+rx)/2; // if(i<m) // set(i,v,2*x+1,lx,m,a,c); // else // set(i,v,2*x+2,m,rx,a,c); // sums[x]=merge(sums[2*x+1],sums[2*x+2]); // } // public static void set(int i,long v,long a[],char c) // { // set(i,v,0,0,size,a,c); // } // public static long[] calc(int l,int r) // { // return calc(l,r,0,0,size); // } // public static long[] calc(int l,int r,int x,int lx,int rx) // { // if(lx>=r || rx<=l) // return new long[0]; // if(lx>=l && rx<=r) // return sums[x]; // int mid=(lx+rx)/2; // long s1[]=calc(l,r,2*x+1,lx,mid); // long s2[]=calc(l,r,2*x+2,mid,rx); // return merge(s1,s2); // } // /* // public static int find_above(long v,int l,int x,int lx,int rx) // { // if(sums[x]<v) // return -1; // if(rx<=l) // return -1; // if(rx-lx==1) // return lx; // int m=(lx+rx)/2; // int res=find_above(v,l,2*x+1,lx,m); // if(res==-1) // res=find_above(v,l,2*x+2,m,rx); // return res; // } // public static int find_above(long v,int l) // { // return find_above(v,l,0,0,size); // } // public static int find(long k,int x,int lx,int rx,int n) // { // if(rx-lx==1) { // if(sums[x]==1) // return lx; // else // return -1; // } // int m=(lx+rx)/2; // long sl=sums[2*x+1]; // if(k<sl) // return find(k,2*x+1,lx,m,n); // else // return find(k-sl,2*x+2,m,rx,n); // } // public static int find(long k,int n) // { // return find(k,0,0,size,n); // }*/ //} class MathLib{ public static int[] sieveEratostheness(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } static boolean[] sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } public static int getMax(int[] inputArray){ int maxValue = inputArray[0]; for(int i=1;i < inputArray.length;i++){ if(inputArray[i] > maxValue){ maxValue = inputArray[i]; } } return maxValue; } // Method for getting the minimum value public static int getMin(int[] inputArray){ int minValue = inputArray[0]; for(int i=1;i<inputArray.length;i++){ if(inputArray[i] < minValue){ minValue = inputArray[i]; } } return minValue; } static int countSetBits(long n) { int count = 0; while (n > 0) { count += n & 1; n >>= 1; } return count; } static long binpow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1)>0) res = res * a; a = a * a; b >>= 1; } return res; } static ArrayList<Integer> printDivisors(int n) { ArrayList<Integer> al=new ArrayList<>(); for (int i=2; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) al.add(i); else // Otherwise print both { al.add(i); al.add(n/i); } } } return al; } static long fastPow(long a, long b, long mod) { if(b == 0) return 1L; long val = fastPow(a, b/2, mod); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } private static long safe_mod(long x, long m){ x %= m; if(x<0) x += m; return x; } private static long[] inv_gcd(long a, long b){ a = safe_mod(a, b); if(a==0) return new long[]{b,0}; long s=b, t=a; long m0=0, m1=1; while(t>0){ long u = s/t; s -= t*u; m0 -= m1*u; long tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if(m0<0) m0 += b/s; return new long[]{s,m0}; } public static long gcd(long a, long b){ a = java.lang.Math.abs(a); b = java.lang.Math.abs(b); return inv_gcd(a, b)[0]; } public static long lcm(long a, long b){ a = java.lang.Math.abs(a); b = java.lang.Math.abs(b); return a / gcd(a,b) * b; } public static long pow_mod(long x, long n, int m){ assert n >= 0; assert m >= 1; if(m == 1)return 0L; x = safe_mod(x, m); long ans = 1L; while(n > 0){ if((n&1) == 1) ans = (ans * x) % m; x = (x*x) % m; n >>>= 1; } return ans; } static long fact[]=new long[1000001]; static long inv_fact[]=new long[1000001]; static int p=1000000007; public static void precomp() { fact[0]=fact[1]=1; for(int i=2;i<=fact.length-1;i++) { fact[i]=(fact[i-1]*i)%p; } inv_fact[(int)1e6]=modpow(fact[(int)1e6],p-2); for(int i=fact.length-2;i>=0;i--) { inv_fact[i]=(inv_fact[i+1]*(i+1))%p; } } public static boolean summ(int n,int a,int b) { while(n>0) { if(n%10!=a && n%10!=b)return false; n/=10; } return true; } public static long modpow(long x,long n) { long res=1; while(n>0) { if(n%2!=0) { res=(res*x)%p;n--; } else { x=(x*x)%p;n/=2; } } return res; } public static long ncr(int n,int r) { if(r>n || r<0 || n<0)return 0; return fact[n]*inv_fact[r]%p*inv_fact[n-r]%p; } public static void bipartite(int u,boolean vis[],int color[],ArrayList<Integer>[] al) { vis[u]=true; for(int v:al[u]) { if(!vis[v]) { if(color[v]==-1) { color[v]=1-color[u]; bipartite(v,vis,color,al); } } } } public static long[] crt(long[] r, long[] m){ assert(r.length == m.length); int n = r.length; long r0=0, m0=1; for(int i=0; i<n; i++){ assert(1 <= m[i]); long r1 = safe_mod(r[i], m[i]), m1 = m[i]; if(m0 < m1){ long tmp = r0; r0 = r1; r1 = tmp; tmp = m0; m0 = m1; m1 = tmp; } if(m0%m1 == 0){ if(r0%m1 != r1) return new long[]{0,0}; continue; } long[] ig = inv_gcd(m0, m1); long g = ig[0], im = ig[1]; long u1 = m1/g; if((r1-r0)%g != 0) return new long[]{0,0}; long x = (r1-r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if(r0<0) r0 += m0; //System.err.printf("%d %d\n", r0, m0); } return new long[]{r0, m0}; } public static long floor_sum(long n, long m, long a, long b){ long ans = 0; if(a >= m){ ans += (n-1) * n * (a/m) / 2; a %= m; } if(b >= m){ ans += n * (b/m); b %= m; } long y_max = (a*n+b) / m; long x_max = y_max * m - b; if(y_max == 0) return ans; ans += (n - (x_max+a-1)/a) * y_max; ans += floor_sum(y_max, a, m, (a-x_max%a)%a); return ans; } public static java.util.ArrayList<Long> divisors(long n){ java.util.ArrayList<Long> divisors = new ArrayList<>(); java.util.ArrayList<Long> large = new ArrayList<>(); for(long i=1; i*i<=n; i++) if(n%i==0){ divisors.add(i); if(i*i<n) large.add(n/i); } for(int p=large.size()-1; p>=0; p--){ divisors.add(large.get(p)); } return divisors; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
cf5badd984a1360d0586670e16bf8f92
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class d3 { static class pair{ long a; long b; pair(long a,long b){ this.a = a; this.b = b; } } static int[] x = {-1,0,1,-1,1,-1,0,1}; static int[] y = {1,1,1,0,0,-1,-1,-1}; static long answer = 0L; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); while(t-->0){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] x = new int[n]; for(int i = 0;i<n;i++){ x[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); ArrayList<Integer> al = new ArrayList<>(); int[] y = new int[n]; for(int i = 0;i<n;i++){ y[i] = Integer.parseInt(st.nextToken()); al.add((y[i]-x[i])); } Collections.sort(al); int left = 0; int right = n-1; while(al.get(left)+al.get(right) < 0 && left < right){ left++; } int count = 0; while(left < right){ while(al.get(left)+al.get(right) < 0 && left < right){ left++; } if(left < right){ count++; left++; right--; } } bw.write(count+"\n"); } bw.flush(); } public static void check(int[] x,int[] y,int mid){ } public static void rec(int[][] mat,int i,int j,ArrayList<Integer> al){ int n = mat.length; int m = mat[0].length; if(i < 0 || j < 0 || i >= n || j >= m){ return; } // if(i == 0 || j == m-1){ // // al.add(mat[i][j]); // answer = Math.max(answer,mat[i][j]); // return; // } // System.out.println(mat[i][j]); al.add(mat[i][j]); rec(mat,i-1,j+1,al); long max = 0; long cur = 0; int size = al.size(); for(int k = 0;k<size;k++){ cur += al.get(k); if(cur < 0){ cur = 0; } max = Math.max(max,cur); } answer = Math.max(max,answer); } public static boolean recur(int[][] mat,int i,int j,int n,int m){ if(i < 0 || j < 0 || i >= n || j >= m){ return true; } if(mat[i][j] == 0){ return false; } return recur(mat, i-1, 1+j, n, m); } public static void bfs(int[][] mat,boolean[][] visited,int si,int sj,int di,int dj){ Queue<Integer> queue = new LinkedList<>(); HashMap<Integer,Integer> map = new HashMap<>(); int n = mat.length; int m = mat[0].length; map.put(si*m+sj, -1); queue.add(si*m+sj); //System.out.println(queue.peek()); queue.add(-1); int req = di*m+dj; int distance = 0; visited[si][sj] = true; boolean found = false; while(!queue.isEmpty()){ int val = queue.poll(); System.out.println("val = "+val); if(queue.size() == 0){ break; } if(val == -1){ distance++; queue.add(-1); continue; } if(val == req){ found = true; break; } int cx = val/m; int cy = val%m; for(int i = 0;i<8;i++){ int x1 = cx+x[i]; int y1 = cy+y[i]; if(x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || visited[x1][y1] == true || mat[x1][y1] == -1){ continue; } map.put(x1*m+y1, val); visited[x1][y1] = true; queue.add(x1*m+y1); } } System.out.println(distance); int parent = req; while(map.get(parent) != -1){ parent = map.get(parent); } StringBuffer sb = new StringBuffer(); for(int i = 0;i<distance;i++){ int m1 = parent/n; int m2 = parent%m; sb.append(m1+","+m2+" -> "); parent = map.get(parent); } } public static int bceil1(int[] al,int val,int low,int high){ int index = 0; while(low <= high){ int mid = low + (high-low)/2; if(al[mid] <= val){ index = Math.max(mid,index); low = mid+1; } else{ high = mid-1; } } return index; } public static void recur1(int cur,int n,int start,int m,ArrayList<Integer> temp){ if(start-1 > m){ return; } if(temp.size() == n){ for(int i = 0;i<n;i++){ System.out.print(temp.get(i)+" "); } System.out.println(); return; } for(int i = start;i<=m;i++){ temp.add(i); recur1(cur+1,n,i+1,m,temp); temp.remove(temp.size()-1); } // recur1(cur, n,start+1,m,temp); } public static void fill(int x,int l,int r,long[] arr,long[] seg){ if(l == r){ seg[x] = arr[l]; //leaf element return; } int mid = (l+r)/2; fill(2*x,l,mid,arr,seg); fill(2*x+1,mid+1,r,arr,seg); seg[x] =seg[2*x]+seg[2*x+1]; } public static long sum(int x,int l,int r,int lr,int rr,long[] arr,long[] seg){ //completely outside if(r < lr || rr < l){ return 0; } // lr.....l....r......rr if(lr <= l && r <= rr){ return seg[x]; } int mid = (l+r)/2; long a = 0; a += sum(2*x,l,mid,lr,rr,arr,seg); a += sum(2*x+1,mid+1,r,lr,rr,arr,seg); return a; } public static void update(int x,int l,int r,int i,int val,long[] arr,long seg[]){ if(l == r){ seg[x] = val; return; } int mid = (l+r)/2; if(l <= i && i <= mid){ update(2*x,l,mid,i,val,arr,seg); } else{ update(2*x+1,mid+1,r,i,val,arr,seg); } seg[x] = (seg[2*x]+seg[2*x+1]); } // public static void mergesort(int[] arr,int low,int high){ // if(low == high){ // return; // } // int mid = (low+high)/2; // mergesort(arr, low, mid); // mergesort(arr, mid+1, high); // merge(arr,low,high); // } // public static void merge(int[] arr,int low,int high){ // int mid = (low+high)/2; // int p1 = low; // int p2 = mid+1; // int k = 0; // int[] temp = new int[high-low+1]; // while(p1 <= mid && p2 <= high){ // if(arr[p1] <= arr[p2]){ // temp[k++] = arr[p1++]; // } // else{ // temp[k++] = arr[p2++]; // count += (mid-p1+1); // } // } // while(p1 <= mid){ // temp[k++] = arr[p1++]; // } // while(p2 <= high){ // temp[k++] = arr[p2++]; // } // for(int i = 0;i<temp.length;i++){ // arr[low+i] = temp[i]; // } // } public static boolean check2(long mid,long n,long r,long c){ long noofrows = mid/r; long percolumn = mid/c; long cur = noofrows*percolumn; if(cur >= n){ return true; } return false; } public static int ceil1(int[] arr,int low,int high,int ele){ while(low <= high){ int mid = (low+high)/2; if(arr[mid] < ele){ low = mid+1; } else{ high = mid-1; } } return low; } public static int floor1(int[] arr,int low,int high,int ele){ while(low <= high){ int mid = (low+high)/2; if(arr[mid] <= ele){ low = mid+1; } else{ high = mid-1; } } return low; } public static int dfs(int[] arr,int i,int[] ans,boolean[] visited){ if(i >= arr.length){ return 0; } if(visited[i]){ return ans[i]; } visited[i] = true; ans[i] += arr[i]+dfs(arr,i+arr[i],ans,visited); return ans[i]; } // public static int dfs(ArrayList<ArrayList<Integer>> graph,int i,boolean[] visited){ // if(!visited[i]){ // visited[i]=true; // for(int child : graph.get(i)){ // if(!visited[child]){ // return dfs(graph, child, visited); // } // else{ // return child; // } // } // } // else{ // return i; // } // return 0; // } public static long power(long a,long b,long mod){ long res = 1L; while(b > 0){ if(b%2 == 1){ res = (res%mod * a%mod)%mod; } b /= 2; a = (a*a)%mod; } return res; } public static boolean comp(String s1,String s2){ int count = 0; for(int i = 0;i<2;i++){ if(s1.charAt(i) == s2.charAt(1-i)){ count++; } else{ return false; } } // System.out.println(s1+" "+s2); return true; } public static int ceil(ArrayList<Long> al,long val){ int low = 0; int high = al.size()-1; int pos = 0; while(low <= high){ int mid = (low+high)/2; if(al.get(mid) == val){ pos = Math.max(pos,mid); low = mid+1; } else if(al.get(mid) < val){ low = mid+1; } else{ high = mid-1; } } return pos; } public static int floor(ArrayList<Long> al,long val){ int low = 0; int high = al.size()-1; int pos = high; while(low <= high){ int mid = (low+high)/2; if(al.get(mid) == val){ pos = Math.min(pos,mid); high = mid-1; } else if(al.get(mid) < val){ low = mid+1; } else{ high = mid-1; } } return pos; } public static long gcd(long a,long b){ if(b == 0L){ return a; } return gcd(b,a%b); } public static int bceil(ArrayList<Integer> al,int val,int low,int high){ int index = 0; while(low <= high){ int mid = low + (high-low)/2; if(al.get(mid) <= val){ index = Math.max(mid,index); low = mid+1; } else{ high = mid-1; } } return index; } public static int bfloor(ArrayList<Integer> al,int val){ int low = 0; int high = al.size()-1; int index = -1; while(low <= high){ int mid = low + (high-low)/2; int mval = al.get(mid); if(mval >= val){ high = mid-1; index = Math.max(index,mid); } else{ low = mid+1; } } return index; } public static void primefact(int n){ for(int i = 2;i*i<=n;i++){ if(n%i == 0){ while(n%i == 0){ n /= i; System.out.println(i); } } } System.out.println(n); } public static boolean prime(long n){ int count = 0; for(long i = 1;i*i <= n;i++){ if(n%i == 0){ if(i*i == n){ count++; } else{ count += 2; } } } if(count == 2){ return true; } return false; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
656a1fee2c31a93e1d8c378e7b3b836e
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
/* Rating: 1378 Date: 12-09-2022 Time: 21-04-27 Author: Kartik Papney Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/ Leetcode: https://leetcode.com/kartikpapney/ Codechef: https://www.codechef.com/users/kartikpapney ----------------------------Jai Shree Ram---------------------------- */ import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class D_Friends_and_the_Restaurant { public static void s() { int n = sc.nextInt(); long[] x = sc.readLongArray(n); long[] y = sc.readLongArray(n); long[] z = new long[n]; for(int i=0; i<n; i++) z[i] = y[i]-x[i]; TreeMap<Long, Integer> min = new TreeMap<>(), max = new TreeMap<>(); for(long val : z) { if(val < 0) min.put(val, min.getOrDefault(val, 0)+1); else max.put(val, max.getOrDefault(val, 0)+1); } long ans = 0; while(max.size() > 0) { long element = max.lastKey(); if(max.get(element) == 1) max.remove(element); else max.put(element, max.get(element)-1); Long leastmin = min.ceilingKey(-element); // p.writes(element +", " + leastmin); if(leastmin == null || leastmin+element<0) { int cnt = 1; while(!max.isEmpty()) { cnt += max.pollFirstEntry().getValue(); } ans+=(cnt)/2; break; } else { if(min.get(leastmin) == 1) min.remove(leastmin); else min.put(leastmin, min.get(leastmin)-1); ans++; } } // p.writeln(); p.writeln(ans); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while (t-- != 0) { s(); } p.print(); } public static boolean isBipartite(ArrayList<ArrayList<Integer>> graph) { int n = graph.size(); Integer[] visited = new Integer[graph.size()]; Queue<int[]> q = new ArrayDeque<>(); for(int i=1; i<=n; i++) { if(visited[i] != null) continue; q.add(new int[]{i, 1}); while(!q.isEmpty()) { int[] poll = q.remove(); int node = poll[0]; int color = poll[1]; if(visited[node] != null) { if(visited[node] == color) continue; return false; } visited[node] = color; for(int nbr : graph.get(node)) { if(visited[nbr] == null) q.add(new int[]{nbr, (color+1)%2}); } } } return true; } public static boolean debug = false; static void debug(String st) { if(debug) p.writeln(st); } static final Integer MOD = (int) 1e9 + 7; static final FastReader sc = new FastReader(); static final Print p = new Print(); static class Functions { static void sort(int... a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long... a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static int max(int... a) { int max = Integer.MIN_VALUE; for (int val : a) max = Math.max(val, max); return max; } static int min(int... a) { int min = Integer.MAX_VALUE; for (int val : a) min = Math.min(val, min); return min; } static long min(long... a) { long min = Long.MAX_VALUE; for (long val : a) min = Math.min(val, min); return min; } static long max(long... a) { long max = Long.MIN_VALUE; for (long val : a) max = Math.max(val, max); return max; } static long sum(long... a) { long sum = 0; for (long val : a) sum += val; return sum; } static int sum(int... a) { int sum = 0; for (int val : a) sum += val; return sum; } public static long mod_add(long a, long b) { return (a % MOD + b % MOD + MOD) % MOD; } public static long pow(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) != 0) res = mod_mul(res, a); a = mod_mul(a, a); b >>= 1; } return res; } public static long mod_mul(long a, long b) { long res = 0; a %= MOD; while (b > 0) { if ((b & 1) > 0) { res = mod_add(res, a); } a = (2 * a) % MOD; b >>= 1; } return res; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long factorial(long n) { long res = 1; for (int i = 1; i <= n; i++) { res = (i % MOD * res % MOD) % MOD; } return res; } public static int count(int[] arr, int x) { int count = 0; for (int val : arr) if (val == x) count++; return count; } public static ArrayList<Integer> generatePrimes(int n) { boolean[] primes = new boolean[n]; for (int i = 2; i < primes.length; i++) primes[i] = true; for (int i = 2; i < primes.length; i++) { if (primes[i]) { for (int j = i * i; j < primes.length; j += i) { primes[j] = false; } } } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < primes.length; i++) { if (primes[i]) arr.add(i); } return arr; } } static class Print { StringBuffer strb = new StringBuffer(); public void write(Object str) { strb.append(str); } public void writes(Object str) { char c = ' '; strb.append(str).append(c); } public void writeln(Object str) { char c = '\n'; strb.append(str).append(c); } public void writeln() { char c = '\n'; strb.append(c); } public void yes() { char c = '\n'; writeln("YES"); } public void no() { writeln("NO"); } public void writes(int... arr) { for (int val : arr) { write(val); write(' '); } } public void writes(long... arr) { for (long val : arr) { write(val); write(' '); } } public void writeln(int... arr) { for (int val : arr) { writeln(val); } } public void print() { System.out.print(strb); } public void println() { System.out.println(strb); } } 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String[] readStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = nextLine(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double[] readArrayDouble(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = new String(); try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
ce3fcd8c900f12f83a8d247688da7a2c
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
/* Author: Spidey2182 #: 1729D */ import java.util.*; public class FriendsAndTheRestaurant implements Runnable { static ContestScanner sc = new ContestScanner(); static ContestPrinter out = new ContestPrinter(); public static void main(String[] args) { new Thread(null, new FriendsAndTheRestaurant(), "main", 1 << 28).start(); } public void run() { int testCases=sc.nextInt(); Multiset<Integer> pos=new Multiset<>(), neg=new Multiset<>(); for(int testCase=1; testCase<=testCases; testCase++) { int n=sc.nextInt(), ans=0, count; int[] x=sc.nextIntArray(n); int[] y=sc.nextIntArray(n); pos.clear(); neg.clear(); for (int i = 0; i < n; i++) { if(y[i]>=x[i]) pos.addOne(y[i]-x[i]); else neg.addOne(y[i]-x[i]); } count= (int) neg.count(0); neg.removeAll(0); for(Integer i; !neg.isEmpty() && !pos.isEmpty(); neg.removeOne(i)) { i=neg.lastKey(); if(i+pos.lastKey()>=0) { ans++; pos.removeOne(pos.ceilingKey(-i)); } } for(int i:pos.keySet()) count+=pos.get(i); out.println(ans+(count>>1)); } out.flush(); out.close(); } static final int MOD = (int) 1e9 + 7; static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } } class Multiset<T> extends java.util.TreeMap<T,Long>{ public Multiset(){ super(); } public Multiset(java.util.List<T> list){ super(); for(T e: list) this.addOne(e); } public long count(Object elm){ return getOrDefault(elm,0L); } public void add(T elm, long amount){ if(!this.containsKey(elm)) put(elm, amount); else replace(elm, get(elm)+amount); if(this.count(elm)==0) this.remove(elm); } public void addOne(T elm){ this.add(elm, 1); } public void removeOne(T elm){ this.add(elm, -1); } public void removeAll(T elm){ this.add(elm, -this.count(elm)); } public static<T> Multiset<T> merge(Multiset<T> a, Multiset<T> b){ Multiset<T> c = new Multiset<>(); for(T x: a.keySet()) c.add(x, a.count(x)); for(T y: b.keySet()) c.add(y, b.count(y)); return c; } } //Credits: https://github.com/NASU41/AtCoderLibraryForJava/tree/master/ContestIO class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in) { this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException { super(new java.io.PrintStream(file)); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public <T> void printArray(T[] array) { this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n - 1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) { this.printArray(array, " ", map); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
c781b0138b773966ca628a50eca8b2d1
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class D { static long mod=(long)1e9+7; static long mod1=998244353; static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args) throws IOException { int t= in.nextInt(); while(t-->0) { int n = in.nextInt(); long[] x = in.readArray(n); long[] y = in.readArray(n); int count = 0; ArrayList<Long> neg = new ArrayList<>(); ArrayList<Long> pos = new ArrayList<>(); for(int i = 0;i<n;i++){ if(y[i]-x[i]<0) neg.add(y[i]-x[i]); else pos.add(y[i]-x[i]); } Collections.sort(neg); Collections.sort(pos); int i = 0,j = pos.size()-1; while(i<neg.size() && j>=0){ if(neg.get(i)+pos.get(j)>=0) { i++; j--; count++; } else{ i++; } } count += (j+1)/2; out.println(count); } out.close(); } static final Random random=new Random(); static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n);long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long gcd(long x, long y){ if(x==0) return y; if(y==0) return x; long r=0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while(a % b != 0){ r = a % b; a = b; b = r; } return r; } static long modulo(long a,long b,long c){ long x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } public static void debug(Object... o){ System.err.println(Arrays.deepToString(o)); } static int upper_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low<high){ mid=low+(high-low)/2; if(x>=arr[mid]) low=mid+1; else high=mid; } return low; } static int lower_bound(int[] arr,int n,int x){ int mid; int low=0; int high=n; while(low<high){ mid=low+(high-low)/2; if(x<=arr[mid]) high=mid; else low=mid+1; } return low; } static String printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000"); return String.valueOf(ft.format(d)); } static int countBit(long mask){ int ans=0; while(mask!=0){ mask&=(mask-1); ans++; } return 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()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long[] readArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
8f756250046fc07204dee00e4ff11a44
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class c{ static int []f; //static int []f2; static int []size; //static int []size2; //static int []a=new int [500001]; static int max=Integer.MAX_VALUE; static int ans=0; static Set<Integer>set; public static void main(String []args) { MyScanner s=new MyScanner(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); PriorityQueue<Integer> q=new PriorityQueue<>((o1,o2)->o2-o1); PriorityQueue<Integer> p=new PriorityQueue<>((o1,o2)->o1-o2); boolean f=true; int []a=new int [n]; int []b=new int [n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } for(int i=0;i<n;i++) b[i]=s.nextInt(); long min=0; long max=0; long res=0; for(int i=0;i<n;i++) { int z=b[i]-a[i]; if(z>=0) { q.add(z); max+=z; } else {p.add(z); min+=z;} } while(!p.isEmpty()&&!q.isEmpty()) { int sum=p.peek()+q.peek(); if(sum>=0) { res++; p.poll(); q.poll(); } else p.poll(); } res+=q.size()/2; out.println(res); } out.close(); } public static void swap(char []a,int j) { char x=a[j]; a[j]=a[j+1]; a[j+1]=x; } public static boolean is(int j) { for(int i=2;i<=(int )Math.sqrt(j);i++) { if(j%i==0)return false; } return true; } public static String addStrings(String num1, String num2) { StringBuilder s=new StringBuilder(); int i=num1.length()-1; int j=num2.length()-1; int res=0; while(i>=0||j>=0||res!=0){ if(i>=0)res+=num1.charAt(i)-'0'; if(j>=0)res+=num2.charAt(j)-'0'; s.append(res%10); res/=10; i--;j--; } return s.reverse().toString(); } public static int find (int []father,int x) { if(x!=father[x]) x=find(father,father[x]); return father[x]; } public static void union(int []father,int x,int y,int []size) { x=find(father,x); y=find(father,y); if(x==y) return ; if(size[x]<size[y]) { int tem=x; x=y; y=tem; } father[y]=x; size[x]+=size[y]; return ; } public static void shufu(int []f) { for(int i=0;i<f.length;i++) { int k=(int)(Math.random()*(f.length)); int t=f[k]; f[k]=f[0]; f[0]=t; } } public static int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } public static int lcm(int x,int y) { return x*y/gcd(x,y); } /* public static void buildertree(int k,int l,int r) { if(l==r) { f[k]=a[l]; return ; } int m=l+r>>1; buildertree(k+k,l,m); buildertree(k+k+1,m+1,r); f[k]= } public static void update(int u,int l,int r,int x,int c) { if(l==x && r==x) { f[u]=c; return; } int mid=l+r>>1; if(x<=mid)update(u<<1,l,mid,x,c); else if(x>=mid+1)update(u<<1|1,mid+1,r,x,c); f[u]=Math.max(f[u+u], f[u+u+1]); } public static int query(int k,int l,int r,int x,int y) { if(x==l&&y==r) { return f[k]; } int m=l+r>>1; if(y<=m) { return query(k+k,l,m,x,y); } else if(x>m)return query(k+k+1,m+1,r,x,y); else { int i=query(k+k,l,m,x,m),j=query(k+k+1,m+1,r,m+1,y); return Math.max(j, Math.max(i+j, i)); } } public static void calc(int k,int l,int r,int x,int z) { f[k]+=z; if(l==r) { return ; } int m=l+r>>1; if(x<=m) calc(k+k,l,m,x,z); else calc(k+k+1,m+1,r,x,z); } */ public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
d6dce1a14e97063084e705f9491ffc66
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) throws IOException{ FastScanner fs = new FastScanner(); int tt = fs.nextInt(); while(tt-- > 0) { int n = fs.nextInt(); int[] x = fs.readArray(n), y = fs.readArray(n); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = y[i]-x[i]; } Arrays.sort(a); int i = 0, j = n - 1; int count = 0; while (i < j) { if (a[i] + a[j] >= 0) { count++; i++; j--; } else { i++; } } System.out.println(count); } } public static long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a%b); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
224dcef247cf6b14bfef3617617a9105
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class D{ public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(st.nextToken()); for(int tc = 0; tc < t; tc++){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] x = new int[n]; int[] y = new int[n]; ArrayList<Integer> positives = new ArrayList<Integer>(); ArrayList<Integer> negatives = new ArrayList<Integer>(); st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++){ x[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++){ y[i] = Integer.parseInt(st.nextToken()); if(x[i] > y[i]){ negatives.add(x[i] - y[i]); }else{ positives.add(y[i] - x[i]); } } Collections.shuffle(negatives); Collections.shuffle(positives); Collections.sort(negatives); Collections.sort(positives); int nIdx = negatives.size() - 1; int days = 0; outer: for(int pIdx = positives.size() - 1; pIdx >= 0; pIdx --){ if(nIdx == -1){ days += (pIdx+1)/2; break outer; } while(positives.get(pIdx) < negatives.get(nIdx)){ nIdx--; if(nIdx == -1){ days += (pIdx + 1)/2; break outer; } } days++; nIdx --; } pw.println(days); } pw.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9b9973537f2eb3681d85345760598252
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class CP { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long mod_mul( long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum( long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void print(long[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static void print(int[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y){ if(y<0) return 0; long m = mod; if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; // factorial private long[] z1; // inverse factorial private long[] z2; // incerse number private long mod; public Combinations(long N , long mod) { this.mod = mod; z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return z1[(int)n]; } long ncr(long N, long R) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = gcd(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(0); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return min(left, right); } public void update(int index , int val) { arr[index] = val; update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static int TC; static StringBuilder sb = new StringBuilder(); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); TC = 0; for(int i = 1 ; i<=tc ; i++) { TC++; // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.print(sb); } static void TEST_CASE() { int n = sc.nextInt(); long[] x = new long[n] , y = new long[n]; for(int i = 0 ;i<n ; i++) x[i] = sc.nextLong(); for(int i = 0 ;i<n ;i++) y[i] = sc.nextLong(); long[] dif = new long[n]; for(int i = 0 ;i<n ; i++) { dif[i] = y[i]-x[i]; } sort(dif); long tot = 0; int i = 0 , j = n-1;; while(i<j) { if(dif[i] + dif[j]>=0) { i++; j--; tot++; }else { i++; } } sb.append(tot+"\n"); } } /*******************************************************************************************************************************************************/ /** */
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
e27a9d88039b506afdb6a1845b6b0ef2
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.*; import java.util.StringTokenizer; public class copy { static int log=30; static int[][] ancestor; static int[] depth; static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p]) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i]) { arr.add(i); } } } public static long fac(long N, long mod) { if (N == 0) return 1; if(N==1) return 1; return ((N % mod) * (fac(N - 1, mod) % mod)) % mod; } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(int n, int r, long p,long[] fac) { if (n < r) return 0; // Base case if (r == 0) return 1; return ((fac[n] % p * (modInverse(fac[r], p) % p)) % p * (modInverse(fac[n-r], p) % p)) % p; } public static int find(int[] parent, int x) { if (parent[x] == x) return x; return find(parent, parent[x]); } public static void merge(int[] parent, int[] rank, int x, int y,int[] child) { int x1 = find(parent, x); int y1 = find(parent, y); if (rank[x1] > rank[y1]) { parent[y1] = x1; child[x1]+=child[y1]; } else if (rank[y1] > rank[x1]) { parent[x1] = y1; child[y1]+=child[x1]; } else { parent[y1] = x1; child[x1]+=child[y1]; rank[x1]++; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int power(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p,int[] fac) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r // int[] fac = new int[n + 1]; // fac[0] = 1; // // for (int i = 1; i <= n; i++) // fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long[][] ncr(int n,int r) { long[][] dp=new long[n+1][r+1]; for(int i=0;i<=n;i++) dp[i][0]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=r;j++) { if(j>i) continue; dp[i][j]=dp[i-1][j-1]+dp[i-1][j]; } } return dp; } public static boolean prime(long N) { int c=0; for(int i=2;i*i<=N;i++) { if(N%i==0) ++c; } return c==0; } public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child) { int c=0; for(int i:arr.get(x)) { if(i!=parent) { // System.out.println(i+" hello "+x); depth[i]=depth[x]+1; ancestor[i][0]=x; // if(i==2) // System.out.println(parent+" hello"); for(int j=1;j<log;j++) ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1]; c+=sparse_ancestor_table(arr,i,x,child); } } child[x]=1+c; return child[x]; } public static int lca(int x,int y) { if(depth[x]<depth[y]) { int temp=x; x=y; y=temp; } x=get_kth_ancestor(depth[x]-depth[y],x); if(x==y) return x; // System.out.println(x+" "+y); for(int i=log-1;i>=0;i--) { if(ancestor[x][i]!=ancestor[y][i]) { x=ancestor[x][i]; y=ancestor[y][i]; } } return ancestor[x][0]; } public static int get_kth_ancestor(int K,int x) { if(K==0) return x; int node=x; for(int i=0;i<log;i++) { if(K%2!=0) { node=ancestor[node][i]; } K/=2; } return node; } public static ArrayList<Integer> primeFactors(int n) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) factors.add(2); while (n%2==0) { 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 if(n%i==0) factors.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { factors.add(n); } return factors; } static long ans=1,mod=1000000007; public static void recur(long X,long N,int index,ArrayList<Integer> temp) { // System.out.println(X); if(index==temp.size()) { System.out.println(X); ans=((ans%mod)*(X%mod))%mod; return; } for(int i=0;i<=60;i++) { if(X*Math.pow(temp.get(index),i)<=N) recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp); else break; } } public static int upper(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)<X) l=mid+1; else { if(mid-1>=0 && temp.get(mid-1)>=X) r=mid-1; else return mid; } } return -1; } public static int lower(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)>X) r=mid-1; else { if(mid+1<temp.size() && temp.get(mid+1)<=X) l=mid+1; else return mid; } } return -1; } public static int[] check(String shelf,int[][] queries) { int[] arr=new int[queries.length]; ArrayList<Integer> indices=new ArrayList<>(); for(int i=0;i<shelf.length();i++) { char ch=shelf.charAt(i); if(ch=='|') indices.add(i); } for(int i=0;i<queries.length;i++) { int x=queries[i][0]-1; int y=queries[i][1]-1; int left=upper(indices,x); int right=lower(indices,y); if(left<=right && left!=-1 && right!=-1) { arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1); } else arr[i]=0; } return arr; } static boolean check; public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited) { visited[x]=true; PriorityQueue<Integer> pq=new PriorityQueue<>(); for(int i:arr.get(x)) { if(color[i]<color[x]) pq.add(color[i]); if(color[i]==color[x]) check=false; if(!visited[i]) check(arr,i,color,visited); } int start=1; while(pq.size()>0) { int temp=pq.poll(); if(temp==start) ++start; else break; } if(start!=color[x]) check=false; } static boolean cycle; public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr) { if(stack[x]) { cycle=true; return; } visited[x]=true; for(int i:arr.get(x)) { if(!visited[x]) { cycle(stack,visited,i,arr); } } stack[x]=false; } public static int check(char[][] ch,int x,int y) { int cnt=0; int c=0; int N=ch.length; int x1=x,y1=y; while(c<ch.length) { if(ch[x][y]=='1') ++cnt; // if(x1==0 && y1==3) // System.out.println(x+" "+y+" "+cnt); x=(x+1)%N; y=(y+1)%N; ++c; } return cnt; } public static void s(char[][] arr,int x) { char start=arr[arr.length-1][x]; for(int i=arr.length-1;i>0;i--) { arr[i][x]=arr[i-1][x]; } arr[0][x]=start; } public static void shuffle(char[][] arr,int x,int down) { int N= arr.length; down%=N; char[] store=new char[N-down]; for(int i=0;i<N-down;i++) store[i]=arr[i][x]; for(int i=0;i<arr.length;i++) { if(i<down) { // Printing rightmost // kth elements arr[i][x]=arr[N + i - down][x]; } else { // Prints array after // 'k' elements arr[i][x]=store[i-down]; } } } public static String form(int C1,char ch1,char ch2) { char ch=ch1; String s=""; for(int i=1;i<=C1;i++) { s+=ch; if(ch==ch1) ch=ch2; else ch=ch1; } return s; } public static void printArray(long[] arr) { for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static boolean check(long mid,long[] arr,long K) { long[] arr1=Arrays.copyOfRange(arr,0,arr.length); long ans=0; for(int i=0;i<arr1.length-1;i++) { if(arr1[i]+arr1[i+1]>=mid) { long check=(arr1[i]+arr1[i+1])/mid; // if(mid==5) // System.out.println(check); long left=check*mid; left-=arr1[i]; if(left>=0) arr1[i+1]-=left; ans+=check; } // if(mid==5) // printArray(arr1); } // if(mid==5) // System.out.println(ans); ans+=arr1[arr1.length-1]/mid; return ans>=K; } public static long search(long sum,long[] arr,long K) { long l=1,r=sum/K; while(l<=r) { long mid=(l+r)/2; if(check(mid,arr,K)) { if(mid+1<=sum/K && check(mid+1,arr,K)) l=mid+1; else return mid; } else r=mid-1; } return -1; } public static void primeFactors(int n,HashSet<Integer> hp) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) hp.add(2); while (n%2==0) { 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 if(n%i==0) hp.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { hp.add(n); } } public static boolean check(String s) { HashSet<Character> hp=new HashSet<>(); char ch=s.charAt(0); for(int i=1;i<s.length();i++) { // System.out.println(hp+" "+s.charAt(i)); if(hp.contains(s.charAt(i))) { // System.out.println(i); // System.out.println(hp); // System.out.println(s.charAt(i)); return false; } if(s.charAt(i)!=ch) { hp.add(ch); ch=s.charAt(i); } } return true; } public static int check_end(String[] arr,boolean[] st,char ch) { for(int i=0;i<arr.length;i++) { if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1)) return i; } for(int i=0;i<arr.length;i++) { if(ch==arr[i].charAt(0) && !st[i]) return i; } return -1; } public static int check_start(String[] arr,boolean[] st,char ch) { for(int i=0;i<arr.length;i++) { // if(ch=='B') // { // if(!st[i]) // System.out.println(arr[i]+" hello"); // } if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0)) return i; } for(int i=0;i<arr.length;i++) { // if(ch=='B') // { // if(!st[i]) // System.out.println(arr[i]+" hello"); // } if(ch==arr[i].charAt(arr[i].length()-1) && !st[i]) return i; } return -1; } public static boolean palin(int N) { String s=""; while(N>0) { s+=N%10; N/=10; } int l=0,r=s.length()-1; while(l<=r) { if(s.charAt(l)!=s.charAt(r)) return false; ++l; --r; } return true; } public static boolean check(long org_s,long org_d,long org_n,long check_ele) { if(check_ele<org_s) return false; if((check_ele-org_s)%org_d!=0) return false; long num=(check_ele-org_s)/org_d; // if(check_ele==5) // System.out.println(num+" "+org_n); return num+1<=org_n; } public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff) { // System.out.println(c); long max=Math.max(c,b_diff); long min=Math.min(c,b_diff); long lcm=(max/gcd(max,min))*min; // System.out.println(lcm); // System.out.println(c); // System.out.println(c_diff); // if(b_diff>c) // { long start_point=c_diff/c-c_diff/lcm; // System.out.println(start_point); // } // else // { // start_point=c_diff/b_diff-c_diff/c; // } // System.out.println(c+" "+start_point); return (start_point%mod*start_point%mod)%mod; } public static boolean check_bounds(int x, int y, int N, int M) { return x>=0 && x<N && y>=0 && y<M; } static boolean found=false; public static void check(int x,int y,int[][] arr,boolean status[][]) { if(arr[x][y]==9) { found=true; return; } status[x][y]=true; if(check_bounds(x-1,y, arr.length,arr[0].length)&& !status[x-1][y]) check(x-1,y,arr,status); if(check_bounds(x+1,y, arr.length,arr[0].length)&& !status[x+1][y]) check(x+1,y,arr,status); if(check_bounds(x,y-1, arr.length,arr[0].length)&& !status[x][y-1]) check(x,y-1,arr,status); if(check_bounds(x,y+1, arr.length,arr[0].length)&& !status[x][y+1]) check(x,y+1,arr,status); } public static int check(String s1,String s2,int M) { int ans=0; for(int i=0;i<M;i++) { ans+=Math.abs(s1.charAt(i)-s2.charAt(i)); } return ans; } public static int check(int[][] arr,int dir1,int dir2,int x1,int y1) { int sum=0,N=arr.length,M=arr[0].length; int x=x1+dir1,y=y1+dir2; while(x<N && x>=0 && y<M && y>=0) { sum+=arr[x][y]; x=x+dir1; y+=dir2; } return sum; } public static int check(long[] pref,long X,int N) { if(X>pref[N-1]) return -1; // System.out.println(pref[0]); if(X<=pref[0]) return 1; int l=0,r=N-1; while(l<=r) { int mid=(l+r)/2; if(pref[mid]>=X) { if(mid-1>=0 && pref[mid-1]<X) return mid+1; else r=mid-1; } else l=mid+1; } return -1; } private static long mergeAndCount(long[] arr, int l, int m, int r) { // Left subarray long[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray long[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(long[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } public static long check(long L,long R) { long ans=0; for(int i=1;i<=Math.pow(10,8);i++) { long A=i*(long)i; if(A<L) continue; long upper=(long)Math.floor(Math.sqrt(A-L)); long lower=(long)Math.ceil(Math.sqrt(Math.max(A-R,0))); if(upper>=lower) ans+=upper-lower+1; } return ans; } public static int check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[]store) { int index=0; ArrayList<Integer> temp=arr.get(x); for(int i:temp) { if(i!=parent) { index+=check(arr,i,x,store); } } store[x]=index; return index+1; } public static void finans(int[][] store,ArrayList<ArrayList<Integer>> arr,int x,int parent) { // ++delete; // System.out.println(x); if(store[x][0]==0 && store[x][1]==0) return; if(store[x][0]!=0 && store[x][1]==0) { ++delete; ans+=store[x][0]; return; } if(store[x][0]==0 && store[x][1]!=0) { ++delete; ans+=store[x][1]; return; } ArrayList<Integer> temp=arr.get(x); if(store[x][0]!=0 && store[x][1]!=0) { ++delete; if(store[x][0]>store[x][1]) { ans+=store[x][0]; for(int i=temp.size()-1;i>=0;i--) { if(temp.get(i)!=parent) { finans(store,arr,temp.get(i),x); break; } } } else { ans+=store[x][1]; for(int i=0;i<temp.size();i++) { if(temp.get(i)!=parent) { finans(store,arr,temp.get(i),x); break; } } } } } public static int dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] store) { int index1=-1,index2=-1; for(int i=0;i<arr.get(x).size();i++) { if(arr.get(x).get(i)!=parent) { if(index1==-1) { index1=i; } else index2=i; } } if(index1==-1) { return 0; } if(index2==-1) { return store[arr.get(x).get(index1)]; } // System.out.println(x); // System.out.println();; return Math.max(store[arr.get(x).get(index1)]+dfs(arr,arr.get(x).get(index2),x,store),store[arr.get(x).get(index2)]+dfs(arr,arr.get(x).get(index1),x,store)); } static int delete=0; public static boolean bounds(int x,int y,int N,int M) { return x>=0 && x<N && y>=0 && y<M; } public static int gcd_check(ArrayList<Integer> temp,char[] ch, int[] arr) { ArrayList<Integer> ini=new ArrayList<>(temp); for(int i=0;i<temp.size();i++) { for(int j=0;j<temp.size();j++) { int req=temp.get(j); temp.set(j,arr[req-1]); } boolean status=true; for(int j=0;j<temp.size();j++) { if(ch[ini.get(j)-1]!=ch[temp.get(j)-1]) status=false; } if(status) return i+1; } return temp.size(); } static long LcmOfArray(int[] arr, int idx) { // lcm(a,b) = (a*b/gcd(a,b)) if (idx == arr.length - 1){ return arr[idx]; } int a = arr[idx]; long b = LcmOfArray(arr, idx+1); return (a*b/gcd(a,b)); // } public static boolean check(ArrayList<Integer> arr,int sum) { for(int i=0;i<arr.size();i++) { for(int j=i+1;j<arr.size();j++) { for(int k=j+1;k<arr.size();k++) { if(arr.get(i)+arr.get(j)+arr.get(k)==sum) return true; } } } return false; } // Returns true if str1 is smaller than str2. static boolean isSmaller(String str1, String str2) { // Calculate lengths of both string int n1 = str1.length(), n2 = str2.length(); if (n1 < n2) return true; if (n2 < n1) return false; for (int i = 0; i < n1; i++) if (str1.charAt(i) < str2.charAt(i)) return true; else if (str1.charAt(i) > str2.charAt(i)) return false; return false; } public static int check(List<String> history) { int[][] arr=new int[history.size()][history.get(0).length()]; for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[0].length;j++) { arr[i][j]=history.get(i).charAt(j)-48; } } for(int i=0;i<arr.length;i++) Arrays.sort(arr[i]); int sum=0; for(int i=0;i<arr[0].length;i++) { int max=0; for(int j=0;j<arr.length;j++) max=Math.max(max,arr[j][i]); sum+=max; } return sum; } // Function for find difference of larger numbers static String findDiff(String str1, String str2) { // Before proceeding further, make sure str1 // is not smaller if (isSmaller(str1, str2)) { String t = str1; str1 = str2; str2 = t; } // Take an empty string for storing result String str = ""; // Calculate length of both string int n1 = str1.length(), n2 = str2.length(); // Reverse both of strings str1 = new StringBuilder(str1).reverse().toString(); str2 = new StringBuilder(str2).reverse().toString(); int carry = 0; // Run loop till small string length // and subtract digit of str1 to str2 for (int i = 0; i < n2; i++) { // Do school mathematics, compute difference of // current digits int sub = ((int)(str1.charAt(i) - '0') - (int)(str2.charAt(i) - '0') - carry); // If subtraction is less then zero // we add then we add 10 into sub and // take carry as 1 for calculating next step if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += (char)(sub + '0'); } // subtract remaining digits of larger number for (int i = n2; i < n1; i++) { int sub = ((int)(str1.charAt(i) - '0') - carry); // if the sub value is -ve, then make it // positive if (sub < 0) { sub = sub + 10; carry = 1; } else carry = 0; str += (char)(sub + '0'); } // reverse resultant string return new StringBuilder(str).reverse().toString(); } static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Returns factorial of n static int fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static void fill(int[][] dp,int[] arr,int N) { for(int i=1;i<=N;i++) dp[i][0]=arr[i]; for (int j = 1; j <= dp[0].length; j++) for (int i = 1; i + (1 << j) <= N; i++) dp[i][j] = Math.max(dp[i][j-1], dp[i + (1 << (j - 1))][j - 1]); } static int start=0; // static boolean status; public static void dfs(ArrayList<ArrayList<Integer>> arr,int[] A,int[] B,int x,int time,int parent,int K,int coins) { if(time>K) return; ans=Math.max(ans,coins); for(int i:arr.get(x)) { if(i!=parent) { dfs(arr,A,B,i,time+1,x,K,coins); dfs(arr,A,B,i,time+1+B[i],x,K,coins+A[i]); } } } public static boolean dfs_diameter(ArrayList<ArrayList<Integer>> arr,int x,int parent,int node1,int node2,int[] child) { boolean status=false; for(int i:arr.get(x)) { if(i!=parent) { boolean ch=dfs_diameter(arr,i,x,node1,node2,child); status= status || ch; if(!ch && x==node1) ans+=child[i]; child[x]+=child[i]; } } child[x]+=1; return status || (x==node2); } public static boolean check_avai(int x,int y,int sx,int sy,int di) { // if(x==4 && y==4) // System.out.println((Math.abs(x-sx)+Math.abs(y-sy))<=di); return (Math.abs(x-sx)+Math.abs(y-sy))<=di; } public static int lower_index(ArrayList<Integer> arr, int x) { // System.out.println(x+" MC"); int l=0,r=arr.size()-1; while(l<=r) { int mid=(l+r)/2; if(arr.get(mid)<x) l=mid+1; else { if(mid-1>=0 && arr.get(mid-1)>= x) r=mid-1; else return mid; } } return -1; } public static int higher_index(ArrayList<Integer> arr, int x) { int l=0,r=arr.size()-1; while(l<=r) { int mid=(l+r)/2; if(arr.get(mid)>x) r=mid-1; else { if(mid+1<arr.size() && arr.get(mid+1)<=x ) l=mid+1; else return mid; } } return -1; } static int time; static void bridgeUtil(int u, boolean visited[], int disc[], int low[], int parent[],ArrayList<ArrayList<Integer>> adj,HashMap<Integer,Integer> hp) { // Mark the current node as visited visited[u] = true; // Initialize discovery time and low value disc[u] = low[u] = ++time; // Go through all vertices adjacent to this Iterator<Integer> i = adj.get(u).iterator(); while (i.hasNext()) { int v = i.next(); // v is current adjacent of u // If v is not visited yet, then make it a child // of u in DFS tree and recur for it. // If v is not visited yet, then recur for it if (!visited[v]) { parent[v] = u; bridgeUtil(v, visited, disc, low, parent,adj,hp); // Check if the subtree rooted with v has a // connection to one of the ancestors of u low[u] = Math.min(low[u], low[v]); // If the lowest vertex reachable from subtree // under v is below u in DFS tree, then u-v is // a bridge if (low[v] < disc[u]) { hp.put(u,v); hp.put(v,u); } } // Update low value of u for parent function calls. else if (v != parent[u]) low[u] = Math.min(low[u], disc[v]); } } static int r; static int b; public static void traverse(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[][] dp,int[] level,int K) { if(parent!=-1) level[x]=level[parent]+1; dp[x][0] += 1; for (int i : arr.get(x)) { if(i!=parent) { traverse(arr,i,x,dp,level,K); for(int j=1;j<=K;j++) dp[x][j]+=dp[i][j-1]; } } } static long fin_ans; public static void calc_pairs(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[][] dp,int[] level,int K,int[][] total) { if(parent!=-1) { // fin_ans+=dp[parent][K-1]-dp[x][K-2]; total[x][1]+=1; for(int i=2;i<=K;i++) total[x][i]+=total[parent][i-1]-dp[x][i-2]; // dp[x][K]+=dp[parent][K-1]-dp[x][K-2]; } fin_ans+=total[x][K]; // System.out.println(fin_ans+" "+x); for(int i:arr.get(x)) { if(i!=parent) { calc_pairs(arr,i,x,dp,level,K,total); } } } static int[] Sub_String_sol(int n,int q,String s,int[][] queries) { ArrayList<ArrayList<Integer>> arr=new ArrayList<>(); for(int i=0;i<26;i++) arr.add(new ArrayList<>()); for(int i=0;i<n;i++) { arr.get(s.charAt(i)-97).add(i); } int[] ans=new int[q]; for(int i=0;i<q;i++) { int l=queries[i][0]-1; int r=queries[i][1]-1; int cnt=0; for(int j=0;j<26;j++) { int index1=lower_index(arr.get(j),l); int index2=higher_index(arr.get(j),r); if(index1!=-1 && index2!=-1) { int ele=index2-index1+1; cnt+=((ele)*(ele-1))/2+ele; } } ans[i]=cnt; } return ans; } static int KMPSearch(int[] pat, int[] txt) { 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 ((N - i) >= (M - j)) { if (pat[j] == txt[i]) { j++; i++; } if (j == M) { return i-j; } // mismatch after j matches else if (i < N && pat[j] != txt[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; } } return -1; } static void computeLPSArray(int[] 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[i] == pat[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++; } } } } public static long fill(ArrayList<Integer> trees,ArrayList<Integer> wells,long[] before_x) { int l=0,r=0; long mod=(long)1e9+7; long[] pref_dist=new long[before_x.length]; int[] cnt=new int[before_x.length]; while(l<trees.size() && r<wells.size()) { if(trees.get(l)<=wells.get(r)) { before_x[r]=(before_x[r]%mod+power(wells.get(r)-trees.get(l),2,mod)%mod)%mod; pref_dist[r]=(pref_dist[r]%mod+(wells.get(r)-trees.get(l))%mod)%mod; cnt[r]+=1; ++l; } else ++r; } for(int i=1;i<before_x.length;i++) { before_x[i]=(before_x[i]%mod+((before_x[i-1]%mod+(cnt[i-1]%mod*power(wells.get(i)-wells.get(i-1),2,mod)%mod)%mod)%mod+(2%mod*((wells.get(i)-wells.get(i-1))%mod*(pref_dist[i-1])%mod)%mod)%mod)%mod%mod); pref_dist[i]=(pref_dist[i]%mod+(pref_dist[i-1]%mod+(cnt[i-1]%mod*(long)(wells.get(i)-wells.get(i-1))%mod)%mod)%mod)%mod; cnt[i]+=cnt[i-1]; } // for(int i=0;i<before_x.length;i++) // System.out.print(before_x[i]+" "); // System.out.println(); long ans=0; for(int i=0;i<before_x.length;i++) ans=(ans%mod+ before_x[i]%mod)%mod; return ans; } public static long fill_after(ArrayList<Integer> trees,ArrayList<Integer> wells,long[] before_x) { int l=trees.size()-1,r=wells.size()-1; long mod=(long)1e9+7; long[] pref_dist=new long[before_x.length]; int[] cnt=new int[before_x.length]; while(l>=0 && r>=0) { if(trees.get(l)>=wells.get(r)) { before_x[r]=(before_x[r]%mod+power(wells.get(r)-trees.get(l),2,mod)%mod)%mod; pref_dist[r]=(pref_dist[r]%mod+(trees.get(l)-wells.get(r))%mod)%mod; cnt[r]+=1; --l; } else --r; } for(int i=before_x.length-2;i>=0;i--) { before_x[i]=(before_x[i]%mod+((before_x[i+1]%mod+(cnt[i+1]%mod*power(wells.get(i)-wells.get(i+1),2,mod)%mod)%mod)%mod+(2%mod*((wells.get(i+1)-wells.get(i))%mod*(pref_dist[i+1])%mod)%mod)%mod)%mod%mod); pref_dist[i]=(pref_dist[i]%mod+(pref_dist[i+1]%mod+(cnt[i+1]%mod*(long)(wells.get(i+1)-wells.get(i))%mod)%mod)%mod)%mod; cnt[i]+=cnt[i+1]; } // for(int i=0;i<before_x.length;i++) // System.out.print(before_x[i]+" "); // System.out.println(); long ans=0; for(int i=0;i<before_x.length;i++) ans=(ans%mod+ before_x[i]%mod)%mod; return ans; } public static ArrayList<ArrayList<Integer>> graph_form(int N) { ArrayList<ArrayList<Integer>> arr=new ArrayList<>(); for(int i=0;i<N;i++) arr.add(new ArrayList<>()); return arr; } public static boolean check(int[] A,int mid) { for(int i=0;i<A.length;i++) { if(A[i]!=A[i%mid]) return false; } return true; } public static boolean check(int[] A) { ArrayList<Integer> arr=new ArrayList<>(); for(int i=2;i*i<=A.length;i++) { if(A.length%i==0) { arr.add(i); arr.add(A.length/i); } } boolean status=false; for(int i:arr) { status =status || check(A,i); } return status; } public static void main(String[] args) throws IOException{ Reader.init(System.in); // BufferedWriter output = new BufferedWriter(new FileWriter("C:/Users/asus/Downloads/arre_hoja.txt")); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int T=Reader.nextInt(); for(int m=1;m<=T;m++) { int N=Reader.nextInt(); long[] x=new long[N]; long[] y=new long[N]; for(int i=0;i<N;i++) x[i]=Reader.nextLong(); for(int i=0;i<N;i++) y[i]=Reader.nextLong(); ArrayList<Long> positive=new ArrayList<>(); ArrayList<Long> negative=new ArrayList<>(); for(int i=0;i<N;i++) { if(y[i]-x[i]>=0) positive.add(y[i]-x[i]); else negative.add(y[i]-x[i]); } Collections.sort(positive,Collections.reverseOrder()); Collections.sort(negative); // System.out.println(positive); // System.out.println(negative); int grp=0, left=positive.size(); int l=0,r=0; while(l<positive.size() && r<negative.size()) { if(positive.get(l)>=Math.abs(negative.get(r))) { grp+=1; --left; ++l; ++r; } else ++r; } // for(int i=0;i<Math.min(positive.size(),negative.size());i++) // { // if(positive.get(i)>=Math.abs(negative.get(i))) // { // grp+=1; // left-=1; // } // else // { //// left=i; // break; // } // } // System.out.println(left); output.write(grp+left/2+"\n"); // if(positive.size()%2==0) // { // output.write(positive.size()/2+"\n"); // } // else // { // if(negative.size()>0 && positive.get(positive.size()-1)>=Math.abs(negative.get(negative.size()-1))) // output.write(positive.size()/2+1+"\n"); // else // output.write(positive.size()/2+"\n"); // } } // output.close(); output.flush(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { left=null; right=null; this.data=data; } } class div { int x; int y; div(int x,int y) { this.x=x; this.y=y; // this.coins=coins; } } class kar implements Comparator<div> { public int compare(div o1,div o2) { if (o1.x < o2.x) return -1; else if (o1.x > o2.x) return 1; else { if (o1.y < o2.y) return -1; else if (o1.y > o2.y) return 1; else return 0; } } } class trie_node { trie_node[] arr; trie_node() { arr=new trie_node[26]; } public static void insert(trie_node root,String s) { trie_node tmp=root; for(int i=0;i<s.length();i++) { if(tmp.arr[s.charAt(i)-97]!=null) { tmp=tmp.arr[s.charAt(i)-97]; } else { tmp.arr[s.charAt(i)-97]=new trie_node(); tmp=tmp.arr[s.charAt(i)-97]; } } } public static boolean search(trie_node root,String s) { trie_node tmp=root; for(int i=0;i<s.length();i++) { if(tmp.arr[s.charAt(i)-97]!=null) { tmp=tmp.arr[s.charAt(i)-97]; } else { return false; } } return true; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
6717195ad2e26e5853dd607d6d87caf8
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; O : while(t-->0) { int n = sc.nextInt(); int[] tmp = sc.readArray(n); ArrayList<Integer> arr = new ArrayList<>(n); for (int i = 0; i < n; i++) arr.add(tmp[i] - sc.nextInt()); Collections.sort(arr); int ans = 0; ArrayDeque<Integer> q = new ArrayDeque<>(arr); while (q.size() >= 2) { if (q.peekFirst() + q.peekLast() <= 0) { q.pollFirst(); q.pollLast(); ans++; } else { q.pollLast(); } } System.out.println(ans); } out.flush(); } static void printN() { System.out.println("NO"); } static void printY() { System.out.println("YES"); } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int nums[]) { int i1=0,i2=nums.length-1; while(i1<i2){ while(nums[i1]%2==0 && i1<i2){ i1++; } while(nums[i2]%2!=0 && i2>i1){ i2--; } int temp=nums[i1]; nums[i1]=nums[i2]; nums[i2]=temp; } return nums; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for(int i=0;i<n;i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length-1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static class pair{ int i,j; pair(int x,int y){ i=x; j=y; } } static int binary_search(int a[],int value) { int start=0; int end=a.length-1; int mid=start+(end-start)/2; while(start<=end) { if(a[mid]==value) { return mid; } if(a[mid]>value) { end=mid-1; } else { start=mid+1; } mid=start+(end-start)/2; } return -1; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
5dba721918dce4adac3c280a1247fe4a
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
/*package whatever //do not write package name here */ import java.util.*; import java.util.concurrent.LinkedTransferQueue; import javax.swing.plaf.synth.SynthPasswordFieldUI; import java.lang.*; import java.lang.reflect.Array; import java.security.cert.CollectionCertStoreParameters; import java.io.*; public class codeforces { static Scanner sc=new Scanner(System.in); static void solve(){ int n=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int diff[]=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); }for(int i=0;i<n;i++){ b[i]=sc.nextInt(); diff[i]=b[i]-a[i]; } Arrays.sort(diff); int count=0; int Rich_pointer=n-1; int Poor_pointer=0; while(Rich_pointer>Poor_pointer){ if(diff[Poor_pointer]+diff[Rich_pointer]>=0){ Rich_pointer--; count++; } Poor_pointer++; } System.out.println(count); } public static void main(String[] args) { int t=sc.nextInt(); while(t-->0){ solve(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9918c08eb119dd5cfe8207149b7079fe
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; public class Main { static final long MOD1=1000000007; static final long MOD=998244353; static final int NTT_MOD1 = 998244353; static final int NTT_MOD2 = 1053818881; static final int NTT_MOD3 = 1004535809; static long MAX = 1000000000000000000l;//10^18 static int index = 2; public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); long[] x = sc.nextLongArray(n); long[] y = sc.nextLongArray(n); out.println(solve(n, x, y)); } out.flush(); } static int solve(int n, long[] x, long[] y) { long[] z = new long[n]; Arrays.setAll(z, k -> y[k] - x[k]); Arrays.sort(z); int from = 0; int to = n/2 + 1; while ((to-from)>=1) { int mid = (to+from)/2; if (f(n, z, mid)) to = mid; else from = mid + 1; } return to - 1; } static boolean f(int n, long[] z, int mid) { int offset = n - 2*mid; if(offset < 0) return false; for (int i = 0; i < mid; i++) { if (z[offset + i] + z[n - i - 1] < 0) { return true; } } return false; } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } } //StringAlgorithm.methodで使う class StringAlgorithm { private static int[] saNaive(int[] s) { int n = s.length; Integer[] _sa = new Integer[n]; for (int i = 0; i < n; i++) { _sa[i] = i; } java.util.Arrays.sort(_sa, (l, r) -> { while (l < n && r < n) { if (s[l] != s[r]) return s[l] - s[r]; l++; r++; } return -(l - r); }); int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = _sa[i]; } return sa; } private static int[] saDoubling(int[] s) { int n = s.length; Integer[] _sa = new Integer[n]; for (int i = 0; i < n; i++) { _sa[i] = i; } int[] rnk = s; int[] tmp = new int[n]; for (int k = 1; k < n; k *= 2) { final int _k = k; final int[] _rnk = rnk; java.util.Comparator<Integer> cmp = (x, y) -> { if (_rnk[x] != _rnk[y]) return _rnk[x] - _rnk[y]; int rx = x + _k < n ? _rnk[x + _k] : -1; int ry = y + _k < n ? _rnk[y + _k] : -1; return rx - ry; }; java.util.Arrays.sort(_sa, cmp); tmp[_sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[_sa[i]] = tmp[_sa[i - 1]] + (cmp.compare(_sa[i - 1], _sa[i]) < 0 ? 1 : 0); } int[] buf = tmp; tmp = rnk; rnk = buf; } int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = _sa[i]; } return sa; } private static final int THRESHOLD_NAIVE = 10; private static final int THRESHOLD_DOUBLING = 40; private static int[] sais(int[] s, int upper) { int n = s.length; if (n == 0) return new int[0]; if (n == 1) return new int[]{0}; if (n == 2) { if (s[0] < s[1]) { return new int[]{0, 1}; } else { return new int[]{1, 0}; } } if (n < THRESHOLD_NAIVE) { return saNaive(s); } if (n < THRESHOLD_DOUBLING) { return saDoubling(s); } int[] sa = new int[n]; boolean[] ls = new boolean[n]; for (int i = n - 2; i >= 0; i--) { ls[i] = s[i] == s[i + 1] ? ls[i + 1] : s[i] < s[i + 1]; } int[] sumL = new int[upper + 1]; int[] sumS = new int[upper + 1]; for (int i = 0; i < n; i++) { if (ls[i]) { sumL[s[i] + 1]++; } else { sumS[s[i]]++; } } for (int i = 0; i <= upper; i++) { sumS[i] += sumL[i]; if (i < upper) sumL[i + 1] += sumS[i]; } java.util.function.Consumer<int[]> induce = lms -> { java.util.Arrays.fill(sa, -1); int[] buf = new int[upper + 1]; System.arraycopy(sumS, 0, buf, 0, upper + 1); for (int d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } System.arraycopy(sumL, 0, buf, 0, upper + 1); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } System.arraycopy(sumL, 0, buf, 0, upper + 1); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; int[] lmsMap = new int[n + 1]; java.util.Arrays.fill(lmsMap, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lmsMap[i] = m++; } } int[] lms = new int[m]; { int p = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms[p++] = i; } } } induce.accept(lms); if (m > 0) { int[] sortedLms = new int[m]; { int p = 0; for (int v : sa) { if (lmsMap[v] != -1) { sortedLms[p++] = v; } } } int[] recS = new int[m]; int recUpper = 0; recS[lmsMap[sortedLms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sortedLms[i - 1], r = sortedLms[i]; int endL = (lmsMap[l] + 1 < m) ? lms[lmsMap[l] + 1] : n; int endR = (lmsMap[r] + 1 < m) ? lms[lmsMap[r] + 1] : n; boolean same = true; if (endL - l != endR - r) { same = false; } else { while (l < endL && s[l] == s[r]) { l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) { recUpper++; } recS[lmsMap[sortedLms[i]]] = recUpper; } int[] recSA = sais(recS, recUpper); for (int i = 0; i < m; i++) { sortedLms[i] = lms[recSA[i]]; } induce.accept(sortedLms); } return sa; } public static int[] suffixArray(int[] s, int upper) { assert (0 <= upper); for (int d : s) { assert (0 <= d && d <= upper); } return sais(s, upper); } public static int[] suffixArray(int[] s) { int n = s.length; Integer[] idx = new Integer[n]; for (int i = 0; i < n; i++) { idx[i] = i; } java.util.Arrays.sort(idx, (l, r) -> s[l] - s[r]); int[] s2 = new int[n]; int now = 0; for (int i = 0; i < n; i++) { if (i > 0 && s[idx[i - 1]] != s[idx[i]]) { now++; } s2[idx[i]] = now; } return sais(s2, now); } public static int[] suffixArray(char[] s) { int n = s.length; int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = s[i]; } return sais(s2, 255); } public static int[] suffixArray(java.lang.String s) { return suffixArray(s.toCharArray()); } public static int[] lcpArray(int[] s, int[] sa) { int n = s.length; assert (n >= 1); int[] rnk = new int[n]; for (int i = 0; i < n; i++) { rnk[sa[i]] = i; } int[] lcp = new int[n - 1]; int h = 0; for (int i = 0; i < n; i++) { if (h > 0) h--; if (rnk[i] == 0) { continue; } int j = sa[rnk[i] - 1]; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rnk[i] - 1] = h; } return lcp; } public static int[] lcpArray(char[] s, int[] sa) { int n = s.length; int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = s[i]; } return lcpArray(s2, sa); } public static int[] lcpArray(java.lang.String s, int[] sa) { return lcpArray(s.toCharArray(), sa); } public static int[] zAlgorithm(int[] s) { int n = s.length; if (n == 0) return new int[0]; int[] z = new int[n]; for (int i = 1, j = 0; i < n; i++) { int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; z[i] = k; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } public static int[] zAlgorithm(char[] s) { int n = s.length; if (n == 0) return new int[0]; int[] z = new int[n]; for (int i = 1, j = 0; i < n; i++) { int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; z[i] = k; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } public static int[] zAlgorithm(String s) { return zAlgorithm(s.toCharArray()); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
ee5f399c4ea41f4705a48fecbc5223d7
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class test { static void solve(int n, long[] x, long[] y) { long d = 0; long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = x[i] - y[i]; } Arrays.sort(a); int l = 0; int r = n - 1; while (l < r) { if (a[l] + a[r] > 0) { r--; } else { d++; l++; r--; } } System.out.println(d); } //[-2,-2,0,0,1,1] public static void main(String[] args) { Scanner w = new Scanner(System.in); int t = w.nextInt(); for (int i = 0; i < t; i++) { int n = w.nextInt(); long[] x = new long[n]; long[] y = new long[n]; for (int j = 0; j < n; j++) { x[j] = w.nextLong(); } for (int j = 0; j < n; j++) { y[j] = w.nextLong(); } solve(n, x, y); } } //------------------// static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static int lcm(int a, int b) { return (a * b) / gcd(a, b); } static void increment(ArrayList<Long> a) { for (int i = 0; i < a.size(); i++) { long val = a.get(i); val++; a.set(i, val); } } static void changeStatus(String a) { for (int i = 0; i < a.length(); i++) { if (a.charAt(i) == '0') { a.replace(a.charAt(i), '1'); } else { a.replace(a.charAt(i), '0'); } } } static long factorial(long n) { long res = 1; for (long i = 2; i <= n; i++) { res *= i; } return res; } static long cGeneral(long n, long r) { return factorial(n)/(factorial(r) * factorial(n-r)); } static String reverse(String s) { StringBuilder input1 = new StringBuilder(); input1.append(s); input1.reverse(); return input1.toString(); } static void reverseArray(String[] a) { for (int i = 0; i < a.length; i++) { a[i] = reverse(a[i]); } } static boolean equalsArray(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } return true; } static long sum(ArrayList<Long> a) { long sum = 0; for (int i = 0; i < a.size(); i++) { sum += a.get(i); } return sum; } static long sum(long[] a) { long sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long max(ArrayList<Long> a) { long max = Long.MIN_VALUE; if (a.size() == 0) { max = 0; } for (int i = 0; i < a.size(); i++) { if (a.get(i) >= max) { max = a.get(i); } } return max; } static long min(ArrayList<Long> a) { long min = Long.MAX_VALUE; if (a.size() == 0) { min = 0; } for (int i = 0; i < a.size(); i++) { if (a.get(i) <= min) { min = a.get(i); } } return min; } static long max(long[] a) { long max = Long.MIN_VALUE; if (a.length == 0) { max = 0; } for (int i = 0; i < a.length; i++) { if (a[i] >= max) { max = a[i]; } } return max; } static long min(long[] a) { long min = Long.MAX_VALUE; if (a.length == 0) { min = 0; } for (int i = 0; i < a.length; i++) { if (a[i] <= min) { min = a[i]; } } return min; } static boolean isPrime(long num) { if(num<=1) { return false; } for(long i=2;i<=num/2;i++) { if((num%i)==0) return false; } return true; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
f6621d59702073d14de7c590a984f9a6
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class D { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tests = Integer.parseInt(sc.next()); for (int t = 0; t < tests; t++) { int n = sc.nextInt(); int[] x = new int[n]; int[] y = new int[n]; int[] diff = new int[n]; for(int i=0; i< n; i++) { x[i] = sc.nextInt(); } for(int i=0; i< n; i++) { y[i] = sc.nextInt(); diff[i] = y[i]-x[i]; } Arrays.sort(diff); int l=0; int r = n-1; int result = 0; while (l<r) { if (diff[r]+diff[l] >=0) { result++; r--; l++; } else { l++; } } pw.println(result); } sc.close(); pw.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
6e68dc1225ac9711c02623fb18da0dc5
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; import java.lang.reflect.Array; public class Main { 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 (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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args) { try { FastReader in = new FastReader(); FastWriter out = new FastWriter(); int testCases = in.nextInt(); while (testCases-- > 0) { int n = in.nextInt(); // String[] str = new String[2]; // str = in.nextLine().split(" "); // int n = Integer.parseInt(str[0]),k = Integer.parseInt(str[1]); String[] s1 = new String[n]; s1 = in.nextLine().split(" "); int[] a=new int[n]; String[] s2 = new String[n]; s2 = in.nextLine().split(" "); int[] y=new int[n]; //double[] a=new double[n]; //HashMap<Integer,Integer> hs=new HashMap<>(); //String s1=in.nextLine(); //HashSet<Integer> hs=new HashSet<>(); int[] diff=new int[n]; ; for(int i = 0; i <n; i++){ a[i]=Integer.parseInt(s1[i]); y[i]=Integer.parseInt(s2[i]); diff[i]=y[i]-a[i]; // hs.put(a[i],hs.getOrDefault(a[i], 0)+1); } //long m=1000000007; //ArrayList<Integer> res=new ArrayList<>(); Arrays.sort(diff); int i=0,j=n-1,res=0; while(i<j){ if((diff[i]+diff[j])<0) i++; else{ res++; i++; j--; } // out.println(i+" "+j+" "+res+" "+(diff[i]+diff[j])); } out.println(res); } out.close(); } catch (Exception e) { e.printStackTrace(); return; } } // public static boolean f(int[] a,int i,int prev,boolean front,int[][][] dp){ // if(i>=a.length && front) return true; // else if(i>=a.length && !front) return false; // int c=0; // if(front) c=1; // if(dp[i+1][prev+1][c]!=0) return (dp[i+1][prev+1][c]==1); // boolean c1=false,c2=false; // if(!front && a[i]==(i-prev-1)) c1=f(a, i+1,i,true,dp); // if(front && (i+a[i])<a.length) c2=f(a,i+a[i]+1,i+a[i],true,dp); // if((c1||c2||f(a,i+1,prev,false,dp))) dp[i+1][prev+1][c]=1; // else dp[i+1][prev+1][c]=-1; // return (dp[i+1][prev+1][c]==1); // } static int lcm(int a, int b) { return (a * b) /gcd(a, b); } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } public static class Pair implements Comparable<Pair> { int val, t; public Pair(int t, int val) { this.val = val; this.t = t; } public int compareTo(Pair o) { if (o.t == this.t) return o.val - this.val; // decending order else return this.t-o.t; // ascending order } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
d66201850e38acc40a0ebe4325ab23c2
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] x = new int[n]; for (int i = 0; i < x.length; ++i) { x[i] = sc.nextInt(); } int[] y = new int[n]; for (int i = 0; i < y.length; ++i) { y[i] = sc.nextInt(); } System.out.println(solve(x, y)); } sc.close(); } static int solve(int[] x, int[] y) { int result = 0; int[] diffs = IntStream.range(0, x.length).map(i -> y[i] - x[i]).sorted().toArray(); int leftIndex = 0; int rightIndex = x.length - 1; while (true) { while (leftIndex < rightIndex && diffs[leftIndex] + diffs[rightIndex] < 0) { ++leftIndex; } if (leftIndex >= rightIndex) { break; } ++result; ++leftIndex; --rightIndex; } return result; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
a0a89276aef72d2250ec120f20205c33
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
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 Main { public static void main (String[] args) throws java.lang.Exception { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); for(int w=1;w<=t;w++) { int n = scn.nextInt(); int [] arr1 = new int[n]; for(int a=0;a<n;a++) {arr1[a] = scn.nextInt();} int [] arr2 = new int[n]; for(int a=0;a<n;a++) {arr2[a] = scn.nextInt();} long [] arr = new long[n]; for(int a=0;a<n;a++) { arr[a] = (long)arr2[a]-(long)arr1[a]; } Arrays.sort(arr); int lo=0; int hi = n-1; long ans=0; while(lo<hi) { long e = arr[hi]; while(hi>lo && arr[hi]+arr[lo]<0) { lo++; } if(lo<hi && arr[lo]+arr[hi]>=0) { lo++; hi--; ans++; } else break; } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
3c62d63788340b570cf8799c248b355d
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class ProblemD { static ArrayList<Integer>res ; public static void main(String[] args) throws Exception { Scanner in = new Scanner(); StringBuilder sb = new StringBuilder(); int test = in.readInt(); Comparator<Pair>cmp = new Comparator<Pair>(){ @Override public int compare(Pair a, Pair b) { return (a.x-a.y) - (b.x - b.y); } }; while(test-->0){ int n = in.readInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i = 0;i<n;i++)a[i] = in.readInt(); for(int i = 0;i<n;i++)b[i] = in.readInt(); ArrayList<Pair>l = new ArrayList<>(); for(int i =0;i<n;i++)l.add(new Pair(a[i],b[i])); Collections.sort(l,cmp); int ind = n-1,count=0; boolean vis[] = new boolean[n]; for(int i=0;i<n;i++){ if(vis[i])continue; while(i < ind && ind>=0 && l.get(ind).y-l.get(ind).x<l.get(i).x-l.get(i).y)ind--; if(i<ind && ind>=0 && l.get(i).x-l.get(i).y<= l.get(ind).y-l.get(ind).x){ vis[ind] =true; //System.out.println(i+" "+ind); ind--; count++; } } sb.append(count+"\n"); } System.out.println(sb); } public static int min(int a,int b,int c){ int first = a-c-1; if(first<0)first+=c; System.out.println(Math.abs(b-a-1)+" "+" "+a+" "+b+" "+(c-1)); return Math.min(Math.min(Math.abs(b-a-1), Math.abs(c-b-1)),a-1); } public static long go(int flag, LinkedList<Integer>ll[]){ int t = flag^1; LinkedList<Integer>l[] = new LinkedList[2]; for(int i = 0;i<2;i++){ l[i] = new LinkedList<>(); for(int it:ll[i])l[i].add(it); } long ans = l[flag].size()>0?l[flag].remove():-1; flag^=1; for(int i = 0;;i++){ if(l[flag].size() == 0 || ans == -1)break; if(l[flag].size()>0)ans+=2*l[flag].removeLast(); flag^=1; } while(l[0].size()>0)ans+=l[0].remove(); while(l[1].size()>0)ans+=l[1].remove(); return ans; } public static boolean vis(boolean vis[][]){ boolean ok = true; int n = vis.length,m = vis[0].length; for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ ok&=vis[i][j]; } } return ok; } public static int begin(ArrayList<Pair>list,int l,int r,long target){ int index = -1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid).y - list.get(mid).x >= target){ index = mid; l = mid+1; }else { r = mid-1; } } return index; } public static void build(int seg[],int ind,int l,int r,int a[]){ if(l == r){ seg[ind] = a[l]; return; } int mid = (l+r)/2; build(seg,(2*ind)+1,l,mid,a); build(seg,(2*ind)+2,mid+1,r,a); seg[ind] = Math.min(seg[(2*ind)+1],seg[(2*ind)+2]); } public static long gcd(long a, long b){ return b ==0 ?a:gcd(b,a%b); } public static long nearest(TreeSet<Long> list,long target){ long nearest = -1,sofar = Integer.MAX_VALUE; for(long it:list){ if(Math.abs(it - target)<sofar){ sofar = Math.abs(it - target); nearest = it; } } return nearest; } public static ArrayList<Long> factors(long n){ ArrayList<Long>l = new ArrayList<>(); for(long i = 1;i*i<=n;i++){ if(n%i == 0){ l.add(i); if(n/i != i)l.add(n/i); } } Collections.sort(l); return l; } public static void swap(int a[],int i, int j){ int t = a[i]; a[i] = a[j]; a[j] = t; } public static boolean isSorted(long a[]){ for(int i =0;i<a.length;i++){ if(i+1<a.length && a[i]>a[i+1])return false; } return true; } public static int first(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index = mid; r = mid-1; }else if(list.get(mid)>target){ r = mid-1; }else l = mid+1; } return index; } public static int last(ArrayList<Long>list,long target){ int index = -1; int l = 0,r = list.size()-1; while(l<=r){ int mid = (l+r)/2; if(list.get(mid) == target){ index= mid; l = mid+1; }else if(list.get(mid)<target){ l = mid+1; }else r = mid-1; } return index; } public static void sort(int arr[]){ ArrayList<Integer>list = new ArrayList<>(); for(int it:arr)list.add(it); Collections.sort(list); for(int i = 0;i<arr.length;i++)arr[i] = list.get(i); } static class Pair{ int x,y; public Pair(int x,int y){ this.x = x; this.y = y; } } static class Scanner{ BufferedReader br; StringTokenizer st; public Scanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String read() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public double readDouble(){return Double.parseDouble(read());} public String readLine() throws Exception { return br.readLine(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
01629df91db7d35f8179ca78d2c2dc78
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
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) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int arr[]=new int[n]; int arr1[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); for(int i=0;i<n;i++) arr1[i]=sc.nextInt(); for(int i=0;i<n;i++) { arr1[i]-=arr[i]; } Arrays.sort(arr1); int i=0,j=n-1,sum=0; while(i<j) { if(arr1[i]+arr1[j]>=0) { sum++; j--; i++; } else i++; } System.out.println(sum); } // your code goes here } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
70c724f593d2af2c9ca2b9bd30c7b269
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class MergeSort { static int[][] moveInEight = {{1, 1}, {1, 0}, {1, -1}, {0, 1}, {-1, 1}, {0, -1}, {-1, -1}, {-1, 0}}; static int[][] moveInFour = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static void main(String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while (test-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; int[] res = new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=0;i<n;i++){ int ele = sc.nextInt(); res[i]=ele-a[i]; } Arrays.sort(res); int i=0,j=n-1,ans=0; while (j>i){ if(res[i]+res[j]>=0) { ans++;i++;j--; } else i++; } System.out.println(ans); } } static class Group implements Comparable<Group> { int x; int y; Group(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Group o) { return x - o.x; } } //primes private static ArrayList<Integer> primeFactUsingSieve(int n) { int[] a = new int[n + 1]; Arrays.fill(a, 0); ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (a[i] != 0) continue; for (int j = 1; i * j <= n; j++) { if (a[j * i] == 0) a[j * i] = i; } } int x = n; while (x > 1) { arrayList.add(a[x]); x = x / a[x]; } return arrayList; } private static List<Integer> getAllPrimes(int n){ int[] primeNum = new int[n+1]; Arrays.fill(primeNum,0); for(int i=2;i*i<n;i++){ if(primeNum[i]==1) continue; for(int j=2;i*j<=n;j++) primeNum[i * j] = 1; } ArrayList<Integer> list = new ArrayList<>(); for (int i=2;i<n;i++){ if(primeNum[i]==0) list.add(i); } return list; } private static boolean primalityTest(int n){ if(n==1) return false; for(int i=2;i*i<n;i++){ if(n%i==0) return false; } return true; } //////////Helper Functions//////////////////////////////////// private static char getCharFromASCII(int c) { return (char) (c + 96); } private static boolean isValidPos(long x, long y, long i, long j) { return i < x && i >= 0 && j >= 0 && j < y; } private static int getFactorial(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } private static boolean isPalindrome(char[] s) { int start = 0, last = s.length - 1; while (start < last) { if (s[start] != s[last]) return false; start++; last--; } return true; } private static void swap(int i, int j, long[] arr) { long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private static void swap(int i, int j) { int temp = i; i = j; j = temp; } private static boolean isEven(int a) { return a % 2 == 0; } static int findGCD(int x, int y) { int r = 0, a, b; a = Math.max(x, y); b = Math.min(x, y); r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } static int findLcm(int a, int b) { return (a * b) / findGCD(a, b); } private static void swap(int i, int j, char[] arr) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static Pair asFraction(long a, long b) { long gcd = gcd(a, b); return new Pair(a / gcd, b / gcd); } static class Pair { long x, y, z; Pair(int x, int y) { this.x = (int) x; this.y = (int) y; } Pair(long x, long y) { this.x = x; this.y = y; } Pair(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
2cd82210faccd3301d9cf85da488e498
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
//package prog_temps; import java.util.*; import java.io.*; public class Java_Template { static boolean[] primecheck = new boolean[1000002]; static int M = 1000000007; static int mn = Integer.MIN_VALUE; static int mx = Integer.MAX_VALUE; static int vis[]; static ArrayList<ArrayList<Integer>> list; public static char rev(char c){ int diff = c-'A'; char ans = (char)('Z' - diff); return ans; } public static void swap(int a[], int i,int j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static int getFirstSetBitPos(int n) { return (int)((Math.log10(n & -n)) / Math.log10(2)); } public static void sortbyColumn(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter w = new PrintWriter(System.out); int t = 1; t = sc.nextInt(); while (t-- > 0) { solve(sc, w); } w.close(); } public static void solve(FastReader sc, PrintWriter w) throws Exception { //1729D int n =sc.nextInt(); int X[] = new int[n]; int Y[] = new int[n]; int d[] = new int[n]; sc.readArr(X,n); sc.readArr(Y,n); for (int i = 0; i < n; i++) { d[i] = Y[i] - X[i]; } Arrays.sort(d); int i,j; i = 0; j = n-1; int count=0; while (i<j){ if(d[i]+d[j]>=0){ count++; j--; } i++; } w.println(count); } public static void debug(String X , String x){ System.out.println(X+ " : "+x); } public static void debug(String X , int x){ System.out.println(X+ " : "+x); } public static void debug(String X , long x){ System.out.println(X+ " : "+x); } public static void debug(String X , double x){ System.out.println(X+ " : "+x); } public static void debug(String X , boolean x){ System.out.println(X+ " : "+x); } public static void debug(String X , int[] x){ System.out.print(X+ " : "); for (int ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void debug(String X , char[] x){ System.out.print(X+ " : "); for (char ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void debug(String X , long[] x){ System.out.print(X+ " : "); for (long ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void debug(String X , boolean[] x){ System.out.print(X+ " : "); for (boolean ele : x) { System.out.print(ele+" "); } System.out.println(); } public static void merge( int[] a, int[] l, int[] r, int left, int right) { int i = 0, j = 0, k = 0; while (i < left && j < right) { if (l[i] <= r[j]) { a[k++] = l[i++]; } else { a[k++] = r[j++]; } } while (i < left) { a[k++] = l[i++]; } while (j < right) { a[k++] = r[j++]; } } public static void mergeSort(int[] a, int n) { if (n < 2) { return; } int mid = n / 2; int[] l = new int[mid]; int[] r = new int[n - mid]; for (int i = 0; i < mid; i++) { l[i] = a[i]; } for (int i = mid; i < n; i++) { r[i - mid] = a[i]; } mergeSort(l, mid); mergeSort(r, n - mid); merge(a, l, r, mid, n - mid); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void readArr(int[] ar, int n) { for (int i = 0; i < n; i++) { ar[i] = nextInt(); } } } public static boolean perfectSqr(long a) { long sqrt = (long) Math.sqrt(a); if (sqrt * sqrt == a) { return true; } return false; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = false; } } } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
84c5eab97ccab9f2a5c50b96f8ca3132
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0) { int n = scn.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[n]; int[] diff = new int[n]; for(int i=0;i<arr1.length;i++) { arr1[i]=scn.nextInt(); } for(int i=0;i<arr2.length;i++) { arr2[i]=scn.nextInt(); } for(int i=0;i<diff.length;i++) { diff[i]=arr2[i]-arr1[i]; } Arrays.sort(diff); int i=0; int j=diff.length-1; int count=0; while(i<j) { if(diff[i]>=0 && diff[j]>=0) { count+=(j-i+1)/2; break; } if(diff[i]+diff[j]>=0) { count++; i++; j--; }else { i++; } } System.out.println(count); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
7633d903e9dbb890b82d45a698eb264c
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class ProblemA { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr1 = new int[n]; for(int i=0;i<n;i++){ arr1[i] = sc.nextInt(); } int[] arr2 = new int[n]; for(int i=0;i<n;i++){ arr2[i] = sc.nextInt(); } int[] diff = new int[n]; for(int i=0;i<n;i++){ diff[i] = arr2[i]-arr1[i]; } Arrays.sort(diff); int i = 0; int j = diff.length-1; int ans = 0; while(i < j){ int val1 = diff[i]; int val2 = diff[j]; if(val1+val2 < 0){ i++; }else{ ans++; i++; j--; } } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9a736d6fd487a3185910bff074e37eca
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; /** * @author atulanand */ public class Solution { static class Result { public int solve(int[] x, int[] y) { List<Integer> ls = new ArrayList<>(); for (int i = 0; i < x.length; i++) { ls.add(y[i] - x[i]); } Collections.sort(ls); int ans = 0; int j = ls.size() - 1; int i = 0; while (i < j) { while (i < j && ls.get(j) + ls.get(i) < 0) { i++; } if (i < j) { ans++; } i++; j--; } return ans; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = inputInt(br); Result result = new Result(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int[] spec = inputIntArray(br); sb.append(result.solve(inputIntArray(br), inputIntArray(br))).append("\n"); } System.out.println(sb); } public static char[] inputCharArrayW(BufferedReader br) throws IOException { String[] spec = inputString(br).split(""); char[] arr = new char[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = spec[i].toCharArray()[0]; return arr; } public static char[][] inputCharGrid(BufferedReader br, int rows) throws IOException { char[][] grid = new char[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputString(br).toCharArray(); } return grid; } public static int[][] inputIntGrid(BufferedReader br, int rows) throws IOException { int[][] grid = new int[rows][]; for (int i = 0; i < grid.length; i++) { grid[i] = inputIntArray(br); } return grid; } public static char[] inputCharArray(BufferedReader br) throws IOException { return inputString(br).toCharArray(); } public static String inputString(BufferedReader br) throws IOException { return br.readLine().trim(); } public static int inputInt(BufferedReader br) throws IOException { return Integer.parseInt(inputString(br)); } public static long inputLong(BufferedReader br) throws IOException { return Long.parseLong(inputString(br)); } public static int[] inputIntArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); int[] arr = new int[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Integer.parseInt(spec[i]); return arr; } public static int[] inputIntArrayW(BufferedReader br) throws IOException { String[] spec = inputString(br).split(""); int[] arr = new int[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Integer.parseInt(spec[i]); return arr; } public static long[] inputLongArray(BufferedReader br) throws IOException { String[] spec = inputString(br).split(" "); long[] arr = new long[spec.length]; for (int i = 0; i < spec.length; i++) arr[i] = Long.parseLong(spec[i]); return arr; } private String stringify(char[] chs) { StringBuilder sb = new StringBuilder(); for (char ch : chs) { sb.append(ch); } return sb.toString(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
aaa305e0f876801b477b06b8a50d7950
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.lang.*; // import java.math.*; import java.io.*; import java.util.*; public class Main{ public static int mod=(int)(1e9) + 7; public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter ot=new PrintWriter(System.out); public static int[] take_arr(int n){ int a[]=new int[n]; try { String s[]=br.readLine().trim().split(" "); for(int i=0;i<n;i++) a[i]=Integer.parseInt(s[i]); } catch (Exception e) { e.printStackTrace(); } return a; } public static List<Integer> take_list(int n){ List<Integer> a=new ArrayList<>(); try { String s[]=br.readLine().trim().split(" "); for(int i=0;i<n;i++) a.add(Integer.parseInt(s[i])); } catch (Exception e) { e.printStackTrace(); } return a; } public static void main (String[] args) throws java.lang.Exception { try{ int t=Integer.parseInt(br.readLine().trim()); // pre(); // br.readLine(); int cno=1; while(t-->0){ String s[]=br.readLine().trim().split(" "); int n=Integer.parseInt(s[0]); // int m=Integer.parseInt(s[1]); // long n=Long.parseLong(s[0]); // long m=Long.parseLong(s[1]); int a[]=take_arr(n); int b[]=take_arr(n); // char ch[]=br.readLine().trim().toCharArray(); solve(a.length, a, b); } ot.close(); br.close(); }catch(Exception e){ e.printStackTrace(); return; } } static void solve(int n, int a[], int b[]){ TreeMap<Integer, Integer> tm = new TreeMap<>(); for(int i=0;i<n;i++){ int t = b[i]-a[i]; if(t<0) tm.put(-t, tm.getOrDefault(-t, 0)+1); } int ans = 0; int count = 0; // ot.println(tm); for(int i=0;i<n;i++){ int t = b[i]-a[i]; if(t>-1){ Integer temp = tm.floorKey(t); if(temp!=null){ ans++; tm.put(temp, tm.get(temp)-1); if(tm.get(temp)==0) tm.remove(temp); // ot.println(i+" "+temp); } else{ count++; // ot.println(i+" "+temp); } } } ans+=(count/2); ot.println(ans); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
4182706b6eb24376e25d78e032d4e0d0
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class cp { public static void main(String[] args) { int t = inp(); while(t-->0){ int n = inp(); int[] b = inp(n), c = inp(n); var tmp = new int[n]; int l=0; for(int i=0;i<n;++i){ tmp[i]=c[i]-b[i];if(tmp[i]<0)++l; } sort(tmp); int x=0, y=n-1,cnt=0; while(x<l){ if(Math.abs(tmp[x])<=tmp[y]){ ++x;--y;++cnt; }else ++x; } if(x!=y){ int rem = y-x+1; if(rem%2==0)cnt+=rem/2; else if(rem>=3){ rem-=3; ++cnt; cnt+=rem/2; } } out(cnt); } } public static void swap(int[] a, int p, int i){ var tmp=a[p]; a[p]=a[i]; a[i]=tmp; } public static ArrayList<Integer> conv(int[] a){ var tmp = new ArrayList<Integer>(); for(int i=0;i<a.length;++i){ tmp.add(a[i]); } return tmp; } public static int intma(){ return Integer.MAX_VALUE; } public static int intmi(){ return Integer.MIN_VALUE; } public static int range(int[] ps, int n, int l, int r) { return ps[r]-(l==0?0:ps[l-1]); } public static int lower_bound(int[]a,int n,int tar) { int k = -1; for(int b=n/2;b>=1;b/=2) while(k+b<n&&a[k+b]<tar) k+=b; return k+1; } public static int upper_bound(int[] a,int n,int tar) { int k = 0; for(int b = n/2; b>=1; b/=2) while(k+b<n&&a[k+b]<=tar) k+=b; return k; } public static int mod=(int)1e9+7; public static Scanner sc = new Scanner(System.in); public static String str() { return sc.hasNextLine()?sc.nextLine():""; } public static int inp() { int n = sc.hasNextInt()?sc.nextInt():0; sc.nextLine(); return n; } public static void sort(int[] a){ Arrays.sort(a); } public static int bs(int[] a, int k) { return Arrays.binarySearch(a,k); } public static int[] inp(int n) { int[] a = new int[n]; for(int i=0;i<n;++i) a[i]=sc.hasNextInt()?sc.nextInt():0; sc.nextLine(); return a; } public static String inp(boolean s) { return sc.hasNextLine()?sc.nextLine():""; } public static void out(String s, boolean chk) { if(!chk) System.out.println(s); else System.out.print(s); } public static void nl() { System.out.print("\n"); } public static void out(int[] a) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); System.out.print("\n"); } public static void out(long[] a) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); System.out.print("\n"); } public static void out(int n) { System.out.println(n); } public static void out(long n) { System.out.println(n); } public static int max(int a, int b) { return Math.max(a,b); } public static long max(long a, long b) { return Math.max(a,b); } public static int min(int a, int b) { return Math.min(a,b); } public static long min(long a, long b) { return Math.min(a,b); } public static int[] cprange(int[] a, int f, int t) { return Arrays.copyOfRange(a,f,t+1); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
0d872b9c49111c9e5f8e0bd4b9943852
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.*; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; public class Yoo { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int recSearch(int arr[], int l, int r, int x) { if (r < l) return -1; if (arr[l] == x) return l; if (arr[r] == x) return r; return recSearch(arr, l+1, r-1, x); } public static void main(String[] args) { FastReader sc=new FastReader(); int i,j=0; int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); //int m = sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; for(i=0;i<n;i++) a[i]=sc.nextInt(); for(i=0;i<n;i++) b[i]=sc.nextInt(); Integer c[]=new Integer[n]; for(i=0;i<n;i++) c[i]=b[i]-a[i]; //c = -Arrays.sort(-c); //Collections.reverse(c); //Arrays.sort(c, Collections.reverseOrder()); Arrays.sort(c, Collections.reverseOrder()); /*for(i=0;i<n;i++) System.out.print(c[i]+" aa "); System.out.println();*/ for(i=0;i<n;i++) { if(c[i]<0) break; } //System.out.println(i); for(j=i;j<n;j++) { c[j]=Math.abs(c[j]); } /*for(j=0;j<n;j++) System.out.print(c[j]+" aab "); System.out.println();*/ int mid = i-1; //System.out.println(mid+"khggbj"); i=0; j=n-1; int ans=0; //System.out.println(+"gvjh"); while(i<=mid && i<j && j>mid) { if(c[i]>=c[j]) { ans++; i++; j--; } else { j--; } } //System.out.println(i+"gvjh"+mid+"hjv"+ans); if(i-1!=mid) ans+=((mid-i+1)/2); System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
beb0cc2dbd288e1bb98fb918155d8739
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[] x = new int[n]; for(int i = 0; i < n; i++) { x[i] = sc.nextInt(); } int[] y = new int[n]; for(int i = 0; i < n; i++) { y[i] = sc.nextInt(); } int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = y[i] - x[i]; } Arrays.sort(arr); int j = 0; int count = 0; for(int i = arr.length - 1; i >= 0; i--) { while(j < i && arr[i] + arr[j] < 0) { j++; } if(j >= i) { break; } count++; j++; } System.out.println(count); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
1aa7a68c695f9c1b98d9f72bc90d5dd4
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int len = obj.nextInt(); while (len-- != 0) { int n=obj.nextInt(); Vector<Long> v=new Vector<>(); long[] a=new long[n]; for(int i=0;i<n;i++)a[i]=obj.nextLong(); for(int i=0;i<n;i++) { long b=obj.nextLong()-a[i]; v.add(b); } Collections.sort(v); int ans=0; int i=0,j=n-1; while(i<j) { long val=v.get(i)+v.get(j); if(val>=0) { ans+=1; j--; } i++; } out.println(ans); } out.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
54ae3139c90c25d8b15e9f4be61ec0aa
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static StringBuilder sb; static dsu dsu; static long fact[]; static int mod = (int) (1e9 + 7); static void print(int[]arr){ for(int i=0;i<arr.length;i++)System.out.print(arr[i]+" "); System.out.println(); } static void print(long[]arr){ for(int i=0;i<arr.length;i++)System.out.print(arr[i]+" "); System.out.println(); } static void print(ArrayList<Integer>ls){ System.out.println(ls); } static void print(int t){ System.out.println(t); } static void print(long t){ System.out.println(t); } static void build(int node, int start, int end,int[]tree,int[]arr) { if (start == end) { tree[node] = arr[start]; } else { int mid = (start + end) / 2; int left = node * 2; int right = node * 2 + 1; build(left, start, mid,tree,arr); build(right, mid + 1, end,tree,arr); tree[node] = Math.max(tree[left], tree[right]); } } static int query(int node, int start, int end, int l, int r,int[]tree,int[]arr) { if (end < l || r < start)return 0; if (start == end) { return tree[node]; } else if (l <= start && end <= r) { return tree[node]; } else { int mid = (start + end) / 2; int left = query(node * 2, start, mid, l, r,tree,arr); int right = query(node * 2 + 1, mid + 1, end, l, r,tree,arr); return (left+ right); } } static void update(int node, int start, int end, int pos, int val,int[]tree,int[]arr) { if (start == end) { arr[start] += val; tree[node] += val; } else { int mid = (start + end) / 2; if ( start <= pos && pos <= mid) { update(node * 2, start, mid, pos, val,tree,arr); } else { update(node * 2 + 1, mid + 1, end, pos, val,tree,arr); } tree[node] = (tree[node * 2]+tree[node * 2 + 1]); } } static void swap(int[]arr,int i,int j){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static void solve() { int n=i(); long[]a=new long[n]; long[]b=new long[n]; for(int i=0;i<n;i++)a[i]=l(); for(int i=0;i<n;i++)b[i]=l(); for(int i=0;i<n;i++)a[i]=b[i]-a[i]; a=sort(a); int neg=0; int pos=n-1; int ans=0; while(pos>neg){ if(a[neg]+a[pos]>=0){ ans++; neg++; pos--; } else { neg++; } } ans+=(pos-neg+1)/2; sb.append(ans+"\n"); } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); // fact=new long[(int)1e6+10]; // fact[0]=fact[1]=1; // for(int i=2;i<fact.length;i++) // { fact[i]=((long)(i%mod)*(long)(fact[i-1]%mod))%mod; } int cnt=1; while (test-- > 0) { solve(); cnt++; } System.out.println(sb); } //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) ; y--; } x = (x * x); y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = i; } int find(int a) { if (parent[a] ==a) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int src; long a; long b; Pair(int src, long a,long b) { this.src = src; this.a = a; this.b = b; } public int compareTo(Pair o) { return (int) (this.b - o.b); } } //****CLASS PAIR ************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { 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 String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static char[] sort(char[] a2) { int n = a2.length; ArrayList<Character> l = new ArrayList<>(); for (char i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArrayi(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
ebfcbc0294061add5aeba9ebbb6fe0b4
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import javax.print.attribute.IntegerSyntax; import java.util.*; import java.io.*; public class Solution { private static class FastIO { private static class FastReader { BufferedReader br; StringTokenizer st; 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; } } private static PrintWriter out = new PrintWriter(System.out); private static FastReader in = new FastReader(); public void print(String s) {out.print(s);} public void println(String s) {out.println(s);} public void println() { println(""); } public void print(int i) {out.print(i);} public void print(long i) {out.print(i);} public void print(char i) {out.print(i);} public void print(double i) {out.print(i);} public void println(int i) {out.println(i);} public void println(long i) {out.println(i);} public void println(char i) {out.println(i);} public void println(double i) {out.println(i);} public void printIntArrayWithoutSpaces(int[] a) { for(int i : a) { out.print(i); } out.println(); } public void printIntArrayWithSpaces(int[] a) { for(int i : a) { out.print(i + " "); } out.println(); } public void printIntArrayNewLine(int[] a) { for(int i : a) { out.println(i); } } public int[] getIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public List<Integer> getIntList(int n) { List<Integer> list = new ArrayList<>(); for(int i = 0; i < n; i++) { list.add(in.nextInt()); } return list; } public static void printKickstartCase(int i) { out.print("Case #" + i + ": "); } public String next() {return in.next();} int nextInt() { return in.nextInt(); } char nextChar() {return in.next().charAt(0);} long nextLong() { return in.nextLong(); } double nextDouble() { return in.nextDouble(); } String nextLine() {return in.nextLine();} public void close() { out.flush(); out.close(); } } private static final FastIO io = new FastIO(); private static class MathUtil { public static final int MOD = 1000000007; public static int gcd(int a, int b) { if(b == 0) { return a; } return gcd(b, a % b); } public static int gcd(int[] a) { if(a.length == 0) { return 0; } int res = a[0]; for(int i = 1; i < a.length; i++) { res = gcd(res, a[i]); } return res; } public static int gcd(List<Integer> a) { if(a.size() == 0) { return 0; } int res = a.get(0); for(int i = 1; i < a.size(); i++) { res = gcd(res, a.get(i)); } return res; } public static int modular_mult(int a, int b, int M) { long res = (long)a * b; return (int)(res % M); } public static int modular_mult(int a, int b) { return modular_mult(a, b, MOD); } public static int modular_add(int a, int b, int M) { long res = (long)a + b; return (int)(res % M); } public static int modular_add(int a, int b) { return modular_add(a, b, MOD); } public static int modular_sub(int a, int b, int M) { long res = ((long)a - b) + M; return (int)(res % M); } public static int modular_sub(int a, int b) { return modular_sub(a, b, MOD); } //public static int modular_div(int a, int b, int M) {} //public static int modular_div(int a, int b) {return modular_div(a, b, MOD);} public static int pow(int a, int b, int M) { int res = 1; while (b > 0) { if ((b & 1) == 1) { res = modular_mult(res, a, M); } a = modular_mult(a, a, M); b = b >> 1; } return res; } public static int pow(int a, int b) { return pow(a, b, MOD); } /*public static int fact(int i, int M) { } public static int fact(int i) { } public static void preComputeFact(int i) { } public static int mod_mult_inverse(int den, int mod) { } public static void C(int n, int r) { }*/ } private static class ArrayUtil { @FunctionalInterface private static interface NumberPairComparator { boolean test(int a, int b); } public static int[] nextGreaterOrSmallerRight(int[] a, NumberPairComparator npc) { int n = a.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for(int i = 0; i < n; i++) { int cur = a[i]; while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) { res[stack.pop()] = i; } stack.push(i); } while(!stack.isEmpty()) { res[stack.pop()] = n; } return res; } public static int[] nextGreaterOrSmallerLeft(int[] a, NumberPairComparator npc) { int n = a.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for(int i = n - 1; i >= 0; i--) { int cur = a[i]; while(!stack.isEmpty() && npc.test(a[stack.peek()], cur)) { res[stack.pop()] = i; } stack.push(i); } while(!stack.isEmpty()) { res[stack.pop()] = n; } return res; } public static Map<Integer, Integer> getFreqMap(int[] a) { Map<Integer, Integer> map = new HashMap<>(); for(int i : a) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } public static long arraySum(int[] a) { long sum = 0; for(int i : a) { sum += i; } return sum; } private static int maxIndex(int[] a) { int max = Integer.MIN_VALUE; int max_index = -1; for(int i = 0; i < a.length; i++) { if(a[i] > max) { max = a[i]; max_index = i; } } return max_index; } } private static final int M = 1000000007; private static final String yes = "YES"; private static final String no = "NO"; private static int MAX = M / 100; private static final int MIN = -MAX; private static final int UNVISITED = -1; private static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return "x: " + x + " y: " + y; } } private static List<List<Integer>> getAdj(int n, int e, boolean directed, boolean shift) { List<List<Integer>> res = new ArrayList<>(); for(int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for(int i = 0; i < e; i++) { int u = io.nextInt(); int v = io.nextInt(); if(shift) { u --; v --; } res.get(u).add(v); if(!directed) { res.get(v).add(u); } } return res; } private static class TreeUtil { private static List<List<Integer>> getTreeInputWithOffset(int n) { List<List<Integer>> res = new ArrayList<>(); for(int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for(int i = 0; i < n - 1; i++) { int u = io.nextInt() - 1; int v = io.nextInt() - 1; res.get(u).add(v); res.get(v).add(u); } return res; } private static int subtreeSizeDfs(int root, int parent, List<List<Integer>> tree, List<Integer> res) { int sum = 0; for(int child : tree.get(root)) { if(child == parent) { continue; } sum += subtreeSizeDfs(child, root, tree, res); } res.set(root, 1 + sum); return 1 + sum; } private static List<Integer> getSubtreeSizes(List<List<Integer>> tree, int root) { List<Integer> res = new ArrayList<>(); for(int i = 0; i < tree.size(); i++) { res.add(0); } subtreeSizeDfs(root, -1, tree, res); return res; } } private static final long e3 = 1_000; private static final long e6 = 1_000_000; private static final long e9 = 1_000_000_000; private static final long e12 = e9 * e3; private static final long e15 = e9 * e6; private static final long e18 = e9 * e9; String mod = "%"; private static final int[] dr = new int[]{-1, 0, 1, -1, 1, -1, 0, 1}; private static final int[] dc = new int[]{-1, -1, -1, 0, 0, 1, 1, 1}; private static final int IMPOSTER = 1; private static final int CREW = 0; private static class Edge { int from; int to; int parity; public Edge(int from, int to, int parity) { this.from = from; this.to = to; this.parity = parity; } } public static void main(String[] args) { int testcases = io.nextInt(); // int testcases = 1; for(int zqt = 0; zqt < testcases; zqt ++) { int n = io.nextInt(); int[][] a = new int[n][2]; for(int i = 0; i < n; i++) { a[i][0] = io.nextInt(); } for(int i = 0; i < n; i++) { a[i][1] = io.nextInt(); } Arrays.sort(a, (p, q) -> (p[1] - p[0]) - (q[1] - q[0])); int res = 0; int l = 0; int r = n - 1; while (l < r) { int diff1 = Math.abs(a[l][1] - a[l][0]); int diff2 = (a[r][1] - a[r][0]); if(diff2 >= diff1) { res += 1; l += 1; r -= 1; } else { l += 1; } } io.println(res); } io.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
ed84e87adb8adc4ed7c7c0dc49167dc9
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.lang.reflect.Array; import java.math.*; public class Simple{ public static class Node{ int v; int val; public Node(int v,int val){ this.val = val; this.v = v; } } static final Random random=new Random(); 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 (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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length; for (int i=0; i<n; i++) { long oi=random.nextInt(n), temp=a[(int)oi]; a[(int)oi]=a[i]; a[i]=temp; } Arrays.sort(a); } public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(Pair other){ return this.y - other.y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } // public boolean equals(Pair other){ // if(this.x == other.x && this.y == other.y)return true; // return false; // } // public int hashCode(){ // return 31*x + y; // } // @Override // public int compareTo(Simple.Pair o) { // // TODO Auto-generated method stub // return 0; // } } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static long modInverse(long n, long p) { return power(n, p - 2, p); } static long[] fac = new long[100000 + 1]; // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(long n, long r, long p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static int gcd(int a, int b) { if(a == 0)return a; if (b == 0) return a; return gcd(b, a % b); } static long gcd_long(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd_long(a%b, b); return gcd_long(a, b%a); } public static class DSU{ int n; int par[]; int rank[]; public DSU(int n){ this.n = n; par = new int[n+1]; rank = new int[n+1]; for(int i=1;i<=n;i++){ par[i] = i ; rank[i] = 0; } } public int findPar(int node){ if(node==par[node]){ return node; } return par[node] = findPar(par[node]);//path compression } public void union(int u,int v){ u = findPar(u); v = findPar(v); if(rank[u]<rank[v]){ par[u] = v; } else if(rank[u]>rank[v]){ par[v] = u; } else{ par[v] = u; rank[u]++; } } } static final int MAXN = 100001; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j=i*i; j<MAXN; j+=i) // marking spf[j] if it is not // previously marked if (spf[j]==j) spf[j] = i; } } } // A O(log n) function returning primefactorization // by dividing by smallest prime factor at every step static Vector<Integer> getFactorization(int x) { Vector<Integer> ret = new Vector<>(); while (x != 1) { ret.add(spf[x]); x = x / spf[x]; } return ret; } // A utility function to get the // middle index of given range. static int getMid(int s, int e) { return s + (e - s) / 2; } /* * A recursive function to get the sum of values in given range of the array. * The following are parameters for this function. * * st -> Pointer to segment tree * node -> Index of current node in * the segment tree. * ss & se -> Starting and ending indexes * of the segment represented * by current node, i.e., st[node] * l & r -> Starting and ending indexes * of range query */ static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) { // If segment of this node is completely // part of given range, then return // the max of segment if (l <= ss && r >= se) return st[node]; // If segment of this node does not // belong to given range if (se < l || ss > r) return -1; // If segment of this node is partially // the part of given range int mid = getMid(ss, se); return Math.max( MaxUtil(st, ss, mid, l, r, 2 * node + 1), MaxUtil(st, mid + 1, se, l, r, 2 * node + 2)); } /* * A recursive function to update the nodes which have the given index in their * range. The following are parameters st, ss and se are same as defined above * index -> index of the element to be updated. */ static void updateValue(int arr[], int[] st, int ss, int se, int index, int value, int node) { if (index < ss || index > se) { System.out.println("Invalid Input"); return; } if (ss == se) { // update value in array and in // segment tree arr[index] = value; st[node] = value; } else { int mid = getMid(ss, se); if (index >= ss && index <= mid) updateValue(arr, st, ss, mid, index, value, 2 * node + 1); else updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2); st[node] = Math.max(st[2 * node + 1], st[2 * node + 2]); } return; } // Return max of elements in range from // index l (query start) to r (query end). static int getMax(int[] st, int n, int l, int r) { // Check for erroneous input values if (l < 0 || r > n - 1 || l > r) { System.out.printf("Invalid Input\n"); return -1; } return MaxUtil(st, 0, n - 1, l, r, 0); } // A recursive function that constructs Segment // Tree for array[ss..se]. si is index of // current node in segment tree st static int constructSTUtil(int arr[], int ss, int se, int[] st, int si) { // If there is one element in array, store // it in current node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then // recur for left and right subtrees and // store the max of values in this node int mid = getMid(ss, se); st[si] = Math.max( constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)); return st[si]; } /* * Function to construct segment tree from given array. This function allocates * memory for segment tree. */ static int[] constructST(int arr[], int n) { // Height of segment tree int x = (int)Math.ceil(Math.log(n) / Math.log(2)); // Maximum size of segment tree int max_size = 2 * (int)Math.pow(2, x) - 1; // Allocate memory int[] st = new int[max_size]; // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, st, 0); // Return the constructed segment tree return st; } public static void main(String args[]){ try { FastReader s=new FastReader(); // FastWriter out = new FastWriter(); // Scanner s = new Scanner(System.in); int testCases= s.nextInt(); for(int t = 1; t <= testCases; t++){ int n = s.nextInt(); int x[] = new int[n]; int y[] = new int[n]; for(int i = 0; i < n; i++){ x[i] = s.nextInt(); } for(int i = 0; i < n; i++){ y[i] = s.nextInt(); } int diff[] = new int[n]; for(int i = 0; i < n; i++){ diff[i] = y[i] - x[i]; } ruffleSort(diff); int i = 0; int j = n-1; int ans = 0; while(i < j){ if(diff[i] + diff[j] >= 0){ ans++; i++; j--; } else{ i++; } } System.out.println(ans); } } catch (Exception e) { System.out.println(e.toString()); // System.ouintt.println("Eh"); return; } } } /* 1 3 4 5 2 2 4 3 5 1 */
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
91123de6ed5e1c6e2b521ef8d227a626
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; //1729D public class FriendsAndTheRestaurant { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int N = Integer.parseInt(br.readLine()); while (N-- > 0) { int n = Integer.parseInt(br.readLine()); int[] x = new int[n]; StringTokenizer token = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) x[i] = -Integer.parseInt(token.nextToken()); token = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) x[i] += Integer.parseInt(token.nextToken()); Arrays.sort(x); int ans = 0; int lp = 0; int rp = n-1; while (lp < rp) { while (lp < rp && -x[lp] > x[rp]) lp++; if (lp < rp) ans++; lp++; rp--; } pw.println(ans); } pw.close(); br.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
d78261faa9852c3e6d19905195f8065b
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedOutputStream; 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.Collections; import java.util.HashMap; import java.util.StringTokenizer; import java.util.function.Supplier; import java.util.stream.Collectors; public class cf { //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static int cnt; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static int reverseBits(int n) { int rev = 0; // traversing bits of 'n' // from the right while (n > 0) { // bitwise left shift // 'rev' by 1 rev <<= 1; // if current bit is '1' if ((int)(n & 1) == 1) rev ^= 1; // bitwise right shift //'n' by 1 n >>= 1; } // required number return rev; } public static void main(String[] args) { // TODO Auto-generated method stub MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); StringBuilder sb = new StringBuilder(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); ArrayList<Integer> x1=new ArrayList<Integer>(); ArrayList<Integer> x2=new ArrayList<Integer>(); ArrayList<Integer> x3=new ArrayList<Integer>(); for(int i=0;i<n;i++) { x1.add(sc.nextInt()); } for(int i=0;i<n;i++) { x2.add(sc.nextInt()); } for(int i=0;i<n;i++) { x3.add(x2.get(i)-x1.get(i)); } Collections.sort(x3); int l=0,r=n-1,ans=0; while(l<r) { if(x3.get(l)+x3.get(r)>=0) { l++; r--; ans++; } else { l++; } } out.println(ans); } out.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9eb2a8ae34a0be5cee6fec1cbf2d2898
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Collections; public class FriendsandtheRestaurant { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-- > 0) { String[] st = br.readLine().split(" "); int n = Integer.parseInt(st[0]); String[] str = br.readLine().split(" "); int x[] = new int[n]; int y[] = new int[n]; for(int i = 0; i < n; i++) { x[i] = Integer.parseInt(str[i]); } str = br.readLine().split(" "); for(int i = 0; i < n; i++) y[i] = Integer.parseInt(str[i]); Integer[] a = new Integer[n]; for(int i = 0; i < n; i++) a[i] = y[i] - x[i]; Arrays.sort(a, Collections.reverseOrder()); int j = n-1; int ans = 0; for(int i = 0; i < n; i++) { while(j > i && a[i] + a[j] < 0) j--; if(j <= i) break; ans++; j--; } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
47ab7bb3527d4d210f8808876169291b
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class D { 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; } } public static void main(String[] args) { FastReader obj = new FastReader(); int t = obj.nextInt(); while (t != 0) { int n = obj.nextInt(); int arr1[] = new int[n]; for (int i = 0; i < n; i++) arr1[i] = obj.nextInt(); int arr2[] = new int[n]; for (int i = 0; i < n; i++) arr2[i] = obj.nextInt(); System.out.println(solve(arr1, arr2)); t--; } } public static int solve(int arr1[], int arr2[]) { int n = arr1.length; ArrayList<Integer> li = new ArrayList<>(); for (int i = 0; i < n; i++) li.add(arr2[i] - arr1[i]); Collections.sort(li); int i = 0; int j = n - 1; int days = 0; while (i < j) { if (li.get(i) < 0 && li.get(j) < 0) return days; else if (li.get(i) + li.get(j) >= 0) { days++; i++; j--; } else i++; } return days; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
555867bb5a2bf1817afe1988ab449e3a
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class CF1562C extends PrintWriter { CF1562C() { super(System.out); } public static Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1562C o = new CF1562C(); o.main(); o.flush(); } static long calculate(long p, long q) { long mod = 1000000007, expo; expo = mod - 2; // Loop to find the value // until the expo is not zero while (expo != 0) { // Multiply p with q // if expo is odd if ((expo & 1) == 1) { p = (p * q) % mod; } q = (q * q) % mod; // Reduce the value of // expo by 2 expo >>= 1; } return p; } static String longestPalSubstr(String str) { // The result (length of LPS) int maxLength = 1; int start = 0; int len = str.length(); int low, high; // One by one consider every // character as center // point of even and length // palindromes for (int i = 1; i < len; ++i) { // Find the longest even // length palindrome with // center points as i-1 and i. low = i - 1; high = i; while (low >= 0 && high < len && str.charAt(low) == str.charAt(high)) { --low; ++high; } // Move back to the last possible valid palindrom substring // as that will anyway be the longest from above loop ++low; --high; if (str.charAt(low) == str.charAt(high) && high - low + 1 > maxLength) { start = low; maxLength = high - low + 1; } // Find the longest odd length // palindrome with center point as i low = i - 1; high = i + 1; while (low >= 0 && high < len && str.charAt(low) == str.charAt(high)) { --low; ++high; } // Move back to the last possible valid palindrom substring // as that will anyway be the longest from above loop ++low; --high; if (str.charAt(low) == str.charAt(high) && high - low + 1 > maxLength) { start = low; maxLength = high - low + 1; } } return str.substring(start, start + maxLength - 1); } long check(long a){ long ret=0; for(long k=2;(k*k*k)<=a;k++){ ret=ret+(a/(k*k*k)); } return ret; } /*public static int getFirstSetBitPos(int n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } public static int bfsq(int n, int m, HashMap<Integer,ArrayList<Integer>>h,boolean v ){ v[n]=true; if(n==m) return 1; else { int a=h.get(n).get(0); int b=h.get(n).get(1); if(b>m) return(m-n); else { int a1=bfsq(a,m,h,v); int b1=bfsq(b,m,h,v); return 1+Math.min(a1,b1); } } }*/ static long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Returns factorial of n static long fact(long n) { long res = 1; for (long i = 2; i <= n; i++) res = res * i; return res; } /*void bfs(int src, HashMap<Integer,ArrayList<Integer,Integer>>h,int deg, boolean v[] ){ a[src]=deg; Queue<Integer>= new LinkedList<Integer>(); q.add(src); while(!q.isEmpty()){ (int a:h.get(src)){ if() } } }*/ /* void dfs(int root, int par, HashMap<Integer,ArrayList<Integer>>h,int dp[], int child[]) { dp[root]=0; child[root]=1; for(int x: h.get(root)){ if(x == par) continue; dfs(x,root,h,in,dp); child[root]+=child[x]; } ArrayList<Integer> mine= new ArrayList<Integer>(); for(int x: h.get(root)) { if(x == par) continue; mine.add(x); } if(mine.size() >=2){ int y= Math.max(child[mine.get(0)] - 1 + dp[mine.get(1)] , child[mine.get(1)] -1 + dp[mine.get(0)]); dp[root]=y;} else if(mine.size() == 1) dp[root]=child[mine.get(0)] - 1; } */ class Pair implements Comparable<Pair>{ int i; int j; Pair (int a, int b){ i = a; j = b; } public int compareTo(Pair A){ return (int)(this.i-A.i); }} /*static class Pair { int i; int j; Pair() { } Pair(int i, int j) { this.i = i; this.j = j; } }*/ /*ArrayList<Integer> check(int a[], int b){ int n=a.length; long ans=0;int k=0; ArrayList<Integer>ab= new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(a[i]%m==0) {k=a[i]; while(a[i]%m==0){ a[i]=a[i]/m; } for(int z=0;z<k/a[i];z++){ ab.add(a[i]); } } else{ ab.add(a[i]); } } return ab; } */ /*int check[]; int tree[]; static void build( int []arr) { // insert leaf nodes in tree for (int i = 0; i < n; i++) tree[n + i] = arr[i]; // build the tree by calculating // parents for (int i = n - 1; i > 0; --i){ int ans= Math.min(tree[i << 1], tree[i << 1 | 1]); int ans1=Math.max((tree[i << 1], tree[i << 1 | 1])); if(ans==0) } }*/ /*static void ab(long n) { // Note that this loop runs till square root for (long i=1; i<=Math.sqrt(n); i++) { if(i==1) { p.add(n/i); continue; } if (n%i==0) { // If divisors are equal, print only one if (n/i == i) p.add(i); else // Otherwise print both { p.add(i); p.add(n/i); } } } }*/ void main() { int g=sc.nextInt(); int mod=1000000007; for(int w1=0;w1<g;w1++){ int n=sc.nextInt(); int a[]= new int[n]; int b[]= new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); for(int i=0;i<n;i++) b[i]=sc.nextInt(); int c[]= new int[n]; for(int i=0;i<n;i++) c[i]=b[i]-a[i]; Arrays.sort(c); int ans=0; int l=0; int r=n-1; while(l<r){ int an=c[l]+c[r]; if(an<0) l++; else{ l++; r--; ans++; } } println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
50d562f23c949ad8127d6a2934bade27
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
/* @Author Shubham Chaudhari */ import java.io.*; import java.lang.reflect.Array; import java.util.*; public class Main { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Long>map=new HashMap<>(); public static void main(String[] args) { int t=in.nextInt(); StringBuilder res=new StringBuilder(); loop: while(t-->0){ int n=in.nextInt(); int[] x=in.readintarray(n); int[] y=in.readintarray(n); ArrayList<Integer>pve=new ArrayList<>(); ArrayList<Integer>nve=new ArrayList<>(); ArrayList<Integer>zeros=new ArrayList<>(); for(int i=0;i<n;i++){ int val=y[i]-x[i]; if(val==0){ zeros.add(val); } else if(val>0){ pve.add(val); } else{ nve.add(val); } } Collections.sort(pve); Collections.sort(nve,Collections.reverseOrder()); // for(int i: pve){ // System.out.print(i+" "); // } // System.out.println(); // for(int i: nve){ // System.out.print(i+" "); // } // System.out.println(); int count=0; int j=0; int k=0; for(int i=0;i<pve.size();i++){ if(j<nve.size()&&pve.get(i)>=-1*nve.get(j)){ count++; j++; } else if(k<zeros.size()){ k++; count++; } else if(i+1<pve.size()){ i++; count++; } } count=count+(zeros.size()-k)/2; res.append(count+"\n"); } System.out.println(res); } static class Pair implements Comparable<Pair>{ int a,b; public Pair(int a,int b){ this.a=a; this.b=b; } @Override public int compareTo(Pair o) { if(a>o.a){ return 1; }else if(a==o.a){ return 0; } return -1; } } static boolean isPalindrome(int n){ int tmp=n; int tmp2=0; while (n>0){ tmp2=tmp2*10+n%10; n/=10; } return tmp==tmp2; } static long calculateSum(long n) { if(n<0) { return 0; } return n*(n+1)/2; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int abs(int a) { if(a<0) return -1*a; return a; } 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; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } static class Node implements Comparable<Node> { int val; long cost; public Node(int val,long cost) { this.val=val; this.cost=cost; } public int compareTo(Node x) { return Long.compare(this.cost,x.cost); } } static class UWGraph{ public static ArrayList<WNode> graph[]; public int V; public static PriorityQueue<WNode> pq; public UWGraph(int V){ graph=new ArrayList[V]; for(int i=0;i<V;i++){ graph[i]=new ArrayList<WNode>(); } this.V=V; } public void addEdge(int v,int u,int w){ graph[v].add(new WNode(u,w)); graph[u].add(new WNode(v,w)); } public int[] dijkstra(int src) { int[] distance = new int[V]; for (int i = 0; i < V; i++) distance[i] = Integer.MAX_VALUE; distance[src] = 0; pq = new PriorityQueue<>(); pq.add(new WNode(src, 0)); boolean done[]=new boolean[V]; while (pq.size() > 0) { WNode current = pq.poll(); if(done[current.getVertex()]) continue; done[current.getVertex()]=true; for (WNode n : graph[current.getVertex()]) { if (distance[current.getVertex()] + n.getWeight() < distance[n.getVertex()]) { distance[n.getVertex()] = n.getWeight() + distance[current.getVertex()]; pq.add(new WNode(n.getVertex(), distance[n.getVertex()])); } } } return distance; } } static class WNode implements Comparable<WNode>{ public int vertex, weight; WNode(int v, int w) { vertex = v; weight = w; } int getVertex() { return vertex; } int getWeight() { return weight; } @Override public int compareTo(WNode o) { return weight-o.weight; } } static class UNGraph{ public ArrayList<Integer> graph[]; public int V; public UNGraph(int V){ graph=new ArrayList[V]; for(int i=0;i<V;i++){ graph[i]=new ArrayList<Integer>(); } this.V=V; } public void addEdge(int v,int u){ graph[v].add(u); graph[u].add(v); } } static class SegmentTree{ long st[]; int minCount; long minVal; public SegmentTree(long arr[],int n){ int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); minCount=0; //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new long[max_size]; // Memory allocation constructSTUtil(arr, 0, n - 1, 0); } long constructSTUtil(long arr[], int ss, int se, int si) { if (ss == se) { st[si] = arr[ss]; return arr[ss]; } int mid = getMid(ss, se); st[si] = Math.min(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } int getMid(int s, int e) { return s + (e - s) / 2; } long updateValueUtil(int ss, int se, int i, long diff, int si) { // Base Case: If the input index lies outside the range of // this segment if (i < ss || i > se) return st[si]; // If the input index is in range of this node, then update the // value of the node and its children if (se != ss) { int mid = getMid(ss, se); st[si] = Math.min( updateValueUtil(ss, mid, i, diff, 2 * si + 1), updateValueUtil(mid + 1, se, i, diff, 2 * si + 2)); } else{ st[si]=diff; } return st[si]; } // The function to update a value in input array and segment tree. // It uses updateValueUtil() to update the value in segment tree void updateValue(long arr[], int n, int i, int new_val) { // Check for erroneous input index if (i < 0 || i > n - 1) { return; } // Get the difference between new value and old value long diff = new_val; // Update the value in array arr[i] = new_val; // Update the values of nodes in segment tree updateValueUtil(0, n - 1, i, diff, 0); } // End of template public long getSum(int n,int l, int r){ minCount=0; minVal= getSumUtil(0,n-1,l,r,0); return minVal; } public long getSumUtil(int ss, int se,int l, int r, int si){ int mid = getMid(ss,se); if(si>=st.length){ return Integer.MAX_VALUE; } if(ss>=l&&se<=r){ if(ss==se && minVal==st[si]){ minCount++; } getSumUtil(ss,mid,l,r,2*si+1); getSumUtil(mid+1,se,l,r,2*si+2); return st[si]; } if (se < l || ss > r) return Integer.MAX_VALUE; if(ss==se && minVal==st[si]){ minCount++; } return Math.min(getSumUtil(ss,mid,l,r,2*si+1),getSumUtil(mid+1,se,l,r,2*si+2)); } void printSegmentTree(){ for(long x: st){ System.out.print(x+" "); } System.out.println(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
755e3ac556edf2e5ef58e36c92c184d5
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static int mod = 998244353; //static int mod = (int)1e9+7; static boolean[] prime = new boolean[1]; static void yes() throws Exception { print("Yes"); } static void no() throws Exception { print("No"); } static int[][] dir1 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; static int[][] dir2 = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; static int inf = 0x3f3f3f3f; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static class DSU { int[] fa; DSU(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static List<Integer>[] lists; static void init(int n) { lists = new List[n]; for(int i = 0; i< n;i++){ lists[i] = new ArrayList<>(); } } static class LCA{ int[] dep; int[][] fa; int[] log; boolean[] v; public LCA(int n){ dep = new int[n+5]; log = new int[n+5]; fa = new int[n+5][31]; v = new boolean[n+5]; for (int i = 2; i <= n; ++i) { log[i] = log[i/2] + 1; } dfs(1,0); } private void dfs(int cur, int pre){ if(v[cur]) return; v[cur] = true; dep[cur] = dep[pre]+1; fa[cur][0] = pre; for (int i = 1; i <= log[dep[cur]]; ++i) { fa[cur][i] = fa[fa[cur][i - 1]][i - 1]; } for(int i : lists[cur]){ dfs(i,cur); } } private int lca(int a, int b){ if(dep[a] > dep[b]){ int t = a; a = b; b = t; } while (dep[a] != dep[b]){ b = fa[b][log[dep[b] - dep[a]]]; } if(a == b) return a; for (int k = log[dep[a]]; k >= 0; k--) { if (fa[a][k] != fa[b][k]) { a = fa[a][k]; b = fa[b][k]; } } return fa[a][0]; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(int[][] a) throws Exception { for (int i[] : a) print(i); } static void print(long[][] a) throws Exception { for (long[] i : a) print(i); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i + ""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int) ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void resort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n - 1 - i]; } static void resort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[n - 1 - i]; } static int max(int a, int b) { return Math.max(a, b); } static int min(int a, int b) { return Math.min(a, b); } static long max(long a, long b) { return Math.max(a, b); } static long min(long a, long b) { return Math.min(a, b); } static int max(int[] a) { int max = a[0]; for (int i : a) max = max(max, i); return max; } static int min(int[] a) { int min = a[0]; for (int i : a) min = min(min, i); return min; } static long max(long[] a) { long max = a[0]; for (long i : a) max = max(max, i); return max; } static long min(long[] a) { long min = a[0]; for (long i : a) min = min(min, i); return min; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static int[] getarr(List<Integer> list) { int n = list.size(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = list.get(i); return a; } public static void main(String[] args) throws Exception { int T = 1; T = get(); while (T-- > 0) { int n = get(); int[] x = getint(), y = getint(); int[] c = new int[n]; for(int i = 0;i < n;i++) c[i] = y[i]-x[i]; sort(c); int ans = 0; int l = 0, r = n-1; while (l < r){ if(c[l]+c[r] >= 0) { r--;l++; ans++; }else{ l++; } } print(ans); } bw.flush(); } static char get(int x){ return (char)(x+'a'); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
451e3846ece88d0d38c37f9da1e7635f
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer ST; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00"); final static int MAX = Integer.MAX_VALUE, MIN = Integer.MIN_VALUE, mod = (int) (1e9 + 7); final static long LMAX = Long.MAX_VALUE, LMIN = Long.MIN_VALUE; final static long INF = (long) 1e18, Neg_INF = (long) -1e18; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] -= readInt(); for (int i = 0; i < n; i++) arr[i] += readInt(); int j = 0, ans = 0; Arrays.sort(arr); for (int i = n - 1; i > j; i--) { while (j < i) { if (arr[i] + arr[j++] >= 0) { ans++; break; } } } out.println(ans); } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java && java CodeForces // change Stack size -> java -Xss16M CodeForces.java // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } BR = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (ST == null || !ST.hasMoreTokens()) ST = new StringTokenizer(readLine()); return ST.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return BR.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray() throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static long multiply(long a, long b) { return (((a % mod) * (b % mod)) % mod); } private static long divide(long a, long b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } boolean insert(String word) { Node curr = root; boolean ans = true; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; if (curr.isEnd) ans = false; } curr.isEnd = true; return ans; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== FENWICK TREE ================================ static class FT { int n; int[] arr; int[] tree; FT(int[] arr, int n) { this.arr = arr; this.n = n; this.tree = new int[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } FT(int n) { this.n = n; this.tree = new int[n + 1]; } // 1 based indexing void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } // 1 based indexing int query(int l, int r) { return getSum(r) - getSum(l - 1); } int getSum(int idx) { int ans = 0; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
bf27bed37835ec417cebd8698fad5b80
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class cf { public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static boolean isok(long x, long h, long k) { long sum = 0; if (h > k) { long t1 = h - k; long t = t1 * k; sum += (k * (k + 1)) / 2; sum += t - (t1 * (t1 + 1) / 2); } else { sum += (h * (h + 1)) / 2; } if (sum < x) { return true; } return false; } public static boolean binary_search(long[] a, long k) { long low = 0; long high = a.length - 1; long mid = 0; while (low <= high) { mid = low + (high - low) / 2; if (a[(int) mid] == k) { return true; } else if (a[(int) mid] < k) { low = mid + 1; } else { high = mid - 1; } } return false; } public static long lowerbound(long a[], long ddp) { long low = 0; long high = a.length; long mid = 0; while (low < high) { mid = low + (high - low) / 2; if (a[(int) mid] == ddp) { return mid; } if (a[(int) mid] < ddp) { low = mid + 1; } else { high = mid; } } // if(low + 1 < a.length && a[(int)low + 1] <= ddp){ // low++; // } if (low == a.length && low != 0) { low--; return low; } if (a[(int) low] > ddp && low != 0) { low--; } return low; } public static long upperbound(long a[], long ddp) { long low = 0; long high = a.length; long mid = 0; while (low < high) { mid = low + (high - low) / 2; if (a[(int) mid] <= ddp) { low = mid + 1; } else { high = mid; } } if (low == a.length) { return a.length - 1; } return low; } // public static class pair implements Comparable<pair> { // long w; // long h; // public pair(long w, long h) { // this.w = w; // this.h = h; // } // public int compareTo(pair b) { // if (this.w != b.w) // return (int) (this.w - b.w); // else // return (int) (this.h - b.h); // } // } public static class pair { long w; long h; public pair(long w, long h) { this.w = w; this.h = h; } } public static class trinary { long a; long b; long c; public trinary(long a, long b, long c) { this.a = a; this.b = b; this.c = c; } } public static long lowerboundforpairs(pair a[], long pr) { long low = 0; long high = a.length; long mid = 0; while (low < high) { mid = low + (high - low) / 2; if (a[(int) mid].w <= pr) { low = mid + 1; } else { high = mid; } } // if(low + 1 < a.length && a[(int)low + 1] <= ddp){ // low++; // } // if(low == a.length && low != 0){ // low--; // return low; // } // if(a[(int)low].w > pr && low != 0){ // low--; // } return low; } public static pair[] sortpair(pair[] a) { Arrays.sort(a, new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) p1.w - (int) p2.w; } }); return a; } public static boolean ispalindrome(String s) { long i = 0; long j = s.length() - 1; boolean is = false; while (i < j) { if (s.charAt((int) i) == s.charAt((int) j)) { is = true; i++; j--; } else { is = false; return is; } } return is; } public static void sort(long[] arr) { ArrayList<Long> a = new ArrayList<>(); for (long i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } public static void sortForObjecttypes(pair[] arr) { ArrayList<pair> a = new ArrayList<>(); for (pair i : arr) { a.add(i); } Collections.sort(a, new Comparator<pair>() { @Override public int compare(pair a, pair b) { return (int) (a.h - b.h); } }); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } public static long power(long base, long pow, long mod) { long result = base; long temp = 1; while (pow > 1) { if (pow % 2 == 0) { result = ((result % mod) * (result % mod)) % mod; pow /= 2; } else { temp = temp * base; pow--; } } result = ((result % mod) * (temp % mod)); // System.out.println(result); return result; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } void readArr(int[] ar, int n) { for (int i = 0; i < n; i++) { ar[i] = nextInt(); } } } public static int bSearchDiff(long[] a, int low, int high) { int mid = low + ((high - low) / 2); int hight = high; int lowt = low; while (lowt < hight) { mid = lowt + (hight - lowt) / 2; if (a[high] - a[mid] <= 5) { hight = mid; } else { lowt = mid + 1; } } return lowt; } public static boolean is[] = new boolean[1000001]; public static Vector<Integer> seiveOfEratosthenes() { Vector<Integer> listA = new Vector<>(); int a[] = new int[1000001]; for (int i = 2; i * i <= a.length; i++) { if (a[i] != 1) { for (long j = i * i; j < a.length; j += i) { a[(int) j] = 1; } } } for (int i = 2; i < a.length; i++) { if (a[i] == 0) { is[i] = true; listA.add(i); } } return listA; } public static Vector<Integer> ans = seiveOfEratosthenes(); public static void solve(FastReader sc, PrintWriter w) throws Exception { int n = sc.nextInt(); long x[] = new long[n]; long y[] = new long[n]; for (int i = 0; i < n; i++) { x[i] = sc.nextLong(); } for (int i = 0; i < n; i++) { y[i] = sc.nextLong(); } long diff[] = new long[n]; for (int i = 0; i < n; i++) { diff[i] = y[i] - x[i]; } sort(diff); // for (int i = 0; i < n; i++) { // System.out.println(diff[i]); // } long ans = 0; int i = 0; int j = n - 1; while (i < j) { long t = diff[i] + diff[j]; i++; while (i < j && t < 0) { t -= diff[i - 1]; t += diff[i]; i++; } if (t >= 0) { ans++; j--; } } System.out.println(ans); } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter w = new PrintWriter(System.out); long o = sc.nextLong(); while (o > 0) { solve(sc, w); o--; } w.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
907159265accc39332acb815cf9d5fe3
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public final class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int i, j = n - 1, ans = 0; int[] x = new int[n]; int[] y = new int[n]; for(i = 0; i < n; i++) { x[i] = sc.nextInt(); } for(i = 0; i < n; i++) { y[i] = sc.nextInt(); y[i] -= x[i]; } Arrays.sort(y); i = 0; while(i < j) { if(y[i] + y[j] >= 0) { ans++; j--; } i++; } System.out.println(ans); } sc.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
13e03787e940877e8ca6047d059d891c
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
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[] x = new int[n]; int[] y = new int[n]; for (int i = 0; i < n; i++) x[i] = sc.nextInt(); for (int i = 0; i < n; i++) y[i] = sc.nextInt() - x[i]; Arrays.sort(y); int cnt = 0; for (int i = 0, j = n - 1; i < j;) { if (y[i] + y[j] >= 0) { cnt++; j--; } i++; } System.out.println(cnt); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
5cbbca1be39f0c14779bb015b41c6ba8
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public final class Solution{ static StringTokenizer st; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int pi(String s) { return Integer.parseInt(s);} static long pl(String s) { return Long.parseLong(s);} static double pd(String s) { return Double.parseDouble(s);} static String is(int no) { return Integer.toString(no);} static String ls(long no) { return Long.toString(no);} static String ds(double no) { return Double.toString(no);} public static void main(String[] args) throws IOException{ int cases = pi(br.readLine()), n, i, j; while (cases-- != 0) { n = pi(br.readLine()); int a[] = new int[n]; st = new StringTokenizer(br.readLine()); for (i = 0; i < n; i++) { a[i] = pi(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (i = 0; i < n; i++) { a[i] = pi(st.nextToken()) - a[i]; } Arrays.sort(a); i = 0; j = n - 1; int cnt = 0; while (i < j) { if (a[j] + a[i] >= 0) { cnt++; j--; i++; } else { i++; } } bw.write(is(cnt) + "\n"); } bw.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
0a8d2cac48b56120aaccf950df6681c8
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public final class Solution{ static StringTokenizer st; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int pi(String s) { return Integer.parseInt(s);} static long pl(String s) { return Long.parseLong(s);} static double pd(String s) { return Double.parseDouble(s);} static String is(int no) { return Integer.toString(no);} static String ls(long no) { return Long.toString(no);} static String ds(double no) { return Double.toString(no);} public static void main(String[] args) throws IOException{ int cases = pi(br.readLine()), n, i, j; while (cases-- != 0) { n = pi(br.readLine()); int a[] = new int[n]; st = new StringTokenizer(br.readLine()); for (i = 0; i < n; i++) { a[i] = pi(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (i = 0; i < n; i++) { a[i] = pi(st.nextToken()) - a[i]; } Arrays.sort(a); i = 0; j = n - 1; int cnt = 0; while (i < j) { if (a[j] + a[i] >= 0) { cnt++; j--; i++; } else { i++; } } bw.write(is(cnt) + "\n"); } bw.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
256adf702d9f881f3d75ee844c61f9bc
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public final class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int i, j = n - 1, ans = 0; int[] x = new int[n]; int[] y = new int[n]; for(i = 0; i < n; i++) { x[i] = sc.nextInt(); } for(i = 0; i < n; i++) { y[i] = sc.nextInt(); y[i] -= x[i]; } Arrays.sort(y); i = 0; while(i < j) { if(y[i] + y[j] >= 0) { ans++; j--; } i++; } System.out.println(ans); } sc.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
3f8b6d7f85f41ca7c67ce71cc39935e3
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class Main{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0; i<t; i++){ int n = sc.nextInt(); int[] x = new int[n]; for(int j=0; j<n; j++){ x[j] = sc.nextInt(); } ArrayList<Integer> arr = new ArrayList<>(); for(int j=0; j<n; j++){ int z = sc.nextInt(); arr.add(z-x[j]); } Collections.sort(arr); int l = 0, r = n-1, ans = 0; while(l<r){ if(arr.get(l)+arr.get(r)>=0){ ans++; l++; r--; } else { l++; } } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
91ce0b381f12d9b8dbfa693c2e3b203e
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.lang.*; public class Solution { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t = sc.nextInt(); //sc.nextLine(); while(t-->0) { int n=sc.nextInt(); int[] cost=new int[n]; for (int i = 0; i <n ; i++) { cost[i]=sc.nextInt(); } int[] money=new int[n]; for (int i = 0; i <n ; i++) { money[i]=sc.nextInt(); } int[] diff=new int[n]; for (int i = 0; i <n ; i++) { diff[i]=money[i]-cost[i]; } int i=0,j=n-1; Arrays.sort(diff); int count=0; while (i < j) { if(diff[i]+diff[j]>=0) { count++; i++; j--; } else if(diff[i]<0) { i++; } if(diff[j]<0) { break; } } System.out.println(count); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
f4bf5762102319966c1456df67a42fed
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0) { int n = scn.nextInt(); int[] xarr = new int[n]; for(int i = 0 ; i < n ; i++) { xarr[i] = scn.nextInt(); } int[] yarr = new int[n]; for(int i = 0 ; i < n ; i++) { yarr[i] = scn.nextInt(); } int[] arr = new int[n]; for(int i = 0 ; i < n ; i++) { arr[i] = yarr[i] - xarr[i]; } Arrays.sort(arr); int i = 0 ; int j = n-1; int ans = 0; while(i < j) { int ith = arr[i]; int jth = arr[j]; if(ith + jth >= 0) { ans++; i++; j--; } else { i++; } } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
1b639a366e320d8c204b90e1e77befe5
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
//package random; import java.util.*; import java.io.*; public class CF { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(br.readLine()); while(q-->0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int[] a = new int[n]; int[] b = new int[n]; st = new StringTokenizer(br.readLine()); for (int i=0; i<n; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (int i=0; i<n; i++) { b[i] = Integer.parseInt(st.nextToken()); } int[] arr = new int[n]; for (int i=0; i<n; i++) { arr[i] = b[i] - a[i]; } for (int i=0; i<n; i++) { int t = (int)Math.random()*n; int temp = arr[t]; arr[t] = arr[i]; arr[i] = temp; } Arrays.sort(arr); int l = 0; int r = n-1; int ans = 0; while(l<r) { if (arr[r]+arr[l]>=0) { ans++; r--; l++; } else { l++; } } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
73d9c640246785e8c153c93ba1747ed5
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc=sc.nextInt(); while(tc-- > 0){ int N=sc.nextInt(); List<Student> students=new ArrayList<>(); for (int i = 0; i < N; i++) { Student s=new Student(); s.p=sc.nextInt(); students.add(s); } for (int i = 0; i < N; i++) { students.get(i).m=sc.nextInt(); students.get(i).diff=students.get(i).m-students.get(i).p; } Collections.sort(students); int groups=0; for(int st=0,end=N-1;st<end;){ if(students.get(st).m+students.get(end).m >= students.get(st).p+students.get(end).p){ st++;end--;groups++; }else{ st++; } } System.out.println(groups); } } } class Student implements Comparable<Student>{ int m,p,diff; @Override public int compareTo(Student o) { if(this.diff==o.diff){ return this.m-o.m; } return this.diff-o.diff; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
afcfef1b2a3187bf8191b79d137ef107
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static long mod = 1000000007; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while( t-- > 0) { int n=sc.nextInt(); int[]diffArray=new int[n]; int[]arr1=new int[n]; int[]arr2=new int[n]; for(int i=0;i<n;i++){ arr1[i]=sc.nextInt(); } for(int i=0;i<n;i++){ arr2[i]=sc.nextInt(); } for(int i=0;i<n;i++){ diffArray[i]=arr2[i]-arr1[i]; } mysort(diffArray); int i=0; int j=n-1; int ans=0; while(i<j){ int val1=diffArray[i]; int val2=diffArray[j]; if(val2+val1>=0){ ans++; i++; j--; }else if(val2+val1<0){ i++; } } out.println(ans); } out.flush(); } /* * WARNING -> DONT EVER USE ARRAYS.SORT() IN ANY WAY. * A B C are easy just dont give up , you can do it! * FIRST AND VERY IMP -> READ AND UNDERSTAND THE QUESTION VERY CAREFULLY. * WARNING -> DONT CODE BULLSHIT , ALWAYS CHECK THE LOGIC ON MULTIPLE TESTCASES AND EDGECASES BEFORE. * SECOND -> TRY TO FIND RELEVENT PATTERN SMARTLY. * WARNING -> IF YOU THINK YOUR SOLUION IS JUST DUMB DONT SUBMIT IT BEFORE RECHECKING ON YOUR END. */ public static boolean ifpowof2(long n ) { return ((n&(n-1)) == 0); } static boolean isprime(long x ) { if( x== 2) { return true; } if( x%2 == 0) { return false; } for( long i = 3 ;i*i <= x ;i+=2) { if( x%i == 0) { return false; } } return true; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } for (long p = 2; p * p <= n; p++) { if (prime[(int)p] == true) { for (long i = p * p; i <= n; i += p) prime[(int)i] = false; } } return prime; } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static void mySort(long[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); long loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long rightmostsetbit(long n) { return n&-n; } static long leftmostsetbit(long n) { long k = (long)(Math.log(n) / Math.log(2)); return k; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } static ArrayList<Integer> allfactors(int abs) { HashMap<Integer,Integer> hm = new HashMap<>(); ArrayList<Integer> rtrn = new ArrayList<>(); for( int i = 2 ;i*i <= abs; i++) { if( abs% i == 0) { hm.put( i , 0); hm.put(abs/i, 0); } } for( int x : hm.keySet()) { rtrn.add(x); } if( abs != 0) { rtrn.add(abs); } return rtrn; } public static int[][] prefixsum( int n , int m , int arr[][] ){ int prefixsum[][] = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1]; } } return prefixsum; } 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
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
51d9d92f8c9efcec92a4a5f930d48684
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)32768; static StringBuilder sb = new StringBuilder(); /* start */ public static void main(String [] args) { int testcases = 1; testcases = i(); // calc(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); int a[] = input(n); int b[] = input(n); int ii[] = new int[n]; for(int i=0;i<n;i++) { ii[i] = b[i]-a[i]; } Arrays.sort(ii); int cnt = 0; int i = 0,j = n-1; while(i<j) { if(ii[i]+ii[j]>=0) { cnt++; i++;j--; } else { i++; } } pl(cnt); } /* end */ 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; } } // print code start static void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static void pl() { out.println(""); } // print code end // static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first < p.first) return 1; else if (first > p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } // segment t start static long seg[] ; static void build(long a[], int v, int tl, int tr) { if (tl == tr) { seg[v] = a[tl]; } else { int tm = (tl + tr) / 2; build(a, v*2, tl, tm); build(a, v*2+1, tm+1, tr); seg[v] = Math.min(seg[v*2] , seg[v*2+1]); } } static long query(int v, int tl, int tr, int l, int r) { if (l > r || tr < tl) return Integer.MAX_VALUE; if (l == tl && r == tr) { return seg[v]; } int tm = (tl + tr) / 2; return (query(v*2, tl, tm, l, min(r, tm))+ query(v*2+1, tm+1, tr, max(l, tm+1), r)); } static void update(int v, int tl, int tr, int pos, long new_val) { if (tl == tr) { seg[v] = new_val; } else { int tm = (tl + tr) / 2; if (pos <= tm) update(v*2, tl, tm, pos, new_val); else update(v*2+1, tm+1, tr, pos, new_val); seg[v] = Math.min(seg[v*2] , seg[v*2+1]); } } // segment t end // }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9ea9b18667a7d81a70e10272ab1f5bdf
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n = scn.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i]=scn.nextInt(); } for(int i=0;i<n;i++){ a[i]=scn.nextInt()-a[i]; } Arrays.sort(a); int i=0,j=n-1,c=0; while(i<j){ if(a[i]+a[j]>=0){ c++; j--; } i++; } System.out.println(c); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
7ae846abb77fe5e873c542d0f68dd873
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
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 { static Scanner sc; public static void main (String[] args) throws java.lang.Exception { sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ solve(); } // your code goes here } static void solve(){ int n=sc.nextInt(); int dif[]=new int[n]; int a[] =new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } for(int i=0;i<n;i++){ int b=sc.nextInt(); dif[i]=b-a[i]; } Arrays.sort(dif); int ptr1=0; int ptr2=n-1; int move=0; while(ptr1<ptr2){ if(Math.abs(dif[ptr1])<=dif[ptr2]) { move++; ptr1++; ptr2--; } else{ ptr1++; } } System.out.println(move); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
10cf32a57f245bef87395d78c8e95cb9
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class c { static int n; static char[] arr; public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int ci = 0; ci < t; ci++) { int n = in.nextInt(); int[] tmp = new int[n]; for (int i = 0; i < n; i++) tmp[i] = in.nextInt(); ArrayList<Integer> arr = new ArrayList<>(n); for (int i = 0; i < n; i++) arr.add(tmp[i] - in.nextInt()); Collections.sort(arr); int ans = 0; ArrayDeque<Integer> q = new ArrayDeque<>(arr); while (q.size() >= 2) { if (q.peekFirst() + q.peekLast() <= 0) { q.pollFirst(); q.pollLast(); ans++; } else { q.pollLast(); } } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
871fdd7b3a56c5f38ddc52cc4b81e6fc
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class Solution{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; int c[] = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } for(int i=0;i<n;i++){ b[i] = sc.nextInt(); } for(int i=0;i<n;i++){ c[i] = b[i]-a[i]; } Arrays.sort(c); int res = 0; int i=0, j=n-1; while(i<j){ if(c[i]+c[j]>=0){ res++; i++; j--; } else{ i++; } } System.out.println(res); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
ace2f4e1027060edb7b0a35b97ff36dc
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class D { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { int n=input.scanInt(); int x[]=new int[n]; int y[]=new int[n]; int arr[]=new int[n]; for(int i=0;i<n;i++) { x[i]=input.scanInt(); } for(int i=0;i<n;i++) { y[i]=input.scanInt(); arr[i]=y[i]-x[i]; } sort(arr,0,n-1); int ll=0,rr=n-1; int cnt=0; while(ll<rr) { if(arr[ll]+arr[rr]<0) { ll++; continue; } cnt++; ll++; rr--; } ans.append(cnt+"\n"); } System.out.print(ans); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
da8ec0ae7edff12c8f192b319bb8db7b
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedReader; import java.io.BufferedWriter; public class D { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); int numCases = Integer.parseInt(br.readLine()); for (int i=0; i<numCases; i++) { solveProblem(br); } br.close(); } static void solveProblem(BufferedReader br) throws Exception { int n = Integer.parseInt(br.readLine()); int[] diffArr = new int[n]; int i=0; for (String s : br.readLine().split(" ")) { diffArr[i] = -Integer.parseInt(s); i++; } i = 0; for (String s : br.readLine().split(" ")) { diffArr[i] += Integer.parseInt(s); i++; } // printArr(diffArr); Arrays.sort(diffArr); // printArr(diffArr); int s_lp = 0; int lp = 0; int s_rp = n - 1; int rp = n - 1; int sum = 0; int num_groups = 0; sum += diffArr[lp]; sum += diffArr[rp]; while (lp < rp) { // System.out.print("Sum: " + sum + " | lp: " + lp + " | rp: " + rp); if (sum < 0) { sum -= diffArr[lp]; lp++; sum += diffArr[lp]; } else if (sum >= 0) { num_groups++; sum = 0; lp++; rp--; sum += diffArr[lp]; sum += diffArr[rp]; // System.out.print(" [[ Made group ]]"); } // System.out.println(); } System.out.println(num_groups); } static void printArr (int[] a) { for (int _a : a) { System.out.print(_a + " "); } System.out.println(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
214c42488956d3a42cefcdafa5df5071
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class Aa { public static int solve(int arr[],int n ) { Arrays.sort(arr); int max1 = arr[arr.length-1]; int max2 = arr[arr.length-2]; int min1 = arr[0] ; int min2 = arr[1] ; System.out.println(Arrays.toString(arr)); return max1+max2-min1-min2; } static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static void main(String args[]) { FastReader sc = new FastReader(); PrintWriter w = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int x[] = new int[n] ; int y[] = new int[n] ; for(int i = 0 ;i<n ;i++) { x[i] = sc.nextInt() ; } for(int i = 0 ;i<n ;i++) { y[i] = sc.nextInt() ; } int diff[] = new int[n] ; for(int i = 0 ; i<n ;i++) { diff[i] = y[i] - x[i] ; } Arrays.sort(diff); int i = 0 ; int j = n-1 ; int ans = 0 ; while(i<j) { if(diff[i] + diff[j] >=0) { ans++; i++ ; j--; } else i++; } w.println(ans); } w.close(); } 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
8b446940189b4f02eada3bc8122c68d9
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class CP { static Scanner s = new Scanner(System.in); static class pair{ long key; int count; pair(long key , int count){ this.count = count; this.key = key; } } static class sort implements Comparator<Integer>{ @Override public int compare(Integer o1, Integer o2) { if(Math.abs(o1) > Math.abs(o2)){ return 1; }else if(Math.abs(o1) < Math.abs(o2))return -1; return 0; } } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(new FileInputStream(s))); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } static private long gcd(long a , long b){ if(b == 0)return a; return gcd(b , a % b); } static private void printArrayList(ArrayList<Object> al){ String st = ""; for(Object i : al){ st += i + " "; } System.out.println(st); } static private long digitSum(long n){ long sum = 0; while(n != 0){ sum += n%10; n = n/10; } return sum; } static boolean checkPrime(int x){ for(int i = 2; i <= Math.sqrt(x);i++){ if(x % i == 0){ return false; } } return true; } static boolean checkPallindrome(String st){ int i = 0, j = st.length() - 1; while (i <= j){ if(st.charAt(i) != st.charAt(j))return false; i++; j--; } return true; } static boolean checkPowerOfTwo(int n){ final double v = Math.log(n) / Math.log(2); return (int)(Math.ceil(v)) == (int)(Math.floor(v)); } static ArrayList<Long> insert(long n) throws IOException { ArrayList<Long> al = new ArrayList<>(); for(long i =0 ; i < n; i++){ al.add(s.nextLong()); } return al; } static int debugger = 1; static void debug(){ System.out.println("Reached " + debugger++); } static String reverse(String st){ String ans = ""; for(int i = st.length() - 1; i >= 0; i--)ans += st.charAt(i); return ans; } static ArrayList<Long> sortDescendingOrder(ArrayList<Long> al){ Collections.sort(al); ArrayList<Long> ans = new ArrayList<>(); for(int i = al.size() - 1; i >= 0; i--){ ans.add(al.get(i)); } return ans; } static void dfs(int root , Map<Integer , ArrayList<Integer>> children){ if(children.size() == 0)return; for(int i = 0; i < children.size(); i++){ // do operations } } /* ArrayList<Integer> al = new ArrayList<>(); Map<Integer , ArrayList<Integer>> children = new HashMap<>(); for(int i = 0; i < n; i++){ al.add(s.nextInt()); children.put(i + 1 , new ArrayList<>()); } for(int i = 0; i < m; i++){ int x = s.nextInt(); int y = s.nextInt(); ArrayList<Integer> temp = children.get(x); temp.add(y); children.put(x , temp); } */ //funtions that you have ramakant // a sort (comparator) // gcd // digitSum // checkPrime (sqrt method) // checkPallindrome (two pointer) // checkPowerOfTwo (log method) // reverse a string // dfs of graph / tree // insert in arrayList // print ArrayList // sort ArrayList in descending order public static void main(String[] args) { try { StringBuffer sb = new StringBuffer(); ArrayList<Long> al; int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int a[] = new int[n]; int b[] = new int[n]; ArrayList<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++){ a[i] = s.nextInt(); } for(int i = 0; i < n; i++){ b[i] = s.nextInt(); } for(int i = 0; i < n; i++){ arr.add(b[i] - a[i]); } Collections.sort(arr); int left = 0, right = n - 1 ; int ans = 0; while(left < right){ if((arr.get(left) + arr.get(right)) >= 0){ ans++; left++; right--; }else{ left++; } } sb.append(ans + "\n"); } System.out.println(sb); }catch (Exception e){ e.printStackTrace(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
6fa7887bf24c13c93f2c093fd6e116e0
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import java.math.BigInteger; import java.util.stream.*; import java.util.ArrayList; import java.lang.*; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Collections; import java.util.Iterator; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class Main { static long mod = (long)(1e9) + 7; static int max_num = (int)1e5 + 5; public static void main(String[] args) { FastReader in = new FastReader(); FastWriter out = new FastWriter(); Scanner scn = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[n]; for(int i = 0 ; i < arr1.length ; i++){ arr1[i] = in.nextInt(); } ArrayList<Integer> list = new ArrayList<>(); for(int i = 0 ; i < arr2.length ; i++){ int k = in.nextInt(); arr2[i] = k - arr1[i]; list.add(arr2[i]); } Collections.sort(list); Collections.reverse(list); int pa = 0; int ri = arr1.length - 1; for(int i = 0 ; i < arr1.length ; i++){ while(ri>pa && list.get(pa)+list.get(ri) < 0) ri--; if(pa >= ri) break; pa++; ri--; } System.out.println(pa); } } public static void solve(String s) { } // agar 2d array ke ek coloumn ko sort karna h to -> Arrays.sort(arr, (a, b) -> a[0] - b[0]); // chote a ka ascii code 97 // 0 ka ascii code 48 // bade A ka ascii code 65 // int ko char array m karne h to -> char[] arr = (n+"").toCharArray(); // String ko integer banane ke lia -> Integer.parseInt("10"); public static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } public static boolean checkPalin(String str) { int i = 0; int j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) { return false; } i++; j--; } return true; } public static int maxInArray(int[] arr) { int max = arr[0]; for (int i = 0 ; i < arr.length ; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } static long square(long n) { if (n == 0) return 0; if (n < 0) n = -n; long x = n >> 1; if (n % 2 != 0) return ((square(x) << 2) + (x << 2) + 1); else return (square(x) << 2); } static int square(int n) { if (n == 0) return 0; if (n < 0) n = -n; int x = n >> 1; if (n % 2 != 0) return ((square(x) << 2) + (x << 2) + 1); else return (square(x) << 2); } public static void printString(String str) { System.out.println(str); } public static void printCharArray(char[] arr) { for (int i = 0 ; i < arr.length ; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static <K, V> K getKey(Map<K, V> map, V value) { for (Map.Entry<K, V> entry : map.entrySet()) { if (value.equals(entry.getValue())) { return entry.getKey(); } } return null; } public static void printArray(int[] arr) { for (int i = 0 ; i < arr.length ; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static void print2dArray(int[][] arr){ for(int i = 0 ; i < arr.length ; i++){ for(int j = 0 ; j < arr[0].length ; j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int highestPowerof2(int n) { int res = 0; for (int i = n; i >= 1; i--) { if ((i & (i - 1)) == 0) { res = i; break; } } return res; } public static int countElementInArray(int[] arr, int k) { int cc = 0; for (int i = 0 ; i < arr.length ; i++ ) { if (arr[i] == k) { cc++; } } return cc; } public static long integerFromBinary(String str) { long j = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '1') { j = j + (long)Math.pow(2, str.length() - 1 - i); } } return (long) j; } static int minInArray(int[] arr) { int min = arr[0]; for (int i = 0 ; i < arr.length ; i++) { if (arr[i] < min) { min = arr[i]; } } return min; } static void printN() { System.out.println("No"); } static void printY() { System.out.println("Yes"); } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for (int v : a) { c0[(v & 0xff) + 1]++; c1[(v >>> 8 & 0xff) + 1]++; c2[(v >>> 16 & 0xff) + 1]++; c3[(v >>> 24 ^ 0x80) + 1]++; } for (int i = 0; i < 0xff; i++) { c0[i + 1] += c0[i]; c1[i + 1] += c1[i]; c2[i + 1] += c2[i]; c3[i + 1] += c3[i]; } int[] t = new int[n]; for (int v : a)t[c0[v & 0xff]++] = v; for (int v : t)a[c1[v >>> 8 & 0xff]++] = v; for (int v : a)t[c2[v >>> 16 & 0xff]++] = v; for (int v : t)a[c3[v >>> 24 ^ 0x80]++] = v; return a; } static int[] EvenOddArragement(int nums[]) { int i1 = 0, i2 = nums.length - 1; while (i1 < i2) { while (nums[i1] % 2 == 0 && i1 < i2) { i1++; } while (nums[i2] % 2 != 0 && i2 > i1) { i2--; } int temp = nums[i1]; nums[i1] = nums[i2]; nums[i2] = temp; } return nums; } 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 (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().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static int DigitSum(int n) { int r = 0, sum = 0; while (n >= 0) { r = n % 10; sum = sum + r; n = n / 10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if (n == 0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length - 1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i = i; this.j = j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static class pair { int i, j; pair(int x, int y) { i = x; j = y; } } static int binary_search(int a[], int value) { int start = 0; int end = a.length - 1; int mid = start + (end - start) / 2; while (start <= end) { if (a[mid] == value) { return mid; } if (a[mid] > value) { end = mid - 1; } else { start = mid + 1; } mid = start + (end - start) / 2; } return -1; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
f25acf3325656179060d3c66f0d729db
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.util.*; public class Main{ static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); static StreamTokenizer st = new StreamTokenizer(bf); public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = I(); while(t-->0) { int n = I(); int x[] = new int [n]; int y[] = new int [n]; for (int i = 0 ; i < n ; i++) x[i] = I(); for (int i = 0 ; i < n ;i++) { y[i] = I(); y[i] -= x[i]; } Arrays.sort(y); int l = 0 , r = n-1,ans = 0; while(l<r) { if(y[l]+y[r]>=0) { l++;r--;ans++; } else l++; } pw.println(ans); } pw.flush(); } static int I() throws IOException { st.nextToken(); return (int)st.nval; } static long L() throws IOException { st.nextToken(); return (long)st.nval; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 11
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
afb0ca9fe97e680ddb3c710ac5fa3c15
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; public class Div820 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCases = sc.nextInt(); while (testCases -- > 0) { int n = sc.nextInt(); int[] x = new int[n]; int[] y = new int[n]; for(int i = 0; i < n; i++) x[i] = sc.nextInt(); for(int i = 0; i < n;i++) y[i] = sc.nextInt(); int ans = 0; Integer[] diff = new Integer[n]; for(int i = 0; i < n; i++) diff[i] = y[i] - x[i]; Arrays.sort(diff); int i = 0; int j = n - 1; while (i < j) { long sum = diff[i]; sum += diff[j]; if(sum < 0) { i++; } else { ans++; j--; i++; } } System.out.println(ans); } } // amortized time is like upper bound on average time of the operation. // amortized time is correct if summation of the actual operation up to m is less than total amortized time. }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9078e482b723e5a7d5d18fa645656625
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
// package com.company; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static int solve(int[] diff) { int n = diff.length; int groups = 0; int i = 0; int j = n - 1; while(i < j) { int rich = diff[j]; if(rich < 0) { break; } while(i < j) { if(rich + diff[i] >= 0) { break; } i++; } if(i < j) { groups++; } else { break; } i++; j--; } return groups; } 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[] spent = new int[n]; int[] wallet = new int[n]; for(int j = 0; j < n; j++) { spent[j] = sc.nextInt(); } for(int j = 0; j < n; j++) { wallet[j] = sc.nextInt(); } int[] diff = new int[n]; for(int j = 0; j < n; j++) { diff[j] = wallet[j] - spent[j]; } Arrays.sort(diff); System.out.println(solve(diff)); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
0f2da693dfbe97d1e5f149c025fd8fa8
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
// package com.company; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static int solve(int[] diff) { int n = diff.length; int groups = 0; int i = 0; int j = n - 1; Set<Integer> paired = new HashSet<>(); while(i < j) { int rich = diff[j]; if(rich < 0) { break; } while(i < j) { if(rich + diff[i] >= 0 && !paired.contains(i)) { paired.add(i); break; } i++; } if(i < j) { groups++; } else { i = 0; } j--; } return groups; } 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[] spent = new int[n]; int[] wallet = new int[n]; for(int j = 0; j < n; j++) { spent[j] = sc.nextInt(); } for(int j = 0; j < n; j++) { wallet[j] = sc.nextInt(); } int[] diff = new int[n]; for(int j = 0; j < n; j++) { diff[j] = wallet[j] - spent[j]; } Arrays.sort(diff); System.out.println(solve(diff)); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
6a1d48ae4ad30c167c28a1fd356c9fcc
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
// package com.company; import java.util.Arrays; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static int solve(int[] diff) { int n = diff.length; int groups = 0; int i = 0; int j = n - 1; Set<Integer> paired = new HashSet<>(); while(i < j) { int rich = diff[j]; if(rich < 0) { break; } while(i < j) { if(rich + diff[i] >= 0 && !paired.contains(i)) { paired.add(i); break; } i++; } if(i < j) { groups++; } else { i = 0; } j--; } return groups; } 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[] spent = new int[n]; int[] wallet = new int[n]; for(int j = 0; j < n; j++) { spent[j] = sc.nextInt(); } for(int j = 0; j < n; j++) { wallet[j] = sc.nextInt(); } int[] diff = new int[n]; for(int j = 0; j < n; j++) { diff[j] = wallet[j] - spent[j]; } Arrays.sort(diff); System.out.println(solve(diff)); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
387befc7caf93db00843f54645f3c805
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class CF_D { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int x[] = new int[n]; int y[] = new int[n]; for (int i = 0; i < n; i++) x[i] = in.nextInt(); for (int i = 0; i < n; i++) y[i] = in.nextInt(); int diff[] = new int[n]; for (int i = 0; i < n; i++) diff[i] = y[i] - x[i]; Arrays.sort(diff); int posStart = -1; for (int i = 0; i < n; i++) { if (diff[i] >= 0) { posStart = i; break; } } int negStart = posStart - 1; if (posStart < 0) { System.out.println(0); } else if (negStart < 0) { System.out.println(n / 2); } else { int prevCost = -1, days = 0; for (int i = posStart; i < n; i++) { if (prevCost >= 0) { days++; prevCost = -1; } else { prevCost = diff[i]; if (negStart >= 0 && prevCost + diff[negStart] >= 0) { days++; negStart--; prevCost = -1; } } } System.out.println(days); } } in.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
c6b14705ddd187d46dc10d308cd985c0
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
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[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); int neg=0,pos=0,zero=0; for(int i=0;i<n;i++){ a[i]=sc.nextInt()-a[i]; if(a[i]<0) neg++; else if(a[i]>0) pos++; } // System.out.println(Arrays.toString(a)); // System.out.println(neg+" "+pos); zero=n-pos-neg; int ans=(n-neg)/2,count=0,posleft=pos; Arrays.sort(a); int i=neg-1; int j=n-pos; while(i>=0 && j<n){//-8 -3 0 1 2 5 int num=a[i]+a[j]; if(num>=0) { i--; j++; posleft=n-j; } else if(num<0){ if(zero>0){ zero--; j++; posleft=n-j; } else{ if(j+1>=n) break; j=j+2; posleft=n-j; } } count++; } ans=Math.max(ans,count+(posleft+zero)/2); System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
bdcd1afb7562bf4101c8cf697876299b
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class FriendsAndTheRestaurant { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int[] X = io.nextIntArray(N); final int[] Y = io.nextIntArray(N); CountMapInt<Integer> counts = new CountMapInt<>(); for (int i = 0; i < N; ++i) { counts.increment(Y[i] - X[i], 1); } int days = 0; while (!counts.isEmpty()) { Integer richest = counts.lastKey(); counts.increment(richest, -1); Integer poorestPair = counts.ceilingKey(-richest); if (poorestPair == null) { break; } counts.increment(poorestPair, -1); ++days; } io.println(days); } /** * Counts the frequency of objects. * Change to extend TreeMap instead, if ordering of objects is required. */ public static class CountMapInt<T> extends TreeMap<T, Integer> { private static final long serialVersionUID = -1501598139835601959L; public int getCount(T k) { return getOrDefault(k, 0); } public void increment(T k, int v) { int next = getCount(k) + v; if (next == 0) { remove(k); } else { put(k, next); } } public static <T> CountMapInt<T> fromArray(T[] A) { CountMapInt<T> cm = new CountMapInt<>(); for (T x : A) { cm.increment(x, 1); } return cm; } } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 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 printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
b9534a4a34bf521f3e5bd4c311e49146
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class FriendsAndTheRestaurant { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int[] X = io.nextIntArray(N); final int[] Y = io.nextIntArray(N); CountMapInt<Integer> counts = new CountMapInt<>(); for (int i = 0; i < N; ++i) { counts.increment(Y[i] - X[i], 1); } int days = 0; while (!counts.isEmpty()) { Integer richest = counts.lastKey(); counts.increment(richest, -1); Integer poorestPair = counts.ceilingKey(-richest); if (poorestPair == null) { break; } counts.increment(poorestPair, -1); ++days; } io.println(days); } /** * Counts the frequency of objects. * Change to extend TreeMap instead, if ordering of objects is required. */ public static class CountMapInt<T> extends TreeMap<T, Integer> { private static final long serialVersionUID = -1501598139835601959L; public int getCount(T k) { return getOrDefault(k, 0); } public void increment(T k, int v) { int next = getCount(k) + v; if (next == 0) { remove(k); } else { put(k, next); } } public static <T> CountMapInt<T> fromArray(T[] A) { CountMapInt<T> cm = new CountMapInt<>(); for (T x : A) { cm.increment(x, 1); } return cm; } } /** * Counts the frequency of objects. * Change to extend TreeMap instead, if ordering of objects is required. */ public static class CountMapLong<T> extends TreeMap<T, Long> { private static final long serialVersionUID = -9079906779955923767L; public long getCount(T k) { return getOrDefault(k, 0L); } public void increment(T k, long v) { long next = getCount(k) + v; if (next == 0) { remove(k); } else { put(k, next); } } public static <T> CountMapLong<T> fromArray(T[] A) { CountMapLong<T> cm = new CountMapLong<>(); for (T x : A) { cm.increment(x, 1); } return cm; } } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 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 printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
31a2e709206cc1053ea592c84a8b86c1
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class TemplateTestCases { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int[] X = io.nextIntArray(N); final int[] Y = io.nextIntArray(N); CountMapInt<IntKey> counts = new CountMapInt<>(); for (int i = 0; i < N; ++i) { counts.increment(IntKey.of(Y[i] - X[i]), 1); } int days = 0; while (!counts.isEmpty()) { IntKey richestKey = counts.lastKey(); counts.increment(richestKey, -1); IntKey poorestPairKey = counts.ceilingKey(IntKey.of(-richestKey.value)); if (poorestPairKey == null) { break; } counts.increment(poorestPairKey, -1); ++days; } io.println(days); } /** * Counts the frequency of objects. * Change to extend TreeMap instead, if ordering of objects is required. */ public static class CountMapLong<T> extends TreeMap<T, Long> { private static final long serialVersionUID = -9079906779955923767L; public long getCount(T k) { return getOrDefault(k, 0L); } public void increment(T k, long v) { long next = getCount(k) + v; if (next == 0) { remove(k); } else { put(k, next); } } public static <T> CountMapLong<T> fromArray(T[] A) { CountMapLong<T> cm = new CountMapLong<>(); for (T x : A) { cm.increment(x, 1); } return cm; } } /** * Counts the frequency of objects. * Change to extend TreeMap instead, if ordering of objects is required. */ public static class CountMapInt<T> extends TreeMap<T, Integer> { private static final long serialVersionUID = -9079906779955923767L; public int getCount(T k) { return getOrDefault(k, 0); } public void increment(T k, int v) { int next = getCount(k) + v; if (next == 0) { remove(k); } else { put(k, next); } } public static <T> CountMapInt<T> fromArray(T[] A) { CountMapInt<T> cm = new CountMapInt<>(); for (T x : A) { cm.increment(x, 1); } return cm; } } public static class IntKey implements Comparable<IntKey> { public int value; private IntKey(int value) { this.value = value; } @Override public int hashCode() { return mulberry32(value + ADD_MIX); } @Override public boolean equals(Object obj) { IntKey other = (IntKey) obj; return value == other.value; } @Override public String toString() { return Integer.toString(value); } @Override public int compareTo(IntKey other) { return Integer.compare(value, other.value); } private static final int mulberry32(int x) { int z = (x + 0x6D2B79F5); z = (z ^ (z >>> 15)) * (z | 1); z ^= z + (z ^ (z >>> 7)) * (z | 61); return z ^ (z >>> 14); } public static IntKey of(int value) { if (CACHE_MIN <= value && value < CACHE_MAX) { return CACHE[value - CACHE_MIN]; } return new IntKey(value); } private static final int ADD_MIX = mulberry32((int) System.nanoTime()); private static final int CACHE_MIN = -256; private static final int CACHE_MAX = 256; private static final IntKey[] CACHE = new IntKey[CACHE_MAX - CACHE_MIN]; static { for (int i = CACHE_MIN; i < CACHE_MAX; ++i) { CACHE[i - CACHE_MIN] = new IntKey(i); } } } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 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 printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
9180dd4708d5280a1a0bf4786330e16f
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class TemplateTestCases { private static final int START_TEST_CASE = 1; public static void solveCase(FastIO io, int testCase) { final int N = io.nextInt(); final int[] X = io.nextIntArray(N); final int[] Y = io.nextIntArray(N); CountMap<IntKey> counts = new CountMap<>(); for (int i = 0; i < N; ++i) { counts.increment(IntKey.of(Y[i] - X[i]), 1); } int days = 0; while (!counts.isEmpty()) { IntKey richestKey = counts.lastKey(); counts.increment(richestKey, -1); IntKey poorestPairKey = counts.ceilingKey(IntKey.of(-richestKey.value)); if (poorestPairKey == null) { break; } counts.increment(poorestPairKey, -1); ++days; } io.println(days); } /** * Counts the frequency of objects. * Change to extend TreeMap instead, if ordering of objects is required. */ public static class CountMap<T> extends TreeMap<T, Long> { private static final long serialVersionUID = -9079906779955923767L; public long getCount(T k) { return getOrDefault(k, 0L); } public void increment(T k, long v) { long next = getCount(k) + v; if (next == 0) { remove(k); } else { put(k, next); } } public static <T> CountMap<T> fromArray(T[] A) { CountMap<T> cm = new CountMap<>(); for (T x : A) { cm.increment(x, 1); } return cm; } } public static class IntKey implements Comparable<IntKey> { public int value; private IntKey(int value) { this.value = value; } @Override public int hashCode() { return mulberry32(value + ADD_MIX); } @Override public boolean equals(Object obj) { IntKey other = (IntKey) obj; return value == other.value; } @Override public String toString() { return Integer.toString(value); } @Override public int compareTo(IntKey other) { return Integer.compare(value, other.value); } private static final int mulberry32(int x) { int z = (x + 0x6D2B79F5); z = (z ^ (z >>> 15)) * (z | 1); z ^= z + (z ^ (z >>> 7)) * (z | 61); return z ^ (z >>> 14); } public static IntKey of(int value) { if (CACHE_MIN <= value && value < CACHE_MAX) { return CACHE[value - CACHE_MIN]; } return new IntKey(value); } private static final int ADD_MIX = mulberry32((int) System.nanoTime()); private static final int CACHE_MIN = -256; private static final int CACHE_MAX = 256; private static final IntKey[] CACHE = new IntKey[CACHE_MAX - CACHE_MIN]; static { for (int i = CACHE_MIN; i < CACHE_MAX; ++i) { CACHE[i - CACHE_MIN] = new IntKey(i); } } } public static void solve(FastIO io) { final int T = io.nextInt(); for (int t = 0; t < T; ++t) { solveCase(io, START_TEST_CASE + t); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 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 printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
f4d3ea8de7e6838b3f9c91044956a912
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; //static String INPUT = "in.txt"; static String INPUT = ""; static String OUTPUT = ""; //global const //private final static long BASE = 998244353L; private final static int ALPHABET = (int)('z') - (int)('a') + 1; private final static long BASE = 1000000007l; private final static int INF_I = 1000000000; private final static long INF_L = 10000000000000000l; private final static int MAXN = 200100; private final static int MAXK = 31; private final static int[] DX = {-1,0,1,0}; private final static int[] DY = {0,1,0,-1}; private static boolean inside(int x, int y, int N, int M) { return (0<=x && x<N && 0<=y && y<M); } private static int toInt(char ch) { return (int)ch; } private static char toChar(int i) { return (char)i; } //global static void solve() { //int ntest = 1; int ntest = readInt(); for (int test=0;test<ntest;test++) { int N = readInt(); int[] A = new int[N]; for (int i=0; i<N; i++) A[i] = -readInt(); for (int i=0; i<N; i++) A[i] += readInt(); Arrays.sort(A); int ans=0; int l = -1, r = N; for (int i=0; i<N; i++) { if (A[i] < 0) l = i; if (A[i] > 0) { r = i; break; } } // 0 - l : <0 // l+1 - r-1 : = 0 // r - N-1 > 0 ans = Math.max(0, r-l-1)/2; if ((r-l-1)%2 == 1) l++; int cnt = 0; for (int i=0, j=N-1; i<=l && j>=r; i++) { if (A[i] + A[j] >= 0) { cnt++; j--; } } ans += cnt + Math.max(0, (N-r-cnt))/2; out.println(ans); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class MultiSet<T extends Comparable<T>> { private TreeSet<T> set; private Map<T, Integer> count; public MultiSet() { this.set = new TreeSet<>(); this.count = new HashMap<>(); } public void add(T x) { this.set.add(x); int o = this.count.getOrDefault(x, 0); this.count.put(x, o+1); } public void remove(T x) { int o = this.count.getOrDefault(x, 0); if (o==0) return; this.count.put(x, o-1); if (o==1) this.set.remove(x); } public void removeAll(T x) { int o = this.count.getOrDefault(x, 0); if (o==0) return; this.count.put(x, 0); this.set.remove(x); } public T first() { return this.set.first(); } public T last() { return this.set.last(); } public int getCount(T x) { return this.count.getOrDefault(x, 0); } public int size() { int res = 0; for (T x: this.set) res += this.count.get(x); return res; } } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(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 static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
a9f51a68bbce8f9207d54983fc431301
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(;t>0;t--) { int n=sc.nextInt(); int[] x=new int[n]; int[] y=new int[n]; for(int i=0;i<n;i++) { x[i]=sc.nextInt(); } for(int i=0;i<n;i++) { y[i]=sc.nextInt(); } PriorityQueue<Integer> morep=new PriorityQueue<Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { if(o1>o2) { return 1; } else if(o1==o2) { return 0; } return -1; } }); PriorityQueue<Integer> lessp=new PriorityQueue<Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { if(o1>o2) { return 1; } else if(o1==o2) { return 0; } return -1; } }); for(int i=0;i<n;i++) { int dis=x[i]-y[i]; if(dis>0) { lessp.add(dis); } else { morep.add(-dis); } } int ans=0; int count=0; while(!lessp.isEmpty()) { int tmp=lessp.poll(); while(!morep.isEmpty()) { int cur=morep.poll(); if(cur<tmp) { count++; } else { tmp=0; break; } } if(tmp>0) { break; } else { ans++; } } // if(!lessp.isEmpty()) { // System.out.println(0); // continue; // } count+=morep.size(); ans+=count/2; System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
8a31b729bf2a95cb224c57f6376d0ec8
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class A { static int mod = (int) (1e9 + 7); public static void main(String[] args) throws IOException { // Scanner sc = new Scanner(new File("second_hands_input.txt")); // PrintWriter pw = new PrintWriter("second_hands_output.txt"); Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); } Integer[] arr = new Integer[n]; ArrayList<Integer> pos = new ArrayList<>(); TreeMap<Integer, Integer> neg = new TreeMap<>(); for (int i = 0; i < n; i++) { arr[i] = b[i] - a[i]; if (arr[i] < 0) neg.put(-1 * arr[i], neg.getOrDefault(-1 * arr[i], 0) + 1); else pos.add(arr[i]); } Collections.sort(pos); int cnt = 0; int aa = 0; for (int i = 0; i < pos.size(); i++) { if (neg.lowerKey(pos.get(i)+1) != null) { Map.Entry<Integer, Integer> en = neg.lowerEntry(pos.get(i)+1); cnt++; if (en.getValue() == 1) neg.remove(en.getKey()); else neg.put(en.getKey(), en.getValue() - 1); } else { aa++; } // pw.println(cnt); // pw.println(pos); // pw.println(neg); } pw.println(cnt + (aa / 2)); } pw.flush(); } static class pair implements Comparable<pair> { int x, f; char y; public pair(int x, char y) { this.x = x; this.y = y; } @Override public String toString() { return x + " " + y; } @Override public int compareTo(pair o) { return y - o.y; } } static long modPow(long a, long e, int mod) // O(log e) { a %= mod; long res = 1; while (e > 0) { if ((e & 1) == 1) res = (res * 1l * a) % mod; a = (a * 1l * a) % mod; e >>= 1; } return res; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(File s) throws FileNotFoundException { br = new BufferedReader(new FileReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
fe6175bf9bdc5e7151196c68aaea189d
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.Scanner; public class problemD { public static int a[], s[], h[]; public static void swap(int a[], int i, int j) { int tg = a[i]; a[i] = a[j]; a[j] = tg; } public static void quickSort(int d, int c) { int i = d; int j = c; int mid = a[(d + c) / 2]; while (i <= j) { while (a[i] < mid) i++; while (a[j] > mid) j--; if (i <= j) { swap(a, i, j); i++; j--; } } if (d < j) quickSort(d, j); if (i < c) quickSort(i, c); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); s = new int[n + 5]; h = new int[n + 5]; a = new int[n + 5]; for (int i = 1; i <= n; i++) s[i] = sc.nextInt(); for (int i = 1; i <= n; i++) h[i] = sc.nextInt(); for (int i = 1; i <= n; i++) { a[i] = h[i] - s[i]; } quickSort(1, n); int dem = 0; System.out.println(); int j = n; int i = 1; while (i < j) { if (a[i] + a[j] >= 0) { i++; j--; dem++; } else i++; } System.out.println(dem); } sc.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
6f120d60035c10f41936c8f4c81bc1a7
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; public class exer { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.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 = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { FastScanner f = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int e = f.nextInt(); for (int i = 0; i < e; i++) { int n = f.nextInt(); int[] x = new int[n]; for (int j = 0; j < n; j++) { x[j] = f.nextInt(); } int[] y = new int[n]; for (int j = 0; j < n; j++) { y[j] = f.nextInt(); } int[] rel = new int[n]; for (int j = 0; j < n; j++) { rel[j] = y[j] - x[j]; } Arrays.sort(rel); int count = 0; int l = 0; int r = n-1; while(l<r){ if(rel[l]+rel[r]>=0){ count++; l++; r--; } else l++; } out.println(count); } out.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
60d45137b0b23994e844f2b477f15f3c
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Random; import java.util.StringTokenizer; public class exer { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.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 = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { InputReader f = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int e = f.nextInt(); for (int i = 0; i < e; i++) { int n = f.nextInt(); int[] x = new int[n]; for (int j = 0; j < n; j++) { x[j] = f.nextInt(); } int[] y = new int[n]; for (int j = 0; j < n; j++) { y[j] = f.nextInt(); } int[] rel = new int[n]; for (int j = 0; j < n; j++) { rel[j] = y[j] - x[j]; } Arrays.sort(rel); int count = 0; int l = 0; int r = n-1; while(l<r){ if(rel[l]+rel[r]>=0){ count++; l++; r--; } else l++; } out.println(count); } out.close(); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
84b5ce2fec24e31da8ce0a3ba4d9e663
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class D { 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader f = new FastReader(); int e = f.nextInt(); for (int i = 0; i < e; i++) { int n = f.nextInt(); int[] x = new int[n]; for (int j = 0; j < n; j++) { x[j] = f.nextInt(); } int[] y = new int[n]; for (int j = 0; j < n; j++) { y[j] = f.nextInt(); } int[] rel = new int[n]; for (int j = 0; j < n; j++) { rel[j] = y[j] - x[j]; } Arrays.sort(rel); int count = 0; int l = 0; int r = n-1; while(l<r){ if(rel[l]+rel[r]>=0){ count++; l++; r--; } else l++; } System.out.println(count); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
4e70614562cbb5b00a529a5cf284c753
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; 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.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class D_Round_820_Div3 { public static long MOD = 998244353; static int[] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); for (int z = 0; z < T; z++) { int n = in.nextInt(); PriorityQueue<Integer> pos = new PriorityQueue<>((a, b) -> Integer.compare(b, a)); PriorityQueue<Integer> neg = new PriorityQueue<>((a, b) -> Integer.compare(b, a)); int[][] data = new int[2][n]; for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { data[j][i] = in.nextInt(); } } // if (z == 23) { // System.out.println(Arrays.toString(data[0])); // System.out.println(Arrays.toString(data[1])); // } for (int i = 0; i < n; i++) { int v = data[1][i] - data[0][i]; if (v >= 0) { pos.add(v); continue; } neg.add(-v); } int re = 0; //System.out.println(pos); //System.out.println(neg); while (!pos.isEmpty()) { while (!neg.isEmpty() && neg.peek() > pos.peek()) { neg.poll(); } if (!neg.isEmpty() && neg.peek() <= pos.peek()) { re++; pos.poll(); neg.poll(); continue; } if (pos.size() > 1) { re++; pos.poll(); pos.poll(); continue; } break; } out.println(re); } out.close(); } static int cal(int index, int[] nxt, ArrayList<Integer>[] map) { nxt[index] = index; int max = 0; for (int i : map[index]) { int tmp = cal(i, nxt, map); if (tmp <= max) { continue; } max = tmp; nxt[index] = i; } return max + 1; } static int abs(int a) { return a < 0 ? -a : a; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } 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("input.txt")))); } 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
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
80c50755146b7854f8454479b76110e8
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class Balabizo { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-->0){ int n = sc.nextInt(); int[] x = sc.nextIntArray(n) , y = sc.nextIntArray(n); Integer[] a = new Integer[n]; for(int i=0 ;i<n ;i++) a[i] = y[i] - x[i]; Arrays.sort(a); int ans = 0 , i = 0 , j = n-1; while(i < j){ if(a[i] + a[j] >= 0){ ans++; i++; j--; } else i++; } pw.println(ans); } pw.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
451b6af1c1422e9d87f2b800188301c1
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.io.*; public class _1729D { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long[] x = new long[n]; for (int i = 0; i < n; i++) x[i] = sc.nextInt(); long[] y = new long[n]; for (int i = 0; i < n; i++) y[i] = sc.nextInt() - x[i]; Arrays.sort(y); int start=-2; int pos = -2, neg = -1; for (int i = 0; i < n; i++) { if (i == 0 && y[i] < 0) neg = i; else if (y[i] >=0) { if (start == -2) start = i-1; pos = i; } } int groups = 0; while (neg <= start && pos > start && y[pos] > 0) { if (neg != -1 && y[pos] + y[neg] >= 0) { groups++; pos--; } neg++; } if (pos > start) groups += (pos - start) / 2; System.out.println(groups); } } public static int gcd(int a, int b) { return (a == 0) ? b : gcd(b % a, a); } static int power(int x, int y) { int ans = 1; while (y > 0) { if ((y & 1) != 0) ans *= x; y = y >> 1; x *= x; } return ans; } public static List<Integer> sieve(int n) { List<Integer> primes = new LinkedList<>(); boolean[] composite = new boolean[n + 1]; for (int i = 2; i * i <= n; i++) for (int j = i * i; j <= n && !composite[i]; j += i) composite[j] = true; for (int j = 2; j < composite.length; j++) if (!composite[j]) primes.add(j); return primes; } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
3dee2551fe5ece4ab71e564c62ab9f7b
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class CodeF { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); //sc.nextLine(); while(t-- > 0) { int n = sc.nextInt(); int x[] = new int[n]; int y[] = new int[n]; for(int i = 0 ; i < n ; i++) { x[i] = sc.nextInt(); } for(int i = 0 ; i < n ; i++) { y[i] = sc.nextInt(); } int arr[] = new int[n]; for(int i = 0 ; i < n ; i++) { arr[i] = x[i]-y[i]; } Arrays.sort(arr); int i = 0 , j = n-1; int ans = 0; while(i < j) { if(arr[i]+arr[j] <= 0) { i++; j--; ans++; } else { j--; } } System.out.println(ans); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
37ad7f98ba67030c757c641970077895
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; public class HelloWorld { static long smallestDivisor(long n) { if (n % 2 == 0) return 2; for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } ArrayList <Integer> ans = new ArrayList<>(); public static int solve(int n, int x,int y){ if(x!=0 && y!=0){ return -1; } int p = Math.max(x,y); if(p==0){ return -1; } if((n-1)%p != 0){ return -1; } int condi= 0; int pi = p; int o = 1; for(int j = 0;j<n-1;j+=p){ for(int i = 0;i<p;i++){ System.out.print(o + " "); } if(condi == 0){ condi=1; o+=pi+1; } else{ o+=pi; } } System.out.println(); return 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long t = sc.nextLong(); while(t>0){ t--; int n = sc.nextInt(); int a1[] = new int[n]; int sum1=0; int sum2=0; int a2[] = new int[n]; int count=0; for(int i=0;i<n;i++){ a1[i] = sc.nextInt(); sum1+=a1[i]; } for(int i=0;i<n;i++){ a2[i] = sc.nextInt(); sum2+=a2[i]; } int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = a2[i] - a1[i]; } int max = 0; int j = n-1; Arrays.sort(arr); for(int i = 0;i<n;i++){ if(arr[i] == 0 ){ max++; } } int i=0; while(i<j){ if(arr[i]+arr[j] >= 0){ count++; j--; } i++; } if(count == 0 && sum2-sum1>0){ } System.out.println(count); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
ee193b6da09a0443bbf2cd9236c66827
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(in.readLine()); while(t-- > 0) { int n = Integer.parseInt(in.readLine()); String[] s0 = in.readLine().split(" "); String[] s1 = in.readLine().split(" "); int[] c = new int[n]; int[] a = new int[n]; int[] d = new int[n]; for (int i = 0; i < n; i++) { c[i] = Integer.parseInt(s0[i]); a[i] = Integer.parseInt(s1[i]); d[i] = a[i] - c[i]; } List<Integer> neg = new ArrayList<>(); List<Integer> pos = new ArrayList<>(); int groupCount = 0; for (int v : d) { if (v >= 0) { pos.add(v); } else { neg.add(v); } } Collections.sort(neg, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1 - o2; } }); Collections.sort(pos, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); //System.out.println(pos); //System.out.println(neg); int j = 0; int posCount = 0; for (int i = 0; i < pos.size(); i++) { int v = pos.get(i); boolean gc = false; while (j < neg.size()) { if (v + neg.get(j) >= 0) { gc = true; j++; break; } j++; } if (v >= 0 && gc) { posCount++; groupCount++; } } groupCount += ((pos.size() - posCount) / 2); out.write(groupCount+"\n"); out.flush(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
0d0dbf9faa667ba1df6ec76bf493830a
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.lang.reflect.Parameter; import java.math.BigInteger; import java.util.*; public class codeMaster { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); int[] spend = sc.nextIntArray(n); int[] have = sc.nextIntArray(n); Long[] diffs = new Long[n]; for(int i = 0; i<n; i++)diffs[i] = (long) (have[i] - spend[i]); Arrays.sort(diffs); int ans = 0; for(int i = n - 1, j = 0; i>=0; i--){ while(i > j && diffs[i] + diffs[j] < 0) j++; if(j >= i)break; ans++; j++; } pw.println(ans); } pw.flush(); } static class SegmentTree{ long[] tree; int N; public SegmentTree(long[] arr){ N = arr.length; tree = new long[2*N - 1]; build(tree, arr); } public void build(long[] tree, long[] arr){ for(int i = N-1, j = 0; i<tree.length; i++, j++)tree[i] = arr[j]; for(int i = tree.length - 1, j = i - 1, k = N-2; k>=0; i -= 2, j-= 2, k--){ tree[k] = tree[i] + tree[j]; } } public void update(int idx, int val){ tree[idx + N - 2] = val; boolean f = true; int i = idx + N - 2; int j = i - 1; if(i % 2 != 0){ i++; j++; } for(int k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); k>=0; ){ tree[k] = tree[i] + tree[j]; f = !f; i = k; j = k - 1; if(k % 2 != 0){ i++; j++; } k = (tree.length - N - 1) - ((tree.length - 1 - i)/2); } } } public static boolean isSorted(int[] arr){ boolean f = true; for(int i = 1; i<arr.length; i++){ if(arr[i] < arr[i - 1]){ f = false; break; } } return f; } public static int binary_Search(long key, long[] arr){ int low = 0; int high = arr.length; int mid = (low + high) / 2; while(low <= high){ mid = low + (high - low) / 2; if(arr[mid] == key)break; else if(arr[mid] > key) high = mid - 1; else low = mid + 1; } return mid; } public static int differences(int n, int test){ int changes = 0; while(test > 0){ if(test % 10 != n % 10)changes++; test/=10; n/=10; } return changes; } static int maxSubArraySum(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return start; } static int maxSubArraySum2(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return end; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static class Pair { //implements Comparable<Pair> char x; int y; public Pair(char x, int y){ this.x = x; this.y = y; } // public int compareTo(Pair x){ // if(this.x != x.x)return this.x - x.x; // else return this.y - x.y; // } public String toString(){ return "("+this.x + ", " + this.y + ")"; } } public static class Tuple implements Comparable<Tuple>{ int x; int y; int z; public Tuple(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } // public int compareTo(Tuple x){ // if(this.z == x.z){ // return (this.x + this.y) - (x.x + x.y); // } else return this.z - x.z; // } public int compareTo(Tuple x){ if(this.x == x.x){ return this.y - x.y; } else return this.x - x.x; } public String toString(){ return "("+this.x + ", " + this.y + "," + this.z + ")"; } } public static class Tuple2 implements Comparable<Tuple2>{ int x; int y; int z; public Tuple2(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } // public int compareTo(Tuple x){ // if(this.z == x.z){ // return (this.x + this.y) - (x.x + x.y); // } else return this.z - x.z; // } public int compareTo(Tuple2 x){ if(this.y == x.y){ return this.x - x.x; } else return this.y - x.y; } public String toString(){ return "("+this.x + ", " + this.y + "," + this.z + ")"; } } public static boolean isSubsequence(char[] arr, String s){ boolean ans = false; for(int i = 0, j = 0; i<arr.length; i++){ if(arr[i] == s.charAt(j)){ j++; } if(j == s.length()){ ans = true; break; } } return ans; } public static void sortIdx(long[]a,long[]idx) { mergesortidx(a, idx, 0, a.length-1); } static void mergesortidx(long[] arr,long[]idx,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesortidx(arr,idx,b,m); mergesortidx(arr,idx,m+1,e); mergeidx(arr,idx,b,m,e); } return; } static void mergeidx(long[] arr,long[]idx,int b,int m,int e) { int len1=m-b+1,len2=e-m; long[] l=new long[len1]; long[] lidx=new long[len1]; long[] r=new long[len2]; long[] ridx=new long[len2]; for(int i=0;i<len1;i++) { l[i]=arr[b+i]; lidx[i]=idx[b+i]; } for(int i=0;i<len2;i++) { r[i]=arr[m+1+i]; ridx[i]=idx[m+1+i]; } int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<=r[j]) { arr[k++]=l[i++]; idx[k-1]=lidx[i-1]; } else { arr[k++]=r[j++]; idx[k-1]=ridx[j-1]; } } while(i<len1) { idx[k]=lidx[i]; arr[k++]=l[i++]; } while(j<len2) { idx[k]=ridx[j]; arr[k++]=r[j++]; } return; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
e2c1c9686b4fd28a73ccc22792938f73
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class Sept13{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static int dp[][]; static void solve() { int n = sc.nextInt(); int arr[] = sc.readIntArray(n); int brr[] = sc.readIntArray(n); int crr[] = new int[n]; PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); for(int i = 0;i<n;i++){ crr[i] = brr[i]-arr[i]; pq.add(crr[i]); } int ans = 0; sort(crr); int i = 0;int j = n-1; while(i<j){ if(crr[i]+crr[j]>=0){ ans++; i++; j--; }else i++; } out.println(ans); } static void solve2(){ int n = sc.nextInt(); String str = sc.nextLine(); ArrayList<Character> ans = new ArrayList<>(); for(int i = 0;i<n;i++){ if(i+3<n && str.charAt(i+3)=='0') ans.add((char)('a'+Integer.parseInt(str.substring(i,i+1))-1)); else if(i+2<n && str.charAt(i+2)=='0'){ ans.add((char)('a'+Integer.parseInt(str.substring(i,i+2))-1)); i+=2; }else ans.add((char)('a'+Integer.parseInt(str.substring(i,i+1))-1)); } for(char c : ans) out.print(c); out.println(); } static void solve3() { String str = sc.nextLine(); int n = str.length(); ArrayList<Pas> ans = new ArrayList<>(); char a = str.charAt(0); char b = str.charAt(n-1); if(a>b){ char temp = a; a = b; b = temp; } for(int i = 1;i<n-1;i++){ if(str.charAt(i)>=a && str.charAt(i)<=b) ans.add(new Pas(str.charAt(i),i+1)); } Collections.sort(ans); out.println((b-a)+" "+(ans.size()+2)); Collections.sort(ans); out.print(1+" "); if(str.charAt(0)>str.charAt(n-1)){ for(int i = ans.size()-1;i>=0;i--) out.print(ans.get(i).ind+" "); }else{ for(int i = 0;i<ans.size();i++) out.print(ans.get(i).ind+" "); } out.print(n); out.println(); } static class Pas implements Comparable<Pas>{ char num; int ind; Pas(char n,int i){ num = n; ind = i; } public int compareTo(Pas p){ return this.num - p.num; } } static int size(int n){ int temp = n; int ans =0; while(temp>0){ temp/=10; ans++; } return ans; } static void solve4(){ } static void solve5(){ } static void solve6(){ } static void solve7(){ } static void reverse(int arr[]){ int i= 0; int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static void swap(char arr[][],int i,int j){ for(int k = j;k>0;k--){ if(arr[i][k]=='.'&& arr[i][k-1]=='*'){ char temp = arr[i][k]; arr[i][k] = arr[i][k-1]; arr[i][k-1] = temp; } } } static int search(int pre,int suf[],int i,int j){ while(i<=j){ int mid = (i+j)/2; if(suf[mid]==pre) return mid; else if(suf[mid]<pre) j = mid-1; else i = mid+1; } return Integer.MIN_VALUE; } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) 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; } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); // int t= 1; while(t-- >0){ solve(); // solve2(); // solve3(); // solve4(); // solve5(); // solve6(); // solve7(); // solve8(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } 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; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
09017349ff526cdf3535cd82d1fe4a96
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { 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(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static MyScanner s=new MyScanner(); public static int process(char a,char b){ return Math.abs(a-b); } public static void main(String[] args) { int K = s.nextInt(); while (K > 0) { K--; int n=s.nextInt(); int[]d=new int[n]; for(int i=0;i<n;i++)d[i]=s.nextInt(); for(int i=0;i<n;i++)d[i]=s.nextInt()-d[i]; Arrays.sort(d); int l=-1,r=n; boolean[]isvi=new boolean[n]; int cnt=0,ans=0; for(int i=0;i<n;i++)if(d[i]==0){ isvi[i]=true;cnt++; } for(int i=0;i<n;i++)if(d[i]<0)l=i; for(int i=n-1;i>=0;i--)if(d[i]>0)r=i; ans=(cnt/2); if(cnt%2==1&&r<n){ ans++;r++; } if(r>n){ System.out.println(ans); continue; } if(l==-1){ ans+=(((n-1)-(r)+1)/2); System.out.println(ans);continue; } //r是未被使用的,l是未被使用 int right=n-1,left=0; while(right>=r&&left<=l){ while(left<=l&&d[left]+d[right]<0){ left++; } if(left>l)break; ans++; right--; left++; } ans+=(right-r+1)>>1; System.out.println(ans); } } } //区间dp问题,看两端(求回文时看两端),(最后两端可以作为方向需要加状态) //看内部,dp[i][j] 与dp[i][k],dp[k+1][j]看顺序还是逆序(一般是逆序) //递推方程(最优子结构) /* 5 541408759 636014462 664058243 906405360 65998050 668310524 281481076 678509965 385782861 696386847 946646054 818865505 122407847 270493687 793205264 951022207 645270183 2434797 859142651 887597062 * */
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
fae1bc0187acbdf5d355f07d875dfc0c
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static Main2 admin = new Main2(); public static void main(String[] args) { admin.start(); } } class Main2 { //---------------------------------INPUT READER-----------------------------------------// public BufferedReader br; StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine());} catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long n) {int[]ret=new int[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;} long[] nal(long n) {long[]ret=new long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;} Integer[] nA(long n) {Integer[]ret=new Integer[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;} Long[] nAl(long n) {Long[]ret=new Long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;} //--------------------------------------PRINTER------------------------------------------// PrintWriter w; void p(int i) {w.println(i);} void p(long l) {w.println(l);} void p(double d) {w.println(d);} void p(String s) { w.println(s);} void pr(int i) {w.print(i);} void pr(long l) {w.print(l);} void pr(double d) {w.print(d);} void pr(String s) { w.print(s);} void pl() {w.println();} //--------------------------------------VARIABLES-----------------------------------------// long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; long mod = 1000000007; { w = new PrintWriter(System.out); br = new BufferedReader(new InputStreamReader(System.in)); try {if(new File(System.getProperty("user.dir")).getName().equals("Main_CP")) { w = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt"))); br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));} } catch (Exception ignore) { } } //----------------------START---------------------// void start() { int t = ni(); while(t-- > 0) solve(); w.close(); } void solve() { int n = ni(); int[] a = na(n), b = na(n); List<Integer> pos = new ArrayList<>(); List<Integer> neg = new ArrayList<>(); for(int i = 0; i < n; i++) { int curr = b[i] - a[i]; if(curr >= 0) pos.add(curr); else neg.add(curr); } int npos = pos.size(); int nneg = neg.size(); int best = npos / 2; int pt = 0; Collections.sort(pos); Collections.sort(neg, Collections.reverseOrder()); for(int i = 0; i < nneg; i++) { int need = -1 * neg.get(i); while(pt < npos && pos.get(pt) < need) pt++; if(pt < npos) best = Math.max(best, ((npos - (i + 1)) / 2) + (i + 1)); pt++; if(pt >= npos) break; } p(best); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
de598fa10d78aac7884bcbeb6ea1cef6
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.*; import java.nio.MappedByteBuffer; import java.util.*; import java.util.concurrent.RecursiveTask; public class Solution { InputStream is; FastWriter out; String INPUT = ""; public static void main(String[] args) throws Exception { new Solution().run();// Here run assign I/O calls solve also tell total time invested } void solve() { int t=ni(); while(t-->0) { int n=ni(), ans=0; int a[]=ni(n); int b[]=ni(n); PriorityQueue<Integer> pq1=new PriorityQueue<>(); PriorityQueue<Integer> pq2=new PriorityQueue<>((x,y)->y-x); for(int i=0;i<n;i++) { if(a[i]<=b[i]) pq1.add(a[i]-b[i]); else pq2.add(a[i]-b[i]); } while(!pq1.isEmpty() && !pq2.isEmpty()) { if(pq1.peek()+pq2.peek()<=0) { ans++; pq1.poll(); pq2.poll(); } else pq2.poll(); } ans+=pq1.size()/2; System.out.println(ans); } } void go() { } static boolean prime[]; static int count=0; void sieve(int n) { prime=new boolean[n+1]; Arrays.fill(prime, true); for(int i=2;i<=Math.sqrt(n);i++) if(prime[i]) for(int j=i*i;j<=n;j+=i) prime[j]=false; } boolean isPrime(int n) { for(int i=2;i<=Math.sqrt(n);i++) if(n%i==0) return false; return true; } long gcd(long a,long b) { if(a==0) return b; return gcd(b%a,a); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new FastWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] ni(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nl(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private Long[] nL(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private Integer[] nI(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private char[][] ns(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] ni(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = ni(m); return map; } private long[][] nl(int n, int m) { long[][] map = new long[n][]; for(int i = 0;i < n;i++)map[i] = nl(m); return map; } private Integer[][] nI(int n, int m) { Integer[][] map = new Integer[n][]; for(int i = 0;i < n;i++)map[i] = nI(m); return map; } private Long[][] nL(int n, int m) { Long[][] map = new Long[n][]; for(int i = 0;i < n;i++)map[i] = nL(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } public static class FastWriter { private static final int BUF_SIZE = 1<<13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter(){out = null;} public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if(ptr == BUF_SIZE)innerflush(); return this; } public FastWriter write(char c) { return write((byte)c); } public FastWriter write(char[] s) { for(char c : s){ buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte)c; if(ptr == BUF_SIZE)innerflush(); }); return this; } public FastWriter write(int x) { if(x == Integer.MIN_VALUE){ return write((long)x); } if(ptr + 12 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if(x == Long.MIN_VALUE){ return write("" + x); } if(ptr + 21 >= BUF_SIZE)innerflush(); if(x < 0){ write((byte)'-'); x = -x; } int d = countDigits(x); for(int i = ptr + d - 1;i >= ptr;i--){ buf[i] = (byte)('0'+x%10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if(x < 0){ write('-'); x = -x; } x += Math.pow(10, -precision)/2; // if(x < 0){ x = 0; } write((long)x).write("."); x -= (long)x; for(int i = 0;i < precision;i++){ x *= 10; write((char)('0'+(int)x)); x -= (int)x; } return this; } public FastWriter writeln(char c){ return write(c).writeln(); } public FastWriter writeln(int x){ return write(x).writeln(); } public FastWriter writeln(long x){ return write(x).writeln(); } public FastWriter writeln(double x, int precision){ return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for(int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for(long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte)'\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for(char[] line : map)write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c){ return writeln(c); } public FastWriter println(int x){ return writeln(x); } public FastWriter println(long x){ return writeln(x); } public FastWriter println(double x, int precision){ return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } public void trnz(int... o) { for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" "); System.out.println(); } // print ids which are 1 public void trt(long... o) { Queue<Integer> stands = new ArrayDeque<>(); for(int i = 0;i < o.length;i++){ for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x)); } System.out.println(stands); } public void tf(boolean... r) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } public void tf(boolean[]... b) { for(boolean[] r : b) { for(boolean x : r)System.out.print(x?'#':'.'); System.out.println(); } System.out.println(); } public void tf(long[]... b) { if(INPUT.length() != 0) { for (long[] r : b) { for (long x : r) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } System.out.println(); } } public void tf(long... b) { if(INPUT.length() != 0) { for (long x : b) { for (int i = 0; i < 64; i++) { System.out.print(x << ~i < 0 ? '#' : '.'); } } System.out.println(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output
PASSED
61751463eee35015694ab582eb01a697
train_110.jsonl
1662993300
A group of $$$n$$$ friends decide to go to a restaurant. Each of the friends plans to order meals for $$$x_i$$$ burles and has a total of $$$y_i$$$ burles ($$$1 \le i \le n$$$). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $$$x_i$$$ values in the group must not exceed the sum of $$$y_i$$$ values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be $$$n = 6$$$ friends for whom $$$x$$$ = [$$$8, 3, 9, 2, 4, 5$$$] and $$$y$$$ = [$$$5, 3, 1, 4, 5, 10$$$]. Then: first and sixth friends can go to the restaurant on the first day. They will spend $$$8+5=13$$$ burles at the restaurant, and their total budget is $$$5+10=15$$$ burles. Since $$$15 \ge 13$$$, they can actually form a group. friends with indices $$$2, 4, 5$$$ can form a second group. They will spend $$$3+2+4=9$$$ burles at the restaurant, and their total budget will be $$$3+4+5=12$$$ burles ($$$12 \ge 9$$$). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is $$$2$$$. Friends will visit the restaurant for a maximum of two days. Note that the $$$3$$$-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given $$$n$$$, $$$x$$$ and $$$y$$$.
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.PrintWriter; import java.util.*; public class T1729D { private static final T1729DFastScanner in = new T1729DFastScanner(); private static final PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) { int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int spends[] = new int[n]; for (int j = 0; j < n; j++) { spends[j] = in.nextInt(); } int money[] = new int[n]; for (int j = 0; j < n; j++) { money[j] = in.nextInt(); } List<Integer> positiveBalances = new LinkedList<>(); int neutralBalances = 0; int negativeBalances[] = new int[n]; int currentNegativeAdded = 0; for (int j = 0; j < n; j++) { int balance = (money[j] - spends[j]); if (balance > 0) { positiveBalances.add(balance); } else if (balance == 0) { neutralBalances++; } else { negativeBalances[currentNegativeAdded] = balance; currentNegativeAdded++; } } Collections.sort(positiveBalances, Collections.reverseOrder()); Arrays.sort(negativeBalances); int answer = 0; int nextPotentialNegative = 0; while (positiveBalances.size() > 0) { int positiveBalance = positiveBalances.get(0); int currentNegative = -1; while (negativeBalances[nextPotentialNegative] != 0 && nextPotentialNegative < n) { if (-negativeBalances[nextPotentialNegative] <= positiveBalance) { currentNegative = nextPotentialNegative; break; } else { nextPotentialNegative++; } } if (currentNegative == -1) { break; } answer++; nextPotentialNegative++; positiveBalances.remove(0); } answer += (positiveBalances.size() + neutralBalances) / 2; out.println(answer); } out.flush(); } } class Friend { int index; int balance; public Friend(int index, int balance) { this.index = index; this.balance = balance; } } class T1729DFastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public T1729DFastScanner() { in = new BufferedInputStream(System.in, BS); } public T1729DFastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n' && c != '\r') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["6\n\n6\n\n8 3 9 2 4 5\n\n5 3 1 4 5 10\n\n4\n\n1 2 3 4\n\n1 1 2 2\n\n3\n\n2 3 7\n\n1 3 10\n\n6\n\n2 3 6 9 5 7\n\n3 2 7 10 6 10\n\n6\n\n5 4 2 1 8 100\n\n1 1 1 1 1 200\n\n6\n\n1 4 1 2 4 2\n\n1 3 3 2 3 4"]
2 seconds
["2\n0\n1\n3\n1\n3"]
NoteThe first test case in explained in the problem statement.In the second test case, friends cannot form at least one group of two or more people.In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($$$1+3+10 \ge 2+3+7$$$). Note that they do not have the option of splitting into two groups.
Java 8
standard input
[ "greedy", "sortings", "two pointers" ]
7c8884b72dcc3c51e0696eec8d6aa8ef
The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of friends. The second line of each test case contains exactly $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$). The value of $$$x_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ plans to spend at the restaurant. The third line of each test case contains exactly $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$). The value $$$y_i$$$ corresponds to the number of burles that the friend numbered $$$i$$$ has. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$10^5$$$.
1,200
For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
standard output