想明白算法之后特别水,因为b只有可能出现一次,所以直接在b的左右找就行了,比他大的为1,比他小的为-1,然后维护前缀和就行了。

假如b有可能出现多次呢?按照这种方法好像也很好办,就是枚举每个点就行了,复杂度有点大,所以直接求一遍前缀和就行了。

题干:

Description
给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。
Input
第一行为两个正整数n和b ,第二行为1~n 的排列。
Output
输出一个整数,即中位数为b的连续子序列个数。
Sample Input Sample Output HINT
第三个样例解释:{}, {,,}, {,,,,}和{,,,,,,}
N<=

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int n,b,point;
int a[],sum[];
int l[],r[];
int main()
{
read(n);read(b);
duke(i,,n)
{
read(a[i]);
if(a[i] > b)
a[i] = ;
else if(a[i] == b)
{
a[i] = ;
point = i;
}
else
a[i] = -;
}
l[n] = ;r[n] = ;
lv(i,point - ,)
{
sum[i] = sum[i + ] + a[i];
l[sum[i] + n]++;
}
duke(i,point + ,n)
{
sum[i] = sum[i - ] + a[i];
r[sum[i] + n] ++;
}
int ans = ;
duke(i,, * n - )
{
ans += l[i] * r[ * n - i];
}
printf("%d\n",ans);
return ;
}
/*
7 4
5 7 2 4 3 1 6
*/

B1303 [CQOI2009] 中位数图 数学的更多相关文章

  1. bzoj 1303: [CQOI2009]中位数图 数学

    1303: [CQOI2009]中位数图 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  2. BZOJ 1303 CQOI2009 中位数图 水题

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

  3. BZOJ 1303: [CQOI2009]中位数图【前缀和】

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

  4. 【BZOJ1303】[CQOI2009]中位数图(模拟)

    [BZOJ1303][CQOI2009]中位数图(模拟) 题面 BZOJ 洛谷 题解 把大于\(b\)的数设为\(1\),小于\(b\)的数设为\(-1\).显然询问就是有多少个横跨了\(b\)这个数 ...

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1303 令c[i]表示前i个数中,比d大的数与比d小的数的差,那么如果c[l]=c[r],则[l+1, ...

  6. BZOJ1303 [CQOI2009]中位数图 【乱搞】

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 3086  Solved: 1898 [Submit][Sta ...

  7. BZOJ1303 [CQOI2009]中位数图

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

  8. 【BZOJ】1303: [CQOI2009]中位数图(特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1303 依旧是题解流,,,不看题解没法活,,,第一眼就是瞎搞,然后就是暴力,显然TLE..题解啊题解. ...

  9. bzoj 1303: [CQOI2009]中位数图

    题目链接 给n个数,一个值b, 统计所有以b为中位数的序列的个数.序列长度为奇数.数字在1-n之间, 每个数只出现一次. 如果一个数大于b, 那么将他赋值为1, 小于b赋值为-1, 记录数组中b出现的 ...

随机推荐

  1. CNN结构:图片风格分类效果已成(StyleAI)

    CNN结构:图片风格分类效果已成.可以在色彩空间对图片风格进行分类,并进行目标分类. StyleAI构架:FasterRCnn + RandomTrees 为何不使用MaskRCNN? MaskRCN ...

  2. (转) Arcgis4js实现链家找房的效果

    http://blog.csdn.net/gisshixisheng/article/details/71009901 概述 买房的各位亲们不知是否留意过链家的"地图找房",这样的 ...

  3. quartz 数据库表含义解释

    http://blog.csdn.net/tengdazhang770960436/article/details/51019291 一.表信息解析: 1.1.qrtz_blob_triggers : ...

  4. Redis 之仿微博demo

    一.用户注册登录 include './header.php'; include './function.php'; $username = p('username'); $password = p( ...

  5. 9 Java 堆排序

    堆是具有以下性质的完全二叉树,每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆:或者每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆.如下图: 同时,我们对堆中的结点按层进行编号,将这种逻 ...

  6. Tensorflow读取csv文件(转)

    常用的直接读取方法实例:#加载包 import tensorflow as tf import os #设置工作目录 os.chdir("你自己的目录") #查看目录 print( ...

  7. 【郑轻邀请赛 A】tmk射气球

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2127 [题意] [题解] 把气球和飞艇所代表的直线投影到xoy面上 设气球所在位置为 ...

  8. LMDB installation

    Official Website: http://lmdb.readthedocs.io/en/release/ Install commands for Ubuntu: $ sudo apt-get ...

  9. hdu_1029-Ignatius and the Princess IV_201310180916

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  10. C - Reading comprehension 二分法 求等比数列前N项和

    Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024 ...