POJ3263 Tallest Cow 【差分,思维】

根据题目很容易想到,可以把初始身高都设置为H,A能看到B的话,只需要把[A+1,B-1]里面的牛身高都减一就可以啦~

但现在就存在一些问题:若A可以看到B,那么A的身高必须要<=B,而且中间的牛也可能会比A高,只是-1不可行,怎么办呢?

实际上,A的身高本来就必定不可能大于B。因为两对A_x,B_x不能存在交叉,只能像括号序列一样一一匹配。所以既然不可能会有不同的A_x,B_x对跨过当前的AB,那么他们的最大高度一定是相同的。同理,[A+1,B-1]里面也是只能比A低不能比A高,所以就可以放心地差分啦~

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10000 + 5;

int n, I, H, r, sum[N];

map <pair <int, int>, bool> _exist;

int main () {
    // freopen ("data.in", "r", stdin);
    cin >> n >> I >> H >> r;
    sum[1] = H;
    while (r--) {
        int x, y;
        cin >> x >> y;
        if (x > y) swap (x, y);
        if (_exist.count(make_pair (x, y))) {
            continue;
        } 
        _exist[make_pair(x, y)] = true;
        sum[x + 1] -= 1;
        sum[y] += 1;
    }
    cout << sum[1] << endl;
    for (int i = 2; i <= n; ++i) {
        // cout << "sum[" << i << "] = " << sum[i] << endl;
        sum[i] += sum[i - 1];
        cout << sum[i] << endl;
    }
}
posted @ 2020-10-10 13:14  maomao9173  阅读(64)  评论(0编辑  收藏  举报