﻿ • 价格透明
• 信息保密
• 进度掌控
• 售后无忧 # W2 - Tallest Cow

## 题目描述

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.

9 3 5 5
1 3
5 3
4 3
3 7
9 8

5
4
5
3
4
4
5
5
5

## Code

``````/*
* @Description: Tallest Cow
* @version:
* @Author:
* @Date: 2021-04-08 13:47:09
* @LastEditTime: 2021-04-08 17:22:29
*/
// 提示
// NOTE:可能有重复信息
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
vector<int> v;
set<pair<int, int>> s;
int n, index, maxh, r;
void init()
{
v.push_back(0);
for (int i = 0; i < n; i++)
v.push_back(maxh);
}
void change(const int a, const int b)
{
for (int i = a + 1; i < b; i++)
{
v[i]--;
}
}
void display()
{
for (int i = 1; i < v.size(); i++)
cout << v[i] << endl;
}
int main(void)
{
cin >> n >> index >> maxh >> r;
init();
for (int i = 0; i < r; i++)
{
int a, b;
cin >> a >> b;
if (s.find(make_pair(a, b)) != s.end())
continue;
s.insert(make_pair(a, b));
if (v[b] < v[a])
v[b] = v[a];
if (abs(a - b) == 1)
continue;
else
change(min(a, b), max(a, b));
}
display();
system("pause");
return 0;
}
/*
2、差分序列：

*/
/* #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int N = 10010;               //牛的最多多少个
int c[N];                          //牛的差分序列
map<pair<int, int>, bool> existed; //判断关系对是否存在重复

int main()
{
int n, p, h, m;
cin >> n >> p >> h >> m;
memset(c, 0, sizeof(c));
for (int i = 1; i <= m; i++)
{
int a, b;
cin >> a >> b;
if (a > b)
swap(a, b); //保证a比b小，即(a, b)是有效的
if (existed[make_pair(a, b)])
continue; //若关系对之前已判断则不需要再处理
c[a + 1]--;
c[b]++;
// 处理c[i + 1 ~j - 1] 的牛
existed[make_pair(a, b)] = true;
}
for (int i = 1; i <= n; i++)
{
c[i] += c[i - 1];
cout << h + c[i] << endl;
}
system("pause");
return 0;
} */
/*
INPUT:
9 3 5 5
1 3
5 3
4 3
3 7
9 8
*/
/*
5
4
5
3
4
4
5
5
5
*/
// 差分数组：0 0 -1 1 -2 1 0 1 0 0
// 还原：   0 0 -1 0 -2 -1 -1 0 0 0
`````` ### 低价透明 ### 金牌服务 ### 信息保密 