Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)

本文介绍了一种算法,用于解决在一个给定的排列中找到所有使得中位数等于特定值m的区间的问题。该算法通过一次遍历并使用map来记录大于和小于m的元素数量,最终计算出符合条件的区间总数。
E1. Median on Segments (Permutations Edition)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a permutation p1,p2,,pnp1,p2,…,pn. A permutation of length nn is a sequence such that each integer between 11 and nn occurs exactly once in the sequence.

Find the number of pairs of indices (l,r)(l,r) (1lrn1≤l≤r≤n) such that the value of the median of pl,pl+1,,prpl,pl+1,…,pr is exactly the given number mm.

The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.

For example, if a=[4,2,7,5]a=[4,2,7,5] then its median is 44 since after sorting the sequence, it will look like [2,4,5,7][2,4,5,7] and the left of two middle elements is equal to 44. The median of [7,1,2,9,6][7,1,2,9,6] equals 66 since after sorting, the value 66 will be in the middle of the sequence.

Write a program to find the number of pairs of indices (l,r)(l,r) (1lrn1≤l≤r≤n) such that the value of the median of pl,pl+1,,prpl,pl+1,…,pr is exactly the given number mm.

Input

The first line contains integers nn and mm (1n21051≤n≤2⋅105, 1mn1≤m≤n) — the length of the given sequence and the required value of the median.

The second line contains a permutation p1,p2,,pnp1,p2,…,pn (1pin1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.

Output

Print the required number.

Examples
input
Copy
5 4
2 4 5 3 1
output
Copy
4
input
Copy
5 5
1 2 3 4 5
output
Copy
1
input
Copy
15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
output
Copy
48
Note 

In the first example, the suitable pairs of indices are: (1,3)(1,3), (2,2)(2,2), (2,3)(2,3) and (2,4)(2,4).

 

 

题意:给出n个数,中位数m,求在这n个数中的任意区间内中位数是n的个数,区间个数是偶数的时候去左边的为 中位数

解题思路:刚开始我以为这是主席树的模板题,第k大,后来听别人说不用这么复杂,因为是n个数互不重复1-n,因为要求的区间里面肯定包含了m,所以我们先求出m的位置,然后我们仔细想 

可以得知在这个区间里面要使中位数是m的话,奇数区间大于m的个数与小于m的个数是一样的,偶数区间是大于m的个数比小于m的个数多1,所以我们用map记录比m大和小的个数,我们先从

m的位置从右边遍历求出区间大于小于m的情况,用map 存大于m和小于m的差值,这样比较方便,比如mp[0]=1,说明右边大于m和小于m的区间个数相等的区间有1个,比如mp[-1]=2,说明右边

大于m比小于m少一个的区间个数有2个,以此类推,然后我们再此遍历左边,如果左边大于m的个数是1的话,奇数区间那么我就要右边小于m的个数为1,也就是mp[-1],偶数区间就要右边大于m

小于m个数相等,也就是mp[0],从而推出式子  cnt记录大于小于m的个数 sum=sum+mp[-cnt]+mp[1-cnt];

#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
int main()
{
    map<ll,ll> mp;
    ll m,n,a[200005];
    cin>>n>>m;
    int pos;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==m)
        pos=i;
    }
    int cnt=0;
    for(int i=pos;i<n;i++)
    {
        if(a[i]>m) cnt++;
        if(a[i]<m) cnt--;
        mp[cnt]++;
    }
    ll sum=0;
    cnt=0;
    for(int i=pos;i>=0;i--)
    {
        if(a[i]>m) cnt++;
        if(a[i]<m) cnt--;
        sum=sum+mp[-cnt]+mp[1-cnt];
    }
    cout<<sum;
}

 

转载于:https://www.cnblogs.com/Lis-/p/9299800.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值