[Acwing] AcWing 101. 最高的牛 差分

前言

总算可以水一题了 awa
传送门 : https://www.acwing.com/problem/content/103/

思路

题目要求的是 两边的牛高 中间的牛矮

因为数据范围很大 我们不可能 给他们 遍历一遍+1 -1

所以我们可以使用差分进行+1 -1

但是题目最高又限制了 最高的牛 所以我们还需要查重

差分步骤:
n u m [ l ] + + num[l]++ num[l]++
n u m [ r + 1 ] + + num[r+1]++ num[r+1]++
n u m [ l + 1 ] − − num[l+1]- - num[l+1]
n u m [ r ] − − num[r] - - num[r]
看不懂的 要回去巩固一下 差分 哦

CODE

#include <bits/stdc++.h>
using namespace std;
using ll= long long;
const int N = 1e4+10;

set<pair<int,int>> s;
int num[N];

void solve()
{
    int n,p,h,m;cin>>n>>p>>h>>m;

    while(m -- )
    {
        int a,b;
        cin>>a>>b;
        if(b<a) swap(a,b);

        if(s.count(make_pair(a,b))) continue;

        s.insert(make_pair(a,b));

        num[a]++;
        num[b]++;

        num[a+1]--;
        num[a]--;
    }
    for(int i=1;i<=n;i++)
    num[i]+=num[i-1];

    for(int i=1;i<=n;i++)
    cout<<num[i]+h<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值