跳到正文

个人记录

csp-j模拟赛正睿4 比赛总结

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

文章信息

发布
2026年10月17日
收录
2026年10月17日
更新
2026年11月10日
篇幅
约 12 分钟

版本

  1. v1当前

csp-j模拟赛正睿4 比赛总结

题号题目得分
T1额外的字符预计得分 100pts,实际得分 100pts
T2括号预计得分 60pts,实际得分 60pts
T3礼盒预计得分 100pts,实际得分 20pts
T4共享单车0pts

题目总结

题面

题目限制

时间限制:1000MS1000MS 内存限制:512MB512MB

题目描述

给定两个字符串SS和TT,其中TT是由SS插入一个字符得到的,请你输出插入的字符的位置,如果有多个位置请输出最后面的那个

输入格式

第一行输入一个字符串SS

第二行输入一个字符串TT

输出格式

输出一个数字表示答案

提示

数据范围

对于50%的数据,∣S∣,∣T∣≤5000|S|, |T| \leq 5000

对于100%的数据,∣S∣,∣T∣≤5×105|S|, |T| \leq 5 \times 10^5

从左到右依次遍历两个字符串,遇到第一个不一样的即为插入的位置。

ACcode
C++
#include <bits/stdc++.h>
using namespace std;
int main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("extra.in", "r", stdin);
	freopen ("extra.out", "w", stdout);
	string s, t;
	cin >> s >> t;
	s = s + ' ';
	int pos = 0;
	for (int i = 0; i < s.size (); i++) {
		if (s[i] != t[i]) {
			pos = i + 1;
			break;
		}
	}
	cout << pos;
	return 0;
} 

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

T2

题面

题目限制

时间限制:1000MS1000MS 内存限制:512MB512MB

题目描述

给定一个字符串,里面有多个括号,括号里面的内容会被翻转,注意括号可以包含,翻转顺序从内到外,比如:

(a(bc)d(ef))→(acbdfe)→efdbca(a(bc)d(ef)) \rightarrow (acbdfe) \rightarrow efdbca

请你输出这个字符串去掉括号的结果。

输入格式

第一行输入一个字符串SS

输出格式

输出一个字符串表示结果

提示

数据范围

对于30%的数据,∣S∣≤100|S| \leq 100

对于另外30%的数据,保证只出现一对括号

对于100%的数据,∣S∣≤5×105|S| \leq 5 \times 10^5

保证括号从里到外是可两两配对的

没写出来。

考场代码
C++
#include <bits/stdc++.h>
using namespace std;
string s, ans;
stack <int> kh;
inline void reverse (const int & beg, const int & end, string & s) {
	string t = s;
	int len = end - beg + 1;
	for (int i = 1; i <= len; i++)
		s[i + beg - 1] = t[end + 1 - i];
	return;
}
int main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("brackets.in", "r", stdin);
	freopen ("brackets.out", "w", stdout);
	char c = getchar ();
	while (c == ' ' || c == '\n') c = getchar ();
	while (c != ' ' && c != '\n') {
		s += c;
		c = getchar ();
	}
	for (int i = 0; i < s.size (); i++) {
		char c = s[i];
		if (c == '(') kh.push (i);
		else if (c == ')') {
			int beg = kh.top ();
			kh.pop ();
			reverse (beg, i, s);
			//reverse (beg, i);
		}
	}
	for (int i = 0; i < s.size (); i++)
		if (s[i] != '(' && s[i] != ')') putchar (s[i]);
	return 0;
}

一个 O(n2)O(n^2) 的做法,肯定会爆。

60pts\Huge \color{2d8cf0}60pts

T3

题面

题目限制

时间限制:1000MS1000MS 内存限制:512MB512MB

题目描述

有nn组礼盒,每组有一个红礼盒和一个蓝礼盒,里面分别有价值aia_i和bib_i的礼品。现在你想选xx个红礼盒和n−xn - x个蓝礼盒,每组里面只能恰好选一个。问你能获得的最大价值是多少

输入格式

第一行输入两个整数n,xn, x

接下来nn行,每行输入两个整数ai,bia_i, b_i

输出格式

输出一个数表示答案

提示

数据范围

对于30%的数据,n≤20n \leq 20

对于60%的数据,n≤2000n \leq 2000

对于100%的数据,n≤2×105,0≤x≤n,1≤ai,bi≤109n \leq 2 \times 10^5, 0 \leq x \leq n, 1 \leq a_i, b_i \leq 10^9

一个看起来简单实际上却不难的贪心(虽然我也不知道我怎么没想到)

开始先全部取蓝礼盒,算出它们的和,再用一个数组存下红礼盒价值减去蓝礼盒价值的差(即将蓝礼盒替换为红礼盒增加的价值),然后再将其降序排列(为了找到将蓝礼盒替换为红礼盒增加的价值最高的前x个),然后每次加上前x个

ACcode
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 5;
int n, k, ans;
int a[N];
inline bool cmp (const int & x, const int & y) { return x > y; }
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("gift.in", "r", stdin);
	freopen ("gift.out", "w", stdout);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
        int x, y; cin >> x >> y;
        a[i] = x - y; ans += y;
    }
    stable_sort (a + 1, a + n + 1, cmp);
    for (int i = 1; i <= k; i++) ans += a[i];
	cout << ans;
	return 0;
}
考场代码
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 5;
int n, x, ans, cnta, cntb;
bool vis[N];
struct node {
	int x, num, f;
	bool operator < (const node & p) const { return x > p.x; }
}a[N], b[N], c[N];
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("gift.in", "r", stdin);
	freopen ("gift.out", "w", stdout);
	cin >> n >> x;
	for (int i = 1; i <= n; i++) {
		cin >> c[2 * i - 1].x >> c[2 * i].x;
		c[2 * i - 1].num = c[2 * i].num = i;
		c[2 * i - 1].f = 0;
		c[2 * i].f = 1;
	}
	sort (c + 1, c + 2 * n + 1);
	for (int i = 1; ; i++) {
		if (cnta == x && cntb == n - x) break;
		if (cnta < x && c[i].f == 0 && !vis[c[i].num]) {
			cnta++;
			vis[c[i].num] = 1;
			ans += c[i].x;
		} else if (cntb < n - x && c[i].f == 1 && !vis[c[i].num]) {
			cntb++;
			vis[c[i].num] = 1;
			ans += c[i].x;
		}
	}
	cout << ans;
	return 0;
}

20pts\Huge\color{2d8cf0}20pts

T4

INFO

题目限制

时间限制:1000MS1000MS 内存限制:512MB512MB

题目描述

小明需要使用nn次共享单车,第ii次在第aia_i分钟。现在有mm种骑车卡,每种卡需要花费cic_i元,从购买开始后的kik_i分钟(买的时候对应的时间算第一分钟)内可以免费骑车。现在小明需要保证他在需要单车时身上至少有一种骑车卡(不关心剩余时间,可以在需要的时刻立即购买),问小明最少要花多少钱?

输入格式

第一行输入一个整数n,mn, m

第二行nn个整数a1,a2,⋯ ,ana_1, a_2, \cdots, a_n

接下来mm行,每行两个整数ci,kic_i, k_i

输出格式

输出一个数字表示答案

提示

数据范围

对于30%的数据,n,m≤100n, m \leq 100

对于60%的数据,n,m≤500n, m \leq 500

对于100%的数据,n≤105,m≤500,a1<a2<⋯<an,1≤ci,ki≤109n \leq 10^5, m\leq 500, a_1 < a_2 < \cdots < a_n, 1 \leq c_i, k_i \leq 10^9

二分+动态规划。

dp[i] 表示前 i 次骑车所用的最小费用。

对于第 i 次骑车的时候购买第 j 种骑车卡的情况,令 k 是这张骑车卡失效后第一次骑车的序号

状态转移:

dpk−1=min(dpk−1,dpi−1+cj)\Huge dp_{k−1}=min(dp_{k−1},dp_{i−1}+c_{j})

ACcode
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, M = 505;
int n, m;
int a[N], c[M], k[M], dp[N], len[N];
signed main () {
	ios::sync_with_stdio (NULL);
	cin.tie (nullptr); cout.tie (nullptr);
	freopen ("bike.in", "r", stdin);
	freopen ("bike.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) cin >> c[i] >> k[i];
   	for (int i = 1; i <= n; i++) {
        dp[i] = 1e18;
        for (int j = 1; j <= m; j++) {
            while (a[i] - a[len[j] + 1] + 1> k[j]) len[j]++;
            dp[i] = min (dp[i], dp[len[j]] + c[j]);
        }
    }
    cout << dp[n];
	return 0;
}

0pts\Huge \color{ed3f14}0pts

总体评价

本次基本都在做题并试图打部分分,实际上玩了1.5h的html\color{white}{实际上玩了1.5h的html},但分数与预估的差距太远,下次做贪心的题还是自己多出几个数据(大样例都过了但就是PAC)。

原文