倍增水题求条叫
查看原帖
倍增水题求条叫
386547
Infter楼主2024/9/8 21:06
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
#define debug(x) cerr << #x << " = " << x << endl
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define gn(u, v) for (int v : G.G[u])
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define tomin(x, y) ((x) = min((x), (y)))
#define tomax(x, y) ((x) = max((x), (y)))
#define pq priority_queue

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, k, cnt;
    cin >> n >> k;
    vi a(n * 2);
    vi pre(n * 2 + 1);
    rep(i, 0, n - 1)
        cin >> a[i];

    rep(i, n, n * 2 - 1)
        a[i] = a[i - n];
    n *= 2;
    pre[0] = 0;
    rep(i, 0, n - 1)
        pre[i + 1] = pre[i] + a[i];

    auto check = [&](int lim) {
        cnt = 0;
        vvi c(n, vi(__lg(k) + 3, -1));

        int j = -1;
        rep(i, 0, n - 1) {
            while (j < n && pre[j + 1] - pre[i] < lim)
                j++;
            if (j + 1 >= n)
                break;
            if (pre[j + 1] - pre[i] >= lim)
                c[i][0] = j + 1;
        }
        rep(i, 0, n - 1)
            rep(j, 1, __lg(k) + 2)
                if (~c[i][j - 1])
                    c[i][j] = c[c[i][j - 1]][j - 1];
        rep(i, 0, n / 2 - 1) {
            int x = i;
            per(j, __lg(k) + 2, 0)
                if ((k >> j) & 1 && ~x)
                    x = c[x][j];
            if (~x && x <= i + n / 2)
                cnt++;
        }
        return cnt > 0;
    };

    int now = 0;
    for (int i = (1 << 30); i; i >>= 1)
        if (!check(now += i))
            now -= i;
    
    cout << now << " " << n / 2 - cnt << endl;

    return 0;
}

要疯了。

2024/9/8 21:06
加载中...