Problem - D - Codeforces
codeforces.com
two pointer
- 제목
Friends and the Restaurant
- 조건
time limit per test : 2 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
A group of n friends decide to go to a restaurant. Each of the friends plans to order meals for xi burles and has a total of yi burles (1≤i≤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 xi values in the group must not exceed the sum of yi 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≥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≥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.
- 입력
The first line of the input contains an integer t (1≤t≤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≤n≤10^5) — the number of friends.
The second line of each test case contains exactly n integers x1,x2,…,xn (1≤xi≤10^9). The value of xi 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 y1,y2,…,yn (1≤yi≤10^9). The value yi 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.
- 출력
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.
예제 입력1 | 예제 출력1 |
6 6 8 3 9 2 4 5 5 3 1 4 5 10 4 1 2 3 4 1 1 2 2 3 2 3 7 1 3 10 6 2 3 6 9 5 7 3 2 7 10 6 10 6 5 4 2 1 8 100 1 1 1 1 1 200 6 1 4 1 2 4 2 1 3 3 2 3 4 |
2 0 1 3 1 3 |
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int main() {
fastio;
int tc, sz;
int consume[100000];
int price[100000];
int sum[100000];
cin >> tc;
while(tc--){
cin >> sz;
for(int n = 0 ; n < sz ; n++) cin >> consume[n];
for(int n = 0 ; n < sz ; n++) {
cin >> price[n];
sum[n] = price[n] - consume[n];
}
int cnt = 0, left = 0, right = sz - 1;
sort(sum, sum + sz);
while(left < right){
if(sum[right] < 0) break;
if(sum[right] + sum[left] >= 0){
cnt++;
right--;
}
left++;
}
cout << cnt << endl;
}
return 0;
}