1. 程式人生 > >洛谷P2879 [USACO07JAN]區間統計Tallest Cow

洛谷P2879 [USACO07JAN]區間統計Tallest Cow

ive it is correct %d ref list lis swa names

To 洛谷.2879 區間統計

題目描述

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.

給出牛的可能最高身高,然後輸入m組數據 a b,代表a,b可以相望,最後求所有牛的可能最高身高輸出

輸入輸出格式

輸入格式:

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.

輸出格式:

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

輸入輸出樣例

輸入樣例#1:
9 3 5 5
1 3
5 3
4 3
3 7
9 8
輸出樣例#1:
5
4
5
3
4
4
5
5
5

代碼:

 1 #include<cstdio>
 2 #include<map>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N=10005;
 6 
 7 int n,i,h,r,S[N],F[N];
 8 map<int,bool>Mp[N];
 9 
10 void read(int &now)
11 {
12     now=0;char c=getchar();
13     while(c<0||c>9)c=getchar();
14     while(c>=0&&c<=9)now=(now<<3)+(now<<1)+c-0,c=getchar();
15 }
16 
17 int main()
18 {
19     read(n);read(i);read(h);read(r);
20     while(r--)
21     {
22         int a,b;
23         read(a);read(b);
24         if(a>b) swap(a,b);
25         if(Mp[a][b])//判重,防止 5-3 和 3-5這種情況 
26           continue;
27         Mp[a][b]=1;
28         --S[a+1];++S[b];//前綴和,該區間-1 
29     }
30     for(int i=1;i<=n;++i)
31     {
32         F[i]=S[i]+F[i-1];
33         printf("%d\n",F[i]+h);
34     }
35     return 0;
36 }

洛谷P2879 [USACO07JAN]區間統計Tallest Cow