BestCoder Round #3 BestCoder Sequence

本文深入探讨了C++编程语言的关键特性与实际应用案例,包括数据类型、控制结构、函数、类与对象、模板、异常处理等核心概念。通过实例代码解析,展示了如何运用这些特性解决实际问题。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<stack>
#include<set>
#define ll long long
#define MAX 40010
#define INF INT_MAX
#define eps 1e-8

using namespace std;

int a[MAX],endx[MAX],endy[MAX],begx[MAX],begy[MAX];

multiset<int>S,T;

int main(){
	int n,m;
	while (scanf("%d%d",&n,&m) != EOF){
		int u,ans = 0;
		S.clear();
		T.clear();
		memset(endx,0,sizeof(endx));
		memset(endy,0,sizeof(endy));
		memset(begx,0,sizeof(begx));
		memset(begy,0,sizeof(begy));
		for (int i=1; i<=n; i++){
			 scanf("%d",&a[i]);
			 if (a[i] == m){
			 	u = i;
			 }
		}
		for (int i=u-1; i>0; i--){
			if (a[i] < m){
				begx[i] = begx[i+1] + 1;
				begy[i] = begy[i+1];
			}
			else{
				begx[i] = begx[i+1];
				begy[i] = begy[i+1]+1;
			}
			S.insert(begx[i] - begy[i]);
			if (begx[i] - begy[i] == 0) ans++;
		}
		for (int i=u+1; i<=n; i++){
			if (a[i] < m){
				endx[i] = endx[i-1] + 1;
				endy[i] = endy[i-1];
			}
			else{
				endx[i] = endx[i-1];
				endy[i] = endy[i-1] + 1;
			}
			T.insert(endx[i] - endy[i]);
			if (endx[i] - endy[i] == 0) ans++;
		}
		multiset<int>::iterator it;
		for (it = S.begin(); it != S.end(); it++){
			int e = *it;
			//printf("e = %d\n",e);
			ans += T.count(-e);
		}
		printf("%d\n",ans+1);		
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值