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.c++
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.spa
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.code
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.orm
Lines 1..N: Line i contains the maximum possible height of cow i.input
9 3 5 5 1 3 5 3 4 3 3 7 9 8
5 4 5 3 4 4 5 5 5
给出n
头牛的身高,和m
对关系(a[i]
能看见b[i]
。即他们中间的牛都比他们矮,且a[ i ] <= b[ i ]。已知最高的牛为第p
头,身高为h
。求每头牛的身高最大多是多少。string
首先总体说一下作法:it
n 头奶牛初始化为 H ,io
对于每一个限制,区间内若是有一头奶牛高度过高超限,则整个区间中各个元素 -1 ;continue;ast
最后输出获得的每一个奶牛高度.form
具体说一下:
有点偏贪心,首先将全部奶牛初始化为H,也就是最大值(咱们能够假设一开始没有限制,这就是最优解).对于每次的两只奶牛x 和 y,咱们只需将x 和 y之间的奶牛处理好就能够,若是中间有一只奶牛 i 的高度 \(a_i\) >= \(a_x\),那么咱们要将这个区间[ x+1, y-1 ]全部的奶牛都 -1,为何是整个区间而不是这一个第 i 头呢?
有一个条件不知道你们有木有发现,全部的限制的区间不会交叉,就是说不会出现好比3 看到 8, 5 看到 9,由于这是互相矛盾的,8左侧的点(包括3的右边,8的左边)严格小于8,因此5不可能越过8看到9.如此来看,这些区间就有了一些规律可循.(固然题目保证数据合法)
若是对于每次输入的区间处理中,咱们在其中发现了一个超限的点,咱们若是只修改它,那么这个区间内的原始相对关系可能被打破,若是题目中有这个区间内部的一个区间的限制的话就会错,因此咱们把这一整个区间都 -1 以维持相对的内部关系,而减的是1又保证了最优性,此题得解.
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn=10000+5; int a[maxn],n,r; int main(){ // freopen("1.in","r",stdin); int x,y,p; scanf("%d%d%d%d",&n,&x,&y,&r); for(int i=1;i<=n;++i)a[i]=y; for(int i=1;i<=r;++i){ scanf("%d%d",&x,&y);p=x; if(x>y)swap(x,y); for(int i=x+1;i<=y-1;++i) if(a[i]>=a[p]){ for(int i=x+1;i<=y-1;++i) a[i]--; break; } } for(int i=1;i<=n;++i)printf("%d\n",a[i]); return 0; }
hzoi