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/ ...
随机推荐
- MVC的viewPage 通用属性运用。
试想下在MVC的前端页面JS或者html中需要使用多语言,而后端的多语言是维护在资源文件中的,前端如果使用的话需要使用AJAX频繁的获取,一个页面中可能会存在大量的需要语言转换的地方,频繁使用AJAX ...
- AngularJS所有版本下载地址
AngularJS官网本身采用AngularJS库构建,页面中的AngularJS库通过Google的CDN(内容分发网络)引入,所以国内访问会有问题. 大家可以从下面地址获取AngularJS所以版 ...
- 设置Cookie最大存活时间
Cookie和Session都是由Tomcat自动创建的对象,Cookie的默认最大存活时间是 -1 ,即当浏览器关闭时Cookie就消失了:Session的默认最大存活时间是30分钟. 使用Sess ...
- 【extjs6学习笔记】1.2 初始:MVC MVVM
模型 这表示数据层.该模型可以包含数据验证和逻辑来保持数据.在 ext js 中, 大多数模型都与一个数据存储一起使用. 视图 这表示用户界面. 是用户在屏幕上看到的组件. 在每次互动的用户与应用程序 ...
- SQL 中的group by (转载)
概述 原始表 简单Group By Group By 和 Order By Group By中Select指定的字段限制 Group By All Group By与聚合函数 Having与Where ...
- 【TensorFlow入门完全指南】神经网络篇·自动编码机
自动编码机(Autoencoder)属于非监督学习,不需要对训练样本进行标记.自动编码机(Autoencoder)由三层网络组成,其中输入层神经元数量与输出层神经元数量相等,中间层神经元数量少于输入层 ...
- ES6, Angular,React和ABAP中的String Template(字符串模板)
String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专 ...
- Iterator中的next()
DBExchangeMoney类: 1 package com.ch.test15; import java.sql.DriverManager; import java.sql.ResultSet; ...
- Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)
E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 无旋Treap【模板】P3369
题目 详情见链接. 代码 #include<cstdio> #include<iostream> #define outd(x) printf("%d\n" ...