E1. Median on Segments (Permutations Edition)[如何判断无序中位数]

本文探讨了E1.MedianonSegments(PermutationsEdition)题目,即在一个长度为n的全排列中找到包含特定数值m的区间,这些区间中m作为中位数的数量。文章介绍了一种有效的算法实现思路,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E1. Median on Segments (Permutations Edition)

题意:一个长为n的打乱的全排列,给定m,问包含m的区间中,有多少个区间的中位数是m

思路:假设m的位置为pos,先处理出[pos+1~n]中到达每个点的,比m大和比m小的差值.  假设一个区间可以,那么有 大于m的数-小于m的数=1 or 0 那么. 再从[pos-1,1]处理一遍,就处理出所有的答案

#include<bits/stdc++.h>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int MOD=1e9+7;
int a[N],sum1[N],sum2[N];
map<int,ll> mp;
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n,m,pos;
    cin >>n>>m;
    for(int i=1;i<=n;i++){
        cin >> a[i];
        if(a[i]==m) pos=i;
    }
    int now=0;
    mp[now]++;
    for(int i=pos+1;i<=n;i++){
        if(a[i]>m)  now++;
        else    now--;
        mp[now]++;
    }
    now=0;
    ll ans=mp[0]+mp[1];
    for(int i=pos-1;i>=1;--i){
        if(a[i]<m)  now++;
        else    now--;
        ans+=mp[now]+mp[now+1];
    }
    cout << ans << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值