POJ3263 Tallest Cow 差分

题目描述

FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of that cow.

FJ has made a list of R (0 ≤ R ≤ 10,000) lines of the form "cow 17 sees cow 34". This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.

For each cow from 1..N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

Input

Line 1: Four space-separated integers: N, I, H and R
Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ A, BN), indicating that cow A can see cow B.

Output

Lines 1..N: Line i contains the maximum possible height of cow i.

Sample Input

9 3 5 5
1 3
5 3
4 3
3 7
9 8

Sample Output

5
4
5
3
4
4
5
5
5

分析

一句话题意:给出n头牛的身高,和m对关系(a[i]b[i]可以相互看见。即他们中间的牛都比他们矮)。已知最高的牛为第p头,身高为h,求每头牛的身高最大可能是多少。

因为我们要是每一头牛的身高尽量高,所以我们初始的时候设每一头牛的身高都为(h)

因为两头牛(x,y)之间可以相互看到,所以我们需要把区间([x+1,y-1])内牛的身高都减去1

直接操作的话显然会T掉,所以我们可以用差分来维护

注意重复的操作要判断一下

代码

#include<cstdio>
#include<map>
#include<iostream>
#include<algorithm>
#include<utility>
using namespace std;
const int maxn=1e6+5;
map<pair<int,int>,bool> ma;
int d[maxn];
int main(){
    int n,m,h,r;
    scanf("%d%d%d%d",&n,&m,&h,&r);
    for(int i=1;i<=r;i++){
        int aa,bb;
        scanf("%d%d",&aa,&bb);
        if(aa>bb) swap(aa,bb);
        if(ma[make_pair(aa,bb)]) continue;
        d[aa+1]--,d[bb]++;
        ma[make_pair(aa,bb)]=1;
    }
    for(int i=1;i<=n;i++){
        d[i]+=d[i-1];
        printf("%d
",h+d[i]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liuchanglc/p/12879622.html