第一周作业讲评
注意事项
- 当前阶段 CSP 中无需考虑
STL效率问题(list除外),极其推荐使用。 STL在绝大多数情况下比手写快(开启O2优化)。- 自带的函数不手写(如
std::sort) - CSPJS/NOIP 2020 及之前没有
O2优化,C++版本为C++98。 - CSPJS/NOIP 2021 及以后有
O2优化,C++版本为C++14。
各题讲解
Split with Sequence
- 模拟桶排序
爱心专座
- 数学题:设有 \(m\) 个爱心专座,则扶手个数 \(n + 1 - \cfrac{m}{2}\)。
- 又因为只有 \(n\) 个人,所以答案为 \(max(n,n + 1 - \cfrac{m}{2})\)
弱化版五子棋
- 枚举三连子的中间一个,就可以向四面八方扩展
代码
#include <bits/stdc++.h>
using namespace std;
char a[35][35];
int check(int x, int y) {
if (a[x][y] == '.')
return 0;
if (a[x - 1][y] == a[x][y] && a[x][y] == a[x + 1][y]) // 横着
return 1;
if (a[x][y - 1] == a[x][y] && a[x][y] == a[x][y + 1]) // 竖着
return 1;
if (a[x - 1][y - 1] == a[x][y] && a[x][y] == a[x + 1][y + 1]) // 斜着
return 1;
if (a[x - 1][y + 1] == a[x][y] && a[x][y] == a[x + 1][y - 1]) // 斜着
return 1;
return 0;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
if (check(i, j) == 1) {
cout << a[i][j] << endl;
return 0;
}
}
}
cout << "ongoing" << endl;
return 0;
}
机器翻译
代码
#include <bits/stdc++.h>
using namespace std;
int isInQueue[1005];
queue<int> q;
int main() {
int m, n, ans = 0;
cin >> m >> n;
for (int i = 1; i <= n; i ++) {
int x;
cin >> x;
if (isInQueue[x]) continue;
if (q.size() < m) {
q.push(x);
isInQueue[x] = 1;
++ans;
} else {
isInQueue[q.front()] = 0;
q.pop();
q.push(x);
isInQueue[x] = 1;
++ans;
}
}
cout << ans << endl;
return 0;
}
City and States
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, cnt;
map<string, int> s;
vector<string> vec[200005];
int main() {
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i ++) {
string x, y;
cin >> x >> y;
string xx = x.substr(0, 2);
int cur = s[xx];
if (cur != 0 && xx != y) {
for (int i = 0; i < vec[cur].size(); i ++) {
if (vec[cur][i] == y) {
++ans;
}
}
}
if (s[y] == 0) {
++cnt;
s[y] = cnt;
}
vec[s[y]].push_back(xx);
}
cout << ans << endl;
return 0;
}
小小的埴轮兵团
代码
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n, m, k;
ll a[300005];
deque<ll> dq;
int main() {
cin >> n >> m >> k;
for (ll i = 1; i <= n; i ++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (ll i = 1; i <= n; i ++) {
dq.push_back(a[i]);
}
ll l = -k, r = k;
while (m--) {
ll op, x;
cin >> op;
if (1 <= op && op <= 2) {
cin >> x;
if (op == 1) {
// 正方向移动士兵
// 反方向移动范围
l -= x;
r -= x;
} else {
// 反方向移动士兵
// 正方向移动范围
l += x;
r += x;
}
while (!dq.empty() && dq.back() > r) {
dq.pop_back();
}
while (!dq.empty() && dq.front() < l) {
dq.pop_front();
}
} else {
cout << dq.size() << endl;
}
}
return 0;
}
队列安排
- 双链表实现
军训
- 巨模拟

浙公网安备 33010602011771号