1303: [CQOI2009]中位数图

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

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

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

题意

题解:

把大于b的置为1,把小于b的置为-1

然后左右都扫一遍,l[i]表示左边和为i的个数,r[i]表示右边和为i的个数

if(i+j==0)ans+=l[i]*r[i]

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int num[maxn];
int sum[maxn];
int l[maxn],r[maxn];
int point;
int main()
{
int n,b;
scanf("%d%d",&n,&b);
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i]==b)
point=i;
if(num[i]>b)
num[i]=;
else if(num[i]<b)
num[i]=-;
else
num[i]=;
}
l[n]=,r[n]=;
for(int i=point-;i>=;i--)
{
sum[i]=sum[i+]+num[i];
l[sum[i]+n]++;
}
for(int i=point+;i<=n;i++)
{
sum[i]=sum[i-]+num[i];
r[sum[i]+n]++;
}
int ans=;
for(int i=;i<=*n;i++)
{
ans+=l[i]*r[*n-i];
}
cout<<ans<<endl;
}

bzoj 1303: [CQOI2009]中位数图 数学的更多相关文章

  1. BZOJ 1303 CQOI2009 中位数图 水题

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

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

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

  3. [BZOJ 1303] [CQOI2009] 中位数图 【0.0】

    题目链接:BZOJ - 1303 题目分析 首先,找到 b 的位置 Pos, 然后将数列中小于 b 的值赋为 -1 ,大于 b 的值赋为 1 . 从 b 向左扩展,不断算 Sum[i, b - 1] ...

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

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

  5. BZOJ 1303: [CQOI2009]中位数图 【水题】

    给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Out ...

  6. BZOJ 1303: [CQOI2009]中位数图 问题转化_扫描_思维

    将比 b 大的设成 1,比 b 小的设成 -1,开个桶左右扫描一下,乘法原理乘一乘就好了. 虽然一眼切,不过这个基于中位数的转化还是相当重要的.middle 那个主席树的题也需要该做法 Code: # ...

  7. BZOJ 1303: [CQOI2009]中位数图(思路题)

    传送门 解题思路 比较好想的思路题.首先肯定要把原序列转化一下,大于\(k\)的变成\(1\),小于\(k\)的变成\(-1\),然后求一个前缀和,还要用\(cnt[]\)记录一下前缀和每个数出现了几 ...

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

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

  9. 1303: [CQOI2009]中位数图

    早起一AC,整天萌萌哒. Problem: 1303 User: forgot93 Language: C++ Result: Accepted Time:56 ms Memory:2108 kb * ...

随机推荐

  1. Neuroph studio 入门教程

    PERCEPTRON Perceptron is a simple two layer neural network with several neurons in input layer, and ...

  2. flask插件系列之flask_caching缓存

    前言 为了尽量减少缓存穿透,同时减少web的响应时间,我们可以针对那些需要一定时间才能获取结果的函数和那些不需要频繁更新的视图函数提供缓存服务,可以在一定的时间内直接返回结果而不是每次都需要计算或者从 ...

  3. Linux进程调度原理【转】

    转自:http://www.cnblogs.com/zhaoyl/archive/2012/09/04/2671156.html Linux进程调度的目标 1.高效性:高效意味着在相同的时间下要完成更 ...

  4. 生成器(generator)和迭代(iterable , iterator, iteration)

    在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...

  5. vue头像上传

    项目四知识点 默认头像 选择头像 <template> <div class="adatar"> <img :src="adatar?ada ...

  6. gan对抗式网络

    感觉好厉害,由上图噪声,生成左图噪声生成右图以假乱真的图片, gan网络原理: 本弱又盗了一坨博文,不是我写的,如下:(跪膜各路大神) 前面我们已经讲完了一般的深层网络,适用于图像的卷积神经网络,适用 ...

  7. linux系统kill一些类名称相同的进程

    jps | grep "Main" | awk '{print $1}' | xargs kill 将其中的 Main 替换为需要kill的进程名即可.

  8. AssetBundle——外部加载资源Asset

    几篇很不错的文章  AssetBundle创建到使用入门 全面理解Unity加载和内存管理 实用的创建AssetBundle的脚本   相关资源 相关的共享资源下载  本共享包括创建assetbund ...

  9. Morris Traversal方法遍历

    实现二叉树的遍历且只需要O(1)的空间. 参考:http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html

  10. plt-3D打印1

    plt-3D打印 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D ...