题解+AC代码
首先将木棍按长度排序,可以确定的是可以组成的最大三角形的三根木棍就是连着的三个如果偷走的木棍不是这三根木棍中的其中一个,那么答案显然,否则分类处理一下。
#include<iostream>
#include<cstring>
#include<cstdbool>
#include<algorithm>
using namespace std;
int n, p;
struct node
{
long long x, y;
}a[100005],b[100005];
bool com(node p, node q)
{
return p.y > q.y;
}
int main()
{
while (cin >> n >> p)
{
for (int i = 0; i < n; ++i)
{
cin >> a[i].y;
a[i].x = i+1;
}
for (int i = 0; i < n; ++i)
b[i] = a[i];
sort(b, b + n,com);
int ans = 0,ant = 1, off = 0;
if (n < 4)
ant = 0;
while (p--)
{
cin >> ans;
long long nod[5];
if (ant)
{
int flag = 1;
for (int i = 0; i < n ; ++i)
{
if (b[i].x != ans)
{
nod[flag] = b[i].y;
++flag;
if (flag == 4)
{
if (nod[1] < nod[2] + nod[3])
{
off = 1;
break;
}
else
{
nod[1] = nod[2];
nod[2] = nod[3];
nod[3] = 0;
flag = 3;
}
}
}
}
}
if (ant&&off)
cout << nod[1] + nod[2] + nod[3] << endl;
else
cout << -1 << endl;
}
}
return 0;
}
一不小心,写了69行,太长了,我不开心。等我看完代码简洁之道再优化吧。
原文:https://www.cnblogs.com/cattree/p/9195888.html