Make Rounddog Happy (2019 Nian Hangzhou electric multi-tenth school field 1011 + HDU6701 + heuristic partition)

Topic Link

Portal

The meaning of problems

Seeking to meet the number of subintervals \ (a_l, a_ {l + 1}, \ dots, a_r \) are not the same and \ (max (a_l, a_ { l + 1}, \ dots, a_r) - (r-l +1) \ Leq K \) .

Thinking

I heard heuristic partition and then went to learn how to set the next board, wrote a local twist can not be over-sample the game, after the game change overnight.

Heuristic idea of this partition in question is in the process looks \ ([l, r] \ ) to find the interval maximum value of the position \ (MID \) , then look at the left half of the section length shorter or short right part, and violence that part of the contribution of statistics short.

In the first preprocessed \ (I \) as the left point, right point is not within the interval the same number of \ (R [i] \) and in \ (I \) left point of the right endpoint of the interval is not the same number of \ (L [I] \) , then \ (ST \) table processing interval maximum value of the position.

Because at the time of partition in which we know the maximum position, then when the count contributions (take a left part as an example) the enumeration of inequality \ (L \) , and then calculates the recently transposed the right spot to meet the meaning of the title \ (R & lt \ GEQ a [MID]. 1 -K-L + \) , then for \ (L \) is not the same number of points in the right left point range \ (R & lt [L] \) (meaning the most satisfying title the far right end point) to subtract this value is the contribution, the complexity of the left point to \ (O (nlog (the n-)) \) .

Code

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 300000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

LL ans;
int t, n, K;
int a[maxn], L[maxn], R[maxn], vis[maxn], dp[maxn][20], pos[maxn][20];

void init() {
    for(int j = 1; j < 20; ++j) {
        if(1<<(j-1) > n) break;
        for(int i = 1; i + (1<<j) - 1 <= n; ++i) {
            if(dp[i][j-1] >= dp[i+(1<<(j-1))][j-1]) {
                dp[i][j] = dp[i][j-1];
                pos[i][j] = pos[i][j-1];
            } else {
                dp[i][j] = dp[i+(1<<(j-1))][j-1];
                pos[i][j] = pos[i+(1<<j-1)][j-1];
            }
        }
    }
}

int query(int l, int r) {
    int k = log(r - l + 1) / log(2);
    if(dp[l][k] >= dp[r-(1<<k)+1][k]) return pos[l][k];
    else return pos[r-(1<<k)+1][k];
}

void solve(int l, int r) {
    if(l > r) return;
    int mid = query(l, r);
    if(r - mid > mid - l) {
        for(int i = l; i <= mid; ++i) {
            int rs = a[mid] - K + i - 1;
            rs = max(rs, mid);
            int dd = min(r, R[i]);
            if(rs > dd) continue;
            ans += dd - rs + 1;
        }
    } else {
        for(int i = mid; i <= r; ++i) {
            int ls = K - a[mid] + i + 1;
            ls = min(ls, mid);
            int dd = max(l, L[i]);
            if(ls < dd) continue;
            ans += ls - dd + 1;
        }
    }
    solve(l, mid - 1), solve(mid + 1, r);
}

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &K);
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            dp[i][0] = a[i];
            pos[i][0] = i;
            vis[i] = 0;
        }
        int r = 2;
        vis[a[1]] = 1;
        for(int i = 1; i <= n; ++i) {
            while(r <= n && !vis[a[r]]) {
                vis[a[r]] = 1;
                ++r;
            }
            vis[a[i]] = 0;
            R[i] = r - 1;
        }
        vis[a[n]] = 1;
        int l = n - 1;
        for(int i = n; i >= 1; --i) {
            while(l >= 1 && !vis[a[l]]) {
                vis[a[l]] = 1;
                --l;
            }
            vis[a[i]] = 0;
            L[i] = l + 1;
        }
        init();
        ans = 0;
        solve(1, n);
        printf("%lld\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Dillonh/p/11390927.html