D. Friends and the Restaurant
A group of nn friends decide to go to a restaurant. Each of the friends plans to order meals for xixi burles and has a total of yiyi burles (1≤i≤n1≤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 xixi values in the group must not exceed the sum of yiyi values in the group.
What is the maximum number of days friends can visit the restaurant?
For example, let there be n=6n=6 friends for whom xx = [8,3,9,2,4,58,3,9,2,4,5] and yy = [5,3,1,4,5,105,3,1,4,5,10]. Then:
- first and sixth friends can go to the restaurant on the first day. They will spend 8+5=138+5=13 burles at the restaurant, and their total budget is 5+10=155+10=15 burles. Since 15≥1315≥13, they can actually form a group.
- friends with indices 2,4,52,4,5 can form a second group. They will spend 3+2+4=93+2+4=9 burles at the restaurant, and their total budget will be 3+4+5=123+4+5=12 burles (12≥912≥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 22. Friends will visit the restaurant for a maximum of two days. Note that the 33-rd friend will not visit the restaurant at all.
Output the maximum number of days the friends can visit the restaurant for given nn, xx and yy.
Input
The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — 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 nn (2≤n≤1052≤n≤105) — the number of friends.
The second line of each test case contains exactly nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤1091≤xi≤109). The value of xixi corresponds to the number of burles that the friend numbered ii plans to spend at the restaurant.
The third line of each test case contains exactly nn integers y1,y2,…,yny1,y2,…,yn (1≤yi≤1091≤yi≤109). The value yiyi corresponds to the number of burles that the friend numbered ii has.
It is guaranteed that the sum of nn values over all test cases does not exceed 105105.
Output
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.
题意:两个数组,长度为n,第一个数组为实际花费,第二个数组为预算花费。
要求,至少两个人才能去餐厅一天,且他们的实际花费之和 <= 预算花费之和。一个人只能去一次餐厅,输出最多可以去餐厅几天。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t; cin >> t;
while(t--){
int n; cin >> n;
vector<ll> x(n),y(n),a,b;
for(int i = 0;i < n;i++) cin >> x[i];
int sum=0;
for(int i = 0;i < n;i++){
cin >> y[i];
x[i]=y[i] - x[i]; // 差值
if(x[i] > 0) a.push_back(x[i]);
else if(x[i] == 0) sum++; //实际花费等于预算, 单独计数
else b.push_back(x[i]);
}
sort(a.begin(),a.end()); //升序
sort(b.rbegin(),b.rend()); // 降序
ll ans=0;
int l=0,r=b.size();
for(int i = 0;i < a.size();i++){
if(l < r){
if(b[l] + a[i] >=0 ){ //满足答案要求
ans++;
l++;
}
else sum++; //不满足答案要求,但a[i]是正数,对答案有影响,计数(两个正的满足答案要求)
}
else sum++; //正数多 ,计数
}
cout<< ans + sum/2 <<endl; // 需要计数的最少两个一起才能满足答案要求,所以/2
}
return 0;
}