链接: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. HTML常用标签效果展示

    HTML常用标签效果展示 一.文本效果 段落1---收到了开发建设看来得更加快乐圣诞节福利肯定是减肥的路上苏里科夫就是打开了飞机都是风口浪尖上的疯狂了大煞风景圣诞快乐的索科洛夫几点上课了关键是低空掠过 ...

  2. python中 with 的作用

    with是 Python2.5 引入的一个新语法,它是一种上下文管理协议,目的在于从流程中吧 try...except 和 finally 关键字和资源释放相关代码统统去掉,简化 try...exce ...

  3. windows下 DEV-C++无法连接到pthread.h的解决办法

    参考的这个博文,原博文有图片:http://lslin.iteye.com/blog/776325 (我只是为了方便写.copy一遍) dev-C++编写C/C++程序时,非常方便轻巧,但是今天学习多 ...

  4. HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和

    Clarke and digits  Accepts: 16  Submissions: 29  Time Limit: 5000/3000 MS (Java/Others)  Memory Limi ...

  5. CVPR 2019 行人检测新思路:

    CVPR 2019 行人检测新思路:高级语义特征检测取得精度新突破 原创: CV君 我爱计算机视觉 今天 点击我爱计算机视觉置顶或标星,更快获取CVML新技术 今天跟大家分享一篇昨天新出的CVPR 2 ...

  6. VS Code 入门

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

  7. JS html页面

    js窗口置顶 if (window != top) top.location.href = location.href; js打开新窗口 js window.open()弹出窗口参数说明及居中设置 f ...

  8. 删除hdfs上的内容报错:rm: Cannot delete /wxcm/ Name node is in safe mode.

    问题:在执行删除hdfs上的内容时(hdfs dfs -rm -f -r -skipTrash /wxcm)报错:rm: Cannot delete /wxcm/ Name node is in sa ...

  9. Vue - 动态绑定class

    <div :class="{active:item.index==1}" >  </div>

  10. Python 面试问答基础篇

    1.       Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对 ...