链接:https://ac.nowcoder.com/acm/contest/102/C
来源:牛客网

题目描述

 We define a value of an interval is the second largest number of it's elements, and of course an interval has at least two elements.

Given an array A with n elements and a number k, can you find the value of the kth largest interval?

输入描述:

The first line contains an integer number T, the number of test cases. 
For each test case : 
The first line contains two integer numbers n,k(2 ≤ n ≤ 105,1 ≤ k ≤ n(n−1)/2), the number of test cases. 
The second lines contains n integers Ai(1 ≤ Ai ≤ 109), the elements of array A.
 

输出描述:

For each test case print the value of the k

th

 largest interval.
示例1

输入

复制

2
3 3
1 2 3
5 1
1 2 2 3 3

输出

复制

1
3

说明

For the sample input, there are three intervals.
Interval [1 2 3] has value 2.
Interval [2 3] has value 2.
Interval [1 2] has value 1.
So the 3

rd

 largest interval is [1 2] whose value is 1.

思路:求第k大,本题和POJ3579相似,二分然后贪心检验,尺取法,最少2个元素,就维护2个元素的队列,例如:
1 2 3 4, 选择2,3时,前面可选择1或2, 后面可选择3或4,就是4种
typedef long long LL;

const int maxm = 1e5+;

int buf[maxm], n, q[maxm];
LL k; bool check(int x) {
LL sum = , front = , rear = , last = -;
for(int i = ; i < n; ++i) {
if(buf[i] >= x) q[front++] = i;
if(front - rear > ) {
sum += (q[rear] - last) * (n - i);
last = q[rear++];
}
}
return sum >= k;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%lld", &n, &k);
for(int i = ; i < n; ++i)
scanf("%d", &buf[i]);
int l = , r = 1e9, ans, mid;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid)) {
ans = mid;
l = mid + ;
} else
r = mid - ;
}
printf("%d\n", ans);
}
return ;
}



Day6 - 牛客102C的更多相关文章

  1. Day6 - 牛客203E

    https://ac.nowcoder.com/acm/contest/203/E 埋坑不会做

  2. 牛客国庆集训派对Day6 A Birthday 费用流

    牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A 题意: 恬恬的生日临近了.宇扬给她准备了一个蛋糕. 正如往常一样, ...

  3. 牛客网国庆集训派对Day6 题目 2018年

    链接:https://www.nowcoder.com/acm/contest/206/A来源:牛客网 Birthday 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576 ...

  4. 牛客国庆集训day6 B Board (模拟标记思维或找规律或分块???)

    链接:https://www.nowcoder.com/acm/contest/206/B来源:牛客网 题目描述 恬恬有一个nx n的数组.她在用这个数组玩游戏: 开始时,数组中每一个元素都是0. 恬 ...

  5. 牛客网程序员面试金典:1.1确定字符互异(java实现)

    问题描述: 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同, ...

  6. 牛客网 --java问答题

    http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...

  7. 【面试笔试算法】牛客网一站通Offer编程题2016.4.19

    牛客网一站通offer (一)字符串变形 1. 题目: 对于一个给定的字符串,我们需要在线性(也就是O(n))的时间里对它做一些变形.首先这个字符串中包含着一些空格,就像"Hello Wor ...

  8. 牛客网《BAT面试算法精品课》学习笔记

    目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...

  9. 牛客小白月赛13 小A买彩票 (记忆化搜索)

    链接:https://ac.nowcoder.com/acm/contest/549/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

随机推荐

  1. 使用mvn deploy命令将本地jar包上传到maven私服

    记录一下,以后少走弯路 前提:已经搭建好nexus maven私服,地址192.168.110.240:9091 在maven的setting.xml中找到<mirrors></mi ...

  2. 本地启动tomcat的时候报java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space

    1.问题:我在tomcat中放入了大量的war包,启动的时候报:java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: ...

  3. Debug运行项目时报错,connected to the target VM, address: '127.0.0.1:50495', transport: 'socket'

    Debug运行项目时报错,无法进入Debug,猜想出错原因可能是未正确关闭IDEA. 解决方法,先直接运行项目,然后停掉项目,再用Debug模式启动,问题解决.

  4. 零基础学完Python的7大就业方向,哪个赚钱多?

    “ 我想学 Python,但是学完 Python 后都能干啥 ?” “ 现在学 Python,哪个方向最简单?哪个方向最吃香 ?” “ …… ” 相信不少 Python 的初学者,都会遇到上面的这些问 ...

  5. Design and History FAQ for Python3

    Source : Design and History FAQ for Python3 Why is there no goto? 你可以通过异常来获得一个可以跨函数调用的 "goto 结构 ...

  6. [转]ubuntu备份与恢复

    在 使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过Windows系统,那么你一定记忆犹新:首先需要找到一个备份工 具(通常都是私有软件),然后重启电脑进入备份工具提供 ...

  7. vue项目真机测试

    1.在项目中重启命令行运行ipconfig(如果是ios系统运行ifconfig)获取到项目在本机运行的ip地址 2.在项目的package.json中修改允许访问ip地址(--host 0.0.0. ...

  8. java猜拳

    import java.util.Scanner; public class startGuess { Person jia; Computer yi; int count; public void ...

  9. Restful API及接口安全

    一.简介 REST(Representational State Transfer,具体状态转移),是一种基于HTTP协议.URI(统一资源定位符).JSON和XML这些现有协议与标准的,针对网络应用 ...

  10. C 语言入门---第六章 C语言数组

    数组就是一些列具有相同类型的数据的集合,这些数据在内存中一次挨着存放,彼此之间没有缝隙. 我们可以将二维数组看作一个Excel表格,有行有列,length1 表示行数,length2 表示列数,要在二 ...