思维 ---- 两两匹配问题 2021杭电多校第6场 E - Median

题目链接
在这里插入图片描述


这种两两匹配问题很常见,打比赛的时候想了好久才想起来!,就要特判那个最大的集合,跟其他集合的关系


#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {
   x = 0;char ch = getchar();ll f = 1;
   while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
   while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
   read(first);
   read(args...);
}
int n, m;
int arr[maxn];
int main() {
    //IOS;
    int T;
    cin >> T;
    while(T --) {
        cin >> n >> m;
        for(int i = 1; i <= m; ++ i) cin >> arr[i];
        sort(arr+1,arr+1+m);
        if(m == n) {
            puts("YES");
            continue;
        }
        int max1 = -1, minnum;
        for(int i = 1; i <= m; ++ i)
          if(max1 <= arr[i] - arr[i-1] - 1) {
              max1 = arr[i] - arr[i-1] - 1;
              minnum = arr[i-1] + 1;
          }

        if(max1 <= n - arr[m]) {
              max1 = n - arr[m];
              minnum = arr[m] + 1;            
        }
        // cout << max1 << " " << minnum << endl;
        if(n - m - max1 >= max1) puts("YES");
        else {
           int pos = lower_bound(arr+1,arr+1+m,minnum) - arr;
        //    cout << pos << endl;
           if(pos - 1 >= max1 - (n - m - max1)) puts("YES");
           else puts("NO"); 
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值