題解 CF766B【Mahmoud and a Triangle】
阿新 • • 發佈:2021-08-17
這道題的排序演算法很多大佬都講得很清楚了,所以這裡給出一種奇奇怪怪的做法。
我們指定\(1-3\)號為選擇的邊,然後每次模擬退火,隨機交換\(1-3\)號中的邊和\(4-n\)號中的邊,通過前三條邊中較小的兩條邊之和與第三邊之差更新答案。
如此我們理論上就求得了最大可能的差值,和0比個大小就可以了。
\(Code:\)
/* * @Author: Luisvacson * @LastEditors: Luisvacson * @Descriptions: None * @Date: 2021-05-22 20:03:50 * @LastEditTime: 2021-05-22 20:06:23 * @FilePath: \C++\Practice\CF766B Mahmoud and a Triangle.cpp */ #include <bits/stdc++.h> using namespace std; const double T = 0.996; #define MAXN 100005 int n, ans; int a[MAXN]; int Solve() { sort(a + 1, a + 4); return a[1] + a[2] - a[3]; } void SA() { double tp = 10000; while (tp > 1e-15) { int ix = rand() % 3 + 1, iy = rand() % n + 1; if (ix == iy) continue; swap(a[ix], a[iy]); int tans = Solve(); int dif = tans - ans; if (tans > ans) { ans = tans; } else if (!(exp(-dif / tp) * RAND_MAX > rand())) { swap(a[ix], a[iy]); } tp *= T; } } signed main() { scanf("%lld", &n); register int i; for (i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (i = 1; i <= 100; ++i) SA(); puts(ans > 0 ? "YES" : "NO"); return 0; }