本文共 2828 字,大约阅读时间需要 9 分钟。
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.
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.
9 3 5 5
1 3 5 3 4 3 3 7 9 8
5
4 5 3 4 4 5 5 5
需要注意可能输入重复的信息。思路就是先将所有的身高初始化为最高身高,根据信息将I+1 - j - 1位置的牛全部-1。实现方法有朴素法和差分。
/* * @Description: Tallest Cow * @version: * @Author: * @Date: 2021-04-08 13:47:09 * @LastEditors: Please set LastEditors * @LastEditTime: 2021-04-08 17:22:29 */// 提示// NOTE:可能有重复信息#include#include #include #include #include using namespace std;vector v;set > 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、差分序列:使用一个差分数组处理, 在朴素方法上处理中间牛的方法简化,只需要在c[i + 1] 上加1,c[j] 减一。 则可以达到(i, j) 中间的牛[i + 1 ~j - 1] -1 的同样效果。 复杂度分析:若给出 M 个关系对,平均处理长度为 N 。处理牛的复杂度O(1),O(M)*//* #include #include #include #include #include
转载地址:http://tjwqz.baihongyu.com/