voidsolve() { int n, x, m; cin >> n >> x >> m; x--; vector<int> vec(n); for (int i = 0; i < n; i++) cin >> vec[i]; int lft = INT_MAX, rt = INT_MAX; for (int i = x - 1; i >= 0 && lft == INT_MAX; i--) if (vec[i] != 0 && vec[i] <= m) lft = i; for (int i = x + 1; i < n && rt == INT_MAX; i++) if (vec[i] != 0 && vec[i] <= m) rt = i; int ans = min(abs(lft - x), abs(rt - x)) * 10; cout << ans << endl; }
voidsolve() { int n; cin >> n; vector<int> vec(n + 1); map<int, vector<int>> pos; for (int i = 1; i <= n; i++) cin >> vec[i], pos[vec[i]].push_back(i); int ans = 0; for (int i = 2; i < n; i++) { auto search = pos[vec[i] + 1]; auto lft = lower_bound(all(search), i); if (lft != search.end()) ans += distance(search.begin(), lft) * distance(lft, search.end()); } cout << ans << endl; }
C.平分苹果
思路
赛时写了个特别难绷的模拟。
赛后队友点了一下发现,不进位的二进制加法 == 异或。
于是就成水题了。
只要异或起来不等于0,无解。否则和减去最小值即可。
代码
1 2 3 4 5 6 7 8 9 10
voidsolve() { int n; cin >> n; vector<int> vec(n); int x = 0, sum = 0, mn = INT_MAX; for (i64 i = 0; i < n; i++) cin >> vec[i], sum += vec[i], mn = min(mn, vec[i]), x ^= vec[i]; cout << (x ? -1 : sum - mn) << endl; }
voidsolve() { int n, m; cin >> n >> m; vector<queue<int>> bar(m); for (int i = 0; i < m; i++) { int k; cin >> k; int ball; while (k--) cin >> ball, bar[i].push(ball); } map<int, vector<int>> ontop_idx; queue<int> both; for (int i = 0; i < m; i++) { int ball = bar[i].front(); ontop_idx[ball].push_back(i); if (ontop_idx[ball].size() == 2) both.push(ball); } while (!both.empty()) { int ball = both.front(); both.pop(); for (int i = 0; i < 2; i++) { int idx = ontop_idx[ball][i]; bar[idx].pop(); if (!bar[idx].empty()) { int cur_top = bar[idx].front(); ontop_idx[cur_top].push_back(idx); if (ontop_idx[cur_top].size() == 2) both.push(cur_top); } } } for (auto it : bar) { if (!it.empty()) { cout << "No" << endl; return; } } cout << "Yes" << endl; }
voidsolve(int n) { map<int, vector<int>, greater<>> mp; vector<int> days; for (int i = 0; i < n; i++) { int p, d; cin >> p >> d; mp[d].push_back(p); } for (auto &[k, v] : mp) days.push_back(k); days.push_back(0); int ans = 0; priority_queue<int> pool; for (int i = 1; i < days.size(); i++) { int cnt = days[i - 1] - days[i]; for (auto v : mp[days[i - 1]]) pool.push(v); while (cnt-- && !pool.empty()) ans += pool.top(), pool.pop(); } cout << ans << "\n"; }