1 条题解
-
0
平衡照片(Balanced Photo G) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct BIT { int n; vector<int> b; BIT(int n) : n(n), b(n + 1, 0) {} void add(int idx) { for (int i = idx; i <= n; i += i & -i) ++b[i]; } int query(int idx) const { int s = 0; for (int i = idx; i > 0; i -= i & -i) s += b[i]; return s; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; if (!(cin >> n)) return 0; vector<int> h(n), vals(n); for (int i = 0; i < n; ++i) { cin >> h[i]; vals[i] = h[i]; } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); int m = (int)vals.size(); for (int i = 0; i < n; ++i) { h[i] = (int)(lower_bound(vals.begin(), vals.end(), h[i]) - vals.begin()) + 1; } vector<int> L(n), R(n); BIT bitL(m), bitR(m); for (int i = 0; i < n; ++i) { L[i] = i - bitL.query(h[i]); bitL.add(h[i]); } for (int i = n - 1; i >= 0; --i) { R[i] = (n - 1 - i) - bitR.query(h[i]); bitR.add(h[i]); } int ans = 0; for (int i = 0; i < n; ++i) { int mn = min(L[i], R[i]); int mx = max(L[i], R[i]); if (mx > 2 * mn) ++ans; } cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 8639
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者