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/ ...
随机推荐
- Kendo UI 单页面应用(二) Router 类
Kendo UI 单页面应用(二) Router 类 Route 类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route 通过 Url 的片段功能(#url)和流量器的浏览历史功能融合在一 ...
- 利用bintray-release插件上传到Bintray- HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]问题解决
凡是网上教程 有个5678步的总有这样或者那样的坑. 上周撸了一个小工具准备上传到jcenter,方便管理以及以后使用.看了一下教程,短短几步,弄了很久. 按Hongyang的教程http://www ...
- SharePoint Server和Office 365之间的混合模式集成概述
正如您可能已经知道的那样,云中的Microsoft Office 365和SharePoint Server 2013/2016内部部署可以通过多种方式协同工作.这些通常被称为混合模式,因为它们将功能 ...
- Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...
- 似水流年 ? Chrome调试大全
http://www.360doc.com/content/12/1107/20/7851074_246467307.shtml 作为一名前端开发者,打交道最多的可能是和浏览器.市面上各种浏览器多 ...
- 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- Windows服务管理
按键:win+R 输入:services.msc “服务和应用程序”界面选项打开 * sc命令的使用:create(创建) delete(删除)等 * service可执行文件路径的修改:win+R ...
- Android开发出现 StackOverflowError
问题:StackOverflowError 在HTC或者摩托罗拉的手机上测试出现 StackOverflowError 的错误. 06-12 10:28:31.750: E/AndroidRuntim ...
- 摘自 dd大牛的《背包九讲》
P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路 这是最 ...
- 解决TS报错Property 'style' does not exist on type 'Element'
在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...