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
5dce424e2763c3d7f8d2308a406f918d
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.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastScanner f = new FastScanner(); PrintWriter w = new PrintWriter(System.out); int T = f.nextInt(); while (T-- > 0) { int n = f.nextInt(); int[] a = new int[n + 1], b = new int[n + 1]; int[] c = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = f.nextInt(); for (int i = 1; i <= n; i++) { b[i] = f.nextInt(); c[i] = b[i] - a[i]; } Arrays.sort(c, 1, n + 1); int l = 1, r = n; int ans = 0; while (l < r) { if (c[l] + c[r] < 0) { l++; } else { l++; r--; ans++; } } w.println(ans); } w.flush(); } private static class FastScanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner() throws IOException { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9'); if (neg) return (short) -ret; return ret; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
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
ccf6d1f2028bee2e54d218b3d3d53dd1
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 { 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 FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } 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); } static int MOD = (int) (1e9 + 7); public static void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { System.out.print(i + " "); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) System.out.print(n); System.out.println(); } static boolean isPrime(int val) { for (int i = 2; i <= Math.sqrt(val); i++) { if (val % i == 0) { return false; } } return true; } static long pow(long x, long y, long n) { if (y == 0) { return 1; } long val = pow(x, y / 2, n); if (y % 2 == 1) { return (((val * val) % n) * x) % n; } else { return (val * val) % n; } } static int bsearch1(int[] b, int st, int end, int val) { int l=st, r=end; while(l<=r) { int mid = (l+r)/2; if(b[mid]>=val) { if(mid == 0 || b[mid-1]<val) { return mid; } else { r=mid-1; } } else { l=mid+1; } } return -1; } static boolean isEven(char[] arr, int i, int j) { for(int x=i;x<j;x+=2) { if(arr[x]!=arr[x+1]) { return false; } } return true; } public static void main(String[] args) throws IOException { StringBuilder ans = new StringBuilder(); int t = ri(); // int t=1; // int tc=1; while (t-- > 0) { int n=ri(); int[] x=rai(n); int[] y=rai(n); List<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { list.add((long) (y[i]-x[i])); } list.sort(Collections.reverseOrder()); long sum = 0; int size = n; for(int i=0;i<n;i++) { sum+=list.get(i); if(sum<0) { size = i; break; } } // System.out.println(size); // System.out.println(list); while(list.size()>size) { list.remove(list.size()-1); } // System.out.println(list); List<Long> pos = new ArrayList<>(); List<Long> neg = new ArrayList<>(); for(long i:list) { if(i>=0) { pos.add(i); } else { neg.add(i); } } neg.sort(new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return Long.compare(o1, o2); } }); // System.out.println(pos); // System.out.println(neg); int count = 0; int i=0, j=0; while(i<pos.size() && j<neg.size()) { if(pos.get(i)+neg.get(j)>=0) { count++; i++;j++; } else { j++; } } count+=Math.max(((pos.size()-i)/2), 0); ans.append(count).append("\n"); // int count = 0; // int ind =0; // for(int i=0;i<neg.size() && ind < pos.size();i++) // { // long val = neg.get(i); // int c = 0; // while(val<0 && ind<pos.size() && c<2) // { // val+=pos.get(ind++); // c++; // } //// System.out.println(ind); // if(c==2 || val>=0) // count++; // } // count+=(pos.size()-ind)/2; // ans.append(count).append("\n"); // int i=0, j=list.size()-1; // boolean[] vis = new boolean[list.size()]; // long lsum = 0, rsum = 0; // int posTaken = 0; // int count =0 ; // while(i<j) // { //// System.out.println(i+" "+j); // if(!vis[i]) { // lsum += list.get(i); // vis[i]=true; // posTaken++; // } // if(!vis[j]) { // rsum += list.get(j); // vis[j]=true; // } // // if(lsum+rsum>=0) // { // lsum=0; // rsum=0; // count++; // i++;j--; // } // else // { // i++; // } // } } out.print(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 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
4954366057861dee5e0d90e3b05cc7f2
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.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public class Practice1 { public static class UnionFind { private final int[] p; public UnionFind(int n) { p = new int[n+1]; for(int i=1;i<=n;i++) { p[i]=i; } } public int find(int x) { return x == 0 ? x : (p[x] = find(p[x])); } public void union(int x, int y) { x = find(x); y = find(y); if (x != y) { p[x] = y; } } } public static void printarr(int[] arr) { int n=arr.length; for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } System.out.println(); } // /** Code for Dijkstra's algorithm **/ public static class ListNode { int vertex, weight; ListNode(int v,int w) { vertex = v; weight = w; } int getVertex() { return vertex; } int getWeight() { return weight; } } public static int[] dijkstra( int V, HashMap<Integer,ArrayList<ListNode> > graph, int source) { int[] distance = new int[V]; for (int i = 0; i < V; i++) distance[i] = Integer.MAX_VALUE; distance[0] = 0; PriorityQueue<ListNode> pq = new PriorityQueue<>( (v1, v2) -> v1.getWeight() - v2.getWeight()); pq.add(new ListNode(source, 0)); while (pq.size() > 0) { ListNode current = pq.poll(); for (ListNode n : graph.get(current.getVertex())) { if (distance[current.getVertex()] + n.getWeight() < distance[n.getVertex()]) { distance[n.getVertex()] = n.getWeight() + distance[current.getVertex()]; pq.add(new ListNode( n.getVertex(), distance[n.getVertex()])); } } } // If you want to calculate distance from source to // a particular target, you can return // distance[target] return distance; } //Methos to return all divisor of a number static ArrayList<Long> allDivisors(long n) { ArrayList<Long> al=new ArrayList<>(); long i=2; while(i*i<=n) { if(n%i==0) al.add(i); if(n%i==0&&i*i!=n) al.add(n/i); i++; } return al; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long power(long N,long R) { long x=998244353; if(R==0) return 1; if(R==1) return N; long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p temp=(temp*temp)%x; if(R%2==0){ return temp%x; }else{ return (N*temp)%x; } } static int[] sort(int[] arr) { int n=arr.length; ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static int[] sortrev(int[] arr) { int n=arr.length; ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al,Collections.reverseOrder()); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static long[] sort(long[] arr) { long n=arr.length; ArrayList<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static ArrayList<Integer> allDivisors(int n) { ArrayList<Integer> al=new ArrayList<>(); int i=2; while(i*i<=n) { if(n%i==0) al.add(i); if(n%i==0&&i*i!=n) al.add(n/i); i++; } return al; } static boolean isPrime(int n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; int sqrtN = (int)Math.sqrt(n)+1; for(int i = 6; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static boolean check(String str,int ind,String tar) { int n1=str.length(); int n2=tar.length(); if(n1-ind<n2) return false; for(int i=0;i<n2;i++) { if(str.charAt(ind)!=tar.charAt(i)) return false; ind++; } return true; } public static void main (String[] args) { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // out.print(); //out.println(); FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long[] arr=new long[n]; long[] brr=new long[n]; ArrayList<Long> pos=new ArrayList<>(); ArrayList<Long> neg=new ArrayList<>(); for(int i=0;i<n;i++) { arr[i]=sc.nextLong(); } for(int i=0;i<n;i++) { brr[i]=sc.nextLong(); } for(int i=0;i<n;i++) { arr[i]=brr[i]-arr[i]; if(arr[i]>=0) { pos.add(arr[i]); }else { neg.add(arr[i]); } } Collections.sort(pos,Collections.reverseOrder()); Collections.sort(neg); int ind1=0,ind2=0,n1=pos.size(),n2=neg.size(); int count=0; while(ind1<n1) { long a=pos.get(ind1); while(ind2<n2&&a+neg.get(ind2)<0) ind2++; if(ind2<n2) { ind2++; ind1++; count++; }else { if(ind1<n1-1) count++; ind1+=2; } } out.println(count); } out.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 { 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 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
f4282606bf6da403fb2bd314e2e56db6
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 boolean isfile = false; static final String FileName = "input"; static PrintWriter out = new PrintWriter(System.out); static FastReader fr = new FastReader(); static int OO = (int) 1e9; public static void main(String[] args) throws FileNotFoundException { if (isfile) { fr = new FastReader(FileName + ".txt"); out = new PrintWriter(new File("output.txt")); } int tt = 1; tt = fr.nextInt(); while (tt-- > 0) { solve(); } out.close(); } public static void solve() { int n = fr.nextInt(); long total = 0; int[]arr1 = fr.readArray(n); int[]arr2 = fr.readArray(n); Integer[]arr3 = new Integer[n]; int pos =0; for (int i = 0; i < arr3.length; i++) { arr3[i]=arr2[i]-arr1[i]; if (arr3[i]>0) { pos++; } } Arrays.sort(arr3,Collections.reverseOrder()); for (int i = 0,j=n-1; i < j;) { if (arr3[i]+arr3[j]>=0) { total++; if (arr3[j]>0) { pos--; } if (arr3[i]>0) { pos--; } arr3[i]=0; arr3[j]=0; i++; j--; }else { j--; } } out.println(pos>1?total+(pos/2):total); } static class FastReader { BufferedReader br; StringTokenizer st = new StringTokenizer(""); public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } Long[] readArrayL(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } public static int lcm(int a, int b) { return a * b / gcd(a, b); } public static int gcd(int a, int b) { while (b != 0) { int m = a % b; a = b; b = m; } return a; } public static long factorial(int n) { if (n == 0) return 1; long res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } public static long nCr(int n, int r) { return factorial(n) / (factorial(r) * factorial(n - r)); } public static ArrayList<Integer> factors(long n) { ArrayList<Integer> list = new ArrayList<>(); for (int i = 2; i < n / i; i++) { while (n % i == 0) { if (!list.contains(i)) { list.add(i); n /= i; } } } if (n > 2) { if (!list.contains((int) n)) { list.add((int) n); } } return list; } public static int numOfPrimes(int n) { if (n < 2) { return 0; } boolean[] bool = new boolean[n + 1]; outer: for (int i = 2; i < bool.length / i; i++) { if (bool[i]) { continue outer; } for (int j = 2 * i; j < bool.length; j += i) { bool[j] = true; } } int counter = 0; for (int i = 0; i < bool.length; i++) { if (!bool[i]) { counter++; } } return counter; } public static int numOfDivisors(int x) { int to = 0; for (int i = 1; i <= Math.sqrt(x); i++) { if (x % i == 0) { if (x / i == i) { to++; } else { to += 2; } } } return to; } public static long fastPow(long a, long n, long mod) { long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } public static void sort2DGivenArray(int[][] arr, int colNum) { Arrays.sort(arr, (val1, val2) -> { if (val1[colNum] == val2[colNum]) { return 0; } else if (((Integer) (val1[colNum])).compareTo(((Integer) (val2[colNum]))) < 0) { return 1; } else { 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 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
1af8b9dd4b888c7555e56beaaaf18170
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 { public static void main(String[] args) { FastScanner sc = new FastScanner(); 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[] c = new long[n]; for(int i = 0; i < n; i++) c[i] = b[i] - a[i]; Arrays.sort(c); int l = 0; int r = n - 1; int ans = 0; while(l < r){ if(c[l] + c[r] >= 0) ans++; else { // advance l until we find a match for current r int tmp = l; while(tmp < r){ if(c[tmp] + c[r] >= 0) { ans++; break; } tmp++; } l = tmp; } l++; r--; } System.out.println(ans); } } static boolean isPS(long x){ long sr = (long) Math.sqrt(x); return ((sr * sr) == x); } static long getNextPS(long x){ if(isPS(x)) return x; long sq = (long) Math.sqrt(x); sq++; return sq * sq; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["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
2596ce3cb4ebd0fdde4ab10830838d94
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.PriorityQueue; import java.util.StringTokenizer; public class D_Friends_and_the_Restaurant { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); StringTokenizer st; int t = Integer.parseInt(br.readLine()); while(t-- > 0) { int n = Integer.parseInt(br.readLine()); int[] x = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) { x[i] = Integer.parseInt(st.nextToken()); } PriorityQueue<Integer> pos = new PriorityQueue<>(); PriorityQueue<Integer> neg = new PriorityQueue<>(); st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) { int y = Integer.parseInt(st.nextToken()); if(y - x[i] >= 0) pos.add(y-x[i]); else neg.add(x[i]-y); } int max = pos.size() / 2; int cnt = 0; while(true) { if(!pos.isEmpty() && !neg.isEmpty()) { int sum = pos.peek() - neg.peek(); if(sum >= 0) { pos.poll(); neg.poll(); cnt++; } else { if(pos.size() >= 2) { pos.poll(); pos.poll(); cnt++; } else { break; } } } else break; } cnt += pos.size() / 2; sb.append(Math.max(max, cnt)).append("\n"); } System.out.println(sb); } }
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
1c67aed9fbe721cd293706ab7b5701ed
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.util.StringTokenizer; public class Friends_and_the_Restaurant { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static PrintWriter out; public static void main (String[] args) throws java.lang.Exception { out = new PrintWriter(System.out); try { //Scanner obj = new Scanner (System.in); Reader obj = new Reader (); int t = obj.nextInt(); while (t > 0) { t--; int n = obj.nextInt(); //String str = obj.next(); long x[] = new long[n]; long y[] = new long[n]; ArrayList<Long> pos = new ArrayList<Long>(); ArrayList<Long> neg = new ArrayList<Long>(); int i, j, c = 0; for (i=0;i<n;i++) { x[i] = obj.nextInt(); } for (i=0;i<n;i++) { y[i] = obj.nextInt(); } for (i=0;i<n;i++) { long num = y[i] - x[i]; if (num >= 0) { pos.add (num); } else { neg.add (num); } } Collections.sort (pos); Collections.sort (neg); i = pos.size() - 1; j = 0; while (i >= 0 && j < neg.size()) { long sum = pos.get(i) + neg.get(j); if (sum >= 0) { c++; i--; j++; } else { j++; } } out.println (c + ((i+1) / 2)); } }catch(Exception e){ return; } out.flush(); 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
c50c49bda84cb140e14f8859cc10cfef
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 FriendsAndTheRestaurant { public static void main(String[] args)throws Exception{ FastReader f=new FastReader(System.in); int t=f.nextInt(); while (t-->0){ int n=f.nextInt(); int[] x=new int[n]; node[] arr=new node[n]; for (int i=0;i<n;i++){ x[i]=f.nextInt(); } for (int i=0;i<n;i++){ arr[i]=new node(x[i], f.nextInt()); } Arrays.sort(arr, (a,b)->a.diff-b.diff); int p1=0; int p2=n-1; int days=0; while (p1<p2){ if (arr[p1].diff+arr[p2].diff>=0){ days++; p1++; p2--; } else { p1++; } } System.out.println(days); } } } class node{ int x; int y; int diff; public node(int x, int y){ this.x=x; this.y=y; this.diff=y-x; } } class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public String next() { return nextString(); } 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 == ',') { c = read(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } 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 String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["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
2a7e936f86e19a117a9113a76bb18049
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 { public static void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); long a[] = new long[n]; long b[] = new long[n]; for (int i = 0; i < n; i++) { a[i]= input.nextLong(); } for (int i = 0; i < n; i++) { b[i]= input.nextLong(); } ArrayList<Long> smaller = new ArrayList<>(),bigger = new ArrayList<>(); for (int i = 0; i <n; i++) { if(b[i]-a[i]>=0) { bigger.add(b[i]-a[i]); } else { smaller.add(b[i]-a[i]); } } Collections.sort(smaller); Collections.sort(bigger,Collections.reverseOrder()); // System.out.println(smaller); // System.out.println(bigger); int j =0; int ans=0; for (int i = 0; i <smaller.size(); i++) { if(j<bigger.size()&&(bigger.get(j)+smaller.get(i))>=0) { ans++; j++; } } int remain = bigger.size()-j; ans+=(remain/2); System.out.println(ans); } } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { 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 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
02c11a8f13c5ea87b2f71f22d547c6e1
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 codechef { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); Scanner sc=new Scanner(System.in); int t=sc.nextInt(); // String testcases[]=br.readLine().split(" "); // int t=Integer.parseInt(testcases[0]); for(int i=0;i<t;i++) { int n=sc.nextInt(); long a[]=new long[n]; long b[]=new long[n]; for(int j=0;j<n;j++) a[j]=sc.nextLong(); for(int j=0;j<n;j++) b[j]=sc.nextLong(); ArrayList<Long> neg=new ArrayList<>(); ArrayList<Long> pos=new ArrayList<>(); int zeros=0; for(int j=0;j<n;j++) { if(b[j]-a[j]==0) zeros++; else if(b[j]-a[j]>0) pos.add(b[j]-a[j]); else neg.add(b[j]-a[j]); } Collections.sort(pos); Collections.sort(neg); int count=zeros/2; if(zeros%2!=0) { if(pos.size()>0) { count++; pos.remove(0); } } int x=0; int y=neg.size()-1; while(x<pos.size()&&y>=0) { long sum=pos.get(x)+neg.get(y); if(sum>=0) { x++; y--; count++; } else { x++; if(x<pos.size()) { x++; count++; } } } int left=pos.size()-x; count+=left/2; 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
6e1c7b3d7ff3ec4cc89426efb13a71f9
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.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.HashSet; import java.util.TreeSet; import java.util.HashMap; import java.util.Queue; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; import java.util.Collections; import java.util.Queue; import java.util.LinkedList; import java.util.Arrays; import java.math.BigInteger; public class Main{ private static FastReader fs; public static void main(String[] args) throws Exception{ fs = new FastReader(); int test = 1; test =fs.nextInt(); while(test-- > 0) solve(); } 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[] readArray(int n){ int ar[] = new int[n]; for(int i=0; i<n; i++) ar[i] = fs.nextInt(); return ar; } int gcd(int a, int b){ int result = Math.min(a, b); while (result > 0) { if (a % result == 0 && b % result == 0) { break; } result--; } return result; } } // use fs as scanner // snippets -> sieve, power, Node , lowerbound, upperbound, readarray public static void solve(){ int n = fs.nextInt(); int ar[] = new int[n]; for(int i=0;i<n;i++) ar[i] = fs.nextInt(); for(int i=0;i<n;i++) ar[i] = fs.nextInt() - ar[i]; Arrays.sort(ar); if(ar[0] >= 0){ System.out.println(n / 2); return ; } if(ar[n - 1] < 0){ System.out.println(0); return ; } int cnt = 0; int start = 0; int last = n-1; while(start <= last ){ if(ar[start] < 0 && ar[last] < 0){ break; } if(ar[start] < 0){ int h = Math.abs(ar[start]); if(h <= ar[last]){ start++;last--;cnt++; } else{ start++; } } else{ cnt += ((last - start + 1)/2); break; } } System.out.println(cnt); } } /* do something when you are stuck and be calm */
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
292ec4cd76146ebaa1c0974a1873d33b
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 code { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t= sc.nextInt(); while(t-->0){ solve(); } } static void solve() { 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(); } ArrayList<Integer> diff=new ArrayList<>(); for (int i = 0; i < n; i++) { int x=b[i]-a[i]; diff.add(x); } Collections.sort(diff,Collections.reverseOrder()); int i=0,j=diff.size()-1; int ans=0; while(i<j){ if(Math.abs(diff.get(j))>diff.get(i)) j--; 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 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
f5d02fe2aa8366c4997cb140cf7f57e8
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.PrintWriter; import java.lang.*; import java.io.IOException; import java.io.InputStreamReader; import java.net.SocketOption; import java.security.spec.RSAOtherPrimeInfo; import java.util.*; public class SEC { static long mod = (long) (1e9 + 7); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void 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); } } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long fact(long number) { if (number == 0 || number == 1) { return 1; } else { return number * fact(number - 1); } } public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> arr = new ArrayList<>(); long count = 0; while (n % 2 == 0) { arr.add(2l); n /= 2; } for (long i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { arr.add(i); n /= i; } } if (n > 2) arr.add(n); return arr; } static public long[] prime(long number) { long n = number; long count = 0; long even = 0; for (long i = 2; i <= n / i; i++) { while (n % i == 0) { if (i % 2 == 1) { count++; } else { even++; } n /= i; } } if (n > 1) { if (n % 2 == 1) { count++; } else { even++; } } return new long[]{even, count}; } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int search(ArrayList<Integer> arr, int tar) { int low = 0, hi = arr.size() - 1; int ans = -1; while (low <= hi) { int mid = (low + hi) / 2; if (arr.get(mid) > tar) { ans = arr.get(mid); hi = mid - 1; } else { low = mid + 1; } } return ans; } static long gcd(long a, long b) { // if b=0, a is the GCD if (b == 0) return a; else return gcd(b, a % b); } static long power(long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int get(long[] arr, long tar) { int ans = -1; int l = 0; int h = arr.length - 1; while (l <= h) { int mid = l + (h - l) / 2; if (arr[mid] <= tar) { ans = mid; l = mid + 1; } else { h = mid - 1; } } return ans; } static void print_Array(int[] arr) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } static boolean con(char ch) { if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') return true; return false; } static void swap(char[] ch, int i, int j) { char c = ch[i]; ch[i] = ch[j]; ch[j] = c; } static void swap(long[] ch, int i, int j) { long c = ch[i]; ch[i] = ch[j]; ch[j] = c; } /* 5 12 1 1 1 1 1 2 2 2 1 1 2 2 2 2 2 4 2 2 2 2 3 3 2 2 2 4 3 3 3 3 2 2 8 7 7 7 7 7 7 7 (()) (((()))) (()()) */ public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); 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 ans = 0; long[] ab = new long[n]; for(int i = 0; i < n; i++){ ab[i] = b[i] - a[i]; } sort(ab); int i= 0, j = n-1; while(i < j){ if(ab[i]+ab[j] >= 0){ i++; j--; ans++; }else{ 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 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
940e7dbadd38b57e183f5ecd44c48bc2
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 javax.print.attribute.IntegerSyntax; public class ProblemA { // Fast Reader Class public static class FastReader { // Reader object BufferedReader reader; // Constructor public FastReader(){ // Initialize the reader reader = new BufferedReader(new InputStreamReader(System.in)); } // String tokenizer StringTokenizer tokenizer; // Function to read integer public int ri() throws IOException{ return Integer.parseInt(reader.readLine()); } // Function to read a single long public long rl() throws IOException { return Long.parseLong(reader.readLine()); } // Function to read a array of numsInts integers in 1 line public int[] ria(int numInts) throws IOException { int[] nums = new int[numInts]; tokenizer = new StringTokenizer(reader.readLine()); // Input Numbers for (int i = 0; i < numInts; i++) { nums[i] = Integer.parseInt( tokenizer.nextToken()); } return nums; } // Function to read string public String rs() throws IOException{ return reader.readLine(); } } // Fast Writer Class public static class FastWriter { // Writer object BufferedWriter writer; // Constructor public FastWriter(){ // Initialize the writer writer = new BufferedWriter(new OutputStreamWriter(System.out)); } // Function to write single integer public void wi(int i)throws IOException{ writer.write(Integer.toString(i)); writer.newLine(); writer.flush(); } // Function to write a single long public void wl(long i) throws IOException{ writer.write(Long.toString(i)); writer.newLine(); writer.flush(); } // Function to write a Integer of array with spaces in 1 line public void wias(int[] nums) throws IOException{ for (int i = 0; i < nums.length; i++) { writer.write(nums[i] + " "); } writer.newLine(); writer.flush(); } // Function to write a Integer of array without spaces in 1 line public void wiaws(int[] nums) throws IOException { for (int i = 0; i < nums.length; i++) { writer.write(Integer.toString(nums[i])); } writer.newLine(); writer.flush(); } // Function to write a String public void ws(String s) throws IOException{ writer.write(s); writer.newLine(); writer.flush(); } } // Initialize a pair // Pair<Integer, Integer> x = new Pair<Integer, Integer>(1, 2); static class Pair implements Comparable<Pair> { int x; int y; Pair() { } Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if(this.y == o.y) { return o.x - this.x; } else { return o.y - this.y; } } } public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); FastWriter fw = new FastWriter(); int t = fr.ri(); while(t-- > 0) { int n = fr.ri(); String[] x = fr.rs().split(" "); String[] y = fr.rs().split(" "); int[] arr = new int[n]; for(int i = 0; i < n; i++) { int u = Integer.parseInt(x[i]); int v = Integer.parseInt(y[i]); arr[i] = v - u; } Arrays.sort(arr); int ans = 0; int i = 0; int j = n - 1; while(i < j) { int sum = arr[i] + arr[j]; if(sum < 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 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
5511fa0e246dbc3a16f6569ca36a863a
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 friends_rest { public static void main(String [] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t != 0) { int n=sc.nextInt(); int []arrx=new int[n]; int [] arry=new int[n]; for(int i=0;i<n;i++) arrx[i]=sc.nextInt(); for (int i=0;i<n;i++) arry[i]=sc.nextInt(); for(int i=0;i<n;i++) arrx[i]=arry[i]-arrx[i]; Arrays.sort(arrx); int i=0,j=n-1; int count=0; while(i<j){ if(arrx[i]+arrx[j]>=0 ) {count++;i++;j--; } else{ i++; } } System.out.println(count); t--; } } }
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
82d3ab68748ca9aa58445152f74875a2
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeMap; public class D { static final long mod = (long) 1e9 + 7l; private static void solve(int t){ int n =fs.nextInt(); int x[]= fs.readArray(n); int y[]= new int[n]; for (int i = 0; i < n; i++) { y[i] = fs.nextInt() - x[i]; } sortByCollections(y, false); int ans =0; for (int i = 0,j=n-1; i < j;) { if(y[i]+y[j]>=0){ ans++; j--; } i++; } out.println(ans); } static class Pair implements Comparable<Pair> { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if (a != p.a) return a - p.a; return p.b - b; } @Override public String toString() { return "Pair{" + "a=" + a + ", b=" + b + '}'; } } private static int[] sortByCollections(int[] arr, boolean reverse) { ArrayList<Integer> ls = new ArrayList<>(arr.length); for (int i = 0; i < arr.length; i++) { ls.add(arr[i]); } if(reverse)Collections.sort(ls, Comparator.reverseOrder()); else Collections.sort(ls); for (int i = 0; i < arr.length; i++) { arr[i] = ls.get(i); } return arr; } public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); int t = fs.nextInt(); for (int i = 1; i <= t; i++) solve(t); out.close(); // System.err.println( System.currentTimeMillis() - s + "ms" ); } static boolean DEBUG = true; static PrintWriter out; static FastScanner fs; static void trace(Object... o) { if (!DEBUG) return; System.err.println(Arrays.deepToString(o)); } static void pl(Object o) { out.println(o); } static void p(Object o) { out.print(o); } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void sieveOfEratosthenes(int n, int factors[]) { factors[1] = 1; for (int p = 2; p * p <= n; p++) { if (factors[p] == 0) { factors[p] = p; for (int i = p * p; i <= n; i += p) factors[i] = p; } } } static long mul(long a, long b) { return a * b % mod; } static long fact(int x) { long ans = 1; for (int i = 2; i <= x; i++) ans = mul(ans, i); return ans; } static long fastPow(long base, long exp) { if (exp == 0) return 1; long half = fastPow(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return fastPow(x, mod - 2); } static long nCk(int n, int k) { return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n - k)))); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class _Scanner { InputStream is; _Scanner(InputStream is) { this.is = is; } byte[] bb = new byte[1 << 15]; int k, l; byte getc() throws IOException { if (k >= l) { k = 0; l = is.read(bb); if (l < 0) return -1; } return bb[k++]; } byte skip() throws IOException { byte b; while ((b = getc()) <= 32) ; return b; } int nextInt() throws IOException { int n = 0; for (byte b = skip(); b > 32; b = getc()) n = n * 10 + b - '0'; return 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 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
d7d1506824ed65707e43c68f707ff626
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 FriendsandtheRestaurant { static class Pair implements Comparable<Pair> { int a,b; double c; Pair(int a,int b,double c) { this.a=a; this.b=b; this.c=c; } public int compareTo(Pair o) { double ans=this.c-o.c; if(ans<0) { return -1; } else if(ans>0) { return 1; } else { return 0; } } } public static void main(String args[]) { int i,j,k,t,n; Scanner sc=new Scanner(System.in); t=sc.nextInt(); while(t-->0) { n=sc.nextInt(); int x[]=new int[n]; int y[]=new int[n]; ArrayList<Pair>list=new ArrayList<>(); for(i=0;i<n;i++) { x[i]=sc.nextInt(); } for(i=0;i<n;i++) { y[i]=sc.nextInt(); list.add(new Pair(x[i],y[i],((double)(y[i]))-x[i])); } Collections.sort(list); /*for(i=0;i<n;i++) { System.out.println(list.get(i).a+" "+list.get(i).b+" "+list.get(i).c); }*/ i=n-1; j=0; int count=0; while(true) { if(i<=j) { break; } long tot=list.get(i).b; long need=list.get(i).a; while(true) { if(i<=j) { break; } else { if(tot+list.get(j).b>=need+list.get(j).a) { count++; j++; i--; break; } else { 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 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
40d2a05f9d9263c11f3db6383db9f893
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.Comparator; import java.util.Scanner; public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); in.nextLine(); int[][] arr = new int[n][3]; for (int i = 0; i < n; i++) { arr[i][0] = in.nextInt(); } for (int i = 0; i < n; i++) { arr[i][1] = in.nextInt(); } for (int i = 0; i < n; i++) { arr[i][2] = arr[i][1] - arr[i][0]; } Arrays.sort(arr, Comparator.comparingInt(a -> a[2])); int l = 0; int r = n - 1; int ans = 0; while (l < r) { if (arr[l][2] < 0) { if (arr[r][2] >= -arr[l][2]) { ans++; l++; r--; } else { l++; } }else { ans+=(r-l+1)/2; 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 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
0ea8c12ce06417c5e4a833f388f1e022
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.io.*; import java.util.*; public class Main { private static final int VOID = 0; private static void solve(int n, int[] a, int[] b){ PriorityQueue<Integer> pos = new PriorityQueue<>((x,y)->(y-x)); PriorityQueue<Integer> neg = new PriorityQueue<>((x,y)->(y-x)); int res = 0; int remain = 0; for(int i = 0; i < n; i++){ if(a[i] == b[i]){ remain++; } else if(a[i] > b[i]){ neg.add(Math.abs(a[i] - b[i])); } else{ pos.add(Math.abs(a[i] - b[i])); } } while(!pos.isEmpty() && !neg.isEmpty()) { int capacity = pos.peek(); while(!neg.isEmpty() && neg.peek() > capacity){ neg.poll(); } if(!neg.isEmpty() && neg.peek() <= capacity){ neg.poll(); pos.poll(); res++; }else { pos.poll(); remain++; } } remain += pos.size(); res += (remain / 2); out.println(res); } public static void main(String[] args){ MyScanner scanner = new MyScanner(); int numOfTests = scanner.nextInt(); for(int i = 1; i <= numOfTests; i++){ int n = scanner.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for(int j = 0; j < n; j++){ a[j] = scanner.nextInt(); } for(int j = 0; j < n; j++){ b[j] = scanner.nextInt(); } solve(n, a, b); } out.close(); } private static void printResult(int caseIdx, boolean res){ out.println("Case #"+caseIdx+": "+ (res?"YES":"NO")); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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
58222351f1021ee441c8f5a5e7bcb6be
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.TreeMap; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for (int tNum = 1; tNum <= t; tNum++) { int N = Integer.parseInt(br.readLine()); int[] nums = new int[N]; TreeMap<Integer, Integer> map = new TreeMap<>(); StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) { nums[i] -= Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) { int num = nums[i] + Integer.parseInt(st.nextToken()); map.put(num, map.getOrDefault(num, 0) + 1); } int count = 0; while(!map.isEmpty() && map.firstKey() < 0) { int minKey = map.firstKey(); if (map.ceilingKey(-minKey) == null) { map.remove(minKey); } else { int revKey = map.ceilingKey(-minKey); int countMin = map.get(minKey); int countRev = map.get(revKey); int min = Math.min(countMin, countRev); count += min; map.put(minKey, countMin - min); map.put(revKey, countRev - min); if (map.get(minKey) == 0) { map.remove(minKey); } if (map.get(revKey) == 0) { map.remove(revKey); } } } int elseCount = 0; for(int key : map.keySet()) { elseCount += map.get(key); } count += elseCount / 2; sb.append(count).append("\n"); } System.out.print(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 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
3778eea2d12b222210f3717f89932b5e
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 class1 { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long a[]=new long[n]; PriorityQueue<Long> pq1=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<Long> pq2=new PriorityQueue<>(); int i; long ans=0,c=0; for(i=0;i<n;i++) a[i]=sc.nextLong(); for(i=0;i<n;i++) { long x=sc.nextLong(); long val=x-a[i]; if(val==0) c++; else if(val>0) pq1.add(val); else pq2.add(val); } while(pq1.size()>0 && pq2.size()>0) { long val1=pq1.peek(); long val2=pq2.peek(); if(val1+val2>=0) { ans++; pq1.poll(); pq2.poll(); } else pq2.poll(); } int size=pq1.size(); ans+=(c+size)/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
88d442ca2b741243db6475390ce8e0f7
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 Codeforces; import java.io.*; import java.util.*; public class D { public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder op = new StringBuilder(); StringTokenizer stz; int T = Integer.parseInt(br.readLine()); while(T-- > 0) { int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; int[] b = new int[n]; int pos = 0; PriorityQueue<Integer> min = new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<Integer> max = new PriorityQueue<>(Collections.reverseOrder()); stz = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(stz.nextToken()); } stz = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { b[i] = Integer.parseInt(stz.nextToken()); if(b[i] >= a[i]) { ++pos; int diff = b[i] - a[i]; max.add(diff); } else { int diff = a[i] - b[i]; min.add(diff); } } // System.out.println("pos:" + pos); while ( !min.isEmpty() ) { if(max.isEmpty()) break; int mn = min.poll(); int mx = max.peek(); if(mx >= mn) { ++pos; max.poll(); } } int ans = pos >> 1; op.append(ans); op.append("\n"); } System.out.println(op); // END OF CODE } }
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
e225518030545067429018dc4e5f312a
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 codeforces1729D { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int numCases = in.nextInt(); while (numCases-->0) { int n = in.nextInt(); int [] delta = new int[n]; for (int i = 0; i<n;i++) { delta[i] -= in.nextInt(); } for (int i = 0; i<n;i++) { delta[i]+=in.nextInt(); } ArrayList<Integer> sort = new ArrayList<>(); for (int i = 0; i<n;i++) { sort.add(delta[i]); } Collections.sort(sort); for (int i = 0; i<n;i++) { delta[i] = sort.get(i); } int left = 0; int right = n-1; int count = 0; while (left<right) { while (delta[right]+delta[left]<0 && left<right) { left++; } if (delta[right]+delta[left]>=0 && left<right) { left++; } else break; count++; right--; } pw.println(count); } pw.close(); } private static class FastScanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; private FastScanner() throws IOException { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private short nextShort() throws IOException { short ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = (short) (ret * 10 + c - '0'); while ((c = read()) >= '0' && c <= '9'); if (neg) return (short) -ret; return ret; } private int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do ret = ret * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } private char nextChar() throws IOException { byte c = read(); while (c <= ' ') c = read(); return (char) c; } private String nextString() throws IOException { StringBuilder ret = new StringBuilder(); byte c = read(); while (c <= ' ') c = read(); do { ret.append((char) c); } while ((c = read()) > ' '); return ret.toString(); } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
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
a48c1b7aa536e8100a982843c953449a
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.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction; /* */ public class Codeforces { static long mod= 10000_0000_7; static class Node{ int val, ind; Node(int val, int ind){ this.val=val; this.ind=ind; } } public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); // DecimalFormat formatter= new DecimalFormat("#0.000000"); int t=fs.nextInt(); // int t=1; outer:for(int time=1;time<=t;time++) { int n=fs.nextInt(); int x[]=fs.readArray(n); int y[]=fs.readArray(n); int ans=0; List<Integer> list=new ArrayList<>(); for(int i=0;i<n;i++) list.add(y[i]-x[i]); Collections.sort(list); int l=0, r=list.size()-1; while(l<r) { if(list.get(l)+list.get(r)>=0) { ans++; l++; r--; } else l++; } out.println(ans); } out.close(); } static long pow(long a,long b) { long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } static void sort(long[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); long temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } 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(); } String nextLine() { String str=""; try { str= (br.readLine()); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readArrayL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["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
0786b68fd515ca1194b81f5132e48a35
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; public class MainA { 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) { int n = Integer.parseInt(br.readLine()); String[] x = br.readLine().split(" "); String[] y = br.readLine().split(" "); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = Integer.parseInt(y[i]) - Integer.parseInt(x[i]); } Arrays.sort(d); int l = 0; int r = n - 1; int ans = 0; while (l < r) { if (d[l] + d[r] >= 0) { ans++; r--; } 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 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
589477dd0a4a55d8f9e1fec4cc8e9579
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 javax.swing.text.StyledEditorKit.BoldAction; import java.io.*; import java.lang.*; public class main1{ FastScanner in; PrintWriter out; public static void main(String[] arg) { new main1().run(); } /////////////////////////////////////////////////////////////////// ///////////////// /MAIN PROGRAM STARTS HERE/////////////////////// ////////////////////////////////////////////////////////////////// public void solve() throws IOException { int tc=in.nextInt(); while(tc-->0) { int n=in.nextInt(); int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++)a[i]=in.nextInt(); for(int i=0;i<n;i++)b[i]=in.nextInt(); ArrayList<Integer> v=new ArrayList<>(); for(int i=0;i<n;i++) { v.add(b[i]-a[i]); } Collections.sort(v); Collections.reverse(v); int ans=0; int j=n-1; for(int i=0;i<n;i++) { while(j>i && v.get(i)+v.get(j)<0)j--; if(j<=i)break; ans++; j--; } out.println(ans); } } /////////////////////////////////////////////////////////////// //////////////////////MAIN PROGRAM ENDS HERE/////////////////// /////////////////////////////////////////////////////////////// public void run() { try { in = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader f) { br = new BufferedReader(f); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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; } } }
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
a09351df5fffffc21e92df238ab784fb
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{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(); Pair p[]=new Pair[n]; int a[]=sc.readArray(n),b[]=sc.readArray(n); for(int i=0;i<n;i++)p[i]=new Pair(a[i],b[i]); Arrays.sort(p); int cnt=0; for(int i=0,j=n-1;i<j;i++) { if(p[i].d+p[j].d>=0) { cnt++; j--; } } System.out.println(cnt); } } static class Pair implements Comparable<Pair>{ int a,b,d; Pair(int a,int b){ this.a=a; this.b=b; this.d=b-a; } public int compareTo(Pair o) { return this.d-o.d; } } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); 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 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
e20ee5afe471d4228306bcf6ed4a0945
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
// 48 to 57 import java.util.*; public class D1729 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int tt=0;tt<t;tt++) { int n = sc.nextInt(); long x[] = new long[n]; long y[] = new long[n]; long check[] = 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(); check[i] = y[i] - x[i]; } Arrays.sort(check); // System.out.println(Arrays.toString(check)); int f = 0; int l = n-1; int count =0; while(f<l) { if(check[f]+check[l]>=0) { count++; f++; l--; } else { f++; } } 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
2bed9bcfe85355f37235191156282736
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.List; import java.util.ArrayList; import java.util.Collections; public class MyClass { static Scanner in = new Scanner(System.in); static int testCases, n; static char s[]; static long a, b, c, mod = 998244353L; static long x[], y[]; static void solve() { long x = 1L, y = 0L, mid = 2L; for(int i = 4; i <= (int)a; i += 2) { mid = (mid * 2 * (i - 1)) / (i / 2); x = (mid / 2L) + y; y = mid - x - 1L; } System.out.println(x % mod + " " + y % mod + " 1"); } static char ch[] = {'-', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; static void B() { int zero = 0; for(char i : s) { if((i - '0') == 0 ) { ++zero; } } StringBuilder ans = new StringBuilder(); if(zero == 0) { for(char i : s) { ans.append(ch[i - '0']); } System.out.println(ans.toString()); return; } else { int i = n - 1; while(i >= 0) { if(s[i] != '0') { ans.append(ch[s[i] - '0']); --i; } else { String t = Integer.parseInt(((s[i - 2] - '0') + "") + ((s[i - 1] - '0') + "") ) + ""; ans.append(ch[Integer.parseInt(t)]); i -= 3; } } } ans.reverse(); System.out.println(ans.toString()); } static void C() { /* 1 2 3 4 5 -> index l o g i c -> elements l i g c -> letter visit sequence 1 4 3 5 -> jumping index total jump = 4 cost = (l - i) + (i - g) + (g - c) = |12 - 9| + |9 - 7| + |7 - 3| = 3 + 2 + 4 = 9 all character have to in range of the first and the last character of the serise */ List<Integer> list = new ArrayList<>(); int cost = 0; for(int i = 1; i < n - 1; ++i) { if(s[i] >= s[0] && s[i] <= s[n - 1]) { list.add(i); } else if(s[0] >= s[i] && s[n - 1] <= s[i]) { list.add(i); } } int moves = list.size() + 2; cost = Math.abs(s[0] - s[n - 1]); System.out.println(cost + " " + moves); if(s[0] < s[n - 1]) { Collections.sort(list, (x, y) -> s[x] - s[y]); } else { Collections.sort(list, (x, y) -> s[y] - s[x]); } System.out.print(1 + " "); for(int i : list) { System.out.print((i + 1) + " "); } System.out.print(n); System.out.println(); } static void D() { /* they order the metal of the x(i) burls but they have y(i) burls. solve an example: 1 2 3 4 5 6 a :-> 8 3 9 2 4 5 -> they order b :-> 5 3 1 4 5 10 -> has - - - - - 3 0 8 -2 -1 -5 (1, 6) -> order = 13 have = 15 (2, 4, 5) -> order = 9 have = 12 sort the list according to the diff of the list's have money and the need money in desending order. then count that two pair is until their summation in negative. */ tour t[] = new tour[n]; for(int i = 0; i < n; ++i) { t[i] = new tour(y[i], x[i]); } List<tour> list = new ArrayList<>(); for(tour i : t) { list.add(i); } Collections.sort(list); int index = 0; for(tour i : list) { t[index++] = i; } int j = n - 1, team = 0; for(int i = 0; i < n; ++i) { while(j > i && t[i].diff + t[j].diff < 0) { --j; } if(i >= j) { break; } team++; j--; } System.out.println(team); } public static void main(String args[]) { testCases = in.nextInt(); for(int t = 0; t < testCases; ++t) { n = in.nextInt(); x = new long[n]; y = new long[n]; for(int i = 0; i < n; ++i) { x[i] = in.nextLong(); } for(int i = 0; i < n; ++i) { y[i] = in.nextLong(); } D(); } } static class tour implements Comparable<tour> { long have, need, diff; public tour(long have, long need) { this.have = have; this.need = need; this.diff = this.have - this.need; } public int compareTo(tour t) { if(this.diff > t.diff) { return -1; } else if(this.diff < t.diff) { return 1; } return 0; } } }
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
ab46f2f56d8fa5279b5c1fb6b42cc74b
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 friends_and_restaurants { public static void main(String[]args){ Kattio io = new Kattio(); int t = io.nextInt(); for(int i = 0; i < t; i++) { int n = io.nextInt(); int[]arr1 = new int[n]; for(int j = 0; j < n; j++) { arr1[j] = io.nextInt(); } int[]arr2 = new int[n]; for(int j = 0; j < n; j++) { arr2[j] = io.nextInt(); } int[]differences = new int[n]; for(int j = 0; j < n; j++) { differences[j] = arr2[j]-arr1[j]; } Arrays.sort(differences); int start = 0; int end = n-1; int counter = 0; while(start < end) { if(differences[start] + differences[end] >= 0) { counter++; end--; } start++; } io.println(counter); } io.close(); } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName + ".out"); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(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 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
a5c6966931464671f54906e6465eca49
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.Arrays; import java.util.StringTokenizer; /** * @author Nervose.Wu * @date 2022/10/3 12:27 */ public final class S820D { public static void main(String[] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); for (int testNo = sc.nextInt(); testNo > 0; testNo--) { int len = sc.nextInt(); int[] cost = new int[len]; int[] budget = new int[len]; for (int i = 0; i < len; i++) { cost[i] = sc.nextInt(); } for (int i = 0; i < len; i++) { budget[i] = sc.nextInt(); } long[] distance=new long[len]; for (int i = 0; i < len; i++) { distance[i] = budget[i]-cost[i]; } Arrays.sort(distance); int start=0; int end=distance.length-1; int res=0; while (start<end){ if(distance[start]+distance[end]<0){ start++; }else { start++; end--; res++; } } out.println(res); } out.close(); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["6\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
1fbf64d37e6a12795ff7e41c8b0ed5eb
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
//created by Toufique on 19/09/2022 import java.io.*; import java.util.*; public class Div3_820D { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); long[] x = new long[n], y = new long[n]; for (int i = 0; i < n; i++) x[i] = in.nextLong(); for (int i = 0; i < n; i++) y[i] = in.nextLong(); pw.println(solve(x, y, n)); } pw.close(); } static int solve(long[] x, long[] y, int n) { int ans = 0; ArrayList<Long> ls = new ArrayList<>(); for (int i = 0; i < n; i++) { long v = y[i] - x[i]; ls.add(v); } Collections.sort(ls); int richIdx = n-1; int poorIdx = 0; while (poorIdx < richIdx) { long sum = ls.get(poorIdx++) + ls.get(richIdx); if (sum >= 0) { ans++; richIdx--; } } return ans; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
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
3e5704055cde87b4f33fe24a82cb675a
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 Sol { static Scanner rd = new Scanner(System.in); public static void main(String[] args) { int tt = rd.nextInt(); while (tt-- > 0) { new Solution().solve(); } } static class Solution { char[] s; int n; void solve() { // -3, 0, -8, 2, 1, 5 int n = rd.nextInt(); int[] a = new int[n], b = new int[n], c = new int[n]; for (int i = 0; i < n; i++) { a[i] = rd.nextInt(); } for (int i = 0; i < n; i++) { b[i] = rd.nextInt(); } for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } Arrays.sort(c); // Utils.printArray(c); int i = c.length - 1, j = 0, ans = 0; while (j < i) { if (c[i] + c[j] < 0) { j++; } else { ans++; i--; j++; } } System.out.println(ans); } void reverse(int[] a) { int i = 0, j = a.length - 1; while (i < j) { int t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } } } }
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
c8548090c031c5de7c36737193904f8b
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 testCases=sc.nextInt(); for(int i=1;i<=testCases;i++){ int n=sc.nextInt(); int []cost= new int[n]; int []budget= new int[n]; for(int j=0;j<n;j++){ cost[j]=sc.nextInt(); } for(int j=0;j<n;j++){ budget[j]=sc.nextInt(); budget[j]=budget[j]-cost[j]; } System.out.println(maximumGroups(budget)); } } public static int maximumGroups(int[]budget){ Arrays.sort(budget); int first=0,last=budget.length-1; int count=0; while (first<last){ if((budget[first]+budget[last])>=0){ first++; last--; count++; } else{ first++; } } return 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
2792be81de1bd8dc3350ae5a3c3bd312
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; public class Main{ public static void main(String[] args) throws IOException { //输入 StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); re.nextToken(); int t = (int)re.nval; while(t-- > 0) { re.nextToken(); int n = (int)re.nval; int[] x = new int[n]; int[] y = new int[n]; int[] sub = new int[n]; for (int i = 0; i < n; i++) { re.nextToken(); x[i] = (int)re.nval; } for (int i = 0; i < n; i++) { re.nextToken(); y[i] = (int)re.nval; } for (int i = 0; i < n; i++) { sub[i] = y[i] - x[i]; } int j = n - 1; int ans = 0; Arrays.sort(sub); for(int i = 0;i < n;i++) { if(j > i && sub[i] + sub[j] >= 0) { j--; ans++; } } 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
748fc5412c9671033144ea96794dcc27
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 C1729 { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-->0){ int n = scanner.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = scanner.nextInt(); for(int i=0;i<n;i++) arr[i] = scanner.nextInt() - arr[i]; Arrays.sort(arr); int l = 0, r = n-1, ans = 0; while(l<r){ if(arr[l] + arr[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 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
433b0f2de5693855485b9aeae7e93ca5
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.util.*; public class Main { static FastScanner fs; public static void main(String[] args) { fs=new FastScanner(); int t=fs.nextInt(); for( int i =0;i<t;i++){ solve(); } } public static void solve(){ int n=fs.nextInt(); int []v=new int [n]; for( int i=0;i<n;i++){ v[i]=fs.nextInt(); } for( int i=0;i<n;i++){ int x=fs.nextInt(); v[i]=x-v[i]; } // Arrays.sort(v); ArrayList<Integer>pos=new ArrayList<Integer>(); ArrayList<Integer>neg=new ArrayList<Integer>(); for( int i=0;i<n;i++){ if(v[i]>=0) pos.add(v[i]); else neg.add(v[i]); } Collections.sort(pos); Collections.sort(neg); int p=0; int ans=0; for( int i=pos.size()-1;i>=0;i--){ while(p<neg.size()&&neg.get(p)+pos.get(i)<0) p++; if(p<neg.size()){ ans++; p++; } else{ if(i!=0){ i--; ans++; } else break; } } System.out.println(ans); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } 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 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
2c36e46a420995a81593421f07634536
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.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); Integer[] a = new Integer[n]; Integer[] b = new Integer[n]; Integer[] c = new Integer[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int j = 0; j < c.length; j++) { a[j] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (int j = 0; j < c.length; j++) { b[j] = Integer.parseInt(st.nextToken()); } for (int j = 0; j < c.length; j++) { c[j] = b[j]-a[j]; } Arrays.sort(c); //pw.println(Arrays.toString(c)); int first = 0; int last = a.length-1; int cnt =0; while(first<last) { if(c[first]+c[last]>=0) { cnt++; first++; last--; } else first++; } pw.println(cnt); } 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 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
1de6ca9be9ad9428d1c16815d089eb3f
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 codeforces; import java.util.*; import java.io.*; public class Restaurant { public static void main(String[] args) { try { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int testcases = in.nextInt(); while (testcases-- > 0) { int n = in.nextInt(); long want[] = new long[n]; long budget[] = new long[n]; long temp[] = new long[n]; for (int i = 0; i < n; i++) want[i] = in.nextLong(); for (int i = 0; i < n; i++) budget[i] = in.nextLong(); for (int i = 0; i < n; i++) { temp[i] = budget[i] - want[i]; } Arrays.sort(temp); // for (long i : temp) // System.out.print(i + " "); // System.out.println(); int count = 0; int i = 0, j = temp.length - 1; while (i < j) { if (temp[i] + temp[j] >= 0) { count++; i++; j--; } else i++; } out.println(count); } out.close(); } catch (Exception e) { // TODO: handle exception return; } } 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static final Random random = new Random(); static final int mod = 1_000_000_007; static void ruffleSort(int[] a) { int n = a.length;// shuffle, then sort 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 long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } static long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } 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 printArray(int arr[]) { for (int i : arr) { System.out.print(i + " "); } System.out.println(); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
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
70fbc8e7d6a3c810ff54778d3bcd6748
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 Test { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int ii = 0; ii < t; ii++) { int n = in.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); } for(int i = 0; i < n; i++) { a[i] = in.nextInt() - a[i]; } Arrays.sort(a); int l = 0; int r = a.length - 1; int total = 0; while(l < r) { if(a[l] + a[r] >= 0) { total++; l++; r--; } else { l++; } } System.out.println(total); } } }
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
f9c81d93e263912338836a64ded23b83
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.BufferedWriter; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class D { static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static Scanner scanner=new Scanner(new BufferedInputStream(System.in)); static int[] sum = new int[100005]; public static void main(String[] args) { int num = scanner.nextInt(); while (num--!=0){ int n = scanner.nextInt(); int [] list = new int[n]; for (int i = 0; i < n; i++) { sum[i]=scanner.nextInt(); } for (int i = 0; i < n; i++) { sum[i]=scanner.nextInt()-sum[i]; } Arrays.sort(sum,0,n); int number=0; int j=0; int k=n-1; while (j<k){ int t=sum[j]; if ((t<0&&0-t<=sum[k])||t>=0){ number++; j++; k--; }else { j++; } } out.println(number); } out.flush(); } public static int erf(int left,int right,int num){ if (sum[right]<num){ return -1; } int mid; while (left<=right){ mid = (left+right)/2; if (sum[mid]<num){ left = mid + 1; }else { right= mid - 1; } } return left; } }
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
2f53cda21ae0d6def96890b92a09fd0b
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 c1729 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t,j,i,n,l,r,c; long a[],b[]; t=sc.nextInt(); for(j=1;j<=t;j++){ n=sc.nextInt(); a=new long[n]; b=new long[n]; for(i=0;i<n;i++) a[i]=sc.nextLong(); for(i=0;i<n;i++) b[i]=sc.nextLong(); PriorityQueue<Long> pq=new PriorityQueue<>(); for(i=0;i<n;i++) pq.add(b[i]-a[i]); long[] k=new long[n]; for(i=0;i<n;i++) k[i]=pq.poll(); l=0;r=n-1;c=0; while(l<r){ if(k[r]+k[l]>=0){ c++; l++; r--; } else l++; } 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 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
86d62b389e438ac40d4c22aa819a81e3
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 Main { static int getMaxDays(int[] spends, int[] budgets) { int[] remainings = new int[spends.length]; for(int i = 0; i < spends.length; i++) { remainings[i] = budgets[i] - spends[i]; } Arrays.sort(remainings); int count = 0; int first = 0; int last = remainings.length - 1; while(first < last) { if (remainings[last] + remainings[first] >= 0) { count++; first++; last--; } else { first++; } } return count; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-- > 0) { int n = scanner.nextInt(); int[] spends = new int[n]; for(int i = 0; i < n; i++) { spends[i] = scanner.nextInt(); } int[] budgets = new int[n]; for(int i = 0; i < n; i++) { budgets[i] = scanner.nextInt(); } System.out.println(getMaxDays(spends, budgets)); } } }
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
ed4d7c2943111f08fb7685ed43750db4
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 Codechef { public static void main (String[] args) throws java.lang.Exception { try{ FastReader read=new FastReader(); StringBuilder sb = new StringBuilder(); int t=read.nextInt(); for(int k=1;k<=t;k++) { //sb.append("Case #"+k+": "); int n = read.nextInt(); int x[] = new int[n]; int y[] = new int[n]; for(int i=0;i<n;i++) { x[i] = read.nextInt(); } for(int i=0;i<n;i++) { y[i] = read.nextInt(); } ArrayList<pair> al = new ArrayList<>(); for(int i=0;i<n;i++) { al.add(new pair(x[i],y[i],y[i]-x[i])); } Collections.sort(al); int ans = 0; int i=0,j=n-1; while(i<j) { if(al.get(j).C<0) break; if(Math.abs(al.get(i).C)<=Math.abs(al.get(j).C)) { ans++; i++; j--; } else { i++; } } sb.append(ans); sb.append('\n'); } System.out.println(sb); } catch(Exception e) {return; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(ArrayList<Long> A,char ch) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static void sort(ArrayList<Integer> A) { int n = A.size(); Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A.get(i); int randomPos = i + rnd.nextInt(n - i); A.set(i,A.get(randomPos)); A.set(randomPos,tmp); } Collections.sort(A); } static String sort(String s) { Character ch[] = new Character[s.length()]; for (int i = 0; i < s.length(); i++) { ch[i] = s.charAt(i); } Arrays.sort(ch); StringBuffer st = new StringBuffer(""); for (int i = 0; i < s.length(); i++) { st.append(ch[i]); } return st.toString(); } } class pair implements Comparable<pair> { int X,Y,C; pair(int s,int e,int c) { X=s; Y=e; C=c; } public int compareTo(pair p ) { return this.C-p.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 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
36122d8809900fd88ded0db04535b45f
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.io.PrintWriter; import java.util.*; public class a { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); int[] x = fs.readArray(n); int[] y = fs.readArray(n); int[] d = new int[n]; for (int i = 0; i < n; ++i) { d[i] = y[i] - x[i]; } sort(d); int res = 0; for (int l = 0, r = n-1; l < r;) { long sum = d[l] + d[r]; if (sum >= 0) { res++; l++;r--; } else { l++; } } out.println(res); } out.close(); } private static char getChar(int i) { return (char)('a' + i - 1); } static int lowerBound(List<Integer> a, int l, int r, int target) { while (l < r) { int mid = l + (r - l) / 2; if (target > a.get(mid)) { l = mid + 1; } else { r = mid; } } return l; } static int upperBound(List<Integer> a, int l, int r, int target) { while (l < r) { int mid = l + (r - l) / 2; if (a.get(mid) > target) { r = mid; } else { l = mid + 1; } } return l; } 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 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 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
3651f5adddd7204c8ea881441d9fe612
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 { static class Friend implements Comparable<Friend> { long moneyNeeded; long moneyHas; long difference; public Friend(long moneyNeeded, long moneyHas) { this.moneyNeeded = moneyNeeded; this.moneyHas = moneyHas; this.difference = moneyHas-moneyNeeded; } @Override public int compareTo(Friend friend) { if(difference == friend.difference) return 0; else if(difference > friend.difference) return -1; else return 1; } @Override public String toString() { return "Friend [moneyNeeded=" + moneyNeeded + ", moneyHas=" + moneyHas + ", difference=" + difference + "]"; } } public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); PrintWriter output = new PrintWriter(System.out); int t = scan.nextInt(); for(int tt = 0;tt<t;tt++) { int n = scan.nextInt(); int moneyHas[] = scan.readArray(n); int moneyNeeded[] = scan.readArray(n); ArrayList<Friend> friends = new ArrayList<>(); for(int i = 0;i<n;i++) { Friend friend = new Friend(moneyHas[i],moneyNeeded[i]); friends.add(friend); } Collections.sort(friends); int i = 0, j = n-1, groups = 0; while(i < j) { if(friends.get(i).difference + friends.get(j).difference >= 0) { groups++; i++; j--; } else { j--; } } output.println(groups); } output.flush(); } public static int[] sort(int arr[]) { List<Integer> list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i<list.size();i++) arr[i] = list.get(i); return arr; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } public static void printArray(int arr[]) { for(int i:arr) System.out.print(i+" "); System.out.println(); } 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()); } 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 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
a4cd64ae3816c6fa8f6b40f57286b843
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 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
72ad4f0d3b07430d36a8fb3aa32e0c08
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.BigInteger; import java.util.*; import static java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException { //Scanner in = new Scanner(new File("input.txt")); //PrintWriter out = new PrintWriter("output.txt"); int t = nextInt(); for (int k = 0; k < t; k++) { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = -nextInt(); } for (int i = 0; i < n; i++) { a[i] += nextInt(); } Arrays.sort(a); int r = n - 1; int l = 0; int ans = 0; while (l < r) { if (a[l] + a[r] >= 0) { ans++; l++; r--; } else l++; } out.println(ans); } out.close(); } static class Pair implements Comparable<Pair> { int x, y, z; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (x != o.x) return Integer.compare(x, o.x); return Integer.compare(y, o.y); } } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static String next() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static long nextLong() throws IOException { 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 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
85a06d870a7ff10a3f7339d79f5d9198
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 scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int x[] = new int[n]; for(int i=0; i<n; i++) { x[i] = scan.nextInt(); } for(int i=0; i<n; i++) { x[i] = scan.nextInt() - x[i]; } Arrays.sort(x); int left = 0, right = n-1; int res = 0; while(left < right) { if(x[left] + x[right] >= 0) { res++; left++; right--; } else { left++; } } 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 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
4e7ced6503995a702847ed1340b9722f
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 D1729 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); Integer[] diff = new Integer[N]; for (int n=0; n<N; n++) { diff[n] = -in.nextInt(); } for (int n=0; n<N; n++) { diff[n] += in.nextInt(); } Arrays.sort(diff); int answer = 0; int richIdx = N-1; int poorIdx = 0; while (poorIdx < richIdx) { int sum = diff[poorIdx++] + diff[richIdx]; if (sum >= 0) { answer++; richIdx--; } } System.out.println(answer); } } }
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
4f52f88be263b56b94566cfa72f012ca
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 faltu; import java.util.*; import java.util.Map.Entry; import java.io.BufferedReader; import java.io.BufferedWriter; 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.math.BigInteger; public class Main { // ***********************MATHS--STARTS************************************************* // private static ArrayList<Long> get_divisor(long x) { ArrayList<Long>a=new ArrayList<Long>(); for(long i=2;i*i<=x;i++) { if(x%i==0) { a.add((long) i); if(x/i!=i)a.add(x/i); } } return a; } private static int CntOfFactor(long x) { ArrayList<Long>a=new ArrayList<Long>(); for(long i=1;i*i<=x;i++) { if(x%i==0) { a.add((long) i); if(x/i!=i)a.add(x/i); } } return a.size(); } static long[] sieve; static long[] smallestPrime; public static void sieve() { int n=4000000+1; sieve=new long[n]; smallestPrime=new long[n]; sieve[0]=1; sieve[1]=1; for(int i=2;i<n;i++){ sieve[i]=i; smallestPrime[i]=i; } for(int i=2;i*i<n;i++){ if(sieve[i]==i){ for(int j=i*i;j<n;j+=i){ if(sieve[j]==j)sieve[j]=1; if(smallestPrime[j]==j||smallestPrime[j]>i)smallestPrime[j]=i; } } } } static long nCr(long n,long r,long MOD) { computeFact(n, MOD); if(n<r)return 0; if(r==0)return 1; return fact[(int) n]*mod_inv(fact[(int) r],MOD)%MOD*mod_inv(fact[(int) (n-r)],MOD)%MOD; } static long[]fact; static void computeFact(long n,long MOD) { fact=new long[(int)n+1]; fact[0]=1; for(int i=1;i<=n;i++)fact[i]=(fact[i-1]*i%MOD)%MOD; } static long bin_expo(long a,long b,long MOD) { if(b == 0)return 1; long ans = bin_expo(a,b/2,MOD); ans = (ans*ans)%MOD; if(b % 2!=0){ ans = (ans*a)%MOD; } return ans%MOD; } static int ceil(int x, int y) {return (x % y == 0 ? x / y : (x / y + 1));} static long ceil(long x, long y) {return (x % y == 0 ? x / y : (x / y + 1));} static long mod_add(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} static long mod_mul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} static long mod_sub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} static long mod_inv(long n,long p) {return bin_expo(n,p-2,p);} static long gcd(long a, long b){if (a == 0) {return b;}return gcd(b % a, a); } static int gcd(int a, int 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 min(long x,long y) {return Math.min(x, y);}static long max(long x,long y) {return Math.max(x, y);} static int min(int x,int y) {return Math.min(x, y);}static int max(int x,int y) {return Math.max(x, y);} static ArrayList<String>powof2s; static void powof2S() { long i=1; while(i<(long)2e18) { powof2s.add(String.valueOf(i)); i*=2; } } static long power(long a, long b){ a %=MOD;long out = 1; while (b > 0) { if((b&1)!=0)out = out * a % MOD; a = a * a % MOD; b >>= 1; a*=a; } return out; } static boolean coprime(int a, long l){return (gcd(a, l) == 1);} // ****************************MATHS-ENDS***************************************************** // ***********************BINARY-SEARCH STARTS*********************************************** public static int upperBound(long[] arr, long m, int l, int r) { while(l<=r) { int mid=(l+r)/2; if(arr[mid]<=m) l=mid+1; else r=mid-1; } return l; } public static int lowerBound(long[] a, long m, int l, int r) { while(l<=r) { int mid=(l+r)/2; if(a[mid]<m) l=mid+1; else r=mid-1; } return l; } public static int lowerBound(ArrayList<Integer> ar,int k){ int s=0,e=ar.size(); 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; } public static int upperBound(ArrayList<Integer> ar,int k){ int s=0,e=ar.size(); 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; } public static long getClosest(long val1, long val2,long target){if (target - val1 >= val2 - target)return val2; else return val1;} static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a){ int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } int ceilIndex(int input[], int T[], int end, int s){ int start = 0; int middle; int len = end; while(start <= end){ middle = (start + end)/2; if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){ return middle+1; }else if(input[T[middle]] < s){ start = middle+1; }else{ end = middle-1; } } return -1; } static int lowerLimitBinarySearch(ArrayList<Long> v,long k) { int n =v.size(); int first = 0,second = n; while(first <second) { int mid = first + (second-first)/2; if(v.get(mid) > k) { second = mid; }else { first = mid+1; } } if(first < n && v.get(first) < k) { first++; } return first; //1 index } public static int searchindex(long arr[], long t){int index = Arrays.binarySearch(arr, t);return (index < 0) ? -1 : index;} public static long[] sort(long[] a) {ArrayList<Long> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;} public static int[] sort(int[] a) {ArrayList<Integer> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;} // *******************************BINARY-SEARCH ENDS*********************************************** // *********************************GRAPHS-STARTS**************************************************** // *******----SEGMENT TREE IMPLEMENT---***** // -------------START--------------- void buildTree (int[] arr,int[] tree,int start,int end,int treeNode){ if(start==end){ tree[treeNode]=arr[start]; return; } buildTree(arr,tree,start,end,2*treeNode); buildTree(arr,tree,start,end,2*treeNode+1); tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1]; } void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value){ if(start==end){ arr[idx]=value; tree[treeNode]=value; return; } int mid=(start+end)/2; if(idx>mid)updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value); else updateTree(arr,tree,start,mid,2*treeNode,idx,value); tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1]; } long query(int[]arr,int[]tree,int start,int end,int treeNode,int qleft,int qright) { if(start>=qleft&&end<=qright)return tree[treeNode]; if(start>qright||end<qleft)return 0; int mid=(start+end)/2; long valLeft=query(arr,tree,start,mid-1,treeNode*2,qleft,qright); long valRight=query(arr,tree,mid+1,end,treeNode*2+1,qleft,qright); return valLeft+valRight; } // -------------ENDS--------------- //***********************DSU IMPLEMENT START************************* static int parent[]; static int rank[]; static int[]Size; static void makeSet(int n){ parent=new int[n]; rank=new int[n]; Size=new int[n]; for(int i=0;i<n;i++){ parent[i]=i; rank[i]=0; Size[i]=1; } } static void union(int u,int v){ u=findpar(u); v=findpar(v); if(u==v)return; if(rank[u]<rank[v]) { parent[u]=v; Size[v]+=Size[u]; } else if(rank[v]<rank[u]) { parent[v]=u; Size[u]+=Size[v]; } else{ parent[v]=u; rank[u]++; Size[u]+=Size[v]; } } private static int findpar(int node){ if(node==parent[node])return node; return parent[node]=findpar(parent[node]); } // *********************DSU IMPLEMENT ENDS************************* // ****__________PRIMS ALGO______________________**** private static int prim(ArrayList<node>[] adj,int N,int node) { int key[] = new int[N+1]; int parent[] = new int[N+1]; boolean mstSet[] = new boolean[N+1]; for(int i = 0;i<N;i++) { key[i] = 100000000; mstSet[i] = false; } PriorityQueue<node> pq = new PriorityQueue<node>(N, new node()); key[node] = 0; parent[node] = -1; pq.add(new node( node,key[node])); for(int i = 0;i<N-1;i++) { int u = pq.poll().getV(); mstSet[u] = true; for(node it: adj[u]) { if(mstSet[it.getV()] == false && it.getW() < key[it.getV()]) { parent[it.getV()] = u; key[it.getV()] = (int) it.getW(); pq.add(new node(it.getV(), key[it.getV()])); } } } int sum=0; for(int i=1;i<N;i++) { System.out.println(key[i]); sum+=key[i]; } System.out.println(sum); return sum; } // ****____________DIJKSTRAS ALGO___________**** static int[]dist; static int dijkstra(int u,int n,ArrayList<node>adj[]) { dist=new int[n]; Arrays.fill(dist,Integer.MAX_VALUE); dist[u]=0; PriorityQueue<node>pq=new PriorityQueue<node>(new node()); pq.add(new node(u,0)); while(!pq.isEmpty()) { node v=pq.poll(); for(node it:adj[v.getV()]) { if(dist[it.getV()]>it.getW()+dist[v.getV()]) { dist[it.getV()]=(int) (it.getW()+dist[v.getV()]); pq.add(new node(it.getV(),dist[it.getV()])); } } } int sum=0; for(int i=1;i<n;i++){ System.out.println(dist[i]); sum+=dist[i]; } return sum; } private static void setGraph(int n,int m){ vis=new boolean[n+1]; indeg=new int[n+1]; // adj=new ArrayList<ArrayList<Integer>>(); // for(int i=0;i<=n;i++)adj.add(new ArrayList<>()); // for(int i=0;i<m;i++){ // int u=s.nextInt(),v=s.nextInt(); // adj.get(u).add(v); // adj.get(v).add(u); // } adj=new ArrayList[n+1]; // backadj=new ArrayList[n+1]; for(int i=0;i<=n;i++){ adj[i]=new ArrayList<Integer>(); // backadj[i]=new ArrayList<Integer>(); } for(int i=0;i<m;i++){ int u=s.nextInt(),v=s.nextInt(); adj[u].add(v); adj[v].add(u); // backadj[v].add(u); indeg[v]++; indeg[u]++; } // weighted adj // adj=new ArrayList[n+1]; // for(int i=0;i<=n;i++){ // adj[i]=new ArrayList<node>(); // } // for(int i=0;i<m;i++){ // int u=s.nextInt(),v=s.nextInt(); // long w=s.nextInt(); // adj[u].add(new node(v,w)); //// adj[v].add(new node(u,w)); // } } // *********************************GRAPHS-ENDS**************************************************** static int[][] dirs8 = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; static int[][] dirs4 = {{1,0},{-1,0},{0,1},{0,-1}}; //d-u-r-l static long MOD=(long) (1e9+7); static int prebitsum[][]; static boolean[] vis; static int[]indeg; // static ArrayList<ArrayList<Integer>>adj; static ArrayList<Integer> adj[]; static ArrayList<Integer> backadj[]; static FastReader s = new FastReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { // sieve(); // computeFact((int)1e7+1,MOD); // prebitsum=new int[2147483648][31]; // presumbit(prebitsum); // powof2S(); // // try { int tt = s.nextInt(); // int tt=1; for(int i=1;i<=tt;i++) { solver(); } out.close(); // catch(Exception e) {return;} } private static void solver() { int n=s.nextInt(); Long[]x=s.rdLa(n); Long[]y=s.rdLa(n); Long[]a=new Long[n]; for(int i=0;i<n;i++) { a[i]=y[i]-x[i]; } Arrays.sort(a,Collections.reverseOrder()); int cnt=0,i=0,j=n-1; while(i<j) { if(a[i]+a[j]<0)j--; else { cnt++; j--; i++; } } out.println(cnt); } /* *********************BITS && TOOLS &&DEBUG STARTS***********************************************/ static boolean issafe(int i, int j, int r,int c,boolean[][]vis){ if (i < 0 || j < 0 || i >= r || j >= c||vis[i][j]==true)return false; else return true; } static void presumbit(int[][]prebitsum) { for(int i=1;i<=200000;i++) { int z=i; int j=0; while(z>0) { if((z&1)==1) { prebitsum[i][j]+=(prebitsum[i-1][j]+1); }else { prebitsum[i][j]=prebitsum[i-1][j]; } z=z>>1; j++; } } } static void countOfSetBit(long[]a) { for(int j=30;j>=0;j--) { int cnt=0; for(long i:a) { if((i&1<<j)==1)cnt++; } // printing the current no set bit in all array element System.out.println(cnt); } } public static String revStr(String str){String input = str;StringBuilder input1 = new StringBuilder();input1.append(input);input1.reverse();return input1.toString();} static void printA(long[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();} static void printA(int[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();} static void pc2d(boolean[][] vis) { int n=vis.length; int m=vis[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(vis[i][j]+" "); } System.out.println(); } } static void pi2d(char[][] a) { int n=a.length; int m=a[0].length; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } static void p1d(int[]a) { for(int i=0;i<a.length;i++)System.out.print(a[i]+" "); System.out.println(); } // *****************BITS && TOOLS &&DEBUG ENDS*********************************************** } // **************************I/O************************* class FastReader { public BufferedReader reader; public StringTokenizer tokenizer; public FastReader(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 String nextLine() {String str = "";try {str = reader.readLine();}catch (IOException e) {e.printStackTrace();}return str;} public int[] rdia(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = nextInt();return a;} public long[] rdla(int n) {long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;} public Integer[] rdIa(int n) {Integer[] a = new Integer[n];for (int i = 0; i < n; i++) a[i] =nextInt();return a;} public Long[] rdLa(int n) {Long[] a = new Long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;} } class dsu{ int n; static int parent[]; static int rank[]; static int[]Size; public dsu(int n) {this.n=n;this.parent=new int[n];this.rank=new int[n];this.Size=new int[n]; for(int i=0;i<n;i++){parent[i]=i;rank[i]=0;Size[i]=1;} } static int findpar(int node) {if(node==parent[node])return node;return parent[node]=findpar(parent[node]);} static void union(int u,int v){ u=findpar(u);v=findpar(v); if(u!=v) { if(rank[u]<rank[v]) {parent[u]=v;Size[v]+=Size[u];} else if(rank[v]<rank[u]) {parent[v]=u;Size[u]+=Size[v];} else{parent[v]=u;rank[u]++;Size[u]+=Size[v];} } } } class pair{ int x;int y; long u,v; public pair(int x,int y){this.x=x;this.y=y;} public pair(long u,long v) {this.u=u;this.v=v;} } class node implements Comparator<node>{ private int v; private long w; node(int _v, long _w) { v = _v; w = _w; } node() {} int getV() { return v; } long getW() { return w; } @Override public int compare(node node1, node node2) { if (node1.w < node2.w) return -1; if (node1.w > node2.w) return 1; return 0; } }
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
4019589e3092d9d0f73a97e37ac322ad
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.InputStream; 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.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class D { public static void main(String[]args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=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(); int[]dif=new int[n]; for(int i=0;i<n;i++) { b[i]=sc.nextInt(); dif[i]=b[i]-a[i]; } Arrays.sort(dif); int ans=0; for(int i=0,j=n-1;i<j;i++) { if(dif[j]+dif[i]>=0) { ans++;j--; } } out.println(ans); } out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public boolean hasNext() {return st.hasMoreTokens();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} 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
2663ba0f0139fef5116c07b46b1fceaf
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 D_Friends_and_the_Restaurant { static Scanner in = new Scanner(System.in); static int testCases, n; static long x[], y[]; static void solve() { Solve a[] = new Solve[n]; for(int i = 0; i < n; ++i) { a[i] = new Solve(x[i], y[i]); } sort(a, 0, n - 1); /*for(Solve i : a) { System.out.println(i.toString()); }*/ int first_corner = 0, last_corner = n - 1; int team = 0; while(first_corner >= 0 && last_corner < n && first_corner < last_corner) { if(a[first_corner].diff + a[last_corner].diff < 0) { ++first_corner; } else { ++first_corner; --last_corner; ++team; } } System.out.println(team); } public static void main(String [] amit) { testCases = in.nextInt(); for(int t = 0; t < testCases; ++t) { n = in.nextInt(); x = new long[n]; y = new long[n]; for(int i = 0; i < n; ++i) { x[i] = in.nextLong(); } for(int i = 0; i < n; ++i) { y[i] = in.nextLong(); } solve(); } } static class Solve { long need, have, diff; public Solve(long need, long have) { this.need = need; this.have = have; this.diff = have - need; } public String toString() { return this.need + " " + this.have + " " + this.diff; } } static int comparator(Solve x, Solve y) { if(x.diff > y.diff) { return 1; } else if(x.diff < y.diff) { return -1; } return 0; } static void merge(Solve a[], int left, int right, int mid) { int n1 = mid - left + 1, n2 = right - mid; Solve L[] = new Solve[n1]; Solve R[] = new Solve[n2]; for (int i = 0; i < n1; i++) { L[i] = a[left + i]; } for (int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; } int i = 0, j = 0, k1 = left; while (i < n1 && j < n2) { if (/*L[i] <= R[j]*/comparator(L[i], R[j]) <= 0 ) { a[k1] = L[i]; i++; } else { a[k1] = R[j]; j++; } k1++; } while (i < n1) { a[k1] = L[i]; i++; k1++; } while (j < n2) { a[k1] = R[j]; j++; k1++; } } static void sort(Solve a[], int left, int right) { if (left >= right) { return; } int mid = (left + right) / 2; sort(a, left, mid); sort(a, mid + 1, right); merge(a, left, right, mid); } }
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
c1507aa5ddc4453143fb74e19375a460
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.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Solution { static PrintWriter pw; static FastScanner s; public static void main(String[] args) throws Exception { pw=new PrintWriter(System.out); s=new FastScanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); ArrayList<Friend>al=new ArrayList<Friend>(); for(int i=0;i<n;i++) { al.add(new Friend(s.nextInt())); } for(int i=0;i<n;i++) { Friend f=al.get(i); f.y=s.nextInt(); al.set(i, f); } Collections.sort(al,(a,b)->(b.y-b.x)-(a.y-a.x)); int size=al.size(); int i=0; int j=size-1; int pairs=0; while(i<j) { Friend first=al.get(i); Friend second=al.get(j); int money=first.y+second.y; int cost=first.x+second.x; if(money>=cost) { pairs++; i++; j--; } else { j--; } } pw.println(pairs); } pw.flush(); } } class Friend{ int x; int y; public Friend(int x) { this.x = x; } @Override public String toString() { return "x=" + x + ", y=" + y + ""; } } class FastScanner{ BufferedReader br; StringTokenizer st; public FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public FastScanner(String s) throws Exception { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { 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 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
371b5156251c4b1e046e5147cbbf7f82
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:-crazy_coder- */ import java.io.*; import java.util.*; public class cp{ static int ans=0; static BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); static PrintWriter pw=new PrintWriter(System.out); public static void main(String[] args)throws Exception{ int T=Integer.parseInt(br.readLine()); // int t=1; while(T-->0){ solve(); } pw.flush(); } public static void solve()throws Exception{ String[] str=br.readLine().split(" "); int n=Integer.parseInt(str[0]); String[] str1=br.readLine().split(" "); String[] str2=br.readLine().split(" "); ArrayList<Long> x=new ArrayList<>(); ArrayList<Long> y=new ArrayList<>(); ArrayList<Long> diff=new ArrayList<>(); for(int i=0;i<n;i++){ x.add(Long.parseLong(str1[i])); y.add(Long.parseLong(str2[i])); diff.add(y.get(i)-x.get(i)); } Collections.sort(diff); int i=0,j=n-1; int ans=0; while(i<j){ long val1=diff.get(i); long val2=diff.get(j); if(val1+val2>=0){ ans++; i++;j--; }else{ i++; } } pw.println(ans); } public static class Pair implements Comparable<Pair>{ char ch; int idx; Pair(char ch,int idx){ this.ch=ch; this.idx=idx; } public int compareTo(Pair o){ return this.ch-o.ch; } } public static int countDigit(int x){ return (int)Math.log10(x)+1; } //****************************function to find all factor************************************************* public static ArrayList<Long> findAllFactors(long num){ ArrayList<Long> factors = new ArrayList<Long>(); for(long i = 1; i <= num/i; ++i) { if(num % i == 0) { //if i is a factor, num/i is also a factor factors.add(i); factors.add(num/i); } } //sort the factors Collections.sort(factors); return factors; } //*************************** function to find GCD of two number******************************************* public static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } }
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
16458a893d871ee4d6dbb6016611c458
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Sparsh Sanchorawala */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DFriendsAndTheRestaurant solver = new DFriendsAndTheRestaurant(); solver.solve(1, in, out); out.close(); } static class DFriendsAndTheRestaurant { public void solve(int testNumber, InputReader s, PrintWriter w) { int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = s.nextInt(); int[] y = new int[n]; for (int i = 0; i < n; i++) y[i] = s.nextInt(); Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = y[i] - x[i]; Arrays.sort(a); int r = n - 1; int c = 0; for (int l = 0; l < r; l++) { if (a[l] + a[r] < 0) continue; c++; r--; } w.println(c); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["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
65a7820b806b55df66243c22d089e134
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 Solution { static Scanner sc = new Scanner(System.in); static StringBuilder out = new StringBuilder(); static String testCase = "Case #"; static long mod = 998244353; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int t = sc.nextInt(); int tc = 0; while (tc++ < t) { // out.append("Case #"+tc+": "); Solution run = new Solution(); run.run(); } System.out.println(out); } static HashMap<Integer, Integer> hs; static String s; public void run() throws IOException { 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(); } ArrayList<Integer> neg= new ArrayList<>(), pos=new ArrayList<>(); for(int i=0;i<n;i++) { int diff= b[i]-a[i]; if(diff<0) { neg.add(diff); } else pos.add(diff); } Collections.sort(neg); Collections.reverse(neg); Collections.sort(pos); int i=0,j=0,cnt=0; int sum =0; while(i<neg.size() && j<pos.size()) { sum=neg.get(i)+pos.get(j); if(sum>=0) { cnt++; i++; j++; } else{ if(j+1<pos.size()) { j+=2; cnt++; } else j++; } } cnt+= (pos.size()-j)/2; out.append(cnt+"\n"); } static int getParent(int x, int par[]) { if (x == par[x]) return x; par[x] = getParent(par[par[x]], par); return par[x]; } static int solve(int a[]) { ArrayList<Integer>[] gr = new ArrayList[a.length]; for (int i = 0; i < a.length; i++) gr[i] = new ArrayList<>(); for (int i = 0; i < a.length; i++) { if (a[i] == -1) continue; gr[a[i]].add(i); } int res = 0; for (int i = 0; i < a.length; i++) { if (a[i] == -1) { int ans = dfs(i, gr); res = Math.max(ans, res); } } return res; } static int dfs(int u, ArrayList<Integer>[] gr) { int ans = 0; for (int ch : gr[u]) { ans = Math.max(ans, dfs(ch, gr)); } return ans + 1; } static int dp[], coin[]; static int solve(char ch[], char l1, char l2) { int ans = 0; int n = ch.length; int start = -1, end = -1; for (int i = 0; i < n; i++) { if (ch[i] == l1 || ch[i] == l2) { if (start == -1) { start = i; } end = i; } } int l1c = 0; int l2c = 0; for (int i = start; i <= end; i++) { if (ch[i] == l1) l1c++; else if (ch[i] == l2) l2c++; } int total = l1c + l2c; int cur = 0; for (int i = start; i <= end; i++) { if (ch[i] == l1 || ch[i] == l2) cur++; else { ans += Math.min(cur, total - cur); } } int count = 0; int val1 = 0; for (int i = start; i <= end; i++) { if (ch[i] == l1) { val1 += count; } else if (ch[i] == l2) count++; } count = 0; int val2 = 0; for (int i = start; i <= end; i++) { if (ch[i] == l2) { val2 += count; } else if (ch[i] == l1) count++; } ans += Math.min(val1, val2); return ans; } static int bit[]; static void modify(int x, int val) { for (; x < bit.length; x += (x & -x)) bit[x] += val; } static int get(int x) { int sum = 0; for (; x > 0; x -= (x & -x)) sum += bit[x]; return sum; } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["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
d653b6bd4725a5c7b2c4a09991fe5c66
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 practice { public static void solve() { Reader sc = new Reader(); PrintWriter out = 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(); x[i] = y[i] - x[i]; } Arrays.sort(x); int cnt = 0; int start = 0; int end = n - 1; while(start < end) { if(x[start] + x[end] >= 0) { cnt++; end--; } start++; } out.println(cnt); } out.flush(); } public static void main(String[] args) throws IOException { solve(); } 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 { 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 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
62dc70c6d644455f383b60fdef90ac82
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.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.StringTokenizer; public class D { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(); int[] bb=fs.readArray(n), b=fs.readArray(n); int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=b[i]-bb[i]; ruffleSort(a); int p1=0, p2=n-1; int ans=0; while (true) { while (p1<p2 && a[p1]+a[p2]<0)p1++; if (p1<p2) { ans++; p1++; p2--; } else break; } out.println(ans); } out.close(); } static final Random random=new Random(); static final int mod=1_000_000_007; static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort 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 long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } 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 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 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
be816af405c6bdcd3d5ee188d06c9754
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 { public static void solve(InputReader in, PrintWriter out) { 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(); } List<int[]> burles = new ArrayList<>(n); for (int i = 0; i < n; i++) { burles.add(new int[] {x[i], in.nextInt()}); } Collections.sort(burles, Comparator.comparingInt(a -> a[1] - a[0])); int left = 0, right = n - 1, ans = 0; while (left < right) { if (burles.get(left)[0] + burles.get(right)[0] <= burles.get(left)[1] + burles.get(right)[1]) { ans++; right--; } left++; } out.println(ans); } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } 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()); } String nextLine() throws Exception{ String str = ""; try{ str = reader.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } 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 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
aed272c886b920e21264089c48d5e32d
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.InputStream; import java.io.InputStreamReader; import java.util.*; public class FriendsAndTheRestaurant { public static void main(String[] args) { InputReader ir = new InputReader(System.in); StringBuilder sb = new StringBuilder(); int t = ir.nextInt(); while (t > 0) { int n = ir.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = ir.nextInt(); int[] y = new int[n]; for (int i = 0; i < n; i++) y[i] = ir.nextInt(); int[] d = new int[n]; for (int i = 0; i < n; i++) d[i] = y[i] - x[i]; Arrays.sort(d); int l = 0; int r = d.length - 1; while (true) { while (l < r && d[l] + d[r] < 0) l++; if (l >= r) break; r--; l++; } sb.append(d.length - r - 1).append("\n"); t--; } System.out.print(sb); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
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
339dc0c058a79c022a98eb2c5abaa873
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 div_3_815_a { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n=in.nextInt(); int a[]=in.readArray(n); int b[]=in.readArray(n); int c[]=new int[n]; for(int i=0;i<n;i++){ c[i]=b[i]-a[i]; } Arrays.sort(c); int i=0,j=n-1; int ans=0; while(i<j){ if(c[i]+c[j]>=0){ ans++; i++;j--; } else{ i++; } } out.println(ans); } out.close(); } static class Pair implements Comparable<Pair>{ int x,y; Pair(int a,int b){ x=a; y=b; } @Override public int compareTo(Pair obj){ if(obj.x-this.x>0) return -1; else if(obj.x-this.x<0) return 1; else return -1; } } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(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 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
1cbdf7a9df83095c091cf4ea5a17f039
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.math.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int t= in.nextInt(); for (int i = 0; i <t ; i++) { int n=in.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int c[]=new int[n]; for (int j = 0; j <n ; j++) { a[j]=in.nextInt(); } for (int j = 0; j <n ; j++) { b[j]=in.nextInt(); c[j]=b[j]-a[j]; } Arrays.sort(c); int l=0,r=n-1,sum=0; while (l<r){ long x=c[l]+c[r]; if(x>=0){ sum++; l++; r--; } else { l++; } } out.println(sum); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } 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 String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public long nextLong(){ return Long.parseLong(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(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 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
8d45b1a05c859fd6746eec49712eba14
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.math.*; public class D_1729 { public static void main(String[] args) { FastReader sc = new FastReader(); 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(); long[] arr=new long[n]; for(int i=0;i<n;i++) { arr[i]=b[i]-a[i]; } Random rn=new Random(); for(int i=0;i<n;i++) { int in=rn.nextInt(n); long temp=arr[in]; arr[in]=arr[i]; arr[i]=temp; } Arrays.sort(arr); int end=n-1; int ans=0; for(int i=0;i<n && end>i;i++) { if(arr[i]+arr[end]>=0) { ans++; end--; } } System.out.println(ans); } } 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 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
00dbe04fca738a229d06c59053ebb2cb
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.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import javax.print.attribute.standard.NumberUpSupported; /* ********* 1e9 ********* */ public class Solution { int mod = (int) 1e9; static int mod7 = ((int) 1e9) + 7; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = 1; t = sc.nextInt(); outer: while (t-- != 0) { int n = sc.nextInt(); int x[] = new int[n]; int y[] = new int[n]; for (int i = 0; i < y.length; i++) { x[i] = sc.nextInt(); } for (int i = 0; i < y.length; i++) { y[i] = -(x[i] - sc.nextInt()); } Arrays.sort(y); int i = 0; int j = n - 1; int count = 0; int sum = 0; while (i < j) { if (y[j] < 0) { break; } sum = y[j]; while (i < j && sum + y[i] < 0) { i++; } if (i == j) break; i++; count++; j--; } System.out.println(count); } sc.close(); } static boolean b = false; /* ********* 1e9 ********* */ public int lcm(int a, int b) { return a / gcd(a, b) * b; } public int gcd(int a, int b) { while (b != 0) { a %= b; int temp = a; a = b; b = temp; } return a; } 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 divCount(int n) { boolean hash[] = new boolean[n + 1]; Arrays.fill(hash, true); for (int p = 2; p * p < n; p++) if (hash[p] == true) for (int i = p * 2; i < n; i += p) hash[i] = false; int total = 1; for (int p = 2; p <= n; p++) { if (hash[p]) { int count = 0; if (n % p == 0) { while (n % p == 0) { n = n / p; count++; } total = total * (count + 1); } } } return total; } static int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static int fact(int n) { if (n == 0) return 1; int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } static int num_digits(int num) { return (int) (Math.log10(num) + 1); } // 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 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
353f38599742cd53a3c3791facce2d30
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 static java.lang.System.out; import java.util.*; import java.io.*; import java.lang.Math; public class cp{ 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 sort(int[] a,boolean flag){ ArrayList<Integer> temp=new ArrayList<>(); for(int n:a) temp.add(n); Collections.sort(temp); if(flag){ for(int i=0;i<a.length;i++){ a[i]=temp.get(i); } } else{ for(int i=a.length-1;i>=0;i--){ a[i]=temp.get(i); } } } public static int noOfDigits(int n){ return (int)Math.log10((double)n)+1; } public static int gcd(int a,int b){ if (b == 0) return a; else return gcd(b, a % b); } public static void printarr(int[] a){ for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printlist(List<Integer> a){ for(int i:a){ out.print(i+" "); } out.println(); } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static HashMap<Integer, Integer> sortMap(HashMap<Integer, Integer> hm) { List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); 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()); } }); HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static void solve(FastReader sc){ int n=sc.nextInt(); int price[]=new int[n]; int budget[]=new int[n]; for(int i=0;i<n;i++) price[i]=sc.nextInt(); for(int i=0;i<n;i++) budget[i]=sc.nextInt(); ArrayList<int[]> list=new ArrayList<>(); for(int i=0;i<n;i++){ list.add(new int[]{price[i],budget[i]}); } Collections.sort(list,(int[] x,int[] y)->Integer.compare(x[1]-x[0],y[1]-y[0])); int i=0,j=n-1; int count=0; while(i<j){ int firstDiff=list.get(i)[1]-list.get(i)[0]; int secDiff=list.get(j)[1]-list.get(j)[0]; if(firstDiff+secDiff>=0){ i++;j--;count++; } else{ i++; } } out.println(count); } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t=sc.nextInt(); // int t=1; while (t-->0){ solve(sc); } 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
7db6b819bed49b4e4cfcbd356fe0f61f
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 Mridul6 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int y = 0; while(y != t) { y = y + 1; int n = sc.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[n]; int i = -1; while (i != n - 1) { i = i + 1; arr1[i] = sc.nextInt(); } i = -1; while (i != n - 1) { i = i + 1; arr2[i] = sc.nextInt(); } int[] arr3 = new int[n]; i = -1; while (i != n - 1) { i = i + 1; arr3[i] = arr2[i] - arr1[i]; } Arrays.sort(arr3); i = -1; int u = 0; while (i != n - 1) { i = i + 1; if (arr3[i] > 0) { u = i; break; } } int sum = 0; i = 0; int j = n - 1; while (i < j) { if (arr3[i] + arr3[j] >= 0) { i = i + 1; j = j - 1; sum = sum + 1; } if (arr3[i] + arr3[j] < 0) { i = i + 1; } } System.out.println(sum); } } }
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
098d1bd0f10a053df9e24abd692daaf7
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.*; import java.util.concurrent.ConcurrentHashMap; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { static final int INF=Integer.MAX_VALUE; static final long LINF=Long.MAX_VALUE; static FastReader in; static { try { in = new FastReader(); } catch (FileNotFoundException e) { e.printStackTrace(); } } static PrintWriter out = new PrintWriter(System.out); static int n; static int[] a; static int[] b; public static void main(String[] args) throws FileNotFoundException { int t=in.nextInt(); while(t-->0){ n=in.nextInt(); a=new int[n]; b=new int[n]; for(int i=0;i<n;i++) a[i]=in.nextInt(); for(int j=0;j<n;j++) b[j]=in.nextInt(); solve(); } out.flush(); in.close(); } static private void solve(){ int[] diff=new int[n]; int negcount=0; for(int i=0;i<n;i++) { diff[i]=b[i]-a[i]; if(diff[i]<0) negcount++; } if(negcount==0){ out.println(diff.length/2); }else{ Arrays.sort(diff); int[] negarr=new int[negcount]; for(int i=0;i<negcount;i++){ negarr[i]=diff[i]; } int[] posarr=new int[diff.length-negcount]; for(int i=negcount,j=0;i<diff.length;i++,j++){ posarr[j]=diff[i]; } int pairs=0; int prev=posarr.length-1; for(int i=0;i<negcount;i++){ int bound=lowerbound(posarr,-negarr[i],0,prev); if(bound<=prev){ pairs++; prev--; } } pairs+=(posarr.length-pairs)/2; out.println(pairs); } } static int lowerbound(int[] arr,int target,int l,int r){ int best=arr.length; while(l<=r){ int mid=(l+r)/2; if(arr[mid]<target){ l=mid+1; }else{ best=mid; r=mid-1; } } return best; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { //br = new BufferedReader(new InputStreamReader(new FileInputStream("/home/chaitanya/gitrepo/ds/src/main/resources/ts2_input.txt"))); 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; } void close(){ try { br.close(); } catch (IOException 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 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
5654325c1d033997bdba35007a66bcc6
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{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner sc = new Scanner(System.in); int t = sc.nextInt() ; while(t-->0) { int n = sc.nextInt() ; int [] 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 [] diff = new int[n]; for (int i = 0 ; i <n ; i++) diff[i] = b[i] - a[i] ; Arrays.sort(diff); int c =0 ; int i = 0 ; int j = n-1; while(i<j) { if(diff[i]+ diff[j] >=0) { c++ ; i++ ; j-- ; } else { 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 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
0ff8f95ba801fec2f406e6d99c55e764
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.InputStreamReader; import java.util.*; public class Solution { private FastReader reader; private StringBuilder sb; private static final int TEN_POW_FIVE = (int)1e5; private static final int MOD = (int)1e9+7; public Solution() { this.reader = new FastReader(); this.sb = new StringBuilder(); } private void solve() throws Exception { int t = reader.getInt(); while(t-->0) { int n = reader.getInt(); int[] x = reader.getIntArr(n); int[] y = reader.getIntArr(n); for(int i=0; i<n; i++) { x[i] = y[i] - x[i]; } Arrays.sort(x); int days = 0; int left = 0; int right = n-1; while(left<right) { int sum = x[left]+x[right]; if(sum>=0) { right--; days++; } left++; } sb.append(days).append("\n"); } reader.closeReader(); } private void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private int min(Integer... minArr) { int min = minArr[0]; for(int i=1; i<minArr.length; i++) { min = Math.min(min, minArr[i]); } return min; } private int max(Integer... maxArr) { int max = maxArr[0]; for(int i=1; i<maxArr.length; i++) { max = Math.max(max, maxArr[i]); } return max; } private void printArr(int[] arr, int n, int incrementer) { for(int i=0; i<n; i++) { sb.append(arr[i]+incrementer).append(" "); } sb.append("\n"); } public static void main(String[] args) { try { Solution solution = new Solution(); solution.solve(); solution.printOp(); } catch(Exception e) { e.printStackTrace(); } } private void printOp() { System.out.println(sb.toString()); } static class FastReader { private BufferedReader inputReader; private StringTokenizer st; public FastReader() { inputReader = new BufferedReader(new InputStreamReader(System.in)); } private String getNext() throws Exception { if(st==null || !st.hasMoreTokens()) { st = new StringTokenizer(inputReader.readLine()); } return st.nextToken(); } public String[] getLine(String delimiter) throws Exception { return inputReader.readLine().split(delimiter); } public String getString() throws Exception { return getNext(); } public int getInt() throws Exception { return Integer.parseInt(getNext()); } public long getLong() throws Exception { return Long.parseLong(getNext()); } public double getDouble() throws Exception { return Double.parseDouble(getNext()); } public int[] getIntArr(int n) throws Exception { int[] arr = new int[n]; for(int i=0; i<n; i++) { arr[i] = getInt(); } return arr; } public long[] getLongArr(int n) throws Exception { long[] arr = new long[n]; for(int i=0; i<n; i++) { arr[i] = getLong(); } return arr; } public double[] getDoubleArr(int n) throws Exception { double[] arr = new double[n]; for(int i=0; i<n; i++) { arr[i] = getDouble(); } return arr; } public String[] getStrArr(int n) throws Exception { String[] arr = new String[n]; for(int i=0; i<n; i++) { arr[i] = getString(); } return arr; } public void closeReader() throws Exception { inputReader.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
bec5cf91733cfa81490e4e491c255c12
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.lang.*; import java.time.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final int temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Tuple implements Comparable<Tuple> { long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int count = 0; int[] parent; int[] rank; public DSU(int n) { count = n; parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public void union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return; if(rank[a] < rank[b]) { parent[a] = b; } else if(rank[a] > rank[b]) { parent[b] = a; } else { parent[b] = a; rank[a] = 1 + rank[a]; } count--; } public int countConnected() { return count; } } static class Reader { 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) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { 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()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long powMod(long x, long y, long p) { long 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; } static long modInverse(long n, long p) { return powMod(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Long> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Long> l = new ArrayList<>(); 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; } } } for (long p = 2; p<=n; p++) { if (prime[(int)(p)] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { 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; } public static int UpperBound(int a[], int x) { 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; } public static void Sort(long[] a) { List<Long> l = new ArrayList<>(); for (long i : a) l.add((i * 1L)); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); fr:while(tt-- > 0) { int n = sc.nextInt(); long[] a = sc.longReadArray(n); long[] b = sc.longReadArray(n); long[] d = new long[n]; for(int i = 0;i<n;i++) d[i] = (b[i] - a[i]); Sort(d); int ans = 0, i = 0, j = n-1; while(i < j) { if(d[i] + d[j] >= 0) { ans++; i++; j--; } else { i++; } } fout.println(ans); } fout.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
66e0af43b14481e24da826acc2a2abfb
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 PrintWriter out = new PrintWriter(System.out); static Scanner in = new Scanner(System.in); static BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); int a = Integer.parseInt(strs[0]); public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); //String[] strs = re.readLine().split(" "); //int T=Integer.parseInt(strs[0]); int T=in.nextInt(); //nt T=1; while(T>0){ //String[] strs1 = re.readLine().split(" "); //int n=Integer.parseInt(strs1[0]); //String s=re.readLine(); //char arr[]=s.toCharArray(); //Set<Integer>set=new HashSet<>(); //Map<Integer,Integer>map=new HashMap<>(); int n=in.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]=in.nextInt(); for(int i=0;i<n;i++){ b[i]=in.nextInt(); c[i]=b[i]-a[i]; } Arrays.sort(c); int res=0;int low=0;int high=n-1; while(low<high){ if(c[high]+c[low]>=0){ high--;low++;res++; } else{ low++; } } out.println(res); //out.println(Math.min(a,b)); T--; } 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
98d618909bace458727e83c8bbb2a6fd
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(); //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
c94477256dd4875744d4905c54c839a5
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.PrintWriter; public class D { public static void solve(Scanner sc) { PrintWriter writer = new PrintWriter(System.out); int n = sc.nextInt(); int[] arr = new int[n]; int[] arr1 = new int[n]; int[] diff = 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(); int temp = arr1[i] - arr[i]; diff[i] = temp; } Arrays.sort(diff); int i = 0,j = n-1; int ans = 0; while(i < j) { int sum = diff[i]+diff[j]; if(sum < 0) { i++; } else { ans++; i++; j--; } } System.out.println(ans); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { solve(sc); } } } class pair { int a; int b; public pair(int a, int b) { this.a = a; this.b = b; } }
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
a5da62dd867c5ccea1dbf7273adfa4ba
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 cp { static PrintWriter w = new PrintWriter(System.out); static FastScanner s = new FastScanner(); static int mod = 1000000007; static class Edge { int src; int wt; int nbr; Edge(int src, int nbr, int et) { this.src = src; this.wt = et; this.nbr = nbr; } } class EdgeComparator implements Comparator<Edge> { @Override //return samllest elemnt on polling public int compare(Edge s1, Edge s2) { if (s1.wt < s2.wt) { return -1; } else if (s1.wt > s2.wt) { return 1; } return 0; } } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static void prime_till_n(boolean[] prime) { // 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. for (int p = 2; p * p < prime.length; 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 < prime.length; i += p) { prime[i] = false; } } } // int l = 1; // for (int i = 2; i <= n; i++) { // if (prime[i]) { // w.print(i+","); // arr[i] = l; // l++; // } // } //Time complexit Nlog(log(N)) } static int noofdivisors(int n) { //it counts no of divisors of every number upto number n int arr[] = new int[n + 1]; for (int i = 1; i <= (n); i++) { for (int j = i; j <= (n); j = j + i) { arr[j]++; } } return arr[0]; } static char[] reverse(char arr[]) { char[] b = new char[arr.length]; int j = arr.length; for (int i = 0; i < arr.length; i++) { b[j - 1] = arr[i]; j = j - 1; } return b; } static long factorial(int n) { if (n == 0) { return 1; } long su = 1; for (int i = 1; i <= n; i++) { su *= (long) i; } return su; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Vertex { int x; int y; int wt; public Vertex(int x, int y) { this.x = x; this.y = y; } public Vertex(int x, int y, int wt) { this.x = x; this.y = y; this.wt = wt; } } public static long power(long x, int n) { if (n == 0) return 1l; long pow = power(x, n / 2) % mod; if ((n & 1) == 1) // if `y` is odd return ((((x%mod) * (pow%mod))%mod) * (pow%mod))%mod; // otherwise, `y` is even return ((pow%mod) * (pow%mod))%mod; } public static void main(String[] args) { { int t = s.nextInt(); // int t = 1; while (t-- > 0) { solve(); } w.close(); } } public static void solve() { int n = s.nextInt(); Integer arr[]= new Integer[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } for(int i=0;i<n;i++) { arr[i]= s.nextInt()-arr[i]; } Arrays.sort(arr, new Comparator<Integer>(){ public int compare(Integer a,Integer b) { int res = Integer.compare(b.intValue(),a.intValue()); return res; } }); // for(int i=0;i<n;i++) // w.print(arr[i]+" "); int count =0;int curr=1; int l=1; int r= n-1;int temp=arr[0]; while(l<=r) { if(curr<2) { if(temp+arr[r]>=0) { r--;curr=1; count++; temp=arr[l]; l++; } else { r--; } } } w.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
3d11f683c4856022ae45e0f12d2b3a73
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.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.StringTokenizer; import static java.lang.Math.*; public class FriendsAndTheRestaurant implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int T = in.ni(); while (T-- > 0) { int n = in.ni(); int[] x = new int[n], y = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.ni(); } for (int i = 0; i < n; i++) { y[i] = in.ni(); } List<Integer> positive = new ArrayList<>(), negative = new ArrayList<>(); for (int i = 0; i < n; i++) { int diff = y[i] - x[i]; if (diff >= 0) { positive.add(diff); } else { negative.add(diff); } } positive.sort(Comparator.naturalOrder()); negative.sort(Comparator.reverseOrder()); int days = 0, idx = 0, spare = 0; for (int pos : positive) { if (idx < negative.size()) { if (pos + negative.get(idx) >= 0) { days++; idx++; } else { spare++; } } else { spare++; } } days += spare / 2; out.println(days); } } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (FriendsAndTheRestaurant instance = new FriendsAndTheRestaurant()) { instance.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 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
e5d096cd311484ad5ee15fff0f61792b
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{ static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t -- > 0){ int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } for (int i = 0; i < n; i++) { a[i] = sc.nextLong() - a[i]; } Arrays.sort(a); int ans = 0; int i = 0, j = n - 1; while (i < j){ if(a[i] + a[j] >= 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 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
e4bce108f08226ec417493ecc38f2ad1
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.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Problem4 { static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } public static void main(String[] args) throws Exception { Reader.init(System.in); // connect Reader to an input stream int t = Reader.nextInt(); while (t-- > 0) { int n =Reader.nextInt(); int[] arr=new int[n]; int[] brr=new int[n]; for (int i =0;i< n;i++){ arr[i]=Reader.nextInt(); } for (int i =0;i< n;i++){ brr[i]=Reader.nextInt(); } int[] finalArr=new int[n]; for(int i=0;i< n ;i++){ finalArr[i]=brr[i]-arr[i]; } Arrays.sort(finalArr ); int l=0 , r=finalArr.length-1; int ans =0; while(l<r){ if(finalArr[l]+finalArr[r]<0){ l++; } else if(finalArr[l]+ finalArr[r]>=0){ ans++; l++; r--; } } 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
72da9990d6f7ae4b798e66d3505d5354
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 Abai { 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(); } ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(x[i]-sc.nextInt() ); } Collections.sort(arr); int count = 0; int a = 0; int b = n-1; while (b>a){ if(arr.get(a)+arr.get(b) <= 0){ a++; b--; count++; }else{ b--; } } 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 17
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
87a2279804f751a98075c7dbd1a0c591
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 k = sc.nextInt(); for(int t = 0; t < k; t++){ int n = sc.nextInt(); int[][] arr = new int[n][2]; int[] money = new int[n]; for(int i = 0; i < n; i++) arr[i][0] = sc.nextInt(); for(int i = 0; i < n; i++) arr[i][1] = sc.nextInt(); for(int i = 0; i < n; i++) money[i] = arr[i][1] - arr[i][0]; Arrays.sort(money); int ans = 0; int i = 0, j = n - 1; while(i < j){ if(money[i] + money[j] >= 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 17
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
d228afc2966e15d9944d4f767d66ce1f
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 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-- ; } } public static int resturantAndFriends(int arr1[] , int arr2[]){ int ans[] = new int[arr1.length] ; for(int i=0 ; i < arr1.length ; i++){ ans[i] = arr2[i] - arr1[i] ; } Arrays.sort(ans) ; reverse(ans) ; int sum = 0 ; int c = 0 ; int i = 0 ; int j = ans.length - 1 ; while(i < j){ sum = ans[i] + ans[j] ; if(sum < 0){ j-- ; }else{ c++ ; i++ ; j-- ; } } return c ; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T > 0) { int n = sc.nextInt() ; int p[] = new int[n] ; int k[] = new int[n] ; for(int i=0 ; i< n ; i++){ p[i] = sc.nextInt() ; } for(int i=0 ; i< n ; i++){ k[i] = sc.nextInt() ; } int a = resturantAndFriends(p , k) ; System.out.println(a) ; T-- ; } } }
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 17
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
8627957d32b539f85a45b3d07e20eff7
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 text1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cs = sc.nextInt(); for(int dz = 0;dz < cs;dz ++){ int length = sc.nextInt(); long[] want = new long[length]; int zero = 0; for(int dz1 = 0;dz1 < length;dz1 ++) want[dz1] = sc.nextLong(); TreeMap<Long,Integer> zengjia = new TreeMap<>(); TreeMap<Long,Integer> jianshao = new TreeMap<>(); for(int dz1 = 0;dz1 < length;dz1 ++){ long a = sc.nextLong(); if(a - want[dz1] > 0){ if(!zengjia.containsKey(a - want[dz1])) zengjia.put(a - want[dz1], 1); else{ int b = zengjia.get(a - want[dz1]) + 1; zengjia.put(a - want[dz1],b); } }else if(a - want[dz1] < 0){ if(!jianshao.containsKey(want[dz1] - a)) jianshao.put(want[dz1] - a, 1); else{ int b = jianshao.get(want[dz1] - a) + 1; jianshao.put(want[dz1] - a,b); } }else if(a - want[dz1] == 0) zero ++; } int number = 0; for(long a : jianshao.keySet()){ int time = jianshao.get(a); boolean pd = false; while(time > 0){ pd = false; for(long b : zengjia.keySet()){ if(b >= a){ int bTime = zengjia.get(b) - 1; if(bTime == 0) zengjia.remove(b); else zengjia.put(b,bTime); pd = true; number ++; break; } } if(!pd) break; time --; } if(!pd) break; } for(long a : zengjia.keySet()) zero += zengjia.get(a); number += zero / 2; System.out.println(number); } return ; } }
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 17
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
16d8b859141c9fe423856ea96b41b23d
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 BufferedReader br; static long cnt=0; static int mod=998244353; //static ArrayList<ArrayList<Integer>> arr=new ArrayList<>(); static StringBuilder ans=new StringBuilder(""); public static void main(String args[]) throws IOException { // Your code goes here /* 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5 1 3 4 5 4 5 5 code aj abacaba ll codeforces aaaak aaaaj aaaaa zf */ br=new BufferedReader(new InputStreamReader(System.in)); int t=Int(); while(t-->0){ int n=Int(); int a[]=readInt(n); int b[]=readInt(n); int diff[]=new int[n]; int i; TreeMap<Integer,Integer> set=new TreeMap<>(); TreeMap<Integer,Integer> min=new TreeMap<>(); int res=0; for(i=0;i<n;i++){ diff[i]=b[i]-a[i]; if(diff[i]>=0){ // set.putIfAbsent(diff[i]); // set.add(diff[i],set.get(diff[i])+1); res++; } else{ // min.putIfAbsent(diff[i]); // min.add(diff[i],min.get(diff[i])+1); } } Arrays.sort(diff); res=0; i=0; int j=n-1; while(i<j){ if(diff[i]+diff[j]>=0){ res++; i++; j--; } else{ i++; } } ans.append(res+"\n"); } printString(ans.toString()); } public static boolean isPalindrome(String str) { // Initializing an empty string to store the reverse // of the original str String rev = ""; // Initializing a new boolean variable for the // answer boolean ans = false; for (int i = str.length() - 1; i >= 0; i--) { rev = rev + str.charAt(i); } // Checking if both the strings are equal if (str.equals(rev)) { ans = true; } return ans; } public static void dfs(int u,boolean visited[],int color[],ArrayList<Integer> graph[],int c){ visited[u]=true; color[u]=c; int cnt=0; for(int v:graph[u]){ if(visited[v]==true) continue; c=1-c; dfs(v,visited,color,graph,c); } } public static long memo(long dp[][],int i,int j,long arr[]){ if(j==dp[0].length) return 0; if(i>=arr.length) return -1*(long)Math.pow(10,15); if(dp[i][j]!=-1) return dp[i][j]; dp[i][j]=Math.max(memo(dp,i+1,j+1,arr)+arr[i]*(j+1),memo(dp,i+1,j,arr)); return dp[i][j]; } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static void fun(int curr,int count,int x,int y,ArrayList<Integer> pres){ } // public static mine min(mine x,mine y){ // if(x.a>y.a) // return y; // if(y.a>x.a) // return x; // if(x.b>y.b) // return y; // return x; // } public static int[] readInt(int n) throws IOException { String str[]=br.readLine().split(" "); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=Integer.parseInt(str[i]); return a; } public static ArrayList<Integer> readList(int n) throws IOException { String str[]=br.readLine().split(" "); ArrayList<Integer> arr=new ArrayList<>(); for(int i=0;i<n;i++) arr.add(Integer.parseInt(str[i])); return arr; } public static char[] readChar(int n) throws IOException { String str=br.readLine(); char a[]=new char[n]; for(int i=0;i<n;i++) a[i]=str.charAt(i); return a; } public static long[] readLong(int n) throws IOException { String str[]=br.readLine().split(" "); long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=Long.parseLong(str[i]); return a; } public static void printString(String str){ System.out.println(str); } public static void printInt(int str){ System.out.println(str); } public static void printLong(long str){ System.out.println(str); } public static int Int() throws IOException { return Integer.parseInt(br.readLine()); } public static long Long() throws IOException { return Long.parseLong(br.readLine()); } public static String[] readString() throws IOException { return br.readLine().split(" "); } } class mine{ long num; int index; public mine(long x,int y){ num=x; index=y; } }
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 17
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
b79e32644a8b479bbd87b85b4a1abe5b
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.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc-- > 0){ int n = sc.nextInt(); long[] a = new long[n]; long[] b = new long[n]; int suma = 0; int sumb = 0; for(int i = 0 ; i < n ; i++){ a[i] = sc.nextLong(); } for(int i = 0 ; i < n ; i++){ b[i] = sc.nextLong(); } // Arrays.sort(a); // Arrays.sort(b); long[] d = new long[n]; for(int i = 0 ; i < n ; i++){ d[i] = b[i]-a[i]; // if(d[i]>=0){ // positived+=d[i]; // npositived++; // }else if(d[i]<0){ // negatived+=d[i]; // } } Arrays.sort(d); int i = 0; int j = n-1; int ans = 0; while(i < j){ if(d[i] >= 0){ ans += (j-i+1)/2; break; } if(d[j] >= Math.abs(d[i])){ ans++; i++; j--; }else if(d[j]<= Math.abs(d[i])){ 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 17
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
7333a81768c6ed4286ae0dcafae65d65
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.math.*; import java.text.*; public class Main{ public static void main(String args[]) throws IOException{ Read sc=new Read(); int n=sc.nextInt(); for(int i=0;i<n;i++){ int t=sc.nextInt(); long a[]=new long[t]; long b[]=new long[t]; for(int j=0;j<t;j++){ a[j]=sc.nextLong(); } for(int j=0;j<t;j++){ b[j]=sc.nextLong(); } List<Long> list1=new ArrayList<>(); List<Long> list2=new ArrayList<>(); int count=0; for(int j=0;j<t;j++){ long aa=b[j]-a[j]; if(aa==0){ count++; } else if(aa>0){ list1.add(aa); } else{ list2.add(aa); } } Collections.sort(list1); Collections.sort(list2); Collections.reverse(list2); int p=0; int ans=count+list1.size(); for(int j=0;j<list1.size()&&p<list2.size();j++){ if(list1.get(j)+list2.get(p)>=0){ p++; ans++; } } sc.println(ans/2); } //sc.print(0); sc.bw.flush(); sc.bw.close(); } } //记住看数字范围,需要开long吗,需要用BigInteger吗,需要手动处理字符串吗,复杂度数量级控制在1e7或者以下了吗 //开数组的数据范围最高不能超过1e7,数据范围再大就要用哈希表离散化了 //基本数据类型不能自定义sort排序,二维数组就可以了,顺序排序的时候是小减大,注意返回值应该是int class Read{ BufferedReader bf; StringTokenizer st; BufferedWriter bw; public Read(){ bf=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public String nextLine() throws IOException{ return bf.readLine(); } public String next() throws IOException{ while(!st.hasMoreTokens()){ st=new StringTokenizer(bf.readLine()); } return st.nextToken(); } public char nextChar() throws IOException{ //确定下一个token只有一个字符的时候再用 return next().charAt(0); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public float nextFloat() throws IOException{ return Float.parseFloat(next()); } public byte nextByte() throws IOException{ return Byte.parseByte(next()); } public short nextShort() throws IOException{ return Short.parseShort(next()); } public BigInteger nextBigInteger() throws IOException{ return new BigInteger(next()); } public void println(int a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(int a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(String a) throws IOException{ bw.write(a); bw.newLine(); return; } public void print(String a) throws IOException{ bw.write(a); return; } public void println(long a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(long a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(double a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(double a) throws IOException{ bw.write(String.valueOf(a)); return; } public void print(BigInteger a) throws IOException{ bw.write(a.toString()); return; } public void print(char a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(char a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } }
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 17
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
8155357b39344a5bdb31a005bbb44641
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.PriorityQueue; import java.util.StringTokenizer; public class D { private static Scan sc = new Scan(); public static void main(String[] args) { int n = sc.nextInt(); for (int i = 0; i < n; i++) { solve(); } } private static void solve() { int n = sc.nextInt(); int[] a = sc.readArray(n); int[] b = sc.readArray(n); int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = b[i] - a[i]; } PriorityQueue<Integer> pos = new PriorityQueue<Integer>(); PriorityQueue<Integer> neg = new PriorityQueue<Integer>(); int z = 0; for (int i = 0; i < n; i++) { if (d[i] > 0) { pos.add(d[i]); } else if (d[i] < 0) { neg.add(-d[i]); } else { z++; } } int c = 0; int p = 0; while (!pos.isEmpty() && !neg.isEmpty()) { if (pos.poll() >= neg.peek()) { neg.poll(); c++; } else { p++; } } while (!pos.isEmpty()) { pos.poll(); p++; } System.out.println(c + (p + z) / 2); } private static class Scan { BufferedReader br; StringTokenizer st; public Scan() { 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()); } String nextLine() { String line = ""; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } 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 17
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
9336195cd199b2e3c1700f54f24d4ae9
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 { static Scanner in = new Scanner(System.in); static int numOfGroups(int arr[]){ int pair = 0; int l=0; int r=arr.length-1; while (true){ while(l<r && arr[l]+arr[r]<0){ l++; } if(l>=r){ break; } pair++; l++; r--; } // while (l<=r){ // int amount = arr[l]+arr[r]; // if(amount>=0){ // ++pair; // ++l; // --r; // } // if(amount<0){ // ++l; // } // } return pair; } static void ans(){ int n = in.nextInt(); int spend[] = new int[n]; int budget[] = new int[n]; for(int i=0; i<n;i++){ spend[i]=in.nextInt(); } for(int i=0;i<n;i++){ budget[i]=in.nextInt(); } for(int i=0;i<n;i++){ int mon =spend[i]; int budg =budget[i]; spend[i]=budg-mon; } Arrays.sort(spend); //System.out.println(Arrays.toString(spend)); int groups = numOfGroups(spend); System.out.println(groups); return; } public static void main(String[] args) { int test = in.nextInt(); for(int i =0;i<test;i++){ ans(); } //System.out.println(ans(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 17
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
1ee9bbdd1b68d9d732725a20163d0cb8
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 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static int tcs, n; static StringTokenizer st; static int[] xs = new int[100005]; static int[] ys = new int[100005]; static int[] arr = new int[100005]; static void solved() throws IOException { n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); for (int i = 1; i <= n ; i++) { xs[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (int i = 1; i <= n ; i++) { ys[i] = Integer.parseInt(st.nextToken()); } for (int i = 1; i <= n ; i++) { arr[i] = ys[i] - xs[i]; } Arrays.sort(arr, 1, n+1); int l = 1, r = n; int cnt = 0; while (l < r) { if(arr[l] + arr[r] < 0) { l++; continue; } cnt++; l++; r--; } System.out.println(cnt); } public static void main(String[] args) throws IOException { tcs = Integer.parseInt(br.readLine()); while (tcs-- > 0) { solved(); } } }
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 17
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
9dbb49824c24a8b0713410e6e6f5554f
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { // static int[] prime = new int[100001]; final static long mod = 1000000007; public static void main(String[] args) { // sieve(); InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t -- > 0){ int n = in.nextInt(); Integer[] a = intInput(n, in), b = intInput(n, in); for(int i = 0; i < n; i++){ b[i] -= a[i]; } Arrays.sort(b, (c, d) -> d - c); int ans = 0, ind = n-1; for(int i = 0; i < n; i++){ while(ind > i && b[ind] + b[i] < 0) ind--; if(i >= ind) break; ans++; ind--; } out.println(ans); } out.flush(); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static Integer[] intInput(int n, InputReader in) { Integer[] a = new Integer[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextInt(); return a; } static Long[] longInput(int n, InputReader in) { Long[] a = new Long[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextLong(); return a; } static String[] strInput(int n, InputReader in) { String[] a = new String[n]; for (int i = 0; i < a.length; i++) a[i] = in.next(); return a; } // static void sieve() { // for (int i = 2; i * i < prime.length; i++) { // if (prime[i]) // continue; // for (int j = i * i; j < prime.length; j += i) { // prime[j] = true; // } // } // } } class Segment { int[] a; final int n; Segment(int n){ this.n = n; a = new int[4*n]; Arrays.fill(a, Integer.MIN_VALUE); } void set(int index, int val){ set(0, n-1, 0, index, val); } private void set(int s, int e, int ind, int index, int val) { if(s == e){ a[ind] = val; return; } int mid = (s+e)/2; if(mid <= index){ set(s, mid, 2*ind+1, index, val); }else{ set(mid+1, e, 2*ind+2, index, val); } a[ind] = Math.max(a[2*ind+1], a[2*ind+2]); } int get(int l, int h){ return get(0, n-1, l, h, 0); } private int get(int s, int e, int l, int h, int ind) { if(s >= l && e <= h){ return a[ind]; } if(s > h || e < l){ return Integer.MIN_VALUE; } int mid = (s+e)/2; return Math.max(get(s, mid, l, h, 2*ind+1), get(mid+1, e, l, h, 2*ind+2)); } } class Data{ int i, ind; public Data(int i, int ind) { this.i = i; this.ind = ind; } } // class compareVal implements Comparator<Data> { // @Override // public int compare(Data o1, Data o2) { // return (o1.val - o2.val); // } // } // class compareInd implements Comparator<Data> { // @Override // public int compare(Data o1, Data o2) { // return o1.ind - o2.ind == 0 ? o1.val - o2.val : o1.ind - o2.ind; // } // } 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()); } 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; } }
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 17
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
15f360d6c8c399c91d08fe6e6be8219a
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.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Rextester { public static void main(String args[]) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i = 0; i < t; i++) { int n = in.nextInt(); Integer[] cash = new Integer[n]; for(int j = 0; j < n; j++) { cash[j] = -1 * in.nextInt(); } for(int j = 0; j < n; j++) { cash[j] += in.nextInt(); } List<Integer> Lcash = new ArrayList<>(Arrays.asList(cash)); Collections.sort(Lcash); ArrayDeque<Integer> deq = new ArrayDeque<>(Lcash); int count = 0; while(true) { if(deq.size() == 0) break; int max = deq.pollLast(); while(true) { if(deq.size() == 0) break; int min = deq.pollFirst(); if(max + min >= 0) { //System.out.println("" + max + " " + min); count++; break; } } } System.out.println(count); } 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 17
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
236f455d0580a583b751f2f76f6ee022
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 test { static class Obj{ int bud; int exp; int diff; Obj(int bud, int exp, int diff){ this.bud = bud; this.exp = exp; this.diff = diff; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t > 0) { int n = sc.nextInt(); int []exp = new int[n]; int []bud = new int[n]; for(int i=0; i<n; i++){ exp[i] = sc.nextInt(); } for(int i=0;i<n;i++){ bud[i] = sc.nextInt(); } ArrayList<Obj>arr = new ArrayList<>(); for(int i=0; i<n; i++){ arr.add(new Obj(bud[i], exp[i], bud[i]-exp[i])); } Collections.sort(arr, new Comparator<Obj>() { @Override public int compare(Obj o1, Obj o2) { return o1.diff - o2.diff; } }); // for(int i=0; i<n; i++){ // System.out.println(arr.get(i).bud + " " + arr.get(i).exp +" "+ arr.get(i).diff); // } int i=0, j=n-1; int count = 0; while(i<j){ // System.out.println("cou="+ count +" "+ i + " "+ j); if(arr.get(i).diff + arr.get(j).diff >=0){ count++; i++; j--; }else{ i++; } } System.out.println(count); t--; } } }
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 17
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
20596f5481faadd6a231beecd98df64c
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.Collections; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public class Solution { public static class CFScanner { BufferedReader br; StringTokenizer st; public CFScanner() { 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) { // int n = in.nextInt(); // long n = in.nextLong(); // double n = in.nextDouble(); // String s = in.next(); CFScanner in = new CFScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int T = in.nextInt(); for (int t = 1; t <= T; t++) { int n = in.nextInt(); int[] wants = new int[n]; for (int i = 0; i < n; i++) { wants[i] = in.nextInt(); } int[] moneys = new int[n]; for (int i = 0; i < n; i++) { moneys[i] = in.nextInt(); } out.println(getMaxGroups(n, wants, moneys)); } out.close(); } private static int getMaxGroups(int n, int[] wants, int[] moneys) { int groups = 0; TreeMap<Integer, Integer> posMap = new TreeMap<>(); PriorityQueue<Integer> negHeap = new PriorityQueue<>((a, b) -> (b - a)); int zeros = 0; for (int i = 0; i < n; i++) { int want = wants[i]; int money = moneys[i]; int diff = money - want; if (diff == 0) { zeros++; } else if (diff > 0) { posMap.put(diff, posMap.getOrDefault(diff, 0) + 1); } else { negHeap.add(diff); } } while (!negHeap.isEmpty()) { int neg = -1 * negHeap.poll(); Integer ceilingKey = posMap.ceilingKey(neg); if (ceilingKey == null) { continue; } else { posMap.put(ceilingKey, posMap.get(ceilingKey) - 1); if (posMap.get(ceilingKey) == 0) { posMap.remove(ceilingKey); } groups++; } } int posSize = posMap.values().stream().mapToInt(e -> e).sum(); groups += (posSize + zeros) / 2; return groups; } private static void getIndexes(String s, PrintWriter out) { char first = s.charAt(0); char second = s.charAt(s.length() - 1); List<Node> nodes = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if ((ch >= first && ch <= second) || (ch <= first && ch >= second)) { nodes.add(new Node(ch, i + 1)); } } Collections.sort(nodes, (a, b) -> (a.ch - b.ch)); out.println(Math.abs(first - second) + " " + nodes.size()); StringBuilder strBd = new StringBuilder(); strBd.append(1).append(" "); if (first < second) { for (Node node : nodes) { if (node.pos == 1 || node.pos == s.length()) { continue; } strBd.append(node.pos).append(" "); } } else { for (int i = nodes.size() - 1; i >= 0; i--) { Node node = nodes.get(i); if (node.pos == 1 || node.pos == s.length()) { continue; } strBd.append(node.pos).append(" "); } } strBd.append(s.length()); out.println(strBd); } private static String getStr(String s) { StringBuilder strBd = new StringBuilder(); Set<Integer> zeros = new HashSet<>(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '0') { if (i + 1 < s.length() && s.charAt(i + 1) == '0') { continue; } zeros.add(i); } } for (int i = 0; i < s.length(); i++) { if (zeros.contains(i)) { continue; } else if (zeros.contains(i + 2)) { String sub = s.charAt(i) + "" + s.charAt(i + 1); int val = Integer.valueOf(sub); strBd.append((char)('a' + --val)); i += 2; } else { String sub = s.charAt(i) + ""; int val = Integer.valueOf(sub); strBd.append((char)('a' + --val)); } } return strBd.toString(); } private static int getEle(int a, int b, int c) { int first = Math.abs(a - 1); int second = Math.abs(c - 1) + Math.abs(c - b); if (first == second) { return 3; } return first < second ? 1 : 2; } } class Node { char ch; int pos; public Node(char ch, int pos) { this.ch = ch; this.pos = pos; } }
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 17
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
2da4e9630cdc48ccedb68442ab035162
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 FriendsAndTheRestaurant { static FastReader in = new FastReader(); static int npos; //number of nonnegative yi-xis; static int nneg; //number of negative yi-xis static int[] pos; //array for nonnegative yi-xis; static int[] neg; //array for negative yi-xis; private static void inputs() { int n = in.nextInt(); npos = 0; nneg = 0; int[] nn = new int[n]; for (int i = 0; i < n; i++) { nn[i] = in.nextInt(); nn[i] *= -1; } for (int i = 0; i < n; i++) { int yi = in.nextInt(); nn[i] += yi; if (nn[i] >= 0) { npos++; } else { nneg++; } } pos = new int[npos]; neg = new int[nneg]; npos = 0; nneg = 0; for (int i = 0; i < n; i++) { if (nn[i] >= 0) { pos[npos] = nn[i]; npos++; } else { neg[nneg] = nn[i]; nneg++; } } } private static void calcs() { Arrays.sort(pos); Arrays.sort(neg); int count = 0; int unpairedpositives = 0; int nn = 0; for (int pp = npos-1; pp >= 0; pp--) { boolean paired = false; for (int np = nn; np < nneg; np++) { if (pos[pp] + neg[np] >= 0) { nn = np+1; count++; paired = true; break; } } if (!paired) { unpairedpositives++; } } unpairedpositives/=2; count += unpairedpositives; System.out.println(count); } private static void solve() { inputs(); calcs(); } public static void main(String[] args) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(); } } } 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 17
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
751a848371f616ffc6bdad3291afc563
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 Solution { 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; } } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } static void print(long []arr) { for(long a : arr) System.out.print(a + " "); System.out.println(); } static void print(int []arr) { for(int a : arr) System.out.print(a + " "); System.out.println(); } static void print(long a) { System.out.print(a); } static void printl(long a) { System.out.println(a); } static void printl(char ch) { System.out.println(ch); } static void print(char ch) { System.out.print(ch); } static void printl(String s) { System.out.println(s); } static void print(String s) { System.out.print(s); } static int[] intArray(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); return arr; } static long[] longArray(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextLong(); return arr; } static double[] doubleArray(int n) { double arr[] = new double[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextDouble(); return arr; } static int[][] intMatrix(int n, int m) { int mat[][] = new int[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextInt(); return mat; } static long[][] longMatrix(int n, int m) { long mat[][] = new long[n][m]; for(int i = 0; i < mat.length; i++) for(int j = 0; j < mat[0].length; j++) mat[i][j] = sc.nextLong(); return mat; } static class IntPair { int v1, v2; IntPair(int v1, int v2) { this.v1 = v1; this.v2 = v2; } } static class LongPair { long v1, v2; LongPair(long v1, long v2) { this.v1 = v1; this.v2 = v2; } } static FastReader sc = new FastReader(); public static void main(String[] args) { int tc = sc.nextInt(); while(tc-- > 0) { int n = sc.nextInt(); long []x = longArray(n); long []y = longArray(n); int days = 0; long []diff = new long[n]; for(int i = 0; i < n; i++) diff[i] = y[i] - x[i]; // print(diff); Arrays.sort(diff); int l = 0, r = n - 1; while(l < r) { if(diff[l] + diff[r] >= 0) { days++; l++; r--; } else l++; } printl(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 17
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
bd660faa89d280d0653f275a899a64f0
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.spi.AbstractResourceBundleProvider; public class codeforces { class Pair<I extends Number, I1 extends Number> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } 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(); } for(int i=0;i<n;i++){ b[i] = sc.nextInt(); } int sa[] = new int[n]; for(int i=0;i<n;i++){ sa[i] = a[i] - b[i]; } Arrays.sort(sa); int i=0;int j=n-1;int cnt=0; while(i<j){ while(i<j){ if(sa[i]+sa[j]<=0){ j--; cnt++; break; } 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 17
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
894c908b7dfc8ca6a5da25621831bdd5
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.HashMap; import java.util.Scanner; public class PermutationOperation { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int cases = sc.nextInt(); for(int i = 0; i<cases;i++){ int n = sc.nextInt(); int teams = 0; int[] pay = new int[n]; int[] budge = new int[n]; for(int j = 0;j<n;j++){ pay[j] = sc.nextInt(); } for(int j = 0;j<n;j++){ budge[j] = sc.nextInt(); pay[j] = budge[j]-pay[j]; } Arrays.sort(pay); if(pay[pay.length-1]<0){ System.out.println(0); }else { int s = 0; int e = n-1; while(s<e){ if(pay[s]+pay[e]>=0){ teams++; e--; } s++; } System.out.println(teams); } } } }
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 17
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
5c39934944f0c42bf5b84d2a6cb4a1d7
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.concurrent.atomic.LongAdder; import java.lang.*; import java.math.BigInteger; import java.io.*; public class Solution { static int sieve[]; public static void sieve_initilize(int n) { sieve = new int[n+1]; sieve[2] = 1; for(int i = 0;i<sieve.length;i++) sieve[i] = i; for (int i = 2; i * i < n+1; i++) { if (sieve[i] == i) { for (int j = i * i; j < n+1; j = j + i) { if (sieve[j] == j) sieve[j] = i; } } } } public static long lcm(long x, long y) { return (x * y) / gcd(x, y); } public static long gcd(long x, long y) { if (x == 0) return y; // if(y == 0) // return x; return gcd(y % x, x); } // public static long pow(long x , long n) // { // } public static class Pair{ char a; int idx; public Pair (char a , int idx) { this.a = a; this.idx =idx; } } public static void main(String args[]) { Scanner obj = new Scanner(System.in); int t = 1; t = obj.nextInt(); obj.nextLine(); while (t-- > 0) { int n = obj.nextInt(); obj.nextLine(); ArrayList<Integer> pay = new ArrayList<>(); for(int i = 0;i<n;i++) pay.add(obj.nextInt()); obj.nextLine(); for(int i = 0;i<n;i++) pay.set(i , obj.nextInt() - pay.get(i)); Collections.sort(pay); int l = 0; int r = pay.size() - 1; int count = 0; while(l < r) { if(pay.get(l) + pay.get(r) >= 0) { count++; l++; r--; } else if(pay.get(l) + pay.get(r) < 0) l++; } System.out.println(count); } obj.close(); } } // public static int check(int[] p, int k, int d, ArrayList<Integer> e) { // p[k - 1] = p[k - 1] - d; // return e.size(); // }
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 17
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
97a7ad4d43a815e85164a06d8a53671c
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.math.*; import java.io.*; public class A1{ static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); static StringTokenizer st; static final long mod=1000000007; static final int mod1=998244353; public static void Solve() throws IOException{ st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int[] ar=getArrIn(n); int[] arr=getArrIn(n); int[] dif=new int[n]; for(int i=0;i<n;i++){ dif[i]=ar[i]-arr[i]; } Arrays.sort(dif); int pair=0,i=0,j=n-1; while(i<j){ int t=dif[i]+dif[j]; if(t<=0){ i++;j--;pair++; } else j--; } bw.write(pair+"\n"); } /** Main Method**/ public static void main(String[] YDSV) throws IOException{ //int t=1; int t=Integer.parseInt(br.readLine()); while(t-->0) Solve(); bw.flush(); } /** Helpers**/ private static char[] getStr()throws IOException{ return br.readLine().toCharArray(); } 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[] getArrIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); int[] ar=new int[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static Integer[] getArrInP(int n) throws IOException{ st=new StringTokenizer(br.readLine()); Integer[] ar=new Integer[n]; for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken()); return ar; } private static long[] getArrLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); long[] ar=new long[n]; for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken()); return ar; } private static List<Integer> getListIn(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken())); return al; } private static List<Long> getListLo(int n) throws IOException{ st=new StringTokenizer(br.readLine()); List<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken())); return al; } private static long pow_mod(long a,long b) { long result=1; while(b!=0){ if((b&1)!=0) result=(result*a)%mod; a=(a*a)%mod; b>>=1; } return result; } private static int pow_mod(int a,int b) { int result=1; int mod1=(int)mod; while(b!=0){ if((b&1)!=0) result=(result*a)%mod1; a=(a*a)%mod1; b>>=1; } return result; } private static int lower_bound(int a[],int x){ 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+1; } private static long lower_bound(long a[],long x){ 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+1; } private static int upper_bound(int a[],int x){ 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 upper_bound(long a[],long x){ 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 boolean Sqrt(int x){ int a=(int)Math.sqrt(x); return a*a==x; } private static boolean Sqrt(long x){ long a=(long)Math.sqrt(x); return a*a==x; } }
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 17
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
00a6ed74fc2e9ce80a87d645f775d23e
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{ public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while(t-->0){ int i,n = sc.nextInt(); int [] arr = new int[n]; for(i=0;i<n;i++){ arr[i] = -1* (sc.nextInt()); } ArrayList<Integer> pos = new ArrayList<>(); ArrayList<Integer> neg = new ArrayList<>(); for(i=0;i<n;i++){ arr[i] = arr[i]+ sc.nextInt(); if(arr[i]>=0) pos.add(arr[i]); else neg.add(arr[i]); } int ans = 0; Collections.sort(pos,(a,b)->b-a); Collections.sort(neg); while(pos.size()>0){ if(neg.size()==0){ ans = ans + (pos.size()/2); pos.clear(); break; } int c1 = pos.get(0); int c2= neg.get(0); if(c1+c2>=0){ ans++; pos.remove(0); neg.remove(0); } else{ neg.remove(0); } } out.println(ans); } out.close(); } public static PrintWriter out; public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["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 17
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
83127f9d7ebdee3ba73ae7324f5c625b
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 cf1729.d; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class D { static class Friend implements Comparable<Friend> { int x, y; @Override public int compareTo(Friend o) { return (this.y - this.x) - (o.y - o.x); } public String toString() { return "(" + x + " " + y + ")"; } } public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); Friend[] friends = new Friend[n]; for (int i = 0; i < n; i++) { friends[i] = new Friend(); friends[i].x = s.nextInt(); } for (int i = 0; i < n; i++) { friends[i].y = s.nextInt(); } int result = cf1729d(n, friends); System.out.println(result); } s.close(); } private static int cf1729d(int n, Friend[] friends) { // System.out.println(Arrays.toString(friends)); Arrays.sort(friends, Collections.reverseOrder()); // System.out.println(Arrays.toString(friends)); int l = 0, r = n - 1, count = 0; while (l < r) { int sumx = friends[l].x + friends[r].x; int sumy = friends[l].y + friends[r].y; if (sumx > sumy) { r--; } else if (sumx <= sumy) { l++; r--; count++; } } return 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 17
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