B. Range and Partition

Codeforces Round #768 (Div. 1)

其实这题的思路很简单,二分区间+双指针划分区间 但是有一个结论有点难想

那就是处于[x,y]区间内的数大于不处于该区间的数的数量+k时,无论怎样排列数据,都有至少k个区间是满足要求的,那么我们怎么证明呢?

贪心证明,每次都一直拿拿到范围内的数大于范围外的数,最多大一分开,因此大k即可无论以任意排序下分为k个合法子数组。

ps:最后根据二分出来的范围划分子数组也是这样的贪心写法,特判一下最后一个区间直接右边界为n就行

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 3 * N, B = N;
const int INF = 0x3f3f3f3f;
const ll LLINF = 1e18;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int a[N];
int heap[N];
int s[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)res = (1ll * res * x) % mod;
        x = (1ll * x * x) % mod;
        n >>= 1;
    }
    return res;
}

void init() {}

bool check(int x,int y)
{
    return s[y] - s[x-1] - (n - s[y] + s[x-1]) >= k;
}

void solve()
{
    si(n); si(k);
    pre(i, 1, n) heap[i] = 0;
    pre(i, 1, n) {
        si(a[i]);
        heap[a[i]]++;
    }
    
    pre(i, 1, n) s[i] = s[i - 1] + heap[i];
    
    int y = INF, x = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[n] - s[i-1] -(n-s[n]+s[i-1]) < k)continue;

        int ll = i, rr = n;
        while (ll<rr)
        {
            int mid = ll + rr >> 1;
            if (check(i, mid)) rr = mid;
            else ll = mid + 1;
        }
        if (rr - i < y - x)x = i, y = rr;
    }
    cout << x << ' ' << y << endl;

    vector<PII> subarr;
    for (int i = 1; i <= n; i++)
    {
        if (subarr.size() == k - 1) {
            subarr.push_back({ i, n });
            break;
        }
        PII t; t.x = i;
        int cnt = (a[i] >= x && a[i] <= y) ? 1 : -1, j = i;
        while (j < n && cnt <= 0)
        {
            j++;
            cnt += (a[j] >= x && a[j] <= y) ? 1 : -1;
        }
        t.y = j;
        subarr.push_back({ i,j });i = j;
    }
    for (auto& [l, r] : subarr) cout << l << ' ' << r << endl;
    return;
}

signed main()
{
    int _;
    cin >> _;
    //_ = 1;
    init();
    while (_--)
    {
        solve();
    }
    return 0;
}

量条件的情况下最多只能划分出少于k个区间,由于

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值