【总结】CSP历年真题总结
TechnoxWin · · 个人记录
云剪切板
CSP-J 2021
T1
读题发现答案不超过
if (r / n > l / n) {
cout << n - 1 << endl;
}
由于
else {
cout << r % n << endl;
}
完整代码:
#include <bits/stdc++.h>
using namespace std;
int n, l, r;
int main () {
scanf ("%d%d%d", &n, &l, &r);
if (r / n > l / n) {
printf ("%d\n", n - 1);
} else if (r / n == l / n) {
printf ("%d\n", r % n);
}
return 0;
}
T2
看到这题肯定先想到暴力,每次询问都重新排序。(52分)
部分分代码:
#include <bits/stdc++.h>
using namespace std;
int n, q;
int a[10001];
int main()
{
scanf ("%d%d", &n, &q);
for (int i = 1; i <= n; ++ i) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= q; ++ i) {
int c;
scanf ("%d", &c);
if (c == 1) {
int x, v;
scanf ("%d%d", &x, &v);
a[x] = v;
} else if (c == 2) {
int x;
int b[10001];
for (int i = 1; i <= n; ++ i) {
b[i] = a[i];
}
scanf ("%d", &x);
for (int i = 1; i <= n; ++ i) {
for (int j = i; j >= 2; -- j) {
if (b[j] < b[j - 1]) {
if (j == x) {
x = j - 1;
} else if (j - 1 == x) {
x = j;
}
swap (b[j], b[j - 1]);
}
}
}
printf ("%d\n", x);
}
}
return 0;
}
一眼可知上面程序时间复杂度高得离谱,会
每当修改值时,只把当前修改的值交换到正确位置,复杂度就可以降低一维。注意这里n的范围很小,没必要用map,用了反而会map随机访问时间复杂度是
#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> a;
vector<int> b; //有序的数组
int a2b[10010]; // 通过a中元素索引,找到b中元素索引
int b2a[10010]; // 通过b中元素索引,找到a中元素索引
int main()
{
scanf ("%d%d", &n, &q);
a.resize(n + 1);
b.resize(n + 1);
for (int i = 1; i <= n; ++ i) {
scanf("%d", &a[i]);
b[i] = a[i];
a2b[i] = i;
b2a[i] = i;
}
for (int i = n; i >= 1; -- i) {
for (int j = 1; j < i; ++ j) {
if (b[j] > b[j + 1]) {
swap(b[j + 1], b[j]);
int aind1 = b2a[j];
int aind2 = b2a[j + 1];
a2b[aind1] = j + 1;
a2b[aind2] = j;
b2a[j + 1] = aind1;
b2a[j] = aind2;
}
}
}
while (q --) {
int c;
scanf ("%d", &c);
if (c == 1) {
int x, v;
scanf ("%d%d", &x, &v);
a[x] = v;
int y = a2b[x];
b[y] = v;
bool flag = true;
for (int i = y + 1; i <= n; ++ i) {
if (b[i - 1] > b[i] || (b[i - 1] == b[i] && b2a[i - 1] > b2a[i])) {
swap(b[i], b[i - 1]);
int aind1 = b2a[i - 1];
int aind2 = b2a[i];
a2b[aind1] = i;
a2b[aind2] = i - 1;
b2a[i] = aind1;
b2a[i - 1] = aind2;
flag = false;
} else {
break;
}
}
if (flag) {
for (int i = y - 1; i >= 1; -- i) {
if (b[i + 1] < b[i] || (b[i + 1] == b[i] && b2a[i + 1] < b2a[i])) {
swap(b[i], b[i + 1]);
int aind1 = b2a[i];
int aind2 = b2a[i + 1];
a2b[aind1] = i + 1;
a2b[aind2] = i;
b2a[i] = aind2;
b2a[i + 1] = aind1;
} else {
break;
}
}
}
} else if (c == 2) {
int x;
scanf ("%d", &x);
printf ("%d\n", a2b[x]);
}
}
return 0;
}
T3
大模拟即可(用map(映射)存储服务器会很方便,判断冲突时,只要调用std::map::count (auto)就可以了)。
注意:
- 形如
...:的是不合法地址 port是小于65536 不是65535 - 判断前导
0 时注意不要把单个0 判进去。
完整代码:#include <bits/stdc++.h> using namespace std;
int n; unordered_map<string, int> servers;
bool check (string s) { int ee[210]; bool flag[210]; int cnt = 1; int ind = -1; int port = 0;
memset (ee, 0, sizeof (ee));
memset (flag, false, sizeof (flag));
if (s[0] == '0' && s[1] != '.') {
return false;
}
for (int i = 0; i < s.size (); ++ i) {
if (s[i] == '.') {
++ cnt;
if (s[i + 1] == '0' && s[i + 2] != '.' && s[i + 2] != ':') {
return false;
}
} else if (s[i] == ':') {
ind = i + 1;
if (s[ind] == '0' && s.size () != ind + 1) {
return false;
}
break;
} else if (s[i] >= '0' && s[i] <= '9') {
ee[cnt] = ee[cnt] * 10 + s[i] - '0';
flag[cnt] = true;
if (ee[cnt] > 255) {
return false;
}
} else {
return false;
}
}
if (cnt != 4) {
return false;
}
for (int i = 1; i <= cnt; ++ i) {
if (!flag[i]) {
return false;
}
}
if (ind == -1) {
return false;
}
bool flag2 = false;
for (int i = ind; i < s.size(); ++ i) {
if (s[i] < '0' || s[i] > '9') {
return false;
}
port = port * 10 + s[i] - '0';
flag2 = true;
if (port > 65536) {
return false;
}
}
if (!flag2) {
return false;
}
return true;
}
int main () { // freopen ("network2.in", "r", stdin); // freopen ("network.out", "w", stdout); scanf ("%d", &n); for (int i = 1; i <= n; ++ i) { string op; cin >> op; if (op == "Server") { string address; cin >> address; if (check (address)) { if (!servers.count (address)) { servers[address] = i; cout << "OK"; } else { cout << "FAIL"; } } else { cout << "ERR"; } } else if (op == "Client") { string address; cin >> address; if (check (address)) { if (servers.count (address)) { cout << servers[address]; } else { cout << "FAIL"; } } else { cout << "ERR"; } } cout << endl; } return 0; }
### [T4](https://www.luogu.com.cn/problem/P7912)
由于是最后一题,考虑拿部分分。
发现每次拿第一个,那么考虑使用`std::queue`维护每个块,记录其中所有水果的索引(原位置),并使用一个`int`标记块的种类(是苹果还是橘子),也就是`std::pair<int, std::vector<int>>`。然后用`std::vector`保存所有块。为了节约时间复杂度,可以不真的执行合并操作,而是直接通过判断前后块的标记是否相等来判断是否需要`std::queue::pop ()`。最后删去为空的快即可。(80分)
注意:
1. 不能用`for`来查找空块,因为`vector::erase (iterator)`删除后会导致索引混乱。
2. `std::vector::erase (iterator)`的参数类型是`iterator`(迭代器),不能直接用`std::vector::erase (i)`,要用`std::vector::erase (vector::begin () + i)`,因为`std::vector::begin ()`的函数类型是`iterator`。
**部分分代码**:
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int apple = 1;
constexpr int orange = 0;
int n;
vector<pair<int, queue<int> > > v;
int main () {
scanf ("%d", &n);
v.resize (1);
v[0].first = -1;
int cnt = 0;
for (int i = 1; i <= n; ++ i) {
int x;
scanf ("%d", &x);
if (x == v[cnt].first) {
v[cnt].second.push (i);
} else {
queue<int> tq;
tq.push (i);
v.push_back (make_pair (x, tq));
++ cnt;
}
}
while (v.size () > 1) {
for (int i = 1 ;i < v.size (); ++ i) {
if (v[i].first != v[i - 1].first) {
printf ("%d ", v[i].second.front ());
v[i].second.pop ();
}
}
cout << endl;
int cnnt = 1;
while (cnnt < v.size ()) {
if (v[cnnt].second.empty ()) {
v.erase (v.begin () + cnnt);
} else {
++ cnnt;
}
}
}
return 0;
}
完整代码思路如下:
保存所有0和1的索引,每次取出时轮流找出比当前索引大的最小的0或1的索引,输出,并删除该索引。
这样,就可以避免“块”的拼接造成的时间复杂度过高,从而避免set,因为set的lower_bound方法完美实现了我们的思路,而且其erase方法和lower_bound的时间复杂度均为
注意:
- 一开始要塞一个值
INT_MAX给set,否则会内存溢出。 - 注意如果取
set里的值时若为INT_MAX就退出循环,因为已经到底了,然后下面一行一个数据输出另一个set里剩下的值就是答案了。
完整代码:#include <bits/stdc++.h> using namespace std;
int n; set<int> ss0; set<int> ss1;
int main() { scanf ("%d", &n); for (int i = 1; i <= n; ++ i) { int x; scanf ("%d", &x); if (x == 0) { ss0.insert(i); } else if (x == 1) { ss1.insert(i); } }
ss0.insert (INT_MAX);
ss1.insert (INT_MAX);
while (ss0.size () > 1 && ss1.size () > 1) {
bool tobr = false;
bool tmp = false;
set<int>::iterator ptr0 = ss0.begin();
set<int>::iterator ptr1 = ss1.begin();
int top0 = *ptr0;
int top1 = *ptr1;
if (top0 > top1) {
tmp = true;
} else {
tmp = false;
}
for (bool i = tmp; top0 <= n && top1 <= n; i = !i) {
if (i == 0) {
if (top0 == INT_MAX) {
tobr = true;
break;
}
ss0.erase (ptr0);
cout << top0 << " ";
ptr1 = ss1.lower_bound (top0);
top1 = *ptr1;
} else {
ss1.erase (ptr1);
if (top1 == INT_MAX) {
tobr = true;
break;
}
cout << top1 << " ";
ptr0 = ss0.lower_bound (top1);
top0 = *ptr0;
}
}
cout << endl;
if (tobr) {
break;
}
}
if (ss0.size () == 1) {
while (ss1.size () > 1) {
int top1 = *ss1.begin();
if (top1 == INT_MAX) {
return 0;
}
cout << top1 << "\n";
ss1.erase (ss1.begin());
}
} else if (ss1.size () == 1) {
while (ss0.size () > 1) {
int top0 = *ss0.begin();
if (top0 == INT_MAX) {
return 0;
}
cout << top0 << "\n";
ss0.erase (ss0.begin());
}
}
cout << endl;
return 0;
}
## CSP-J 2022
### [T1](https://www.luogu.com.cn/problem/P8813)
`for`循环,如果$>10^9$ **(注意不是$\ge$)** 或$<0$就`printf("%d", -1)`然后`return 0`了。考虑到$a^0=1$、$a^1=a$、$1^a=1$的特殊性,可以进行特判。(不特判会$\colorbox{red}{\color{white}\texttt{WA}}$)啊!!!
**完整代码**:
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
int res = 1;
bool flag = false;
bool flag2 = false;
int main () {
cin >> n >> m;
if (n == 1) {
cout << 1 << endl;
flag2 = true;
} else if (m == 0) {
cout << 1 << endl;
flag2 = true;
} else if (m == 1) {
cout << n << endl;
flag2 = true;
} else {
for (int i = 1; i <= m; ++ i) {
res *= n;
if (res > 1000000000 || res < 0) {
flag = true;
break;
}
}
}
if (flag) {
cout << -1 << endl;
} else if (!flag2) {
cout << res << endl;
}
return 0;
}
T2
暴力扫描n的每一个因数,判断是否合题意即可(60分)
部分分代码:
#include <bits/stdc++.h>
#define llint long long int
using namespace std;
int k;
llint n, d, e;
int main () {
scanf ("%d", &k);
while (k --) {
scanf ("%lld%lld%lld", &n, &d, &e);
bool flag = false;
for (llint i = 1; i * i <= n; ++ i) {
if (n % i != 0) {
continue;
}
llint j = n / i;
if (i + j == n - (d * e) + 2) {
flag = true;
printf ("%lld %lld\n", i, j);
break;
}
}
if (!flag) {
printf("NO\n");
}
}
return 0;
}
满分代码需要亿点点数学推导:
所以算出
完整代码:
#include <bits/stdc++.h>
#define llint long long int
using namespace std;
int main () {
llint k;
scanf ("%lld", &k);
while (k --) {
llint n, e, d;
scanf ("%lld%lld%lld",&n,&e,&d);
llint u = sqrt ((n - e * d + 2) * (n - e * d + 2) - (n * 4));
llint v = n - e * d + 2;
llint p = (u + v) / 2;
llint q = v - p;
if (p * q == n && e * d == (p - 1) * (q - 1) + 1 && p && q) {
printf ("%lld %lld\n", min (p, q), max (p, q));
}else {
printf ("NO\n");
}
}
return 0;
}
T3
模拟即可(75分代码,最后几个点会
stack取top ()前一定要先判非空!!!(\colorbox{purple}{\color{white}\texttt{RE}} 的罪魁祸首)- 因为整个程序逻辑是递归,所以最后处理的应该在最外层,应该要倒着遍历字符串!!!(
\colorbox{red}{\color{white}\texttt{WA}} 的罪魁祸首)
部分分代码:#include <bits/stdc++.h> using namespace std;
string s; int cnt1 = 0, cnt2 = 0; int rt[1000010];
void getRight () { stack<int> st; int cnt = 0; for (int i = 0; i < s.size (); ++ i) { if (s[i] == '(') { st.push (i); ++ cnt; } else if (s[i] == ')') { if (!st.empty ()) { int u = st.top (); st.pop (); -- cnt; rt[u] = i; } } } }
bool solve (int l, int r) { if (rt[l] == r) { ++ l; -- r; } if (l == r) { if (s[l] == '1') { return true; } return false; } int depth = 0; for (int i = r; i >= l; -- i) { if (s[i] == ')') { ++ depth; } else if (s[i] == '(') { -- depth; } else if (s[i] == '|') { if (depth == 0) { bool resl = solve (l, i - 1); if (resl == 1) { ++ cnt2; return true; } bool resr = solve (i + 1, r); return resl || resr; } } } depth = 0; for (int i = r; i >= l; -- i) { if (s[i] == ')') { ++ depth; } else if (s[i] == '(') { -- depth; } else if (s[i] == '&') { if (depth == 0) { bool resl = solve (l, i - 1); if (resl == 0) { ++ cnt1; return false; } bool resr = solve (i + 1, r); return resl && resr; } } } return false; }
int main () { memset (rt, 0x3f, sizeof (rt)); getline (cin, s); getRight (); cout << solve (0, s.size () - 1) << endl << cnt1 << ' ' << cnt2 << endl; return 0; }
以上代码中我们每次计算都要遍历整个字符串,时间复杂度为$\Theta(n^2)$,会造成$\colorbox{black}{\color{white}{\texttt{TLE}}}$,所以,我们考虑预处理括号的同时记录下当前括号内最后一个运算符,这样我们就可以快速调到运算符,略过运算符两边的表达式,从而减少时间复杂度。
**完整代码**
```cpp
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt1 = 0, cnt2 = 0;
int rt[1000010];
int lAndn[1000010];
int lOrn[1000010];
int lAnd[1000010];
int lOr[1000010];
void init () {
stack<int> st;
int depth = 0;
for (int i = 0; i < s.size (); ++i) {
if (s[i] == '(') {
st.push (i);
++depth;
} else if (s[i] == ')') {
if (!st.empty ()) {
int u = st.top ();
st.pop ();
--depth;
rt[u] = i;
}
} else if (s[i] == '&') {
lAndn[depth] = i;
} else if (s[i] == '|') {
lOrn[depth] = i;
}
lAnd[i] = lAndn[depth];
lOr[i] = lOrn[depth];
}
}
bool solve (int l, int r) {
if (rt[l] == r) {
++l;
--r;
}
if (l == r) {
if (s[l] == '1') {
return true;
}
return false;
}
if (lOr[r] > l) {
int i = lOr[r];
bool resl = solve (l, i - 1);
if (resl == 1) {
++cnt2;
return true;
}
bool resr = solve (i + 1, r);
return resl || resr;
}
if (lAnd[r] > l) {
int i = lAnd[r];
bool resl = solve (l, i - 1);
if (resl == 0) {
++cnt1;
return false;
}
bool resr = solve (i + 1, r);
return resl && resr;
}
return false;
}
int main () {
memset (rt, 0x3f, sizeof (rt));
getline (cin, s);
init ();
cout << solve (0, s.size () - 1) << endl;
cout << cnt1 << ' ' << cnt2 << endl;
return 0;
}
T4
动态规划。使用
这时,我们在Dij算法的基础上,判断当前时间是否大于开放时间,若是,则直接转移:(从
若否,则坐晚几班的车,使得能够通过这条道路:(从
完整代码:
#include <bits/stdc++.h>
using namespace std;
int n, m, k, tot = 0;
int head[10010];
int dis[10010][110];
class Edge {
public:
int to, next;
int minTime;
};
Edge e[200010];
class Node {
public:
int nodeID;
int nowTime;
Node (int newID, int newTime) {
nodeID = newID;
nowTime = newTime;
}
bool operator< (const Node &other) const { return nowTime < other.nowTime; }
bool operator> (const Node &other) const { return nowTime > other.nowTime; }
};
void addEdge (int st, int ed, int newTime) {
++tot;
e[tot].next = head[st];
e[tot].to = ed;
e[tot].minTime = newTime;
head[st] = tot;
}
void dij () {
memset (dis, 0x3f, sizeof (dis));
priority_queue<Node, vector<Node>, greater<Node>> q;
dis[1][0] = 0;
q.push (Node (1, 0));
while (!q.empty ()) {
Node nStatus = q.top ();
int st = nStatus.nodeID;
int nowTime = nStatus.nowTime;
q.pop ();
for (int nEdge = head[st]; nEdge; nEdge = e[nEdge].next) {
int ed = e[nEdge].to;
int minTime = e[nEdge].minTime;
if (nowTime >= minTime) {
int newTime = nowTime + 1;
int modTime = newTime % k;
if (dis[ed][modTime] > newTime) {
dis[ed][modTime] = newTime;
q.push (Node (ed, newTime));
}
} else {
int newTime =
ceil ((minTime - nowTime) * 1.0 / k) * k + nowTime + 1;
int modTime = newTime % k;
if (dis[ed][modTime] > newTime) {
dis[ed][modTime] = newTime;
q.push (Node (ed, newTime));
}
}
}
}
}
int main () {
cin >> n >> m >> k;
for (int i = 1; i <= m; ++i) {
int st, ed, minTime;
cin >> st >> ed >> minTime;
addEdge (st, ed, minTime);
}
dij ();
if (dis[n][0] == 0x3f3f3f3f) {
cout << -1 << endl;
} else {
cout << dis[n][0] << endl;
}
return 0;
}
CSP-J 2024
T1
真的入门,一点思考量都没有,照着题意写就行了。
#include <bits/stdc++.h>
using namespace std;
int n, cnt = 0;
bool p[4][13];
int main () {
memset (p, 0, sizeof (p));
scanf ("%d", &n);
for (int i = 1;i <= n;++ i) {
char a, b;
int c, d;
cin >> a >> b;
if (a == 'D') {
c = 0;
} else if (a == 'C') {
c = 1;
} else if (a == 'H') {
c = 2;
} else if (a == 'S') {
c = 3;
}
if (b == 'A') {
d = 0;
} else if (b == 'T') {
d = 9;
} else if (b == 'J') {
d = 10;
} else if (b == 'Q') {
d = 11;
} else if (b == 'K') {
d = 12;
} else {
d = b - '0' - 1;
}
if (!p[c][d]) {
p[c][d] = true;
++ cnt;
}
}
cout << 52 - cnt << endl;
return 0;
} // DCHS
T2
也是照着题意模拟就行。
#include <bits/stdc++.h>
using namespace std;
int t;
int n, m, k;
int x, y, d; //不要y0!!!啊!!!
bool mp[1010][1010];
bool vis[1010][1010];
constexpr int dx[4] = {0, 1, 0, -1};
constexpr int dy[4] = {1, 0, -1, 0};
int main() {
scanf ("%d", &t);
while (t --) {
int cnt = 1;
memset (mp, 0, sizeof (mp));
memset (vis, 0, sizeof (vis));
scanf ("%d%d%d%d%d%d", &n, &m, &k, &x, &y, &d);
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
char c;
scanf (" %c", &c);
if (c == 'x') {
mp[i][j] = true;
} else {
mp[i][j] = false;
}
}
}
vis[x][y] = true;
for (int i = 1; i <= k; ++ i) {
int xx = x + dx[d];
int yy = y + dy[d];
if (0 < xx && xx <= n && 0 < yy && yy <= m && !mp[xx][yy]) {
if (!vis[xx][yy]) {
vis[xx][yy] = true;
++cnt;
}
x = xx;
y = yy;
} else {
d = (d + 1) % 4;
}
}
printf ("%d\n", cnt);
}
return 0;
}
T3
这道题重在找规律。题目要求“尽可能小”,可以发现,单个数位使用的小木棍越多,在木棍数量一定的情况下,拼出来的数字位数也就越少,整个数就会越小。而小木棍消耗最多的数就是数字8(需要7根小木棍),所以考虑贪心,让靠后的数位都成为8。
观察特殊性质A、B,分别为
- 余数为
0 时全拼8 最小 - 余数为
1 时需要增加7 ,此时拼出10 最小 - 余数为
2 时拼出1 最小。 - 余数为
3 时拼出200 (比同位数788 小)最小。 - 余数为
4 时拼出20 (比同位数48 小)最小。 - 余数为
5 时拼出2 最小。 - 余数为
6 时拼出6 最小。 完整代码:#include <bits/stdc++.h> using namespace std;
int t;
int main() { cin >> t; while (t --) { int n; cin >> n; if (n % 7 == 0) { for (int i = 1; i <= n / 7; ++ i) { cout << 8; } } else if (n % 7 == 1) { if (n == 1) { cout << -1; } else { cout << 10; for (int i = 1; i <= (n - 8) / 7; ++ i) { cout << 8; } } } else if (n % 7 == 2) { cout << 1; for (int i = 1; i <= (n - 2) / 7; ++ i) { cout << 8; } } else if (n % 7 == 3) { if (n == 3) { cout << 7; } else if (n == 10) { cout << 22; } else { cout << 200; for (int i = 1; i <= (n - 17) / 7; i++) { cout << 8; } } } else if (n % 7 == 4) { if (n == 4) { cout << 4; } else { cout << 20; for (int i = 1; i <= (n - 11) / 7; i++) { cout << 8; } } } else if (n % 7 == 5) { cout << 2; for (int i = 1; i <= (n - 5) / 7; i++) { cout << 8; } } else if (n % 7 == 6) { cout << 6; for (int i = 1; i <= (n - 6) / 7; i++) { cout << 8; } } cout << endl; } return 0; }
### [T4](https://www.luogu.com.cn/problem/P11230)
## CSP-S 2024
### [T1](https://luogu.com.cn/problem/P11231)
~~这辈子里S组能看到的普及减难度的题目不多了~~
很简单,让怪兽尽量以小欺大,可以证明,这样最终剩下的怪兽数量就是攻击力相等的怪兽的数量的最大值。
如下面:
```plaintext
10
136 136 136 2417 136 136 2417 136 136 136
排序得:
10
136 136 136 136 136 136 136 136 2417 2417
其中
STEP 1. 所有的
STEP 2. 两只
#include<bits/stdc++.h>
using namespace std;
int n;
int vis[100010];
int maxn = 0;
int main () {
memset (vis, 0, sizeof (vis));
scanf ("%d", &n);
for (int i = 1; i <= n; ++ i) {
int a;
scanf ("%d", &a);
++ vis[a];
maxn = max (vis[a], maxn);
}
cout << maxn << endl;
return 0;
}
T2
问题一:有多少车会被判定为超速。
显然先想到暴力,也就是对于每辆车都遍历所有测速仪,判断有没有在他的超速区间内。时间复杂度
然后发现,可以以超速区间的左端点为界,二分查找第一个测速仪,然后判断它是否在右端点左边。时间复杂度
问题二:最多拆掉几个检测器。
很明显的贪心(经典,然而我就是没做过)
【此部分看了题解。】