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.

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

分析

需要注意可能输入重复的信息。思路就是先将所有的身高初始化为最高身高,根据信息将I+1 - j - 1位置的牛全部-1。实现方法有朴素法和差分。

Code

/*
 * @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 <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、差分序列:
使用一个差分数组处理, 在朴素方法上处理中间牛的方法简化,只需要在c[i + 1] 上加1,c[j] 减一。 则可以达到(i, j) 中间的牛[i + 1 ~j - 1] -1 的同样效果。 
复杂度分析:若给出 M 个关系对,平均处理长度为 N 。处理牛的复杂度O(1),O(M)
*/
/* #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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian圣楠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值