AtCoder Beginner Contest 188 题解
AtCoder Beginner Contest 188
A,B很简单就不多说
C - ABC Tournament
找出前一半的最大值和后一半的最大值,二者中较小的那一个对应的序号就是最后的答案。
时间复杂度:\(\mathcal{O}(2^N)\)
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
ll a[70000];
int n;
cin >> n, n = pow(2, n);
for (int i = 0; i < n; ++i) cin >> a[i];
ll m1 = a[0], idx = 0;
for (int i = 1; i < n / 2; ++i)
if (a[i] > m1) m1 = a[i], idx = i;
ll m2 = a[n / 2], idxx = n / 2;
for (int i = n / 2 + 1; i < n; ++i)
if (a[i] > m2) m2 = a[i], idxx = i;
cout << (m1 < m2 ? idx + 1 : idxx + 1) << "\n";
return 0;
}
D - Snuke Prime
时间复杂度:\(\mathcal{O}(N)\)
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
ll N, C;
cin >> N >> C;
vector<pair<ll, ll>> event;
for (ll i = 0; i < N; ++i) {
ll a, b, c;
cin >> a >> b >> c;
event.push_back({a - 1, c});
event.push_back({b, -c});
}
sort(event.begin(), event.end());
ll ans = 0, fee = 0, time = 0;
for (auto [x, y] : event) {
if (x != time) {
ans += min(C, fee) * (x - time);
time = x;
}
fee += y;
}
cout << ans << "\n";
return 0;
}
E - Peddler
题意:N 个城镇,每个镇子购买和卖出 \(1kg\) 黄金的价格是 \(A_i\) ,现在可以我们有 M条道路,问怎么才能获得最大收益
思路:虽然看过去需要搜索做,但这里只要逆向动态规划即可。
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> A(N + 1);
for (int i = 1; i <= N; ++i) cin >> A[i];
vector<vector<int>> e(N + 1);
for (int i = 0; i < M; ++i) {
int u, v;
cin >> u >> v;
// e[u].emplace_back(v), e[v].emplace_back(u);
e[u].emplace_back(v); // 这里单向和双向都没什么区别
}
vector<int> h(N + 1, -1e9);
int ans = -1e9;
for (int i = N; i > 0; --i) {
for (auto v : e[i]) h[i] = max(h[i], h[v]);
ans = max(ans, h[i] - A[i]);
h[i] = max(h[i], A[i]);
}
cout << ans << "\n";
return 0;
}
F - +1-1x2
- 如果 \(X \ge Y\) ,则答案为\(X - Y\)
- 如果 \(X < Y\) ,则可以考虑用DFS,详细可以看代码
using ll = long long;
ll x, y, ans = 1e18;
map<ll, ll> mp;
ll dfs(ll a) {
if (mp.count(a)) return mp[a];
if (a <= x) return x - a;
if (a & 1)
return mp[a] = min(min(dfs((a + 1) / 2), dfs((a - 1) / 2)) + 2, a - x);
else
return mp[a] = min(dfs(a / 2) + 1, a - x);
}
int main() {
cin >> x >> y;
cout << dfs(y);
}