算阶第一章Tallest Cow

Description

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.

简单翻译:

FarmerJohn 有n头牛,它们按顺序排成一列。 FarmerJohn 只知道其中最高的奶牛的序号及它的高度,其他奶牛的高度都是未知的。现在 FarmerJohn 手上有R条信息,每条信息上有两头奶牛的序号(a和b),其中b奶牛的高度一定大于等于a奶牛的高度,且a,b之间的所有奶牛的高度都比a小。现在FarmerJohn想让你根据这些信息求出每一头奶牛的可能的最大的高度。(数据保证有解)

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.

第1行:四个以空格分隔的整数:nn,ii,hh和RR(nn和RR意义见题面; ii 和 hh 表示第 ii 头牛的高度为 hh ,他是最高的奶牛)
接下来R行:两个不同的整数aa和bb(1≤a,b≤n1≤a,b≤n)

Output

Lines 1…N: Line i contains the maximum possible height of cow i.
一共nn行,表示每头奶牛的最大可能高度.

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
————————————————————————————————————
该题在算阶24页,归为差分,Acwing中的101题,其实感觉语法级别就能水过去。

#include <bits/stdc++.h>
using namespace std;
int cow[10086],n,l,h,r,a,b;
bool flag[10086][10086];
int main()
{
    
    
	scanf("%d%d%d%d",&n,&l,&h,&r);
	cow[l]=h;
	for(int i=1;i<=n;i++) cow[i]=h;
	while(r--)
	{
    
    
		scanf("%d%d",&a,&b);
		if(flag[a][b]==0&&flag[b][a]==0)
		for(int i=min(a,b)+1;i<=max(a,b)-1;i++)
		cow[i]--;
		flag[a][b]=1;
	}
	for(int i=1;i<=n;i++) cout<<cow[i]<<endl;
	return 0;
}

输入完最高的牛后,把每头牛定义成最高的高度,然后两头牛互相看的时候,把中间的牛高度减一,然后做下标记,判断这两头牛是不是互相看了好几遍。

猜你喜欢

转载自blog.csdn.net/ydsrwex/article/details/112329253