P1627 中位数
P1627 中位数
题目描述
给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。
输入输出格式
输入格式:
第一行为两个正整数n和b,第二行为1~n的排列。
【数据规模】
对于30%的数据中,满足n≤100;
对于60%的数据中,满足n≤1000;
对于100%的数据中,满足n≤100000,1≤b≤n。
输出格式:
输出一个整数,即中位数为b的连续子序列个数。
输入输出样例
7 4
5 7 2 4 3 1 6
4
分析
首先大于b的设为1,小于b的设为-1,p点为b值的坐标。处理L数组和R数组,L[i]表示p左边有多少个点到p的和为i,R[i]表示p右边有多少个点到p的和为i,那么ans=Σ L[i]*R[0-i]。为了没有负数坐标,所以代码中统一加n。
code
#include<cstdio> int a[];
int l[],r[],sum[];
int read()
{
int x = , f = ; char ch = getchar();
for (; ch<''||ch>''; ch = getchar())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar())
x = x*+ch-'';
return x*f;
}
int main()
{
int n = read(), d = read(), p,ans = ;
for (int x,i=; i<=n; ++i) {
x = read();
if (d==x) p = i,a[i] = ;
else if (x<d) a[i] = -;
else a[i] = ;
}
l[n] = r[n] = ;
for (int i=p-; i>=; --i) {
sum[i] = sum[i+]+a[i];
l[sum[i]+n]++;
}
for (int i=p+; i<=n; ++i) {
sum[i] = sum[i-]+a[i];
r[sum[i]+n]++;
}
for (int i=; i<=*n-; ++i) ans += l[i]*r[*n-i];
printf("%d",ans);
return ;
}
乱搞70
#include<cstdio>
#include<algorithm> using namespace std; int a[]; int read()
{
int x = , f = ; char ch = getchar();
for (; ch<''||ch>''; ch = getchar())
if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar())
x = x*+ch-'';
return x*f;
} int main()
{
int n = read(), d = read(), p = -, xcnt = , dcnt = ,ans = ; for (int i=; i<=n; ++i) {
a[i] = read();
if (d==a[i]) p = i;
if (p==-) {
if (a[i]<d) xcnt++;
else dcnt++;
}
}
int L = p,da = ,xo = ;
for (int i=; ; i+=) {
if (a[--L]>d) da++;else xo++;
if (a[--L]>d) da++;else xo++;
if (da==xo) ans++;
if (L==||L==) break;
} for (int i=; i<=p; ++i) {
if (i!=) {
if (a[i-]>d) dcnt--;
else xcnt--;
}
da = dcnt,xo = xcnt;
for (int j=p+; j<=n; ++j) {
if (a[j]>d) da++;else xo++;
if (da==xo) ans++;
}
} printf("%d",ans); return ;
}
P1627 中位数的更多相关文章
- Luogu P1627 中位数
Luogu P1627 中位数 先记录目标数的位置,并且把数组映射为: $$a[i]=\begin{cases}-1,a[i]<b\0,a[i]=b\1,a[i]>b\end{cases} ...
- 洛谷 P1627 [CQOI2009]中位数 解题报告
P1627 [CQOI2009]中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式 ...
- 洛谷——P1627 [CQOI2009]中位数
P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...
- luogu P1627 [CQOI2009]中位数
传送门 要求有多少个长度为奇数的区间满足某个数为区间中位数 这样的区间,大于中位数的数个数 等于 小于中位数的数个数 用类似于前缀和的方法,设\(X_i\)为\(i\)和数\(b\)形成的区间内,大于 ...
- P1627 [CQOI2009]中位数 题解
CSDN同步 原题链接 简要题意: 给定一个 \(1\) ~ \(n\) 的排列,求以 \(b\) 为中位数的 连续子序列且长度为奇数 的个数. 显然这段序列包含 \(b\). 中位数的定义:排序后在 ...
- p1627 [CQOI2009]中位数
传送门 分析 https://www.luogu.org/blog/user43145/solution-p1627 代码 #include<iostream> #include<c ...
- [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 ...
- [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 ...
- BZOJ1303 [CQOI2009]中位数图
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- pytorch 安装错误,报 GLIBCXX_3.4.20 错误
pytorch 从源码安装 链接:http://blog.csdn.net/u012442157/article/details/78134888 发现错误: 解决方案: http://blog.cs ...
- node 把base数据合成图片
var cr = new Buffer(img_Datas, 'base64'); var img = params.img_path + '/' + picDevNo + '_' + params. ...
- MovieReview—Ghost in the Shell 2: Innocence(攻壳机动队2:无罪)
Doll killing event The movie was developed around a series of doll murders. Barthes and o ...
- Winform C# 编程 1
http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL01Wmfc0B-QfB2pS1y13peTbq
- Windows环境下使用Apache+mod
1.安装Python和Apache. 2.安装mod_wsgi后获得wsgi.so,并将wsgi.so放到Apache的modules文件夹下. 3.安装webpy. 4.打开httpd.conf(在 ...
- python_79_模块定义导入优化
''' 1.定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件 (文件名:test.py,对应的模块名:test. import ...
- JS Math方法、逻辑
Math.PI; // 返回 3.141592653589793 Math.round(x) 的返回值是 x 四舍五入为最接近的整数. Math.pow(x, y) 的返回值是 x 的 y 次幂. M ...
- springboot 内置tomcat maxPostSizs 无法设置
++++++++++++++++++ RT +++++++++++++++++ 如下代码方可解决: /** * tomcat配置类 * 解决post数据体大于2048kb无法接收问题 * 解决tomc ...
- 题解 P1379 【八数码难题】
传送门 用STL中的queue,map,string写了个广搜,用一个string保存状态(见代码)注:STL比较慢,可以做一些优化(或者开O2) #include<iostream> # ...
- Bootstrap历练实例:默认的缩略图
本章将讲解Bootstrap缩略图,大多数站点都需要要在网格中布局图像,视频,文本.Bootstrap通过缩略图为此提供了一些简便的方法,使用Bootstrap创建缩略图的步骤如下: 1.在图像的周围 ...