牛客网 小白赛4 A三角形【贪心】
【前驱】:在指定长度的棍子中找到能组成最大周长三角形的三根棍子
链接:https://www.nowcoder.com/acm/contest/134/A
来源:牛客网
题目描述
铁子从森林里收集了n根木棍,她开始将它们按顺序的排成一排,从左到右依次为1到n,她回想起
在数学课上老师教她的三角形知识,她开始从这些木棍中间找三根木棍来组成一个周长最大的三角形,
这时她的兄弟顺溜偷偷的溜了过来,偷走了第i根木棍,现在她想知道现在能够组成周长最大的三角形
的周长是多少?
输入描述:
第一行两个整数n和q。(1 ≤ n, q ≤ 105)
第二行n个整数表示第i根木棍的长度ai。(1 ≤ ai ≤ 109)
接下来q行,每行一个整数表示被顺溜偷走的木棍编号。注意每行的事件是独立的,也就是说每一次操作都是对于原来的n根木棍进行的。
输出描述:
对于每个询问输出一行表示答案,如果删除木棍后无法组成三角形则输出 -1 。
示例1
输入
复制
6 2
1 2 3 4 5 6
6
5
输出
复制
12
13
【代码】:
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e5 + 100;
const int maxm = 100;
const double PI = acos(-1.0);
const double eps = 1e-8;
//const int dx[] = {-1,1,0,0,1,1,-1,-1};
//const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
ll n,q,p;
struct node
{
ll x,id;
}a[maxn];
bool cmp(node a, node b)
{
return a.x>b.x; //从最大值开始枚举
}
ll solve()
{
for(ll i=1; i+2<=n; i++) //i+2最大值不能超过n
{
ll k1=i,k2=i+1,k3=i+2; //模拟指针
if(a[i].id == p) { k1++; k2++; k3++;} //遇到要删除的目标,指针后移
else if(a[i+1].id == p) { k2++; k3++; }
else if(a[i+2].id == p) { k3++; }
if(a[k1].x < a[k2].x + a[k3].x) return a[k1].x + a[k2].x + a[k3].x; //合法返回周长
}
return -1;
}
int main()
{
scanf("%lld%lld",&n,&q);
for(ll i=1;i<=n;i++)
{
a[i].id=i;
scanf("%lld",&a[i].x);
}
sort(a+1,a+n+1,cmp);
while(q--)
{
scanf("%lld",&p);
printf("%lld\n",solve());
}
}
牛客网 小白赛4 A三角形【贪心】的更多相关文章
- 牛客网小白月赛5I区间(差分数组)
链接:https://www.nowcoder.com/acm/contest/135/I来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 牛客网训练赛26D(xor)
题目链接:https://www.nowcoder.com/acm/contest/180/D 线性基的学习:https://www.cnblogs.com/vb4896/p/6149022.html ...
- 牛客网-小白月赛6-J-洋灰三角
题目链接https://www.nowcoder.com/acm/contest/136/J 这题我还是不找规律了,老老实实推吧,传说找规律也可以,我还是算了 递推式:f(n)=k*f(n-1)+p ...
- 牛客网小白月赛6C(DFS,思维)
#include<bits/stdc++.h>using namespace std;vector<int>tree[1000010];int sum=0;int dfs(in ...
- 牛客网小白月赛6H(最小生成树【克鲁斯卡尔】)
#include<bits/stdc++.h>using namespace std;long long sum=0;struct node{ int a,b,len;}road[5 ...
- 牛客网小白月赛1 B,I
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> ...
- 牛客网 牛客小白月赛1 E.圆与三角形-公式题
E.圆与三角形 链接:https://www.nowcoder.com/acm/contest/85/E来源:牛客网 这个题把公式推一下, 发现就是1+sinA*r,sinA最大为1,所以 ...
- 牛客小白赛4 A 三角形 数学
链接:https://www.nowcoder.com/acm/contest/134/A来源:牛客网 题目描述 铁子从森林里收集了n根木棍,她开始将它们按顺序的排成一排,从左到右依次为1到n,她回想 ...
- 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...
随机推荐
- 【bzoj1412】[ZJOI2009]狼和羊的故事 网络流最小割
题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈 ...
- JAVA List 一边遍历一边删除元素
JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常 2015年02月10日 14:42:49 zhanzkw 阅读数:3 ...
- Intellij Idea debug 远程部署的的tomcat项目
web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是Intellij idea,可以通过如下方法实现: 开启debug端口,启动tomcat 以tomcat7.0.75为例,打开bi ...
- word公式编辑中的转义字符
Some of the commonly used symbols: \infty - Infinity \leq - Less then or equal \geq - ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- 两个数组的交集 II [ LeetCode - 350 ]
原题地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/description/ 给定两个数组,写一个方法来计算 ...
- CSS中的块级元素、内联元素(行内元素)
Block element 块级元素 顾名思义就是以块显示的元素,高度宽度都是可以设置的.比如我们常用 的<div>.<p>.<ul>默认状态下都是属于块级元 ...
- 解决设置了background-size: cover; 但是图片在ios下显示不完整的问题
设置 background-size: % 99.9%:
- java JDK动态代理的机制
一:前言 自己在稳固spring的一些特性的时候在网上看到了遮掩的一句话“利用接口的方式,spring aop将默认通过JDK的动态代理来实现代理类,不适用接口时spring aop将使用通过cgli ...
- Spring - IoC(1): Spring 容器
BeanFactory & ApplicationContext org.springframework.beans.factory.BeanFactory 是最基本的 Spring 容器接口 ...