Lovers(ACM-ICPC 2017 Asia Xi'an)

这篇博客介绍了ACM-ICPC 2017 Asia Xi'an竞赛中的一道问题,涉及到情侣配对的算法。问题要求根据女孩和男孩的价值匹配他们,条件是两者价值之和大于等于k。博主提供了输入输出格式及样例,并分享了求解思路,即对两个数组进行降序排序,然后用a数组从前向后,b数组从后向前寻找满足条件的配对。
摘要由CSDN通过智能技术生成

One day nn girls and nn boys come to Xi’an to look for a mate . Each girl has a value a[i] , each boy has a value b[i] . Girl i and boy j will fall in love only if a[i]+b[j]≥k .

Please help them make pairs as many as possible .

Input

Several test cases .

First line an integer T(1≤T≤10) . Indicates the number of test cases.

Then TT test cases follows . Each test case begins with two integer N, K(1≤N≤200000,0≤K≤109) . The next line has NN integers indicate a[1] to a[N](0≤a[i]≤109) . The next line has N integers indicate b[1] to b[N](0≤b[i]≤109)

Output

For each test case , print the answer in a single line.

样例输入复制

1
3 4
1 2 3
1 2 3

样例输出复制

3

思路:将两个数组都从大到小排序后,a数组从前往后找,b数组从后往前找,找到一个满足条件的b数组的j才变化。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int mod=1e9+7;
const int M=2e5 + 10;
ll a[M], b[M];
int main()
{
    int t;
    ll n, k;
    scanf("%d", &t);
    while(t--){
        scanf("%lld%lld", &n, &k);
        for (int i = 0; i < n; i++){
            scanf("%lld", &a[i]);
        }
        for (int i = 0; i < n; i++){
            scanf("%lld", &b[i]);
        }
        sort(a, a + n);
        sort(b, b + n);
        ll ans = 0;
        int j = n - 1;
        for (int i = 0; i < n; i++)
        {
            if(a[i]+b[j]>=k){
                j--;
                ans++;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值