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/ ...
随机推荐
- 利用Vagrant and VirtualBox搭建core os环境
利用Vagrant and VirtualBox搭建core os环境 系统环境 ubuntu 14.04 x64 vagrant 1.7.4 virtualbox 4.3.10 git 1.9.1 ...
- IO(转换流、缓冲流)
第1章 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者Output ...
- feign实现服务间的负载均衡
feign Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单.我们只需要使用Feign来创建一个接口并用注解来配置它既可完成.它具备可插拔的注解 ...
- Android ORM对象关系映射之GreenDAO建立多表关联
https://blog.csdn.net/u010687392/article/details/48496299 利用GreenDAO可以非常方便的建立多张表之间的关联 一对一关联 通常我们在操作数 ...
- CRC检错技术原理
一.题外话 说来惭愧,一开始是考虑写关于CRC检错技术更深层次数学原理的,然而在翻看<Basic Algebra>后,我果断放弃了这种不切实际的想法.个人觉得不是因为本人数学水平差或者能力 ...
- Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...
- javaSe-字符型和布尔型
其实java数据类型一节就可以全部写完了,为什么还需要字符型和布尔型呢,原因是这俩个都很重要: 字符型用char表示,字符分三种: 普通字符:char a = 'a',普通字符表示一个普通的字符,没有 ...
- Array - Container With Most Water
/** * 此为暴力解法 * Find two lines, which together with x-axis forms a container, such that the container ...
- 在DataGridView控件中设置数据显示格式
实现效果: 知识运用: DataGridViewCellStyle类的Format属性 //获取或设置应用于DataGridView单元格的文本内容的格式字符串 public string Forma ...
- 解决TS报错Property 'style' does not exist on type 'Element'
在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...