hdu-6601 Keen On Everything But Triangle
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=6601
Description
N sticks are arranged in a row, and their lengths are a1,a2,...,aN.
There are Q querys. For i-th of them, you can only use sticks between li-th to ri-th. Please output the maximum circumference of all the triangles that you can make with these sticks, or print −1 denoting no triangles you can make.
Input
There are multiple test cases.
Each case starts with a line containing two positive integers N,Q(N,Q≤105).
The second line contains N integers, the i-th integer ai(1≤ai≤109) of them showing the length of the i-th stick.
Then follow Q lines. i-th of them contains two integers li,ri(1≤li≤ri≤N), meaning that you can only use sticks between li-th to ri-th.
It is guaranteed that the sum of Ns and the sum of Qs in all test cases are both no larger than 4×105.
Output
For each test case, output Q lines, each containing an integer denoting the maximum circumference.
Sample Input
5 3
2 5 6 5 2
1 3
2 4
2 5
Sample Output
13
16
16
Hint
题意
给一个长度为N的数组,Q个询问,(l, r)区间内任三个数能构成的三角形的最大周长
题解:
对于排序好的数组,若想要构成三角形周长最大,肯定从最大的边开始取,且三条边是连续的,也就是先取第一大第二大第三大,若不能构成三角形则取第二大第三大第四大,依次取下去。
未排序的数组可以用主席数查询第K大,对于每次询问最多只要查询四十多次,因为若要构造出不能构成三角形的数组,最优构造策略是斐波那契数列,1,2,3,5,8,11,19,到四十多项就超过1e9了。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int mx = 1e5+5;
typedef long long ll;
int a[mx], root[mx], cnt;
vector <int> v;
struct node {
int l, r, sum;
}T[mx*40];
int getid(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}
void update(int l, int r, int &x, int y, int pos) {
T[++cnt] = T[y]; T[cnt].sum++; x = cnt;
if (l == r) return;
int mid = (l+r) / 2;
if (mid >= pos) update(l, mid, T[x].l, T[y].l, pos);
else update(mid+1, r, T[x].r, T[y].r, pos);
}
int query(int l, int r, int x, int y, int k) {
if (l == r) return l;
int mid = (l+r) / 2;
int sum = T[T[y].l].sum - T[T[x].l].sum;
if (sum >= k) return query(l, mid, T[x].l, T[y].l, k);
else return query(mid+1, r, T[x].r, T[y].r, k-sum);
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
v.clear(); cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) update(1, n, root[i], root[i-1], getid(a[i]));
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
bool flag = false;
int len = y - x + 1;
for (int j = 1; j <= y-x-1; j++) {
ll a = v[query(1, n, root[x-1], root[y], len-j+1)-1];
ll b = v[query(1, n, root[x-1], root[y], len-(j+1)+1)-1];
ll c = v[query(1, n, root[x-1], root[y], len-(j+2)+1)-1];
if (b+c > a) {
flag = true;
printf("%lld\n", a+b+c);
break;
}
}
if (!flag) puts("-1");
}
}
return 0;
}
hdu-6601 Keen On Everything But Triangle的更多相关文章
- HDU - 6601 Keen On Everything But Triangle 主席树
Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...
- 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解
题意: 有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形.棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\). 思路: 由于不构成 ...
- 2019杭电多校第二场hdu6601 Keen On Everything But Triangle
Keen On Everything But Triangle 题目传送门 解题思路 利用主席树求区间第k小,先求区间内最大的值,再求第二大,第三大--直到找到连续的三个数可以构成一个三角形.因为对于 ...
- hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树
题意: 给定一个数列,每次询问一个区间,问这个区间中的值可组成的周长最大的三角形的周长. 题解: 定理1:给定一些值,这些值中组成边长最大的三角形的三条边的大小排名一定是连续的. 证明:假如第k大,第 ...
- HDU 2018 Multi-University Training Contest 1 Triangle Partition 【YY】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6300 Triangle Partition Time Limit: 2000/1000 MS (Java ...
- 2019 Multi-University Training Contest 2 - 1011 - Keen On Everything But Triangle - 线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6601 首先要贪心地想,题目要最长的边长,那么要怎么构造呢?在一段连续的区间里面,一定是拿出最长的三根出来比,这样 ...
- [2019杭电多校第二场][hdu6601]Keen On Everything But Triangle
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601 题意是说用给定区间内的数字组成周长最大的三角形. 大致做法就是求区间第1大,第2大和第3大然后判 ...
- hdu 6601 区间条件极值 - 区间 最大 三角形周长
题目传送门//res tp hdu 目的 对长度为n的区间,给定q个子区间,求其元素能构成三角形的最大周长.有多组测试. n 1e5 q 1e5 ai [1,1e9] (i∈[1,n]); 数据结构 ...
- 2019hdu多校 Keen On Everything But Triangle
Problem Description N sticks are arranged in a row, and their lengths are a1,a2,...,aN. There are Q ...
随机推荐
- Nginx服务器安全加固tips整理
公司各业务网站大多用到Nginx,花了点时间整理了一下Nginx服务器安全加固的各类tips. 默认配置文件和Nginx端口 /usr/local/nginx/conf/-Nginx配置文件目录,/u ...
- 日常用shell命令
递归更改文件夹权限:chmod -R 767 文件名 mac启动apache sudo apachectl start/restart mac停止apache sudo apachectl stop ...
- Core CLR 自定义的Host官方推荐的一种形式(第一种)
.Net Core CLR提供两种Host API访问 托管代码的形式,按照微软官方的说法,一种是通过CoreClr.DLL来直接调用托管生成的DLL程序集,另外一种是通过CoreClr里面的C导出函 ...
- xpath beautiful pyquery三种解析库
这两天看了一下python常用的三种解析库,写篇随笔,整理一下思路.太菜了,若有错误的地方,欢迎大家随时指正.......(conme on.......) 爬取网页数据一般会经过 获取信息-> ...
- ieda控制台缓冲区限制问题
一.现象 控制台输出数据若超过默认值时,将从后向前取默认值大小数据(1024) 二.解决方案 1.配置文件(idea安装目录/bin/idea.properties) 2.找到该栏:idea.cycl ...
- 【Java例题】7.3 线程题3-素数线程
3.素数线程.设计一个线程子类,依次随机产生10000个随机整数(100-999):再设计另一个线程子类,依次对每一个随机整数判断是不是素数,是则显示:然后编写主类,在主函数中定义这两个线程类的线程对 ...
- weblogic10.3.6漏洞修改方案
1.CVE-2018-2628漏洞 CVE-2018-2628漏洞利用的第一步是与weblogic服务器开放在服务端口上的T3服务建立socket连接,可通过控制T3协议的访问来临时阻断攻击行为. W ...
- python3学习-requests使用
前面我们讲过了urllib模块,知道他是用于网络请求的,这一节讲的requests还是用于网络请求的,只不过urllib是官方模块,而requests是第三方的模块.用过的人都说他才是'人类使用的', ...
- Tomcat源码分析 (三)----- 生命周期机制 Lifecycle
Tomcat里面有各种各样的组件,每个组件各司其职,组件之间又相互协作共同完成web服务器这样的工程.在这些组件之上,Lifecycle(生命周期机制)至关重要!在学习各个组件之前,我们需要看看Lif ...
- Linux启动之旅
引言 某出租房内,某台电脑的电源键被按下,于是开启了一段Linux启动之旅... BIOS 系统启动,首先进入BIOS. ● BIOS 为 Base Input/Output System(基本输入输 ...