HDU 6098 - Inversion | 2017 Multi-University Training Contest 6
/* HDU 6098 - Inversion [ 贪心,数论 ] | 2017 Multi-University Training Contest 6 题意: 求出所有B[i] = max(A[j]) [j % i != 0] 分析: 排个序从大到小找第一个不是它倍数的数. 至多扫倍数的个数,均摊复杂度O(nlogn) */ #include <bits/stdc++.h> using namespace std; struct Node { int x, i; }; bool cmp(Node a, Node b) { return a.x > b.x; } vector<Node> a; int b[100005]; int t, n; int main() { scanf("%d", &t); while (t--) { a.clear(); scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a.push_back(Node{x, i}); } sort(a.begin(), a.end(), cmp); for (int i = 2; i <= n; i++) { for (auto& c : a) { if (c.i % i == 0) continue; b[i] = c.x; break; } } for (int i = 2; i < n; i++) printf("%d ", b[i]); printf("%d\n", b[n]); } }
我自倾杯,君且随意