Tallest Cow (the tallest cow) Acwing101-prefix sum, difference

Tallest Cow

  • There are N cows standing in a row, and the formations are 1, 2, 3...N, and the height of each cow is an integer.
    The two cows can see each other if and only if the height of the cow in the middle of the two cows is shorter than them.
    Now, we only know that the tallest cow is head P, its height is H, and the height of the remaining cows is unknown.
    However, we also know that there are M pairs of relationships in this herd of cattle. Each pair of relationships indicates that two cattle A and B can see each other.
    What is the maximum possible value of the height of each cow?
    Input format
    Input integers N, P, H, M in the first line, and the data is separated by spaces.
    In the next M lines, each line outputs two integers A and B, representing that the cow A and the cow B can see each other, and the data is separated by spaces.
    Output format A
    total of N lines of data are output, and each line outputs an integer.
    The integer output in the i-th line represents the maximum possible height of the i-th cow.

Data range
1≤N≤10000,
1≤H≤1000000
, 1≤A,
B≤10000 , 0≤M≤10000

Input sample:
9 3 5 5
1 3
5 3
4 3
3 7
9 8
Output sample:
5
4
5
3
4
4
5
5
5
Note:
The relationship pair given in this question may be duplicated

/*

因为这题中除最高的牛已知身高,其余的身高均未知。所以可以假设所有的牛与最高的牛身高相同。假设均为 h 
1、朴素方法:
开一个数组并初始化全为身高为h的牛。给出一对关系(i , j)表示牛i与牛j可以相互看到,即中间的牛严格比i和j小。
若此前未给出(i, j)的关系对,即将(i , j)中间的牛[i + 1 ~ j - 1] - 1
复杂度分析:若给出 M 个关系对,平均处理长度为 N。处理牛的复杂度O(N), O(N * M)

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;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45830383/article/details/108680223