链接: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. 为PHP开发搭建环境

    为了能在自己的电脑上(mac OS系统)开始编写PHP代码并完成运行,需要有: 1.安装Web服务器 2.安装PHP 3.安装数据库,比如MySQL 4.一个PHP的IDE 为了上面所提到的1~3步的 ...

  2. [转]Shell编程

    原文链接 Shell编程其实真的很简单(一) 如今,不会Linux的程序员都不意思说自己是程序员,而不会shell编程就不能说自己会Linux.说起来似乎shell编程很屌啊,然而不用担心,其实she ...

  3. 1014 Waiting in Line (30分)

    1014 Waiting in Line (30分)   Suppose a bank has N windows open for service. There is a yellow line i ...

  4. k种球若干,取n个球,输出所有取球方案 (模拟)

    有K种颜色的小球(K<=10),每种小球有若干个,总数小于100个. 现在有一个小盒子,能放N个小球(N<=8),现在要从这些小球里挑出N个小球,放满盒子. 想知道有哪些挑选方式.注:每种 ...

  5. LeetCode 876. Middle of the Linked List(获得链表中心结点)

    题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...

  6. VS Code 入门

    将VSCode设置成中文语言环境 快捷键[Ctrl+Shift+P]—输入[Configure Display Language]—将“en”改为“zh-CN”—打开extention输入[Chine ...

  7. [转]Java——Servlet的配置和测试

    本文转自:http://blog.csdn.net/makefish/article/details/6904807 本文以一个实例介绍如何用Java开发Servlet. 主要内容有: 配置和验证To ...

  8. mkvirtualenv: 未找到命令的解决方法

    1.升级python包管理工具pip pip install --upgrade pip 备注:当你想升级一个包的时候 `pip install --upgrade 包名` 2.python虚拟环境安 ...

  9. window查看连接过的无线密码

    for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do  @echo %j | find ...

  10. Scrapy 中的模拟登陆

    目前,大部分网站都具有用户登陆功能,其中某些网站只有在用户登陆后才能获得有价值的信息,在爬取这类网站时,Scrapy 爬虫程序先模拟登陆,再爬取内容 1.登陆实质 其核心是想服务器发送含有登陆表单数据 ...