题意
给出一个序列,我们要在其中找到不同位置的三个数,满足可以构成三角形,且使它们位置尽量靠前。
有mmm次操作,每个操作形式如下:
1 x y1\ x\ y1 x y:将AxA_xAx改为yyy
222:查询最优的合法解,从小到大给出这三个数(而不是位置)。
思路
直接朴素地做。
因为如果数据要使我们尽量循环次数多,那么答案应该就在后面,但是AiA_iAi最大10910^9109。而斐波那契数列刚好就是三角形不满足情况的最小值,我们发现在505050位的时候就超出范围了,所以在505050的范围内就会找到答案,时间复杂度O(503m)O(50^3m)O(503m)。
代码
#include<cstdio>
#include<algorithm>
int n, m;
int a[100001];
int check(int x, int y, int z) {
int f = 1;
f &= x + y > z;
f &= x + z > y;
f &= y + z > x;
return f;
}
void solve() {
int ans1 = -1, ans2 = -1, ans3 = -1;
for (int k = 3; k <= n && ans1 == -1; k++)
for (int j = 2; j <= k && ans1 == -1; j++)
for (int i = 1; i <= j && ans1 == -1; i++)
if (i != j && j != k && i != k)
if (check(a[i], a[j], a[k])) {
ans1 = a[i], ans2 = a[j], ans3 = a[k];
break;
}
if (ans1 > ans2) std::swap(ans1, ans2);
if (ans1 > ans3) std::swap(ans1, ans3);
if (ans2 > ans3) std::swap(ans2, ans3);
printf("%d %d %d\n", ans1, ans2, ans3);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
int op, x, y;
for (int i = 1; i <= m; i++) {
scanf("%d", &op);
if (op == 1) {
scanf("%d %d", &x, &y);
a[x] = y;
} else solve();
}
}