A
题目大意
给出三维坐标都与空间直角坐标系 $O − xyz$ 的三条轴平行的长方体盒子的底面 $z$ 坐标 $z_0$,高度 $h$,底面对角线坐标 $(u_0, v_0),(u_1, v_1)$ 和 $q$ 个点 $(x_i , y_i , z_i)$,问每个点是否在盒子内。
思路
直接判断即可。
需要注意,这里面的 $u_0$ 不一定大于 $u_1$,以及 $v_0$ 不一定大于 $v_1$,需要在开始进行判断。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> #define int long long using namespace std;int z0, u0, v0, u1, v1, h, q;signed main () { cin >> z0 >> h >> u0 >> v0 >> u1 >> v1 >> q; if (u0 > u1) swap (u0, u1); if (v0 > v1) swap (v0, v1); while (q--) { int x, y, z; cin >> x >> y >> z; if (x >= u0 && x <= u1 && y >= v0 && y <= v1 && z >= z0 && z <= z0 + h) { cout << "YES\n" ; } else { cout << "NO\n" ; } } return 0 ; }
B
题目大意
给一个字符串,可以左移至多 $k$ 次,最大化左移之后 nanjing 子串的数量。
思路
将这个字符串复制一遍,然后寻找字符串中 nanjing 的数量,记录字符串的起始点,然后用系统的二分查找一个区间内的 nanjing 有多少个。
这里有一个很奇妙的错误,就是我在查找 nanjing 字符串的时候,如果 s.size() 不转 int,那么调用 substr 的时候就会 RE,可能是因为 size() 函数的返回值是 size_t。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <bits/stdc++.h> using namespace std;int T, n, k;string s; void solve () { cin >> n >> k >> s; s = s + s; vector<int > v; for (int i = 0 ; i < (int )s.size () - 7 ; i++) { if (s.substr (i, 7 ) == "nanjing" ) { v.push_back (i); } } if (v.empty ()) { cout << "0\n" ; return ; } int ans = 0 ; for (int i = 0 ; i <= min (k, n - 1 ); i++) { int l = lower_bound (v.begin (), v.end (), i) - v.begin (); int r = upper_bound (v.begin (), v.end (), i + n - 7 ) - v.begin (); ans = max (ans, r - l); } cout << ans << '\n' ; } int main () { cin.tie (0 )->sync_with_stdio (0 ); cin >> T; while (T--) { solve (); } return 0 ; }
C
题目大意
给定一个 $n × mk$ 的 ASCII 可见字符网格,按顺序每 $k$ 个代表一个字符串。 输出唯一一个和其他字符串不一样的字符串的位置。
思路
直接枚举判断即可。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <bits/stdc++.h> using namespace std;int n, m, k, cnt;map<string, int > mp; vector<tuple<string, int , int >> v; int main () { cin >> n >> m >> k; for (int i = 1 ; i <= n; i++) { for (int j = 1 ; j <= m; j++) { char ch; string s; for (int l = 1 ; l <= k; l++) { cin >> ch; s += ch; } if (!mp[s]) { mp[s]++; v.push_back ({s, i, j}); } else { mp[s]++; if (v.size () != 1 ) { for (auto [x, y, z] : v) { if (s != x) { cout << y << ' ' << z << '\n' ; return 0 ; } } } } } } for (auto [s, x, y] : v) { if (mp[s] == 1 ) { cout << x << ' ' << y; return 0 ; } } return 0 ; }
D
题目大意
你有 $n$ 个雕像,石像朝向 $4$ 个方向:前方、右方、后方或左方。 每次你都可以进行以下一种操作
选一个雕像不动,其他所有石像同时顺时针旋转 $90$ 度 (别读错题不是 指定的那个雕像顺时针转 $90$ 度)
使所有雕像顺时针旋转 $90$ 度。 要使所有雕像面向前方,最少需要操作多少次?
思路
操作一可以看成,选中的那个雕像逆时针转 $90$ 度,其他雕像不动。
然后我发现肯定是先进行操作一,将所有雕像的朝向都调成一致,再进行操 作二让它们朝向前方,操作二次数一定小于 $4$
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int cnt[4 ], n;int main () { cin >> n; for (int i = 1 , x; i <= n; i++) { cin >> x; cnt[x]++; } int ans = 1e9 ; for (int i = 0 ; i < 4 ; i++) { int sum = 0 ; for (int j = 1 ; j < 4 ; j++) { sum += cnt[(i + j) % 4 ] * j; } sum += 4 - (sum + i + 3 ) % 4 - 1 ; ans = min (ans, sum); } cout << ans; return 0 ; }
E
题目大意
有一篇文章 $S$,只包含字母和特殊字符。给定一个大小为 $n$ 单词集合。 问将文章中的单词添加到单词集合中后,集合中单词的个数增加了多少。
思路
读入字符串后分离单词,用 map 记录一个单词是否已经查过字典,如果没有就答案++。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <bits/stdc++.h> using namespace std;string s; int n, ans;map<string, bool > vis; int main () { getline (cin, s); cin >> n; while (n--) { string t; cin >> t; for (int i = 0 ; i < t.size (); i++) { if (t[i] >= 'A' && t[i] <= 'Z' ) { t[i] = t[i] - 'A' + 'a' ; } } vis[t] = 1 ; } vis["" ] = 1 ; string now; for (int i = 0 ; i < s.size (); i++) { if (s[i] >= 'a' && s[i] <= 'z' ) { now += s[i]; } else if (s[i] >= 'A' && s[i] <= 'Z' ) { now += s[i] - 'A' + 'a' ; } else { if (!vis[now]) { vis[now] = 1 ; ans++; } now = "" ; } } cout << ans; return 0 ; }
F
题目大意
你有一个时钟,给定该时钟目前指向的时刻。你需要找到一个给定范围内的最小时刻,使得该时刻到给定时刻的时针夹角和 分针夹角的角度之和最小。
思路
由于时间较小,可以直接枚举要到那个时刻。
需要注意,分针的转动会影响时针的转动,所以会出现小数点,如果直接用 double 进行计算就会丢失精度,所以要用 int 来计算。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <bits/stdc++.h> using namespace std;int T;int solve (int x0, int y0, int x, int y) { int total_min0 = x0 * 60 + y0; int total_min = x * 60 + y; int d_hour = abs (total_min - total_min0); d_hour = min (d_hour, 720 - d_hour); int min_hand0 = y0 * 12 ; int min_hand = y * 12 ; int d_min = abs (min_hand - min_hand0); d_min = min (d_min, 720 - d_min); return d_hour + d_min; } int main () { cin >> T; while (T--) { int x0, y0, x1, y1, x2, y2; cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2; int ansx = x1, ansy = y1; int ans = solve (x0, y0, ansx, ansy); for (int x = x1; x <= x2; x++) { for (int y = (x == x1 ? y1 : 0 ); y <= (x == x2 ? y2 : 59 ); y++) { int sum = solve (x0, y0, x, y); if (sum < ans) { ansx = x, ansy = y; ans = sum; } else if (sum == ans) { if (x < ansx || (ansx == x && y < ansy)) { ansx = x, ansy = y; } } } } cout << ansx << ' ' << ansy << '\n' ; } return 0 ; }
G
题目大意
$k$ 名工人加工 $n$ 个工件,第 $i$ 个工件在第 $t_i$ 分钟的开头加入 工人 $w_i$ 的收件箱。
每分钟,工人从收件箱里拿出一个工件,完成加工后放入下 一个工人的收件箱(如果是最后一个工人则加工完成)。
问所有工件加工完成需要几分钟。
思路
工件 $i$ 原本的结束时间是 $(t_i + k − w_i)$,但每个时间点只能完成一个工件。
因此设 $a_i$ 表示从小到大排序后的工件完成时间,依次进行 更新 $a_i = \max(a_i , a_i−1 + 1)$ 即可。复杂度 $O(n \log n)$。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <bits/stdc++.h> using namespace std;const int N = 2e5 + 10 ;int T, n, k, a[N];void solve () { cin >> n >> k; for (int i = 1 , w, t; i <= n; i++) { cin >> w >> t; a[i] = t + k - w; } sort (a + 1 , a + n + 1 ); int ans = 0 ; for (int i = 1 ; i <= n; i++) ans = max (ans + 1 , a[i]); cout << ans << '\n' ; } int main () { cin >> T; while (T--) { solve (); } return 0 ; }
H
题目大意
一个游戏比赛,有 $n$ 个关卡和 $m$ 名玩家。比赛过程中会按时间顺序接收到 $q$ 条日志消息,包括关卡开始、玩家完成关卡或玩家放弃关卡的消息。需要根据这些消息计算每个玩家的总积分:
只有当前活动关卡的消息有效(由最近的类型 $1$ 消息决定)。
每个玩家在每个关卡的第一个有效消息决定其状态(完成或放弃),后续消息忽略。
完成关卡的玩家按完成顺序获得积分(第一名 $m$ 分,第二名 $m-1$ 分,依此类推),未完成或放弃的玩家得 $0$ 分。
最终按总积分降序输出玩家ID和积分,积分相同则按ID升序排列。
思路
按照题面模拟即可。
需要注意的是,这道题需要开 long long。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 #include <bits/stdc++.h> using namespace std;const int N = 1e5 + 10 ;int T, n, m, q, contest_score, now_contest;map<int , bool > participants, finish; struct node { int id; long long score; } score[N]; void start (int x) { if (x == now_contest) return ; now_contest = x; contest_score = m; participants.clear (); finish.clear (); } void Contest_finish (int x, int y) { if (y != now_contest) return ; if (participants[x]) return ; if (finish[x]) return ; score[x].score += contest_score; finish[x] = 1 ; contest_score--; } void Contest_abstain (int x, int y) { if (y != now_contest) return ; participants[x] = 1 ; } void init () { for (int i = 1 ; i <= m; i++) score[i] = {i, 0 }; participants.clear (); finish.clear (); now_contest = contest_score = 0 ; } void out () { sort (score + 1 , score + m + 1 , [](node x, node y){ if (x.score == y.score) return x.id < y.id; return x.score > y.score; }); for (int i = 1 ; i <= m; i++) { auto [x, y] = score[i]; cout << x << ' ' << y << '\n' ; } } void solve () { cin >> n >> m >> q; init (); while (q--) { int op, x, y; cin >> op >> x; if (op == 1 ) { start (x); } else if (op == 2 ) { cin >> y; Contest_finish (x, y); } else { cin >> y; Contest_abstain (x, y); } } out (); } int main () { cin.tie (0 )->sync_with_stdio (0 ); cin >> T; while (T--) { solve (); } return 0 ; }
I
题目大意
你知道比赛结束时你们队的过题数和罚时。你通过看榜知道了某个队伍封榜前的过题数和罚时,和封榜后什么时候提交了哪些题,你想知道这个队伍有没有可能排在你们队前面。
如果可能,输出至少过多少题才能排在你们队前面,如果不 可能输出 $-1$。
思路
已经评测出来的就直接计算出来即可,没有评测出来的就用一个 vector 来记录即可。记录完后,按照这些记录的题目的罚时进行排序,然后一个个加,知道满足条件就输出。
注意,可能他都不需要通过题目就可以赢我们。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include <bits/stdc++.h> using namespace std;int T, n, a, b, s;int f[20 ];void solve () { memset (f, 0 , sizeof f); cin >> n >> a >> b >> s; int cnt = 0 , res = 0 ; vector<int > vt; for (int i = 1 ; i <= s; i++) { int t; char p; string v; cin >> t >> p >> v; int id = p - 'A' ; if (f[id] == -1 ) continue ; if (v == "rj" ) f[id]++; else { if (v == "ac" ) { cnt++; res += t + f[id] * 20 ; f[id] = -1 ; } else { vt.push_back (t + f[id] * 20 ); f[id] = -1 ; } } } sort (vt.begin (), vt.end ()); if (cnt > a || (cnt == a && res < b)) { cout << "0\n" ; return ; } int ans = 0 ; for (auto x : vt) { cnt++; ans++; res += x; if (cnt > a || (cnt == a && res < b)) { cout << ans << '\n' ; return ; } } cout << "-1\n" ; } int main () { cin.tie (0 )->sync_with_stdio (0 ); cin >> T; while (T--) { solve (); } return 0 ; }