洛谷P2879 [USACO07JAN]区间统计Tallest Cow

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
",F[i]+h);
34     }
35     return 0;
36 }
原文地址:https://www.cnblogs.com/SovietPower/p/6894216.html