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/ ...
随机推荐
- 前端html与css学习笔记总结篇(超详细)
第一部分 HTML 第一章 职业规划和前景 职业方向规划定位: web前端开发工程师 web网站架构师 自己创业 转岗管理或其他 web前端开发的前景展望: 未来IT行业企业需求最多的人才 结合最新的 ...
- c#基础2-out-ref
//out参数要求在方法的内部 ; JiangJin(ref salary1); Console.WriteLine(salary1); Console.ReadKey(); 必须为其赋值 out.r ...
- [总结] min-25筛
再不写总结我又会忘掉啊啊啊啊啊啊啊啊啊 这个\(min-25\)筛主要用来求一个积性函数的前缀和,就像这样\[\sum_{i=1}^n f(i)\] 不过这个积性函数要满足两个条件:质数\(p\)的函 ...
- RK3288开发过程中遇到的问题点和解决方法之Framework
删除小电池图标及百分比 a.SystemUI/.../statusbar/policy/BatteryController.java mBatteryPercentageView.setVisibil ...
- 《Ionic 2 实例开发》发布
Ionic 2系列教程集结成册,在百度阅读上架发布,名为<Ionic 2实例开发>(点击书名将打开地址:http://yuedu.baidu.com/ebook/ba1bca51e4189 ...
- 备份和导入Outlook 2016 电子邮件签名
在本文中,我将分享您在Outlook 2013和Outlook 2016中备份或导入签名的过程 在清除Outlook配置文件之前,请确保您通过在文件资源管理器中的配置文件中的APPDATA文件夹中复制 ...
- SQL Server 查询性能优化——创建索引原则
索引是什么?索引是提高查询性能的一个重要工具,索引就是把查询语句所需要的少量数据添加到索引分页中,这样访问数据时只要访问少数索引的分页就可以.但是索引对于提高查询性能也不是万能的,也不是建立越多的索引 ...
- Servlet详解之两个init方法的作用
在Servlet中 javax.servlet.GenericServlet类 继承自java.lang.Object 实现了Serializable,,servlet ,ServletConfig ...
- MVC的验证码
后台: /// <summary> /// 创建验证码的图片 /// </summary> /// <param name="validateCode" ...
- StatementHandler-Mybatis源码系列
内容更新github地址:我飞 StatementHandler接口 StatementHandler封装了Mybatis连接数据库操作最基础的部分.因为,无论怎么封装,最终我们都是要使用JDBC和数 ...