2024 University of Shanghai for Science and Technology(USST) Freshman Challenge Contest
A. 我是组题人
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (auto &i: a) cin >> i;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
ranges::sort(p, [&](const int x, const int y) -> bool {
if (a[x] != a[y]) return a[x] < a[y];
return x < y;
});
for (auto i: p)
cout << i + 1 << " ";
}
C. 小学题
\[\frac{m ^ 2}{2} + \frac{( n + m) ( n - m)}{ 2} = \frac{n ^ 2}{2}
\]
发现阴影部分的面积和\(m\)无关。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
i64 n, l, r;
cin >> n >> l >> r;
cout << l << "\n";
return;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
}
E. 昨日方舟
模拟题
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const int inf = INT_MAX;
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector g(n, vector<char>(m, 'O'));
for (auto &gi: g)
for (auto &gij: gi) {
int x;
cin >> x;
if (x == 0) gij = 'X';
}
auto dx = [](char x) {
if (x == 'u') return -1;
if (x == 'd') return 1;
return 0;
};
auto dy = [](char y) {
if (y == 'l') return -1;
if (y == 'r') return 1;
return 0;
};
vector lst(n, vector<int>(m, inf));
for (; k; k--) {
int x, y;
char t;
cin >> x >> y >> t, x--, y--;
if (g[x][y] != 'O') continue;
int fx = x + dx(t), fy = y + dy(t);
if (fx >= 0 and fy >= 0 and fx < n and fy < m and 'a' <= g[fx][fy] and g[fx][fy] <= 'z') {
g[fx][fy] += 'A' - 'a';
} else {
g[x][y] = t, lst[x][y] = k;
int ret = inf;
for (int gx, gy; auto i: "udlr") {
gx = x - dx(i), gy = y - dy(i);
if (gx < 0 or gy < 0 or gx >= n or gy >= m)continue;
if (g[gx][gy] != i) continue;
ret = min(ret, lst[gx][gy]);
}
if (ret == inf) continue;
g[x][y] += 'A' - 'a';
for (int gx, gy; auto i: "udlr") {
gx = x - dx(i), gy = y - dy(i);
if (gx < 0 or gy < 0 or gx >= n or gy >= m)continue;
if (lst[gx][gy] != ret) continue;
g[gx][gy] = 'O';
break;
}
}
}
for (auto &gi: g) {
for (auto &gij: gi) cout << gij;
cout << "\n";
}
return 0;
}
G. 石子游戏
打表找规律,发现先手必胜的情况是连续的若干段。
第一段的长度是\(t_1 = n\),之后每一段长度是$t_i = 2t_i $
第一段的与 0 的间隔是\(d_1 = 2(n + 1)\),之后每一段与前一段的间隔都是\(d_i = 2 d_{i-1}\)
第一段的结尾在\(p_1 = d_1 - 2\),之后每一段的结尾在\(p_{i} = p_{i - 1} + d_{i-1}\)
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
map<pii, int> g;
int sg(int n, int k) {
if (g.count(pair(n, k))) return g[pair(n, k)];
for (int i = 1; i <= n; i++) {
if (n + i > k) break;
if (sg(n + i, k) == 0) return g[pair(n, k)] = 1;
}
return g[pair(n, k)] = 0;
}
void test() {
for (int i = 3; i <= 80; i++) {
cout << i << " " << sg(3, i) << "\n";
}
exit(0);
}
void solve() {
int n, k;
cin >> n >> k;
int d = (n + 1) * 2, p = d - 2, t = n;
while (p < k) {
p += d;
d *= 2, t *= 2;
}
if (k > p - t) cout << "Alice\n";
else cout << "Bob\n";
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
I. 纠缠之圆
主要考虑圆心距离与半径之间的关系。
首先如果重合有无限条切线。
如果圆心距大于半径之和有 4 条
如果圆心距等于半径之和有 3 条
如果圆心距大于半径之差有 2 条
如果圆心距等于半径之差有 1 条
剩下情况有0 条
逐一判断就好
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
i64 o1x, o2x, o1y, o2y, o1r, o2r;
cin >> o1x >> o1y >> o1r;
cin >> o2x >> o2y >> o2r;
if (o1x == o2x and o1y == o2y and o1r == o2r) {
cout << "-1\n";
return;
}
i64 d = (o1x - o2x) * (o1x - o2x) + (o1y - o2y) * (o1y - o2y);
i64 r1 = (o1r + o2r) * (o1r + o2r);
i64 r2 = (o1r - o2r) * (o1r - o2r);
if (d > r1) {
cout << 4 << "\n";
} else if (d == r1) {
cout << 3 << "\n";
} else if (d > r2) {
cout << 2 << "\n";
} else if (d == r2) {
cout << 1 << "\n";
} else {
cout << 0 << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
}
J. 上学
题面的意思,可以抽象为,选三个点,且三个点不在一条链上方案数,这里链指的是从根到叶子的一条路径。
我们可以用总方案数\(C_n^3\)减去三个点在一条链方案数。
我们可以枚举三个点中深度最深的点,然后从这个点到根路径上的点有\(d\)个,那么剩下两个点的选择方案就是\(\frac{d(d-1)}{2}\)。
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
vector<vi> e(n + 1);
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
vi dis(n + 1);
auto dfs = [&](auto &&self, int x, int fa) -> void {
for (auto y: e[x]) {
if (y == fa) continue;
dis[y] = dis[x] + 1;
self(self, y, x);
}
};
dfs(dfs, 1, -1);
int res = n * (n - 1) * (n - 2) / 6;
for (int i = 1; i <= n; i++)
res -= dis[i] * (dis[i] - 1) / 2;
cout << res;
return 0;
}
K. 环形数组(easy)
维护当前点的坐标和移动方向,暴力的把图填出来就好了。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector a(n, vector<int>(m));
int x = 0, y = 0, d = 0;
for (int i = 1, N = n * m, fx, fy; i <= N; i++) {
a[x][y] = i;
fx = x + dx[d], fy = y + dy[d];
if (fx < 0 or fy < 0 or fx >= n or fy >= m or a[fx][fy] != 0) {
d = (d + 1) % 4;
fx = x + dx[d], fy = y + dy[d];
}
x = fx, y = fy;
}
for (auto ai: a) {
for (auto aij: ai)
cout << aij << " ";
cout << "\n";
}
return 0;
}
L. 环形数组(hard)
我们一圈一圈的考虑,第\(i\)的总个数是\(2n + 2m + 4 - 8i\),前\(i\)圈的总个数是\((2n+2m-4i)i\)。这样我们可以二分出应该在第几圈然后暴力判断最后一圈就好了。
#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
void solve() {
int n, m, t;
cin >> n >> m >> t, t--;
int l = 1, r = min(n, m), i = -1;
while (l <= r) {
int mid = (l + r) / 2;
if ((2 * n + 2 * m - 4 * mid) * mid >= t)
i = mid, r = mid - 1;
else l = mid + 1;
}
int x = i, y = i;
i--;
t -= (2 * n + 2 * m - 4 * i) * i, n -= 2 * i + 1, m -= 2 * i + 1;
if (t > 0) y += min(m, t), t -= min(m, t);
if (t > 0) x += min(n, t), t -= min(n, t);
if (t > 0) y -= min(m, t), t -= min(m, t);
if (t > 0) x -= min(n, t), t -= min(n, t);
cout << x << " " << y << "\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}
M. 蔡光数组
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a != b and b == c and a != d and b != d) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}