cf820(Div.4)D. Friends and the Restaurant

原题链接

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.

Input
The first line of the input contains an integer t (1≤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 n (2≤n≤105) — the number of friends.
The second line of each test case contains exactly n integers x1,x2,…,xn (1≤xi≤109). 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≤109). 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 105.

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.

思路:这道题答题思路是贪心,在给定预定资金的情况下访问的最大天数。
解法:这道题是让我们找出每天至少两个朋友(每个人最多参加一次)参加聚餐,最大的聚餐天数。题意两个数组,第一个数组是每个人的花费,第二个数组是每个人的预算资金,既然我们想去聚餐,我们的资金必须大于等于花费,所以维护一个数组记录花费与资金的差值,然后对数组排序,利用贪心的思想,从最小的开始每次跟数组没有更新过的最大值相加大于等于0则这两个人就可以去聚餐,天数加一,更新数组内没有去聚餐资金的最大值。
Accode:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;

int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n;
        cin>>n;
        int A[N],B[N];
        for(int i=0;i<n;i++)cin>>A[i];
        for(int i=0;i<n;i++)
        {
    
    
            int x;
            cin>>x;
            A[i]=x-A[i];
        }
        int cnt=0;
        int j=n-1;
        sort(A,A+n);
        for(int i=0;i<j;i++)
        {
    
    
            if(A[i]+A[j]>=0)
            {
    
    
                cnt++;
                j--;
            }
        }
        cout<<cnt<<endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Stephen_Curry___/article/details/126825625