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