跳到正文

个人记录

[CSP-J]2022年牛客-1 比赛总结

luoguzhanghao2025约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年8月26日
收录
2026年8月26日
更新
2026年11月16日
篇幅
约 13 分钟

版本

  1. v1当前

[CSP-J]2022年牛客-1 比赛总结

比赛总分 400 pts,比赛时间 3.5h,得分 180 pts, 预估 210 pts

分数分布

题号题目得分测试点得分
T1学习除法 (divide)预估100pts,实际100ptsAC 100pts\color{green}{AC~100pts}
T2拆分 (split)预估100pts,实际70ptsAC  70pts\color{green}{AC~~70pts} , TLE  30pts\color{red}{TLE ~~30pts}
T3部落 (town)预估0pts,实际0ptsWA  100pts\color{red}{WA~~100pts}
T4传递 (spread)传递 (spread)AC  10pts\color{green}{AC~~10pts} ,WA  90pts\color{red}{WA~~90pts}

题目思路

T1 :学习除法 (divide)

题目大意

给定一个大于一的自然数,每次操作可以给这个数除以一个非零自然数,问最少经过多少次操作可以将这个数变为质数

题目思路

这道题比较简单,但得先明白一点,任何大于一的自然数必定可以分为一个数与一个质数相乘的形式

因为一个大于一的数至少会有两个因数,而那些因数又可以分为一些数的乘积,分到最后不能在分了,那个数就是质数了,也就是说,任意一个大于1的自然数必定会有一个质因子,所以本题答案要么是0(那个数是质数)要么是1(那个数是合数)

参考代码

**注意:**根据本题的数据,可知本题需要开long long但如果只对n 和 函数参数x开会发现会出现 WA 或是 TLE 的情况,所以要把所有变量(包括循环变量 i)全部开为long long

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n; 
bool p(int x) {
	if (x <= 1) return false;
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0) return false;
	} 
	return true;
}
signed main() {
	freopen("divide.in", "r", stdin);
	freopen("divide.out", "w", stdout);
	cin >> n;
	if (p(n)) cout << 0;
	else cout << 1;
	return 0;
}

T2 :拆分 (split)

题目大意

新定义概念 mex,指求集合中最小没有出现的自然数

例如 :

mex(0,1,2,3)=4mex ({0, 1, 2, 3}) = 4

mex(1,2,3,4,5,6)=0mex({1, 2, 3, 4, 5, 6}) = 0

mex(0,1,2,5)=3mex({0, 1, 2, 5}) = 3

现在给出一个集合,你需要把它分成一些集合,使得每个集合的mex的和最大,求这个和是多少

题目思路

这道题的思路非常容易就想出来,就是从0开始尽可能多的将连续自然数放在一个集合,每个集合尽量都要有0(否则这个集合的mex就是0,对和没有任何贡献),且两个0不同是出现在一个集合里

参考代码

这道题的实现方法五花八门,但基本策略差不多都是这样,怎么实现看个人,这里只展示一种方式,就是模拟放集合里的过程

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, tot;
int a[N], ed[N];
bool vis[N];
int main() {
	freopen("split.in", "r", stdin);
	freopen("split.out", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        bool flag = true;
        for (int j = 1; j <= tot; j++) {
            if (a[i] == 0) break;
            if (ed[j] + 1 == a[i]) {
                ed[j] = a[i];
                flag = false;
                break;
            }
        }
        if (flag) {
            ed[++tot] = a[i];
            if (a[i] != 0) vis[tot] = true;
        }
    }
    long long ans = 0;
    for (int i = 1; i <= tot; i++) {
        if (vis[i]) continue;
        ans += ed[i] + 1;
    }
	cout << ans;
	return 0;
}
比赛时的代码

70pts\huge{70pts}

我这个是将每个数该不该放枚举一遍,如果该放就加入,不该放留到下一回处理,时间复杂度O(n2)O(n^2)

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N];
int main() {
	freopen("split.in", "r", stdin);
	freopen("split.out", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + n + 1);
	int sum = 0, ans = 0;
	while (sum < n) {
		int k = 1, last = a[1];
		while (a[k] == -1) {
			k++;
			last = a[k];
		}
		if (last != 0) break;
		sum++;
		a[k] = -1;
		for (int i = k + 1; i <= n; i++) {
			if (a[i] == -1) continue;
			if (a[i] - last > 1) break;
			else if (a[i] == last) continue;
			else if (a[i] - last == 1) {
				last = a[i];
				sum++;
				a[i] = -1;
			}
		}
		ans += last + 1;
	}
	cout << ans;
	return 0;
}

T3 :部落 (town)

题目大意

给定n和pos以及n个数,要使1 -> pos之间的数从左到右不能出现减少的情况(可以相等),pos -> n之间的数从左到右不可以出现增加的情况(同样也可以相等),问至少修改几个数才可以达到题目要求

基本思路

这道题很明显是一道最长上升子序列 - LIS,只需算出1 -> pos的最长上升子序列长度以及n -> pos(注意,是反着的)最长上升子序列长度再拿n减去这个数就是要修改的次数,因为1 -> pos之间的数从左到右不能出现减少的情况,pos -> n之间的数从左到右不可以出现增加的情况,而将满足条件的最长上升子序列去除,剩下的数就是不满足的数,即需要修改的数。

但这道题数据范围(0≤n≤1050 \le n \le 10^5),O(n2)O(n^2)的时间复杂度要 TLE,只需再使用二分的lower_bound优化就可以通过了

参考代码
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, pos;
int a[N], dp[N];
int main() {
	freopen ("town.in", "r", stdin);
	freopen ("town.out", "w", stdout);
	cin >> n >> pos;
    for (int i = 1; i <= n; i++) cin >> a[i];
    dp[1] = a[1];
    int tot1 = 1;
    for (int i = 2; i <= pos; i++) {
        if (a[i] > dp[tot1]) dp[++tot1] = a[i];
        else {
            int j = lower_bound (dp + 1, dp + tot1 + 1, a[i]) - dp;
        	dp[j] = a[i];
        }
    }
    memset (dp, 0, sizeof dp);
    int tot2 = 1;
    for (int i = n; i > pos; i--) {
        if (a[i] >= dp[tot2]) dp[++tot2] = a[i];
        else {
            int j = lower_bound (dp + 1, dp + tot2 + 1, a[i]) - dp;
            dp[j] = a[i];
        }
    }
    cout << n - tot1 - tot2 + 1;
	return 0;
}
比赛时的代码

因为本题原本的暴力LIS代码没有保存下来,所以提交的是我的骗分代码,所以这里就不展示了

T4 :传递 (spread)

题目大意

两只青蛙 AA、BB想过一条河,每个蛙选择了一条过河的路,路是由一块一块石头打出来的搭石路,第ii块石头与i+1i + 1石头的距离为aia_i,两只青蛙都只能跳到m的距离,现在青蛙AA手上有一个助跳器,如果哪只青蛙拿着它就可以跳到k的距离,AA BB可以将助跳器互相传递,但得保证两蛙间的距离不大于q才可以传递,现在求最少需要传递多少次助跳器 AA BB才可以到达河对岸

题目思路

这道题可以用 DP 来做,但我只讲用01BFS(但跟DP差不多,还有个原因,因为我用BFS写的)

我们可以用一个三位数组dp表示当前代价(就是传助跳器那个),dp[i][j][op]代表AA青蛙在第i快石头上,BB青蛙在第j块石头上,助跳器在op状态下(op = 0代表助跳器在 AA手上,op = 1则代表助跳器在BB手上)传助跳器的次数,看到op只有0和1,我们不难联想到01BFS,如果当前状态可以不花代价(即不传助跳器)就优先出队

参考代码
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, m, k, q;
int dp[N][N][2], a[N];
int b[N];
struct node {
    int i, j, pos;
};
void bfs_01 () {
    deque<node> que;
    memset(dp, 0x3f, sizeof dp);
    dp[0][0][0] = 0;
    que.push_front({0, 0, 0});
    while (!que.empty()) {
        node p = que.front ();
        que.pop_front ();
        int i = p.i, j = p.j, pos = p.pos;
        int cost = dp[i][j][pos];
        if (i == n && j == n) {
        	cout << cost;
            return;
        }
        if (abs(a[i] - b[j]) <= q) {
            int posit = 1 - pos;
            if (cost + 1 < dp[i][j][posit]) {
                dp[i][j][posit] = cost + 1;
                que.push_back({i, j, posit});
            }
        }
        if (i < n) {
            if (pos == 0 || a[i + 1] - a[i] <= m) {
                if (cost < dp[i + 1][j][pos]) {
                    dp[i + 1][j][pos] = cost;
                    que.push_front({i + 1, j, pos});
                }
            }
        }
        if (j < n) {
            if (pos == 1 || b[j + 1] - b[j] <= m) {
                if (cost < dp[i][j + 1][pos]) {
                    dp[i][j + 1][pos] = cost;
                    que.push_front ({i, j + 1, pos});
                }
            }
        }
    }
    return;
}
int main() {
	freopen ("spread.in", "r", stdin);
	freopen ("spread.out", "w", stdout);
    cin >> n >> m >> k >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    bfs_01();
	return 0;
}
比赛时的代码

仅为骗分代码,这里就不展示了

考试整体总结

这次考试在时间上分配不太合理,在T1上浪费了一些时间,T3打了半天却没保存,应该把空闲时间留给 T4,最起码打个暴力也可以拿20pts

原文