1635: [Usaco2007 jan]tallest Cow the highest cow

Source: Internet
Author: User

1635: [Usaco2007 jan]tallest Cow Highest cow time limit:5 Sec Memory limit:64 MB
submit:383 solved:211
[Submit] [Status] Description

FJ ' s n (1 <= n <=) cows conveniently indexed 1..N is standing in a line. Each cow have a positive integer height (which is a bit of secret). You is 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 <=) lines of the form "cow sees cow 34". This means, cow are at the least as tall as cow, and that every cow between and have a height that's Strictly s Maller than that's cow 17. For each cow from 1..N, determine its maximum possible height, and such that all of the information given is still correct. It's guaranteed that it's possible to satisfy all the constraints.

There are N (1 <= n <= 10000) cattle from 1 to n linear arrangement, height per cow is h[i] (1 <= i <= N), now tell you that the maximum height of the cow is maxh, and there is a R-group relationship, each set of relationships enter two numbers, assuming A and B, The first cow can see the cow B, can see the condition is a, b between the height of the other cattle is strictly less than min (H[a], h[b]), and H[b] >= H[a]

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 Input9 3 5 5
1 3
5 3
4 3
3 7
9 8


INPUT DETAILS:

There is 9 cows, and the 3rd was the tallest with height 5.
Sample Output5
4
5
3
4
4
5
5
5
HINT Source

Silver

The puzzle: Okay, I admit I really catch the chicken. Phile: Is it funny? Hansbug: No, I hate it--I used to always use an array, the prefix of this array and what is actually represented is the desired value, which is used frequently when writing a tree array, until today I know this is a differential sequence (QAQ) ... And then this is nothing. Direct code--

1 var2 I,j,k,l,m,n,h,r:longint;3A:Array[0..20000,1..2] ofLongint;4B:Array[0..20000] ofLongint;5 procedureSwapvarx,y:longint);6           varZ:longint;7           begin8z:=x;x:=y;y:=Z;9           End;Ten  One proceduresort (l,r,z:longint); A           varI,j,x,y:longint; -           begin -i:=l;j:=r;x:=a[(L+r)Div 2, z]; the                Repeat -                       whileA[i,z]<x DoInc (i); -                       whileA[j,z]>x DoDec (j); -                      ifI<=j Then +                         begin -Swap (A[i,1],a[j,1]); +Swap (A[i,2],a[j,2]); A Inc (I);d EC (j); at                         End; -                untilI>J; -                ifL<j Thensort (l,j,z); -                ifI<r Thensort (i,r,z); -           End; - begin in readln (n,i,h,r); -       fori:=1  toR Do to          begin +READLN (A[i,1],a[i,2]); -               ifA[i,1]>a[i,2] ThenSwap (A[i,1],a[i,2]); the          End; *Sort1R1); $j:=1;Panax Notoginseng       fori:=1  tor+1  Do -          begin the               ifA[i,1]<>a[j,1] Then +                  begin ASort (j,i-1,2); thej:=i; +                  End; -          End; $       fori:=1  toR Do $          begin -               if(A[i,1]=a[i-1,1]) and(A[i,2]=a[i-1,2]) Thencontinue; -Inc (B[a[i,1]+1]);d EC (B[a[i,2]]); the          End; -l:=0;Wuyi       fori:=1  toN Do the          begin -l:=l+B[i]; WuWriteln (-l+h); -          End; About End.

1635: [Usaco2007 jan]tallest Cow the highest cow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.