A
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int te;
int cas = 1;
cin >> te;
while (te--) {
long long a, b, c;
cin >> a >> b >> c;
cout << "Case #" << cas++ << ": ";
cout << min(b + a, (b - c) * 2 + a) << "\n";
}
return 0;
}
B.数位dp
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll dp[20][2];
int dit[20];
ll n, m;
int next_state(int state, int pos, int i, bool lead) {
if (state == 0) {
return 0;
}
if (lead == 1 && i == 0) {
return 1;
}
return (pos % 2) == (i % 2);
}
ll dfs(int pos, int ps, int state, bool lead, bool limit) {
if (pos == -1) {
return state != 0;
}
if (!limit && !lead && dp[pos][state] != -1) {
return dp[pos][state];
}
int up = limit ? dit[pos] : 9;
ll ans = 0;
for (int i = 0; i <= up; ++i) {
if (lead) {
if (i == 0) {
ans += dfs(pos - 1, ps - 1, 1, 1, limit && (i == up));
}
else {
ans += dfs(pos - 1, ps, next_state(state, ps - pos, i, 0), 0, limit && (i == up));
}
}
else {
ans += dfs(pos - 1, ps, next_state(state, ps - pos, i, 0), 0, limit && (i == up));
}
}
if (!limit && !lead) {
dp[pos][state] = ans;
}
return ans;
}
ll solve(ll k) {
int pos = 0;
while (k) {
dit[pos++] = (int)(k % 10);
k /= 10;
}
return dfs(pos - 1, pos, 1, 1, 1);
}
int main() {
int te; cin >> te; int cas = 1;
memset(dp, -1, sizeof(dp));
while (te--) {
cin >> n >> m;
cout << "Case #" << cas++ << ": ";
cout << solve(m) - solve(n - 1) << '\n';
}
return 0;
}
c
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 100000 + 1;
LL X[maxn], Y[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
for(int tt = 1; tt <= T; tt += 1){
cout << "Case #" << tt << ": ";
int n;
cin >> n;
for(int i = 0; i < n; i += 1) cin >> X[i] >> Y[i];
sort(X, X + n);
sort(Y, Y + n);
for(int i = 0; i < n; i += 1) X[i] -= i;
sort(X, X + n);
LL ans = 0;
for(int i = 0; i < n; i += 1)
ans += abs(X[i] - X[n / 2]) + abs(Y[i] - Y[n / 2]);
cout << ans << "\n";
}
return 0;
}
D
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int inf = 1e9 + 7;
int mp[30][30];
void floyd(int n) {
for (int k = 0; k <= n; k++)
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) {
if (mp[i][k] + mp[k][j] < mp[i][j]) {
mp[i][j] = mp[i][k] + mp[k][j];
}
}
}
string G[100010];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int te;
int cas = 1;
cin >> te;
while (te--) {
cout << "Case #" << cas++ << ": ";
int n, m;
cin >> n >> m;
memset(mp, 0x3f3f3f3f, sizeof mp);
for (int t = 1; t <= n; t++) {
string tmp; cin >> tmp;
G[t] = tmp;
int len = tmp.length();
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
mp[tmp[i] - 'A'][tmp[j] - 'A'] = 1;
mp[tmp[j] - 'A'][tmp[i] - 'A'] = 1;
}
}
}
for (int i = 0; i <= 28; i++)
mp[i][i] = 0;
floyd(28);
while (m--) {
int c, d; cin >> c >> d;
string a = G[c], b = G[d];
int spot = 1, ans = 0x3f3f3f3f;
for (auto u : a) {
for (auto v : b) {
ans = min(ans, mp[u - 'A'][v - 'A']);
ans = min(ans, mp[v - 'A'][u - 'A']);
}
}
if (ans != 0x3f3f3f3f)cout << ans + 2 << " ";
else cout << "-1 ";
}
cout << "\n";
}
return 0;
}
/*
3 5
0 1 0
1 0 1
0 1 0
*/