# 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛（差分序列）

http://www.lydsy.com/JudgeOnline/problem.php?id=1635

h[a+1]~a[b-1]都是比h[a]和h[b]小，那么最佳方案就是将次区间的所有高度-1，那么我们就将整个区间-1

（脑补：假设一般情况下h[a]==h[b]的，而却有c使得(a, c), pos[c]>pos[b]，那么这样b显然比a和c低，这样h[a]==h[b]的情况就打破了，也就是我们按照交换过来后的差分是成立的）

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=10005;
int f[N], n, h, r;
struct dat { int x, y; }a[N];
bool cmp(const dat &a, const dat &b) { return a.x==b.x?a.y<b.y:a.x<b.x; }
int main() {
int tp;
for1(i, 1, r) {
int x=getint(), y=getint();
if(y<x) swap(x, y); a[i].x=x, a[i].y=y;
}
sort(a+1, a+1+r, cmp);
a[0].x=-10;
for1(i, 1, r) {
if(a[i].x==a[i-1].x && a[i].y==a[i-1].y) continue;
--f[a[i].x+1]; ++f[a[i].y];
}
int sum=0;
for1(i, 1, n) {
sum+=f[i];
printf("%d\n", h+sum);
}
return 0;
}


## Description

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, B <= N), 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

INPUT DETAILS:

There are 9 cows, and the 3rd is the tallest with height 5.

5
4
5
3
4
4
5
5
5

## Source

posted @ 2014-09-07 14:19  iwtwiioi  阅读(601)  评论(0编辑  收藏  举报