Codeforces Round 894 (Div. 3) 题解
D , E , F D,E,F D,E,F 题解
D. Ice Cream Balls
题意:
对于一个数列,挑出其中两个数,重复挑视为同一种方案。给定挑选的方案数,求这一系列中数最小为多少。
题解:
若有一个数列 n n n 个数都不同,则方案数为 n ( n − 1 ) / 2 n(n - 1) / 2 n(n−1)/2 ,之后每增加一个重复的数字,方案数都会加一。
#include <bits/stdc++.h>
#define For(i, a, b) for (ll i = a; i <= b; i++)
#define _For(i, a, b) for (ll i = a; i >= b; i--)
typedef long long ll;
using namespace std;
void solve() {
ll n;
cin >> n;
ll l = 1, r = 2e9 + 5;
while (l + 1 < r) {
ll mid = l + r >> 1;
if (mid * (mid - 1) / 2 <= n) l = mid;
else r = mid;
}
cout << l + (n - l * (l - 1) / 2) << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
E. Kolya and Movie Theatre
题意
按顺序给定 n n n 个数,选择其中 k k k ( k ⩽ m ) (k \leqslant m) (k⩽m) 个数。每次选择数字,不仅需要加上 a [ i ] a[i] a[i] ,还要减去这个数的下标与上一个选的数的下标 × \times × d d d 。求能选择出的最大值。
题解
只需要看最后一次选择的是哪个数,再减去 d d d 乘上这个数的下标。那么只需要枚举最后一次选择的数,并维护之前序列中的 m m m 个较大的数即可。可以用优先队列,也可以用 m u l t i s e t multiset multiset 维护。
// 优先队列
#include <bits/stdc++.h>
#define For(i, a, b) for (ll i = a; i <= b; i++)
#define _For(i, a, b) for (ll i = a; i >= b; i--)
typedef long long ll;
using namespace std;
void solve() {
int n, m, d;
cin >> n >> m >> d;
ll ans = 0;
ll sum = 0;
priority_queue <int, vector<int>, greater<int> > q;
For (i, 1, n) {
int t;
cin >> t;
if (q.size() < m) {
if (t > 0) q.push(t), sum += t;
} else if (q.top() < t) {
sum -= q.top(), sum += t;
q.pop(), q.push(t);
}
ans = max(ans, sum - d * i);
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
// multiset
#include <bits/stdc++.h>
#define For(i, a, b) for (ll i = a; i <= b; i++)
#define _For(i, a, b) for (ll i = a; i >= b; i--)
typedef long long ll;
using namespace std;
void solve() {
int n, m, d;
cin >> n >> m >> d;
multiset <int> st; // 注意是 multiset
ll sum = 0, ans = 0;
For (i, 1, n) {
int t;
cin >> t;
if (st.size() < m) {
if (t > 0) st.insert(t), sum += t;
} else if (*st.begin() < t) {
sum -= *st.begin();
st.erase(st.begin());
st.insert(t);
sum += t;
}
ans = max(ans, sum - d * i);
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
F. Magic Will Save the World
题意
两个数字 A A A 与 B B B ,每一秒钟两个数都会加上固定的值 a a a 和 b b b。给定一个序列,求最小增加多少秒,可以满足两个数字可以一起大于给定的序列。
题解
设需要处理的数字和为 s s s ,若 A A A 处理了 t t t 的数字和,则 B B B 处理了 s − t s - t s−t 的数字和。那么则取 max ( ⌈ t a ⌉ , ⌈ s − t b ⌉ ) \max(\lceil \frac{t}{a} \rceil, \lceil \frac{s - t}{b} \rceil) max(⌈at⌉,⌈bs−t⌉) 。
用 01背包 处理出给定序列能取到的数字和。用 i i i 去枚举 A A A 可能取到的数字和,则 s − i s - i s−i 为 B B B 取到的数字和。求两者中最大即可。
// 枚举能取到的血量
#include <bits/stdc++.h>
#define For(i, a, b) for (ll i = a; i <= b; i++)
#define _For(i, a, b) for (ll i = a; i >= b; i--)
typedef long long ll;
using namespace std;
void solve() {
int w, f;
cin >> w >> f;
int n;
cin >> n;
vector <int> a(n + 1, 0);
int s = 0;
For (i, 1, n) {
cin >> a[i];
s += a[i];
}
vector <int> dp(s + 1, 0);
dp[0] = 1;
For (i, 1, n) {
_For (j, s, a[i]) {
dp[j] |= dp[j - a[i]];
}
}
double ans = s;
For (i, 0, s) {
if (dp[i]) {
ans = min(ans, max(ceil(1.0 * i / w), ceil(1.0 * (s - i) / f)));
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
G. The Great Equalizer
TBD.
参考
[1](https://zhuanlan.zhihu.com/p/652200768)