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

给出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

思路:记录中间那个数左边右边 比它大和比它小的个数,然后乘法原理搞一下

#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <iostream>

#include <queue>

#define maxn 500000

#define com 100005

using namespace std;

int righ[maxn],lef[maxn],a[maxn];

int main()

{

int n,b,idx;

long long ans=0;

scanf("%d%d",&n,&b);

for(int i=1;i<=n;i++)

{

scanf("%d",&a[i]);

if(a[i]==b)idx=i;

}

int sum=0;lef[com]=righ[com]=1;

for(int i=idx-1;i>=1;i--)

{

sum+=(a[i]>b)?1:-1;

lef[sum+com]++;

}

sum=0;

for(int i=idx+1;i<=n;i++)

{

sum+=(a[i]<b)?1:-1;

righ[sum+com]++;

}

for(int i=com-n;i<=com+n;i++)ans+=lef[i]*righ[i];

cout<<ans<<endl;

return 0;

}

时间: 2024-08-29 12:58:03

BZOJ 1303: [CQOI2009]中位数图 【水题】的相关文章

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

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的

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

1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2737  Solved: 1698[Submit][Status][Discuss] Description 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Output 输出一个整数,即中位数为b的连续子序列个数.

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

题目链接:BZOJ - 1303 题目分析 首先,找到 b 的位置 Pos, 然后将数列中小于 b 的值赋为 -1 ,大于 b 的值赋为 1 . 从 b 向左扩展,不断算 Sum[i, b - 1] ,然后将 Cnt[Sum[i, b - 1]] 加一,这样就算出每个左边的Sum值有多少个了. 然后从 b 向右扩展,不断算 Sum[b + 1, i] ,然后 Ans += Cnt[Sum[b + 1, i]] . 代码 #include <iostream> #include <cstd

bzoj 1303: [CQOI2009]中位数图

1 #include<cstdio> 2 #include<iostream> 3 #define M 100005 4 using namespace std; 5 int a[M],l[2*M],r[2*M],sum[M],n,m,p,ans; 6 int main() 7 { 8 scanf("%d%d",&n,&m); 9 for(int i=1;i<=n;i++) 10 { 11 scanf("%d",&

1303: [CQOI2009]中位数图

1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1383  Solved: 902[Submit][Status] Description 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Output 输出一个整数,即中位数为b的连续子序列个数. Sample In

【BZOJ 1303】 [CQOI2009]中位数图

1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 1452  Solved: 951 [Submit][Status] Description 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Output 输出一个整数,即中位数为b的连续子序列个数. Sample

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

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()

bzoj[CQOI2009]中位数图

又是一道巧妙的题 将大于b的数标为1,将小于b的数标为-1 以b为界限,向两边分别求后缀和与前缀和,用l,r分别统计左右两边每个前缀和的数量 因为前缀和有负数所以整体加个n 于是答案就是l[i]*r[2*n-i]//和为2*n,即减去加上的n,和为0,此时b为中位数 因为题目中说要满足长度为奇数,此时若两个和加起来为2*n,个数相加一定为偶数,加上一个b就为奇数了 #include<bits/stdc++.h> using namespace std; const int maxn=10000

POJ 2195 一人一房 最小费用流 建图 水题

Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21010   Accepted: 10614 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertica