题目大意:给你一个长度为$n$的序列,要求出长度大于等于$len$的字段的中位数中最大的一个中位数

题解:可以二分答案,对于比它小的数赋成$-1$,大的赋成$1$。求前缀和,若有一段区间的和大于$0$,说明这个数可以作为中位数。因为长度要大于等于$len$,所以可强制长度大于等于$len$。

如何求出最大字段和呢,可以求一个最小的前缀和,然后拿当前的前缀和减去就是以当前为结尾的最大字段和了

卡点:

C++ Code:

#include <cstdio>
#include <algorithm>
#define maxn 100010
const int inf = 0x3f3f3f3f;
int n, len, ans;
int s[maxn], v[maxn];
int pre[maxn];
inline int getmin(int &a, int b) {if (b < a) a = b; return a;}
bool check (int x) {
for (int i = 1, mn = inf; i <= n; i++) {
pre[i] = pre[i - 1] + (s[i] >= x ? 1 : -1);
if (i >= len && pre[i] - getmin(mn, pre[i - len]) > 0) return true;
}
return false;
}
int main() {
scanf("%d%d", &n, &len);
for (int i = 1; i <= n; i++) scanf("%d", s + i), v[i] = s[i];
int l = 1, r = (std::sort(v + 1, v + n + 1), std::unique(v + 1, v + n + 1) - v - 1);
while (l <= r) {
int mid = l + r >> 1;
if (check(v[mid])) {
l = mid + 1;
ans = mid;
} else r = mid - 1;
}
printf("%d\n", v[ans]);
return 0;
}

  

[NC2018-9-9T1]中位数的更多相关文章

  1. [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 ...

  2. [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 ...

  3. BZOJ1303 [CQOI2009]中位数图

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. 在MySQL中,如何计算一组数据的中位数?

    要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...

  5. AC日记——中位数 洛谷 P1168

    题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...

  6. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  7. LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1

    思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...

  8. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  9. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  10. 10G整数文件中寻找中位数或者第K大数

    来源:http://hxraid.iteye.com/blog/649831 题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数.内存限制为 2G.只写出思路即可(内存限制为 2G的意思就 ...

随机推荐

  1. 解决nsis error!cant initialize plug-ins directory.please try again later

    情况1: 调用SectionEnd会释放掉dll初始化标记,所有Section都必须放在函数的最下面. 情况2: 有可能是栈里的数据错乱,特别注意的是,使用BgWorker.dll获取多线程能力的时候 ...

  2. httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

    httpd配置内容 httpd2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf 服务脚本: /etc/rc.d/init.d/ ...

  3. ubuntu修改IP地址和网关的方法

    一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用vi编辑器(也可以 ...

  4. 【LeetCode #179】Largest Number 解题报告

    原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...

  5. 图解HTTP总结(4)——返回结果的HTTP状态码

    HTTP状态码负责表示客户端HTTP请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作. 状态码的类别 2XX 成功 200 OK 表示从客户端发来的请求在服务器端被正常处理了. 在响应 ...

  6. 430. Flatten a Multilevel Doubly Linked List

    /* // Definition for a Node. class Node { public: int val = NULL; Node* prev = NULL; Node* next = NU ...

  7. 理解JAVA与C的运行机制

    1.java的运行机制 java的编译过程,将java的源程序(扩展名为.java的文件),由java编译程序将java的字节码文件(.class文件)在jvm上运行,机器码有cpu运行, jvm编译 ...

  8. python协程和IO多路复用

     协程介绍                                                                                                ...

  9. 内存释放free函数的异常问题

    本次在实际应用中遇到一个问题,首先是定义了一个指针,然后这个指针指向某一个地址,但是这个地址不是用malloc分配的.如果后面用free去释放这个指针会产生什么现象. 首先看下指针的声明和使用 uin ...

  10. Webdriver--获得验证信息

    title:获得当前页面的标题 current_url:获得当前页面的URL text:前面提到过,获得标签对的文本信息 try: couseTitle = driver.find_element_b ...