cf

H. K and Medians

题意

给出1到n的数,可以任意取K位只要递增的数列(可以不连续)保留中位数,问是否可以变成m位的数组

题解

实际上只要考虑最后一个操作,

反过来相当于m位数组填坑,最后一个操作,在某个数左边右边添加任意(k - 1)/ 2位,就可以保证由整个数组的空位也可以,还要保证坑的数量mod (k - 1) = 0

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 10;
int a[MAX];

const int INF = 1e9 + 7;

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n, k, m;
        scanf("%d%d%d", &n, &k, &m);
        vector<int> ans;
        scanf("%d", &a[0]);
        if(a[0] > 1)
            ans.push_back(a[0] - 1);
        int end = m;
        for(int i = 1; i < m; i++) {
            scanf("%d", &a[i]);
            if(a[i] > a[i - 1] + 1) {
                ans.push_back(a[i] - a[i - 1] - 1);
            }
        }
        if(n > a[m - 1])
            ans.push_back(n - a[m - 1]);
        ll tot = 0;
        for(auto x : ans) {
            tot += x;
        }
        ll L = 0, R = 0;
        bool flag = 0;
        for(auto x : ans) {
            L += x;
            R = tot - L;
            if(L - k / 2 >= 0 && R - k / 2 >= 0 && tot % (k - 1) == 0) {
                flag = 1;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值