出题人参考代码 (非题解/std)
题解移步 orz
A
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
std::cin >> n;
std::vector<pair<int, int>> p(n + 1);
for (int i = 1; i <= n; i++) {
auto &[x, y] = p[i];
std::cin >> x;
y = i;
}
std::sort(p.begin() + 1, p.end());
for (int i = 1; i <= n; i++) {
std::cout << p[i].second << " \n"[i == n];
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
B
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, x, y;
std::cin >> n >> x >> y;
std::vector<std::array<int, 4>> p;
std::function<void(int, int, int, int)> dfs = [&](int x1, int y1, int x2, int y2) {
// std::cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n';
if (x1 == x2 && y1 == y2) return;
int len1 = (x2 - x1 + 1), len2 = len1 - 1;
if (x != x1) {
if (y != y1) {
p.push_back({len1, x1, y1, 1});
p.push_back({len2, x1 + 1, y1, 0});
dfs(x1 + 1, y1 + 1, x2, y2);
return;
} else {
p.push_back({len1, x1, y1, 1});
p.push_back({len2, x1 + 1, y2, 0});
dfs(x1 + 1, y1, x2, y2 - 1);
return;
}
} else {
if (y != y1) {
p.push_back({len1, x2, y1, 1});
p.push_back({len2, x1, y1, 0});
dfs(x1, y1 + 1, x2 - 1, y2);
return;
} else {
p.push_back({len1, x2, y1, 1});
p.push_back({len2, x1, y2, 0});
dfs(x1, y1, x2 - 1, y2 - 1);
return;
}
}
};
dfs(1, 1, n, n);
std::cout << "Yes\n";
for (auto [a, b, c, d] : p) {
std::cout << a << ' ' << b << ' ' << c << ' ' << d << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
C
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, l, r;
std::cin >> n >> l >> r;
std::cout << l << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
}
D
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, m;
std::cin >> n >> m;
std::map<int, int> mp;
for (int i = 1; i <= n; i++) {
std::string s;
std::cin >> s;
for (auto x : s) {
mp[x - '0']++;
}
}
int a = mp[1], b = mp[2], c = mp[3];
int y = c - b;
a -= y, b -= y;
a *= 2;
int x = (a - b) / 3;
int z = (b - x) / 2;
std::cout << x << ' ' << y << ' ' << z << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
E
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, m, k;
std::cin >> n >> m >> k;
std::vector<std::vector<int>> is(n + 2, std::vector<int>(m + 2));
auto t = is;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
std::cin >> is[i][j];
}
}
std::map<std::string, int> to;
to["l"] = 0;
to["r"] = 1;
to["u"] = 2;
to["d"] = 3;
std::map<int, std::string> fto;
fto[10] = "l";
fto[11] = "r";
fto[12] = "u";
fto[13] = "d";
fto[20] = "L";
fto[21] = "R";
fto[22] = "U";
fto[23] = "D";
fto[0] = "X";
fto[1] = "O";
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};
auto sml = [&](int x) {
return x == 11 || x == 12 || x == 13 || x == 10;
};
auto big = [&](int x) {
return x == 21 || x == 22 || x == 23 || x == 20;
};
for (int i = 1; i <= k; i++) {
int x, y;
std::string o;
std::cin >> x >> y >> o;
if (is[x][y] != 1) {
continue;
}
// std::cout << i << ' ' << x + dx[to[o]] << ' ' << y + dy[to[o]] << ' ' << is[x + dx[to[o]]][y + dy[to[o]]] << '\n';
if (sml(is[x + dx[to[o]]][y + dy[to[o]]])) {
is[x + dx[to[o]]][y + dy[to[o]]] += 10;
continue;
}
is[x][y] = to[o] + 10;
t[x][y] = i;
std::vector<std::array<int, 4>> a;
for (int j = 0; j < 4; j++) {
int rj = j ^ 1;
if (is[x + dx[j]][y + dy[j]] == rj + 10) {
a.push_back({-t[x + dx[j]][y + dy[j]], x + dx[j], y + dy[j], j});
}
}
std::sort(a.begin(), a.end());
for (auto [T, X, Y, j] : a) {
is[x][y] += 10;
is[X][Y] = 1;
t[X][Y] = 0;
break;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
std::cout << fto[is[i][j]];
} std::cout << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
F
#include <bits/stdc++.h>
using namespace std;
#define int long long
int tr[2000000][21], idx;
int id[2000000];
int to_int(char c) {
if (c >= 'A' && c <= 'Z') {
return c - 'A' + 10;
}
return c - '0';
}
char to_chr(int c) {
if (c < 10) {
return c + '0';
} return c - 10 + 'A';
}
void build(std::string s, int x) {
int now = 0;
for (int i = 0; i < s.size(); i++) {
if (!tr[now][to_int(s[i])]) {
tr[now][to_int(s[i])] = ++idx;
}
now = tr[now][to_int(s[i])];
}
id[now] = x;
}
int query(std::string s) {
int now = 0;
for (int i = 0; i < s.size(); i++) {
for (int j = 15 - to_int(s[i]); j >= 0; j--) {
if (tr[now][j]) {
now = tr[now][j];
goto G;
}
}
for (int j = 15; j > 15 - to_int(s[i]); j--) {
if (tr[now][j]) {
now = tr[now][j];
goto G;
}
}
G:;
}
return id[now];
}
void solve() {
int n, q;
std::cin >> n >> q;
for (int i = 1; i <= n; i++) {
std::string s, t = "";
std::cin >> s;
for (int i = 1; i + s.size() <= 20; i++) {
t += '0';
}
t += s;
build(t, i);
// std::cout << t << '\n';
}
for (int i = 1; i <= q; i++) {
int x;
std::cin >> x;
std::string t = "";
while (x) {
t += to_chr(x % 16);
x /= 16;
}
while (t.size() < 20) {
t += '0';
}
std::reverse(t.begin(), t.end());
// std::cout << t << '\n';
std::cout << query(t) << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
G
#include <bits/stdc++.h>
using namespace std;
#define int long long
int SG[1000][1000];
int sg(int x, int k) {
if (x >= k) {
return 0;
}
if (SG[x][k]) return SG[x][k];
std::set<int> s;
for (int i = 1; i <= x; i++) {
s.insert(sg(x + i, k));
}
for (int i = 0; ; i++) {
if (!s.count(i)) {
return SG[x][k] = i;
}
}
return -1;
}
void solve() {
int n, k;
std::cin >> n >> k;
// for (int i = 1; i <= 50; i++) {
// for (int j = i; j <= 50; j++) {
// std::cout << (sg(i, j) == 0 ? 0 : 1) << " \n"[j == 50];
// }
// }
if (n >= k) {
std::cout << "Bob\n";
return;
}
k -= n - 1;
for (int i = n, j = 1; ; i *= 2, j *= 2) {
k -= j;
if (k <= 0) {
std::cout << "Bob\n";
return;
}
k -= i;
if (k <= 0) {
std::cout << "Alice\n";
return;
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
}
H
#include <bits/stdc++.h>
using namespace std;
#define int long long
int to_int(char c) {
if (c >= 'A' && c <= 'Z') {
return c - 'A' + 10;
}
return c - '0';
}
char to_chr(int c) {
if (c < 10) {
return c + '0';
} return c - 10 + 'A';
}
void solve() {
int n;
std::cin >> n;
std::map<std::string, int> pos, neg;
int ans = 0;
for (int i = 1; i <= n; i++) {
std::string s;
std::cin >> s;
if (s[0] == '-') {
int m = 16;
std::string t = "", rs = "";
for (int j = s.size() - 1; j; j--) {
if (m - to_int(s[j]) != 16) {
t += to_chr(m - to_int(s[j]));
m = 15;
} else {
t += '0';
}
rs += s[j];
}
std::string r = t;
while (r.size() < 100) {
r += 'F';
if (neg.count(r)) {
ans += neg[r];
// std::cout << s << ' ' << r << '\n';
}
}
int pre0 = 1;
r = "";
for (int j = t.size() - 1; j >= 0; j--) {
if (pre0) {
if (to_int(t[j])) {
pre0 = 0;
}
}
if (!pre0) {
r += t[j];
}
}
if (r == "") {
r = "0";
}
std::reverse(r.begin(), r.end());
if (neg.count(r)) {
// std::cout << s << ' ' << r << '\n';
ans += neg[r];
}
neg[rs]++;
} else {
ans += pos[s];
pos[s]++;
}
}
std::cout << ans * 2 << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize(3, "inline")
// #define int long long
#ifndef Cai_Guang
#define debug //
#define test //
#endif
int fto(char c) {
if ('0' <= c && c <= '9') return c - '0';
else return c - 'A' + 10;
}
char to(int x) {
if (0 <= x && x <= 9) return x + '0';
else return x - 10 + 'A';
}
void solve() {
int n;
std::cin >> n;
std::unordered_map<std::string, int> mp_pos, mp_neg;
long long ans = 0;
for (int i = 1; i <= n; i++) {
std::string s;
std::cin >> s;
if (s[0] != '-') {
if (mp_pos.count(s)) {
ans += mp_pos[s];
}
mp_pos[s]++;
} else {
std::string r = "", t = "";
std::reverse(s.begin(), s.end());
auto id = s.end();
s.erase(--id);
int minus = 16;
for (int i = 0; i <= s.size() - 1; i++) {
int g = fto(s[i]);
r += to((minus - g) % 16);
if (g && minus == 16) {
minus = 15;
}
}
t = r;
for (; t.size() <= 100; ) {
t += 'F';
if (mp_neg.count(t)) {
ans += mp_neg[t];
}
}
t = "";
int pre0 = 1;
for (int i = r.size() - 1; i >= 0; i--) {
char x = r[i];
if (x != '0') {
pre0 = 0;
}
if (!pre0) {
t += x;
}
}
if (t == "") {
t = "0";
}
std::reverse(t.begin(), t.end());
ans += mp_neg[t];
// std::cout << "!" << s << ' ' << t << '\n';
mp_neg[s]++;
}
}
std::cout << ans * 2 << '\n';
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
#ifdef Cai_Guang
//freopen("1.in", "r", stdin);
localTest = true;
#endif
int t = 1;
// std::cin >> t;
while(t--) {
solve();
}
}
I
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct C {
int x, y, r;
void read() {
std::cin >> x >> y >> r;
}
};
C c[10];
void solve() {
c[1].read(), c[2].read();
int d2 = (c[1].x - c[2].x) * (c[1].x - c[2].x) + (c[1].y - c[2].y) * (c[1].y - c[2].y);
int rp2 = (c[1].r + c[2].r) * (c[1].r + c[2].r);
int rm2 = (c[1].r - c[2].r) * (c[1].r - c[2].r);
if (c[1].x == c[2].x && c[1].y == c[2].y && c[1].r == c[2].r) {
std::cout << -1 << '\n';
return;
}
if (d2 == rm2) {
std::cout << 1 << '\n';
return;
}
if (d2 == rp2) {
std::cout << 3 << '\n';
return;
}
if (d2 > rp2) {
std::cout << 4 << '\n';
return;
}
if (d2 > rm2) {
std::cout << 2 << '\n';
return;
}
std::cout << 0 << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
}
J
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
std::cin >> n;
std::vector<std::vector<int>> G(n + 1, std::vector<int>());
for (int i = 1; i < n; i++) {
int u, v;
std::cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
std::vector<int> dep(n + 1);
G[0].push_back(1);
int ans = n * (n - 1) * (n - 2) / 6;
std::function<void(int, int)> dfs = [&](int x, int fx) {
dep[x] = dep[fx] + 1;
for (auto y : G[x]) {
if (y == fx) continue;
dfs(y, x);
}
// std::cout << x << ' ' << G[x].size() << ' ' << dep[x] << ' ' << ans << '\n';
if (G[x].size() == 1) {
ans -= dep[x] * (dep[x] - 1) * (dep[x] - 2) / 6;
} else {
ans += dep[x] * (dep[x] - 1) * (dep[x] - 2) / 6 * (G[x].size() - 2);
}
};
dfs(1, 0);
std::cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
K
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void solve() {
int n, m;
std::cin >> n >> m;
int x = 1, y = 0;
std::vector<std::vector<int>> a(n + 1, std::vector<int>(m + 1));
for (int i = 1, j = 0; i <= n * m; i++) {
int nx = x + dx[j], ny = y + dy[j];
if (nx > n || nx < 1 || ny > m || ny < 1 || a[nx][ny]) {
j ++;
j %= 4;
}
x += dx[j], y += dy[j];
a[x][y] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
std::cout << a[i][j] << " \n"[j == m];
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
L
其中开 __int128 是因为原本的数据范围较大,后续对题目进行了调整。
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, m, x;
std::cin >> n >> m >> x;
int l = 0, r = min(n, m) / 2 + 1;
while (l + 1 < r) {
int mid = (l + r + 1) >> 1;
int b = n + n + m + m - 4, e = b - (mid - 1) * 8;
__int128 sum = (__int128)(b + e) * mid / 2;
if (sum < x) {
l = mid;
} else {
r = mid;
}
}
r--;
int b = n + n + m + m - 4, e = b - (r - 1) * 8;
__int128 sum = (__int128)(b + e) * r / 2;
x -= sum;
int L = n - r * 2 - 1, R = m - r * 2 - 1;
if (x <= R) {
std::cout << r + 1 << ' ' << r + x << '\n';
} else if (x <= L + R) {
std::cout << r + x - R << ' ' << r + R + 1 << '\n';
} else if (x <= L + R + R) {
std::cout << r + L + 1 << ' ' << r + R + 1 - (x - L - R) + 1 << '\n';
} else {
std::cout << r + L + 1 - (x - L - R - R) + 1 << ' ' << r + 1 << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
}
M
#include <bits/stdc++.h>
using namespace std;
void solve() {
int a, b, c, d;
std::cin >> a >> b >> c >> d;
if (a != b && b == c && c != d && a != d) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// std::cin >> t;
while (t--) {
solve();
}
}
验题榜单,感谢各位的参与与批评指正/
查看15道真题和解析