链接: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. [Write-up]-Trollcave: 1.2

    关于 下载地址:点我 Flag:root/flag.txt 哔哩哔哩:视频 信息收集 不知道VM虚拟机怎么啦,导入镜像后,用Nmap扫了,发现不了主机.所以这次用了VBox. vboxnet0的IP为 ...

  2. PAT T1010 Lehmer Code

    跟1009几乎是同一道题~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; ]; int r[maxn]; int ...

  3. Beautisoup库

    所看视频: https://www.bilibili.com/video/av9784617/?p=34 一, Beautifulsoup是一个可以从HTML或XML文件中提取数据的Python库,它 ...

  4. Python学习第十六课——静态属性(property, classmethod, staticmethod)

    计算所居住房子的面积 普通写法 class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.ow ...

  5. warning:Pointer is missing a nullability type specifier (__nonnull or __nullable)

    当我们定义某个属性的时候  如果当前使用的编译器版本比较高(6.3+)的话经常会遇到这样一个警告:warning:Pointer is missing a nullability type speci ...

  6. 31 反射方式给类的属性赋值 和 对象赋值(clone)

    1.配置类 package com.da.tool.util.configuration.reflect; /** */ public class JobInfo { private String j ...

  7. SQL数据库入门基础

      SQL(Structure Query Language,结构化查询语言)语言是国际标准化组织(ISO)采纳的标准数据库语言. 数据库就是一幢大楼,我们要先盖楼,然后再招住户(住户当然就是数据库对 ...

  8. 搭建springboot的ssm(spring + springmvc + mybatis)的maven项目

    最终项目目录结构 创建过程 1.创建开关SpringBootApplication 为了创建快速.我们使用idea自带的创建springboot来创建结构,当然创建普通的web项目也是可以的.(使用e ...

  9. pytorch中tensor张量数据基础入门

    pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...

  10. kill和raise

    kill向特定的进程和进程组发送信号 raise向进程自身发送信号