Problem : [CQOI2009]中位数图

这是一个关于[CQOI2009]中位数图的问题,要求判断某数x是否为给定区间内的中位数。通过设定比x小的数为-1,比x大的数为1,x为0,来检查前缀和是否满足中位数条件。题目给出时间限制和内存限制,并提供了样例输入输出及提示。解决方案涉及前缀和和数组cnt的使用,其中cnt[i+n]记录了前缀和i出现的次数,用于判断中位数条件。

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

Problem : [CQOI2009]中位数图

Time Limit: 1 Sec   Memory Limit: 162 MB
Submit: 0   Solved: 0
[ Submit][ Status]

Description

给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。

Input

第一行为两个正整数n和b ,第二行为1~n 的排列。

Output

输出一个整数,即中位数为b的连续子序列个数。

Sample Input

7 45 7 2 4 3 1 6

Sample Output

4

HINT

第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000


思路:要判断某数x在某区间中是中位数,那么把比x小的数都设为-1,把比x大的数都设成1,x设成零,就有前缀和sum[R] - sum[L-1] == 0,也就是sum[R] == sum[L-1]。

且n >= R >= pos ,1<=  L <= pos。设数组cnt[i]记录前缀和为i出现了几次,因为i可能<0,所以要加上n,也就是cnt[i+n]。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 100000 + 100;
int sum[maxn], cnt[maxn<<1];

int main() {
    int n, x;
    while (cin >> n >> x) {
        memset(sum, 0, sizeof(sum));
        memset(cnt, 0, sizeof(cnt));
        int pos = -1;
        for (int i = 1; i <= n; i ++) {
            int temp; cin >> temp;
            if (temp < x) temp = -1;
            else if (temp > x) temp = 1;
            else temp = 0, pos = i;
            sum[i] = sum[i-1] + temp;
            if (pos == -1) cnt[sum[i] + n] ++;
        }
        cnt[n] ++;
        int res = 0;
//        for (int i = 1; i <= n*2; i ++) cout << cnt[i] << ' ';
//        cout << endl;
//         for (int i = 1; i <= n*2; i ++) cout << sum[i] << ' ';
//        cout << endl;
        for (int i = pos; i <= n; i ++) {
            res += cnt[sum[i] + n];
//            cout << "sum[i]+n = " << sum[i] + n << " cnt[sum[i] + n] = " << cnt[sum[i] + n] << endl;
        }
        cout << res << '\n';
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值