比赛链接:Here
A - Arithmetic Sequence (good)
注意细节
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
ll a, b, c;
cin >> a >> b >> c;
ll x = 2 * b - a - c;
ll k = (x >= 0 ? 0 : (1 - x) / 2);
ll ans = x + 3 * k;
cout << ans << endl;
}
B - Increasing Triples (good)
大根堆优先队列
using PQ = priority_queue<int, vector<int>, greater<int>>;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
PQ A, B, C;
for (int i = 0, x; i < n; ++i) {cin >> x; A.push(x);}
for (int i = 0, x; i < n; ++i) {cin >> x; B.push(x);}
for (int i = 0, x; i < n; ++i) {cin >> x; C.push(x);}
int a, b;
int ans = 0;
while (!A.empty()) {
a = A.top(); A.pop();
while (!B.empty() and B.top() <= a)B.pop();
if (B.empty() )break;
b = B.top(); B.pop();
while (!C.empty() and C.top() <= b)C.pop();
if (C.empty()) break;
C.pop();
++ans;
}
cout << ans ;
}
C - 1, 2, 3 - Decomposition (good)
给你一个数,让你分解成不含0的四进制之和,求最少分解成多少个?
从高位到低位思考,每一位有两种情况,一种借一给低位,另一种不借。含高位构造数数目小于等于含低位的构造数数目.
int solve(ll n) {
if (n == 0)return 0;
if (n < 10)return (n - 1) / 3 + 1;
ll mi = 0;
ll pi = n, pa, pb;
pb = solve(pi / 10 - 1);
pa = solve(pi / 10);
if (pa > pi % 10)return max(pb, (pi % 10 + 10 - 1) / 3 + 1);
return min(max(pa, (pi % 10 - 1) / 3 + 1), max(pb, (pi % 10 + 10 - 1) / 3 + 1));
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _; for (cin >> _; _--;) {
ll n; cin >> n;
cout << solve(n) << ‘\n‘;
}
}