NC19913 [CQOI2009]中位数图

将>b的置为1;<b的置为-1.
pos记录b的位置.
算法:
1.在0~pos-1, 从后往前扫, sum记录后缀和, 若sum=0, ans++, 记录sum的个数:cnt[maxn+sum]++;
2.在pos+1~n-1, 同上.
3.在0~n-1,步骤2中,同时记录sum的值,并且可以统计与步骤一种sum的相反结果,ans +=cnt[maxn-sum].

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-10;
const ll inf = 1e18;
const int maxn = 1000005;

inline ll read()
{
    ll s = 0, w = 1; 
    char ch = getchar();
    while (ch < 48 || ch > 57) 
    	{ 
    		if (ch == '-') 
    			w = -1; 
    		ch = getchar(); 
    	}
    while (ch >= 48 && ch <= 57) 
    	s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}

int pos;
int a[maxn], cnt[2 * maxn];
int sum = 0, ans = 0;

int main(int argc, char const *argv[])
{
	int n, b;
	n = read();
	b = read();
	for (int i = 0; i < n; ++i)
	{
		a[i] = read();
		if (a[i] == b)
		{
			pos = i;
		}
	}
	for (int i = 0; i < n; ++i)
	{
		if (a[i] > b)
		{
			a[i] = 1;
		}
		else
			a[i] = -1;
	}

	//0~pos-1
	for (int i = pos-1; i >= 0; i--)
	{
		sum += a[i];
		if (sum == 0)
		{
			ans++;
		}
		cnt[maxn + sum]++;
	}
	//pos+1~n-1
	sum = 0;
	for (int i = pos+1; i <= n-1; ++i)
	{
		sum += a[i];
		if (sum == 0)
		{
			ans++;
		}
		ans += cnt[maxn - sum];
	}
	cout << ans + 1; 
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值