POJ 3263 Tallest Cow (前缀和+思维)

版权声明:希望能在自己成长的道路上帮到更多的人,欢迎各位评论交流 https://blog.csdn.net/yiqzq/article/details/82596143

原题地址:http://poj.org/problem?id=3263

题意:一群奶牛排成一排。他们身高不同。 现在我们知道:有 n 头奶牛,第 I 头的身高最高,为 h
下面给出 m 组关系,每组关系包含两个数,表示这两个数代表的奶牛可以相互看到(两头奶牛之间的所有奶牛都比这两头奶牛矮才能相互看到) 题目要求我们给出所有n头奶牛的可能最大身高。

思路:利用差分的思想,如果l,r能够相互看到,那么说明 ( l , r ) 的值都要减1.

但是上述的复杂度过高,因此考虑前缀和的写法.我们对于 ( l , r ) 不再是枚举之后都减 1. 我们只对 l + 1 , 这个位置进行减 1 操作.那么在计算前缀和的时候,我们也能够影响到l + 1 以后的范围,最后只需要在r这个位置进行加1操作抵消掉减 1 的效果就行了.

这道题目重要的是思想:对一个区间的操作转化为左,右两个端点的操作.

#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <cctype>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 998244353;
int n, l, h, r;
int a[maxn];
typedef pair<int, int>pii;
set<pii>s;
int main() {
    scanf("%d%d%d%d", &n, &l, &h, &r);
    for (int i = 1; i <= r; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        if (u > v) swap(u, v);
        if (s.count(make_pair(u, v))) continue;
        s.insert(make_pair(u, v));
        a[u + 1]--;
        a[v]++;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += a[i];
        printf("%d\n", ans + h);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yiqzq/article/details/82596143