有空就写思路
A - Digit Minimization
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t -- ) {
int n;
cin >> n;
int mn = 10;
if (n < 100) {
cout << n % 10 << endl;
} else {
while (n) {
int tmp = n % 10;
mn = min(mn, tmp);
n /= 10;
}
cout << mn << endl;
}
}
}
B - Z mod X = C
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t -- ) {
int a, b, c;
cin >> a >> b >> c;
cout << a + b + c << ' ' << b + c << ' ' << c << endl;
}
}
C - Column Swapping
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t -- ) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ ) cin >> a[i][j];
int pos = 0;
bool ok = 1;
vector<int> b;
while (pos < n && ok) {
b = a[pos];
sort(b.begin(), b.end());
bool f = 1;
for (int i = 0; i < m; i ++ )
if (b[i] != a[pos][i]) {
f = 0;
break;
}
if (f) pos ++;
else {
ok = 0;
break;
}
}
if (pos == n) {
cout << "1 1\n";
continue;
}
vector<int> res;
for (int i = 0; i < m; i ++ )
if (b[i] != a[pos][i]) res.push_back(i);
if (res.size() == 2) {
int x = res[0], y = res[1];
bool ok = 1;
for (pos = 0; pos < n; pos ++ ) {
b = a[pos];
sort(b.begin(), b.end());
swap(a[pos][x], a[pos][y]);
for (int i = 0; i < m; i ++ )
if (a[pos][i] != b[i]) {
ok = 0;
break;
}
if (!ok) break;
}
if (ok) cout << x + 1 << ' ' << y + 1 << endl;
else cout << -1 << endl;
} else {
cout << -1 << endl;
}
}
}
D - Traps
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef long long LL;
typedef vector<int> VI;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t -- ) {
int n, k;
cin >> n >> k;
VI a(n), vis(n);
vector<pair<int, int>> b(n);
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
b[i] = {a[i] + i, i};
}
sort(b.begin(), b.end(), greater<pair<int, int>>());
for (int i = 0; i < k; i ++ )
vis[b[i].second] = 1;
LL ans = 0;
int cnt = 0;
for (int i = 0; i < n; i ++ )
if (!vis[i]) ans += cnt + a[i];
else cnt ++;
cout << ans << endl;
}
}
E - MEX vs DIFF
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef pair<int, int> PII;
typedef vector<int> VI;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t -- ) {
int n, k;
cin >> n >> k;
VI a(n);
map<int, int> mp;
for (int i = 0; i < n; i ++ ) {
cin >> a[i];
mp[a[i]] ++;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int mex = 0, cnt = 0, pos = 0, res = n;
for (int i = 0; i <= n && res >= cnt; i ++ ) {
if (cnt > k) break;
if (a[pos] == i) pos ++;
else cnt ++;
mex = i;
res -= mp[i];
}
if (cnt > k) cnt = k;
vector<PII> st;
if (pos < a.size()) {
for (auto [x, num] : mp)
if (x >= a[pos] && num)
st.PB({num , x});
}
sort(st.begin(), st.end());
int ans = 0, sum = 0;
for (auto [num, x] : st) {
if (sum >= cnt) ans ++;
else {
if (sum + num > cnt) ans ++;
sum += num;
}
}
cout << ans << endl;
}
return 0;
}