码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj千题计划175:bzoj1303: [CQOI2009]中位数图

时间:2017-12-31 11:52:34      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:output   log   cst   输出   ems   长度   const   中间   gpo   

http://www.lydsy.com/JudgeOnline/problem.php?id=1303

 

令c[i]表示前i个数中,比d大的数与比d小的数的差,那么如果c[l]=c[r],则[l+1,r]满足条件

 

#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e7;
int c[N*2],g[N];
void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-0; c=getchar();}
}
int main()
{
    
    int n,d,x,now=0,pos;
    long long ans=0;
    bool ok=false;
    read(n); read(d);
    c[n]=1;
    for(int i=1;i<=n;i++)
    {
        read(x);
        if(x<d) now--;
        else if(x>d) now++;
        if(x==d) ok=true,pos=i; 
        if(!ok) c[now+n]++;
        else g[i]=now;
    }
    for(int i=pos;i<=n;i++) ans+=c[g[i]+n];
    printf("%lld",ans);
}

 

1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 3125  Solved: 1923
[Submit][Status][Discuss]

Description

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

Input

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

Output

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

Sample Input

7 4
5 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

 

bzoj千题计划175:bzoj1303: [CQOI2009]中位数图

标签:output   log   cst   输出   ems   长度   const   中间   gpo   

原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8157196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!