0
点赞
收藏
分享

微信扫一扫

今日学习在线编程题:相对马高

小磊z 2022-04-14 阅读 107

时间限制:1000ms
内存限制:65535kb
题目描述:W有n头马,他们站作一排,由于马的身高不同,如果马A和B 能够相互看见,那么,在(a,b)区间的马的身高都小于a,b马。
现在给出最高的马的身高h,和他所在的位置i,即编号。
之后W将给出f组数据,表示马A,B可以相互看见,请你推出W的马们的最大的可能的身高
由于W工作向来随性,马的关系之间可能有重复,

输入格式:第一行输入n,i,h,f
第2行 到 第f+1行 输入两个数据a,b,以空格分隔
输出格式:f行,输出每头马的最大可能身高

样例输入:

10 3 9 5
1 3
2 6
2 3
5 9
1 5
样例输出:
9
7
7
7
8
8
8
8
9
9
提示    
(1 ≤ n ≤ 10,000)
(0 ≤ f ≤ 10,000)
(1 ≤ A, B ≤ N)
(1 ≤ H ≤ 1,000,000)
马的身高一定为整数

参考程序:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
#define ll long long 
#define N 21000
ll a[N] = {}, b[N] = {};
struct op{
   int a, b;   
}s[N];
bool operator < (const op &a, const op &b){
    if (a.a == b.a) return a.b < b.b ;
    else return a.a < b.a;
}
set<op> p ;
int main(){
    //freopen("10.in","r",stdin);
    //freopen("10.out","w",stdout);
    ll n, h, I, m ;
    cin >> n >> I >> h >> m;
    for(ll i = 1; i <= m; i++){
        ll a1,a2;
        cin >> a1 >> a2;
        if (a1 == a2) continue;
        if (a1 > a2) swap(a1,a2);
        op f; f.a = a1, f.b = a2 ;
        p.insert(f);
    }
    for (set<op>::iterator i = p.begin(); i != p.end();i++){
        op f = *i;
        b[f.a + 1] = b[f.a + 1] - 1;
        b[f.b] = b[f.b] + 1 ;
    }
    for (ll i = 1;i <= n; i++){
        a[i] = b[i] + a[i - 1] ;
        cout << a[i] + h << endl;
    }
    //fclose(stdout);
    //fclose(stdin);
    return 0;
}

题目来源:码蹄集

https://matiji.net/exam/brushquestion/3/3181/1DC60EA6DF83A333301CFFE1407FBA59

举报

相关推荐

0 条评论