传送门:BestCoder Sequence

题意:给一个序列,里面是1~N的排列,给出m,问以m为中位数的奇数长度的序列个数。

分析:先找出m的位置,再记录左边比m大的状态,记录右边比m大的状态,使得左右两边状态平衡(和为0)就是满足的序列。

举例:

7 4

1 5 4 2 6 3 7

ans=8

m的位置pos=3:0

左边:0  1

右边:-1 0 -1 0

那么左边的0可以和右边的两个0组合(<1 5 4 2 4>,<1 5 4 2 6 3 7>).

左边的1和右边的两个-1组合(<5 4 2>,<5 4 2 6 3>).

中间pos可以和左右两边为0的组合还有自己本身(<1 5 4>,<4 2 6>,<4 2 6 3 7>,<4>)

因此总共8个。

5 3

1 2 3 4 5

ans=3

3的位置pos=3:0

左边:-2 -1

右边:1 2

左边-1可以和右边1可以组合(<2 3 4>)

左边-2和可以右边-2组合(<1 2 3 4 5>).

加上自己本身,因此ans=3.

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 60010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,pos;
int a[N<<],c[N<<];
int main()
{
while(scanf("%d%d",&n,&m)>)
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==m)pos=i;
}
int res=,ans=;
memset(c,,sizeof(c));
for(int i=pos-;i>=;i--)
{
if(a[i]>m)res++;
else res--;
if(res==)ans++;
c[res+n]++;//记录某种状态数量
}
res=;
for(int i=pos+;i<=n;i++)
{
if(a[i]>m)res++;
else res--;
if(res==)ans++;
ans+=c[n-res];//加上左边记录下来的相反状态之和
}
printf("%d\n",ans);
}
}

hdu4908(中位数)的更多相关文章

  1. hdu4908 中位数子串

    题意:       给你N个数字组成的数列,然后问你这里面有多少个是以M为中位数的子序列. 思路:       首先分四中简单的情况求        (1) 就是只有他自己的那种情况 那么sum+1 ...

  2. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  3. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. BZOJ1303 [CQOI2009]中位数图

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. 在MySQL中,如何计算一组数据的中位数?

    要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...

  6. AC日记——中位数 洛谷 P1168

    题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...

  7. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  8. LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1

    思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...

  9. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

随机推荐

  1. perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string

    encode_json $json_text = encode_json $perl_scalar Converts the given Perl data structure to a UTF-8 ...

  2. perl $d = encode_utf8($r); $f = decode_json($d)

    [root@dr-mysql01 ~]# cat a1.pl my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone ...

  3. ZJUT 1423 地下迷宫(期望DP&高斯消元)

    地下迷宫 Time Limit:1000MS  Memory Limit:32768K Description: 由于山体滑坡,DK被困在了地下蜘蛛王国迷宫.为了抢在DH之前来到TFT,DK必须尽快走 ...

  4. 史上最全的java随机数/字符串生成算法(转)

    代码如下: package com.zuidaima.core.util; import java.util.Random; public class RandomUtil { public stat ...

  5. 出现Data Tools 与VS 不兼容问题

    转载自:http://www.yishimei.cn/network/73.html 相信很多人都遇到了“此版本的SQL Server Data Tools与此计算机中安装的数据库运行时组件不兼容”这 ...

  6. 内省(二)之BeanUtils工具类

    上一篇内省(Introspector)讲到的是采用JavaAPI中的类来操作bean及其属性,而Apache也开源了第三方框架来简化和丰富了对bean属性的操作,这个框架就是BeanUtils. 使用 ...

  7. Servlet过滤器——过滤器分析流量

    1.概述 Servlet过滤器可以对用户提交的数据或服务器返回的数据进行更改.任何到达服务器的请求都会首先经过过滤器的处理.本实例应用过滤器的这个特点,编写了一个在过滤器中统计网站流量的实例. 本实例 ...

  8. libcurl编程,整整17篇

    http://www.cnblogs.com/lidabo/category/639633.html

  9. Java字符串找出4个字节长度的字符

    不解释,直接上代码:  由于Iteye代码贴四个字节的UTF-8字符出错,特能图的方式发布几个特殊字符:  public class Byte4Check { public static void m ...

  10. 【玩转cocos2d-x之四十】怎样在Cocos2d-x 3.0中使用opengl shader?

    有小伙伴提出了这个问题.事实上GLProgramCocos2d-x引擎自带了.全然能够直接拿来用. 先上图吧. 使用opengl前后的对照: watermark/2/text/aHR0cDovL2Js ...