其他分享
首页 > 其他分享> > ICPC2021南京区域赛 L.Let's Play Curling(二分、思维、构造)

ICPC2021南京区域赛 L.Let's Play Curling(二分、思维、构造)

作者:互联网

#include<iostream>
#include<algorithm>
using namespace std;
#define debug(x) cout << "***" << x << endl
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int t;
int n, m;
int a[N], b[N];

int find_min(int k)
{
    int l = 1, r = n;
    while(l < r)
    {
        int mid = (l + r + 1) / 2;
        if(a[mid] < k) l = mid;
        else r = mid - 1;
    }
    return l;
}
int find_max(int k)
{
    int l = 1, r = n;
    while(l < r)
    {
        int mid = (l + r) / 2;
        if(a[mid] > k) r = mid;
        else l = mid + 1;
    }
    return l;
}

int main()
{
    cin >> t;
    while(t --)
    {
        int res = 0;
        cin >> n >> m;
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 1; i <= m; i++) cin >> b[i];
        //将红点与蓝点位置从小到大排序
        sort(a + 1, a + n + 1);
        sort(b + 1, b + m + 1);
        b[m+1] = inf; //b[0] = 1, b[m+1] = inf (构造两个特殊蓝点)
        for(int i = 0; i <= m; i++)
        {
            int l = find_max(b[i]); //找到大于区间左端点的第一个红点的位置
            int r = find_min(b[i+1]); //和找到第一个小于区间右端点的第一个红点的位置
            if(a[l] <= b[i] || a[r] >= b[i+1]) continue; //需要特判保证选出的红色冰壶必须在区间内(不能在区间上)
            res = max(res, r - l + 1);
        }
        if(res) cout << res << endl;
        else cout << "Impossible" << endl;
    }
    return 0;
}

/*
1
2 2
2 2
1 2
*/

标签:ICPC2021,Curling,int,位置,蓝点,Play,端点,区间,红点
来源: https://www.cnblogs.com/K2MnO4/p/14826316.html