跳到正文

题解

Test41普转提考试总结

Claire0630约 28 分钟
原文
文章信息与版本

文章信息

发布
2025年10月16日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 28 分钟
获赞
1

版本

  1. v2当前
  2. v1

Test41普转提考试总结

Data

Score:40 = 10 + 10 + 20 + 0

Rank: B组P14,全组P30(diandi)

Preface

坐牢了孩子们,成功再次坠机>___< /(ㄒoㄒ)/~~

浙江的题还是太权威太有水平了:(

确实是想不到,跟状态可能有点关系,但大体还是思路问题,根本就没在正解的路上

T1想到二分就起飞了

T2啥都没想也起飞了

T3没有用数字优化还起飞了

T4瞎JB乱写本来就起飞了

啊啊啊o((>ω< ))o要死了

qc真的要死了

救救QC吧

Statements

A. 玩具(toy)

题面
有 tt 波小朋友,nn 个篮子,每个篮子有 aia_i 个玩具熊。每波小朋友有 kik_i 人,选择区间 [li,ri][l_i, r_i] 内连续的一段篮子,要求每个小朋友分到的玩具熊数量相同。若满足要求则加 wiw_i 分,否则扣 wiw_i 分。初始分为 ss,求最大得分。

输入格式
第一行:t,nt, n
第二行:nn 个整数 aia_i
接下来 tt 行:每行 li,ri,ki,wil_i, r_i, k_i, w_i
最后一行:初始分 ss

输出格式
最大得分 ww

数据范围

  • 对于 20% 的数据:t≤10,n≤10,ki≤10t \leq 10, n \leq 10, k_i \leq 10

  • 对于另外 10% 的数据:数组 aa 中的数全部相同

  • 对于另外 10% 的数据:数组 kk 中的数全部相同

  • 对于 60% 的数据:t≤10000,n≤10000t \leq 10000, n \leq 10000

  • 对于 100% 的数据:t≤100000,n≤100000,ki≤100,wi≤100,s≤100,1≤li≤ri≤n,1≤ai≤10000t \leq 100000, n \leq 100000, k_i \leq 100, w_i \leq 100, s \leq 100, 1 \leq l_i \leq r_i \leq n, 1 \leq a_i \leq 10000

样例输入

Input

reStructuredtext
2 5
2 1 5 2 4 3
2 5 7 4
1 3 9 10
100

Output

reStructuredText
94

Solution

此题因为要满足连续区间是kk的倍数,所以我们想到使用前缀和%k\%k的方式,而非二分.二分只能求到一部分答案,对于特殊的样例构造,二分无法实现

我们使用前缀和%k\%k的方式,又观察到ki≤100k_i \leq 100,我们直接暴力枚举所有的kk,去查找在区间[li,ri][l_i,r_i]范围内是否有两个不同的si%ks_i \% k是相等的,如果有,就直接加,否则就减

这里给一组小数据(甚至逆天到考试的二分代码都能通过???) Input:

reStructuredtext
7 10
1 3 4 2 5 8 6 7 9 11
3 5 1 10
4 9 7 10
1 1 100 10
1 10 11 10
5 6 13 10
6 10 17 10
6 10 18 10
100

Code:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
const int N = 1e5 + 11,M = 1e2 + 11;
LL t,n,org;
LL a[N],s[N],ys[N];
vector<int> v[M];
struct node{
	int l,r,k,w;
}g[N];
int main(){
//    freopen("ex_0.in","r",stdin);
//    freopen("toy.out","w",stdout);
	read(t);read(n);
	for(LL i=1;i<=n;i++) read(a[i]);
	for(LL i=1;i<=n;i++) s[i] = s[i - 1] + a[i];
	for(LL i=1;i<=t;i++){
		read(g[i].l);read(g[i].r);read(g[i].k);read(g[i].w);
		v[g[i].k].emplace_back(i);
	}
	read(org);
	for(int i=0;i<=100;i++){
		if(v[i].size() == 0) continue;
		for(int j=1;j<=n;j++) ys[j] = s[j] % i;
		for(auto x : v[i]){
			int l = g[x].l,r = g[x].r;
			map<int,int> mp;
			mp.clear();
			bool fl = 0;
			for(int k=l-1;k<=r;k++){
				if(mp.find(ys[k]) != mp.end()){
					fl = 1;
					break;
				}
				mp[ys[k]] = k;
			}
			if(fl == 1) org += g[x].w;
			else org -= g[x].w;
		}
	}	
	print(org);
	return 0;
}

B. 数列(seq)

题面
给定数列 A={ai}A = \{a_i\},构造数列 B={bi}B = \{b_i\} 满足:

  • bi>0b_i > 0
  • ai−k≤bi≤aia_i - k \leq b_i \leq a_i
  • 去掉 ff 个元素后,剩余 bib_i 的最大公约数为 gg

输出所有可能的 gg。

输入格式
第一行:TT(数据组数)
每组数据:

  • 第一行:n,k,fn, k, f
  • 第二行:nn 个整数 aia_i

输出格式
每组数据一行,升序输出所有满足条件的 gg

样例输入

reStructuredText
1
6 3 1
78 69 55 102 233 666

样例输出

reStructuredText
1 2 3 4 5 6 11

数据范围 设 aia_i 的最大值为 AA

测试点编号n,k,f,An, k, f, ATT
1-6≤10\leq 10-
7-10≤3×103,f=0\leq 3 \times 10^3, f = 0-
11-12≤5×103\leq 5 \times 10^3≤3\leq 3
13-14≤3×104\leq 3 \times 10^4-
15-16≤5×104\leq 5 \times 10^4-
17-18≤5×105\leq 5 \times 10^5-
19-20≤2×106\leq 2 \times 10^6≤2\leq 2

Solution

我们观察到要满足有至少n−fn - f个元素是gg的倍数,我们思考如何快速实现这一点

我们先使用前缀和将所有的元素个数累加起来

我们可以将所有可能的gg放在数轴上,对于g≤kg \leq k这种情况,因为这时候ai mod g=aia_i \bmod g = a_i,故我们直接前缀和暴力枚举元素个数是否大于等于n−fn - f即可

对于g>kg > k的情况,我们这么进行考虑

我们将区间[ai−k,ai][a_i-k,a_i]放到数轴上,注意到此时gg的间隔已经大于所有区间的长度(其实所有区间等长,都是kk),我们就可以用查分数组标记所有的区间,然后再在枚举gg,此时的gg就可以理解为gg间的间隔,然后累加每个间隔的值,最后判断是否大于等于n−fn - f

Code:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
const int N = 2e6 + 11,M = 25;
int t,n,k,f;
int a[N],mp[N],s[N],c[N];
//unordered_map<int,int> mp;
void init(){
	for(int i=1;i<=N-11;i++) mp[i] = s[i] = c[i] = 0;
	return;
}
int main(){
//    freopen("seq.in","r",stdin);
//    freopen("seq.out","w",stdout);
	read(t);
	while(t --){
		read(n);read(k);read(f);
		init();
		for(int i=1;i<=n;i++){
			read(a[i]);
			mp[a[i]] ++;
		}
		for(int i=1;i<=N-11;i++) s[i] = s[i - 1] + mp[i];
		for(int i=1;i<=k+1;i++){
			if(s[N - 11] - s[i - 1] >= n - f){
				print(i);
				putchar(' ');
			}
		}
		for(int i=1;i<=n;i++){
			int l = max(1,a[i] - k),r = a[i];
			c[l] ++,c[r + 1] --;
		}
		for(int i=1;i<=N-11;i++) c[i] += c[i - 1];
		for(int i=k+2;i<=N-11;i++){
			int cnt = 0;
			for(int j=i;j<=N-11;j+=i) cnt += c[j];
			if(cnt >= n - f){
				print(i);
				putchar(' ');
			}
		}
		putchar('\n');
	}
	return 0;
}

C. 文件(file)

题面
有一个 n×mn \times m 的矩阵,每个元素是一个字符串。经过 qq 次加密,每次加密是交换两个大小相同的子矩形(左上角分别为 (x1,y1)(x_1,y_1) 和 (x2,y2)(x_2,y_2),大小为 l×cl \times c)。给出加密操作(倒序),要求按顺序执行这些操作(即解密),输出最终矩阵。

输入格式
第一行:n,m,qn, m, q
接下来 nn 行:每行 mm 个字符串
接下来 qq 行:每行 x1,y1,x2,y2,l,cx_1, y_1, x_2, y_2, l, c

输出格式
解密后的 n×mn \times m 矩阵

样例输入

reStructuredText
4 4 2
a a b b
a a b b
c c d d
c c d d
1 1 3 3 2 2
3 1 1 3 2 2

样例输出

reStructuredTextre
d d c c
d d c c
b b a a
b b a a

数据范围

  • 对于前 30% 的数据:∣S∣≤n×m|S| \leq n \times m
  • 对于前 60% 的数据:n,m≤100n, m \leq 100
  • 对于 100% 的数据:n,m≤1000,q≤5000,∣S∣≤107n, m \leq 1000, q \leq 5000, |S| \leq 10^7

Solutions

S1.半暴力大法

因为此题时限3s3s,我们直接考虑暴力枚举所有要交换的矩形的每一个点,但注意string的swap操作的时间复杂度并非O(1)O(1),所以对string数组进行编号,使用编号的swap,最大时间复杂度为O(q×n2)O(q\times n^2),结果在赌狗WLS的豪赌下,竟然AC了??!!!,其实我TM随便加强点数据(n,qn,q数据拉满,l,cl,c每次都顶死nn),这个2B就死透了,这个神仙方法也落幕了

王屎山代码:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
inline void read(string &s){
    s = "";
    char c = getchar();
    while(c == ' ' || c == '\n' || c == '\r') c = getchar(); 
    while(c != ' ' && c != '\n' && c != '\r' && c != EOF){
        s += c;
        c = getchar();
    }
}
inline void print(const string &s){
    for(char c : s) putchar(c);
}
const int N = 1e3 + 11,M = 25;
int n,m,q,cnt;
string g[N * N];
int a[N][N];
map<PII,PII> pos;
int main(){
//    freopen("ex_0.in","r",stdin);
//    freopen("file.out","w",stdout);
	read(n);read(m);read(q);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			read(g[++ cnt]);
			a[i][j] = cnt;
		}
	}
	while(q --){
		int x1,y1,x2,y2,l,c;
		read(x1);read(y1);read(x2);read(y2);read(l);read(c);
		for(int i=x1,i2=x2;i<x1+l;i++,i2++){
			for(int j=y1,j2=y2;j<y1+c;j++,j2++){
				swap(a[i][j],a[i2][j2]);
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			print(g[a[i][j]]);
			putchar(' ');
		}
		putchar('\n');
	}
	return 0;
}

S2.正解

还是Cdo大佬权威,这个做法还是太NB太快了

我们分析矩形交换的实际含义

我们发现两个矩形的交换就是断开与原来上下左右四个方向的边框并重新连上新位置的四个边框,因为我们并不能实际的交换位置,所以只能寻找和四周的关系,于是我们就可以使用链表(最好是手写的)来实现这个题目,对于每个点,我们并不需要上下左右四个方向的连接,只需要与右方和下方衔接即可

我们枚举矩形的四个边,将边上的点交换所指向的位置即可,注意边界的处理!

实现:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
inline void read(string &s){
    s = "";
    char c = getchar();
    while(c == ' ' || c == '\n' || c == '\r') c = getchar(); 
    while(c != ' ' && c != '\n' && c != '\r' && c != EOF){
        s += c;
        c = getchar();
    }
}
inline void print(const string &s){
    for(char c : s) putchar(c);
}
const int N = 1e3 + 11,M = 2e6 + 11;
int n,m,q,cnt;
string g[N][N];
struct node{
	int x,y,r,d;
}a[M];
int find_pos(int x,int y){
	int pos = 1;
	for(int i=1;i<x;i++) pos = a[pos].d;
	for(int i=1;i<y;i++) pos = a[pos].r;
	return pos;
}
int main(){
//    freopen("ex_0.in","r",stdin);
//    freopen("file.out","w",stdout);
	read(n);read(m);read(q);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) read(g[i][j]); 
	}
	for(int i=0;i<=n+1;i++){
		for(int j=0;j<=m+1;j++){
			a[++ cnt].x = i;
			a[cnt].y = j;
			a[cnt].d = cnt + m + 2;
			a[cnt].r = cnt + 1;
		}
	}
	while(q --){
		int xl,yl,xr,yr,l,c;
		read(xl);read(yl);read(xr);read(yr);read(l);read(c);
		int rg1 = find_pos(xl,yl),rg2 = find_pos(xr,yr),tp1 = rg1,tp2 = rg2;
		for(int i=1;i<=l;i++){
			tp1 = a[tp1].d;
			tp2 = a[tp2].d;
			int x = a[tp1].r,y = a[tp2].r;
			a[tp1].r = y;
			a[tp2].r = x;
		}
		for(int i=1;i<=c;i++){
			tp1 = a[tp1].r;
			tp2 = a[tp2].r;
			int x = a[tp1].d,y = a[tp2].d;
			a[tp1].d = y;
			a[tp2].d = x;
		}
		tp1 = rg1,tp2 = rg2;
		for(int i=1;i<=c;i++){
			tp1 = a[tp1].r;
			tp2 = a[tp2].r;
			int x = a[tp1].d,y = a[tp2].d;
			a[tp1].d = y;
			a[tp2].d = x;
		}
		for(int i=1;i<=l;i++){
			tp1 = a[tp1].d;
			tp2 = a[tp2].d;
			int x = a[tp1].r,y = a[tp2].r;
			a[tp1].r = y;
			a[tp2].r = x;
		}
	}
	int st = a[a[1].d].r;
	for(int i=1;i<=n;i++){
		int p = st;
		for(int j=1;j<=m;j++){
			print(g[a[p].x][a[p].y]);
			p = a[p].r;
			putchar(' ');
		}
		st = a[st].d;
		putchar('\n');
	}
	return 0;
}

D. 排列(permutation)

题面
有一个长度为 nn 的排列丢失了,已知 mm 个三元组 (ai,bi,ci)(a_i, b_i, c_i) 表示 bib_i 在 aia_i 和 cic_i 之间。要求构造一个排列,满足至少 ⌈m2⌉\lceil \frac{m}{2} \rceil 个三元组。

输入格式
第一行:n,mn, m
接下来 mm 行:每行 ai,bi,cia_i, b_i, c_i

输出格式
一行 nn 个整数,表示构造的排列

数据范围
n,m≤105n, m \leq 10^5,保证有解。

数据范围 注意:本题采用捆绑测试

子任务条件分值
1n≤8n \leq 810 分
2n≤12n \leq 1215 分
3m≤12m \leq 1215 分
4无特殊限制60 分

对于所有数据:n,m≤105n, m \leq 10^5

样例输入

reStructuredtext
5 4
1 2 3
2 3 4
3 4 5
4 5 1

样例输出

reStructuredtext
1 4 3 2 5

Solution

我们思考这道题时,发现此题有SPJ,但问题不大

首先可以枚举所有可能的排列情况,再逐一检查是否满足条件,满足就输出,时间复杂度O(n!×n)O(n!\times n),可以拿到15pts15pts

我们就不多bb了,这题部分分还很多,我们直奔主题

反正我做这道题时考场上是想到了和差分约束的相关东西,因为如果这道题明显规定了ai,bi,cia_i,b_i,c_i的先后顺序,我们就可以直接确定建图,跑拓扑排序了

但,没这么简单,对于此题,aia_i和cic_i没有先后顺序,没有谁位高权重,所以没法直接建图,但明显对于这么逆天的数据,又不可能是二分

对于我们刚刚想到的词-----拓扑排序

OK!恭喜你找到此题正解

我们来看如何进行建图

这样进行建图,我们保证在11被扔进队列时,22也入队,直接将33的入度减2即可

这样保证11和22先后顺序相同

我们进行答案统计时要将三元组的最后一个元素归位时才记录答案,不然会重复,出现矛盾

在统计答案时,我们要把答案从两边往中间记录,这样可以保证先确定两边位置,再确定中间位置,在拓扑到每个点时,去枚举每个包含这个点的三元组,去计算放在当前的最左边还是最右边符合的方案数更多,选择方案数较多的一边记录即可,这样也符合拓扑序,我们就可以边拓扑便记录答案,就更方便.因为每次符合的三元组数量都比不符合的多,就一定可以满足大于等于m2\frac{m}{2}的的限制条件

Code:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
	x = 0;
	LL f = 1;
	char c = getchar();
	for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	x *= f;
}
template <typename T> inline void print(T x){
	if(x < 0) x = -x,putchar('-');
	if(x > 9) print(x / 10);
	putchar(x % 10 + 48);
}
const int N = 1e5 + 11,M = 25;
int n,m,l,r;
struct node{
	int x,y,z;
}a[N];
int iD[N],ans[N],pos[N];
bool st[N];
vector<node> ed[N],md[N];
void add(int u){
	int cntl = 0,cntr = 0;
	for(auto x : ed[u]){
		if(pos[x.x] == 0 && pos[x.y] > 0){
			if(pos[x.y] < l) cntr ++;
			else cntl ++;
		}
	}
	for(auto x : md[u]){
		if(pos[x.x] == 0 && pos[x.y] > 0){
			if(pos[x.y] < l) cntl ++;
			else cntr ++;
		}
		if(pos[x.y] == 0 && pos[x.x] > 0){
			if(pos[x.x] < l) cntl ++;
			else cntr ++;
		}
	}
	if(cntl >= cntr){
		pos[u] = l;
		ans[l] = u;
		l ++;
	}else{
		pos[u] = r;
		ans[r] = u;
		r --;
	}
	return;
}
void toposort(){
	queue<int> q;
	for(int i=1;i<=n;i++){
		if(iD[i] == 0) q.push(i);
	}
	while(q.size()){
		int t = q.front();
		q.pop();
		add(t);
		for(auto x : ed[t]){
			if(st[x.z] == 1) continue;
			st[x.z] = 1;
			iD[x.x] -= 2;
			if(iD[x.x] == 0) q.push(x.x);
		}
	}
}
int main(){
	// freopen("permutation.in","r",stdin);
    // freopen("permutation.out","w",stdout);
	read(n);read(m);
	l = 1,r = n;
	for(int i=1;i<=m;i++){
		read(a[i].x);read(a[i].y);read(a[i].z);
		iD[a[i].y] += 2;
		ed[a[i].x].emplace_back((node){a[i].y,a[i].z,i});
		ed[a[i].z].emplace_back((node){a[i].y,a[i].x,i});
		md[a[i].y].emplace_back((node){a[i].x,a[i].z,i});
	}
	toposort();
	for(int i=1;i<=n;i++){
		print(ans[i]);
		putchar(' ');
	}
	return 0;
}
原文