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
思路:先将所有的牛的高度都设为最大值 然后在输入一组数A B时 将A B之间的牛的高度都减一。
代码如下:
#include<stdio.h>
int main()
{
int n, i, h, r, j, a, b, p[10010] = { 0 }, m, max, min,A[10010],B[10010],flag;
scanf("%d %d %d %d", &n, &i, &h, &r);
for (j = 1; j <= n; j++)//将所有牛的高度设为最大值
{
p[j] = h;
}
for (j = 1; j <= r; j++)
{
flag = 1;
scanf("%d %d", &a, &b); //输入一组A B
A[j] = a; B[j] = b;
for (m = 1; m < j; m++)//防止重复输入
{
if ((A[j] == A[m] && B[j] == B[m])||(A[j]==B[m]&&B[j]==A[m]))
{
flag = 0; break;
}
}
if (flag == 0)continue;
if (a > b) { max = a; min = b; }
else { max = b; min = a; }
for (m = min + 1; m < max; m++)//将A B 区间的都减一
{
p[m]--;
}
}
for (j = 1; j <= n; j++)
{
printf("%d\n", p[j]);
}
return 0;
}
注:小心重复输入使得AB区间内的多减了一。