题目链接
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.
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 Input
9 3 5 5
1 3
5 3
4 3
3 7
9 8
Sample Output
5
4
5
3
4
4
5
5
5
题意
有 n 头牛,编号 1~n ,才开始只知道最高的牛的高度编号,有 R 行输入 a 、b,表示 b 的高度至少和 a 一样,并且在 a 和 b 之间的牛的高度都严格小于 a,输出每个牛的最大高度
思路
才开始把每头牛的高度都看作最大高度,用一个前缀和数组表示每头牛相对于最大高度的变化,max_h - a[i] == 第 i 头牛的高度。
假设一组输入 x y(假设x<=y),那么我们只需要 让 a[x+1] += 1,a[y] -= 1,即可,因为它只牵扯到 (x,y) 区间内相对于 max_h 的减一。
注意判重。。。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=10010;
const int inf = 0x3f3f3f3f;
map<P,bool> mp;
int a[maxn];
int main(void)
{
int n,id,h,m;
int l,r;
scanf("%d%d%d%d",&n,&id,&h,&m);
for(int i=0;i<=n;i++)
a[i] = 0;
for(int i=0;i<m;i++){
scanf("%d%d",&l,&r);
if(l>r) swap(l,r); //转换
if(mp[P(l,r)]) continue; //判重
mp[P(l,r)] = true; //判重
a[l+1]++;
a[r]--;
}
for(int i=1;i<=n;i++){
a[i] += a[i-1];
printf("%d\n",h-a[i]);
}
return 0;
}