1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】
题目:戳这里
百度之星初赛原题:戳这里
题意:n个不同的数,求中位数为m的区间有多少个。
解题思路:
此题的中位数就是个数为奇数的数组中,小于m的数和大于m的数一样多,个数为偶数的数组中,小于m的数比大于m的数少一。因此,维护比m小和比m大的数就行。
m~n进行处理,比m大的cnt++,比m小的cnt--,用vis数组记录每个cnt值的个数。
m~1再进行处理,比m大的cnt++,比m小的cnt--,ans+=vis[cnt]+vis[cnt+1],vis[cnt]可以理解为是右边有cnt个比m大的数的情况(n为奇数,vis[cnt+1]是右边有cnt个比m大的数的情况(n为偶数。(当然真正的数值并不代表这个意思,因为出现比m小的数时cnt--了,在这里形容是为了便于理解。
这样操作的意思是,先预处理[pos[m],r]的所有情况,在遍历[l,pos[m]]的所有情况时,直接求出答案。
代码比文字好理解:
1 #include <bits/stdc++.h>
2 typedef long long ll;
3 const int maxn = 1e6+10;
4 const int inf = 0x3f3f3f3f;
5 const ll mod = 998244353;
6 using namespace std;
7 int a[maxn];
8 map<int,int>vis;
9 int main(){
10 int n, m;
11 scanf("%d %d", &n, &m);
12 int pos = 0;
13 for(int i = 1; i <= n; ++i) {
14 scanf("%d",a+i);
15 if(a[i] == m) pos = i;
16 }
17 int cnt = 0;
18 for(int i = pos; i <= n; ++i) {
19 if(a[i] > m) ++cnt;
20 if(a[i] < m) --cnt;
21 vis[cnt]++;
22 }
23 cnt = 0;
24 ll ans = 0;
25 for(int i = pos; i >= 1; --i) {
26 if(a[i] > m) --cnt;
27 if(a[i] < m) ++cnt;
28 ans += vis[cnt]+vis[cnt+1];//此时m左边的大小情况为cnt,要找右边为cnt和cnt+1的情况
29 }
30 printf("%lld\n", ans);
31 return 0;
32 }
1005E1 Median on Segments (Permutations Edition) 【思维+无序数组求中位数】的更多相关文章
- CF1005E1 Median on Segments (Permutations Edition) 思维
Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 me ...
- Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)
E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 25 ...
- Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...
- Codeforces #496 E1. Median on Segments (Permutations Edition)
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...
- 无序数组求第K大的数
问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...
- [leetcode]4. 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 ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- 无序数组的中位数(set+deque)hdu5249
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 无序数组求第k大/第k小的数
根据http://www.cnblogs.com/zhjp11/archive/2010/02/26/1674227.html 博客中所总结的7种解法,我挑了其中的解法3和解法6进行了实现. 解法3: ...
随机推荐
- 【译】Async/Await(三)——Aysnc/Await模式
原文标题:Async/Await 原文链接:https://os.phil-opp.com/async-await/#multitasking 公众号: Rust 碎碎念 翻译 by: Praying ...
- Windows安全加固
Windows安全加固 # 账户管理和认证授权 # 1.1 账户 # 默认账户安全 # 禁用Guest账户. 禁用或删除其他无用账户(建议先禁用账户三个月,待确认没有问题后删除.) 操作步骤 本地用户 ...
- 用SAP浏览网页
在SAP里,通过两个类就可以做一个简单的,嵌入sap里的网页.这两个类就是 1. cl_gui_custom_container 这个类是自定义屏幕里用得,也就是画一个container,在这个容器中 ...
- 基于 WebRTC 实现自定义编码分辨率发送
2020年如果问什么技术领域最火?毫无疑问:音视频.2020年远程办公和在线教育的强势发展,都离不开音视频的身影,视频会议.在线教学.娱乐直播等都是音视频的典型应用场景. 更加丰富的使用场景更需要我们 ...
- C#从入门到放弃治疗一:初探C#世界
C#是一款高级的面向对象语言,运行于.NET framework之上的高级程序设计语言.其语言规范和,语法和java有着惊人的类似之处.所以如果你在学习C#之前有着java的基础,你将快速地入门.当然 ...
- 初次使用Open Live Writer
关于下载和配置 建议大家不要在官网下载,会出不来.华军软件园(或其他下载站)也提供Open Live Writer最新版的下载. 创建账户时千万不要写错地址,错一个就失败. 体验 体验还是很好的,美中 ...
- Linux防火墙和iptables
1. CentOS 查看防火墙状态: systemctl status firewalld firewall-cmd --state 启停防火墙: # 开启 systemctl start firew ...
- Netty之JAVA BIO模型
一.JAVA BIO模型 1.I/O模型 I/O 模型简单的理解:就是用什么样的通道进行数据的发送和接收,很大程度上决定了程序通信的性能 Java 共支持 3 种网络编程模型/IO 模式:BIO.NI ...
- 显示HDFS中指定的文件读写权限、大小、创建时间、路径等信息。
1 import org.apache.hadoop.fs.*; 2 import java.text.SimpleDateFormat; 3 public class D_ReadFileStatu ...
- MonkeyScript
MonkeyScript的简单使用 一. 什么是MonkeyScript MS 是官方提供的,除了像猴子一样随机乱点之外,还可以通过编写脚本的形式,完成一系列固定的操作.MS 提供一整套完善的 API ...