codeforces 599C (树状数组)

题意是给你n个城堡,最多能把他们分成几个连续的区间使得每个区间按照高度排序完以后整体高度也是升序

用某种数据结构维护一下去前缀和就行了

#include <bits/stdc++.h>
using namespace std;
#define maxn 111111

int c[maxn];
struct node {
    int num, pos;
    bool operator < (node a) const {
        return num < a.num || (num == a.num && pos < a.pos);
    }
}a[maxn];
int n;

int lowbit (int x) {
    return x & (-x);
}

void add (int pos) {
    for (int i = pos; i <= n; i += lowbit (i))
        c[i]++;
}

int sum (int pos) {
    int ans = 0;
    for (int i = pos; i > 0; i -= lowbit (i))
        ans += c[i];
    return ans;
}

int main () {
    scanf ("%d", &n);
    memset (c, 0, sizeof c);
    for (int i = 1; i <= n; i++) {
        scanf ("%d", &a[i].num);
        a[i].pos = i;
    }
    sort (a+1, a+1+n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        add (a[i].pos); //cout << "sum:" << sum (i) << endl;
        if (sum (i) == i)
            ans++;
    }
    cout << ans << endl;
    return 0;
}


你可能感兴趣的:(codeforces 599C (树状数组))