您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

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

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进