跳到正文

个人记录

[CSP-J]2023年正睿-5 比赛总结

luoguzhanghao2025约 9 分钟
原文

这是 2026年10月24日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月24日
收录
2026年10月24日
更新
2026年11月16日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

[CSP-J]2023年正睿-5 比赛总结

比赛得分 270 pts / 400 pts,预计 260 pts

题目思路

T1

description

正常模拟即可,只不过在每次做乘法的时候用 __int128 或 unsigned long long,不然要溢出。

AC code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
long long n, p, sum = 1;
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("power.in", "r", stdin);
	freopen ("power.out", "w", stdout);
	cin >> n;
	p = n % 10;
	n /= 10;
	for (int i = 1; i <= p; i++) {
		__int128 a = sum * n;
		if (a >= 1e18) {
			cout << "inf";
			return 0;
		}
		sum = a;
	}
	cout << sum;
	return 0;
}

100pts\Huge{\color{#19be6b} 100pts}

T2

description

用数组存下每个数字出现的次数及每次出现所在的位置,最后调用即可

AC code
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, q;
struct node {
	int x;
	vector <int> pos;
} a[N];
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("query.in", "r", stdin);
	freopen ("query.out", "w", stdout);
	cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		int x; cin >> x;
		a[x].x++;
		a[x].pos.push_back(i);
	}
	while (q--) {
		int x, k; cin >> x >> k;
		if (x > 1000) {
			cout << "-1\n";
			continue;
		}
		if (a[x].x < k) {
			cout << "-1\n";
			continue;
		}
		cout << a[x].pos[k - 1] << '\n';
	}
	return 0;
}

100pts\Huge{\color{19be6b}{100pts}}

T3

description

用双指针,先把第二个指针持续向后移,如果每次移动后序列仍合法,则序列数加 j − i + 1j~-~i~+~1 ;如不合法,则将 ii 持续向后移,直到该序列合法

AC code
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, k, ans;
int a[N], cnt[N];
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("ambush.in", "r", stdin);
	freopen ("ambush.out", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int i = 1, j = 0;
    while(j < n){
        j++, cnt[a[j]]++;
        if (cnt[a[j]] < k) ans += j - i + 1;
        else {
            while (cnt[a[j]] == k) cnt[a[i]]--, i++;
            ans += j - i + 1;
        }
    }
    cout << ans << endl;
	return 0;
}
考场代码
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, cnt, maxn;
int a[N];
vector <int> t;
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("ambush.in", "r", stdin);
	freopen ("ambush.out", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		maxn = max (maxn, a[i]);
	}
	if (k == 1) {
		cout << 0;
		return 0;
	}
	t.resize (maxn + 1);
	int mx = 1;
	for (int i = 1, j = 1; i <= n; ) {
		t[a[j]]++;
		if (t[a[j]] > mx) mx = t[a[j]];
		if (mx >= k) {
			mx = 1;
			t.clear ();
			t.resize (maxn + 1);
			i++;
			j = i;
		} else {
			j++;
			cnt++;
		}
		if (j > n) {
			mx = 1;
			t.clear ();
			t.resize (maxn + 1);
			i++;
			j = i;
		}
	}
	cout << cnt;
	return 0;
}

70 pts\Huge{\color{2d8cf0}70~pts}

T4

description

DP

状态定义 dp[i][j] :以 ii 结尾的子串总和为 jj 的方案数

状态转移 :

  • 当把 ai,ja_{i,j} 变为负数时

    dpi,j=dpi−1,j−ai+dpi,j\Huge{dp_{i,j}=dp_{i-1,j-a_i+dp_{i,j}}}

  • 当把 ai,ja_{i,j} 变为正数时(即不动)

    dpi,j=dpi−1,j+ai+dpi,j\Huge{dp_{i,j}=dp_{i-1,j+a_i+dp_{i,j}}}

AC code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005, mod = 99824435, m = 1e4;
int n, ans;
int a[N], dp[N][m + 5];
signed main () {
	freopen ("zero.in", "r", stdin);
	freopen ("zero.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        dp[i][m / 2 - a[i]] = dp[i][m / 2 + a[i]] = 1;
        for (int j = 0; j <= m - a[i]; j++) 
            dp[i][j] = (dp[i][j] + dp[i - 1][j + a[i]]) % mod;
        for (int j = a[i]; j <= m; j++) 
            dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i]]) % mod;
        ans = (ans + dp[i][m / 2]) % mod;
    }
    cout << ans << endl;
	return 0; 
}

0pts\Huge{\color{ed3f14}0pts}

整体总结

本次总共用时1.5h(不计算监考员操作而导致的时间的浪费\color{white}不计算监考员操作而导致的时间的浪费),考的很(yì)好(tuó)。