跳到正文

个人记录

8.28 总结

Believe_in_dreams约 21 分钟
原文

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

文章信息与版本

文章信息

收录
2026年2月28日
更新
2026年5月7日
篇幅
约 21 分钟

版本

  1. v2当前
  2. v1

8.28总结

  • NOIP 考试题
  • 与绵阳中学高二、本校高考联考
  • 联考排名 20/41 , 本校排名 10/23 。
  • 失误比较严重

题目

T1

剪切字符串(lcp)

多组测试,每组给 33 个长度为 nn 的字符串,选 i<ji<j ,把三个字符串分成三段,求三段两两最长公共前缀长度和的最小值。

考时情况

  • T1T1 只得了 50pts50pts ,真服了。
  • 八点开考,开始看 T1T1 。
  • 花费 15min15min 看完题目。
  • 看数据范围,先看部分分。
  • 如果 n≤10n\le 10 ,我甚至都不知道这档部分分是给谁的。
  • 如果 n≤100n\le 100 ,这是最朴素的暴力,直接枚举 i,ji,j ,再暴力验证。
  • 我发现最大的 nn 能到达 10510^5 ,所以是不是可能解法会带 loglog 或者  \sqrt \ 。
  • 考虑是否会带  \sqrt \ ,然后我发现不能进行分块,于是叉掉。
  • 考虑是否会带 loglog ,我想到我们可以用 O(n×logn)O(n\times log n) 的时间复杂度预处理字符串匹配哈希,对于每一个 ii ,我们用 O(n×logn)O(n\times log n) 的时间复杂度获取从 ii 开始的 bb 字符串与从 11 开始的 aa 字符串的最大匹配长度,记为 dpidp_i ,所以 LCP(a,b)LCP(a,b) 的答案贡献为 min(j,dpi)−imin(j,dp_i)-i 。
  • 然后我就一直试图用同样的思路解决 LCP(b,c)LCP(b,c) ,LCP(a,c)LCP(a,c) ,于是在这里浪费了 60min60min 。
  • 突然发现如果我让 aa 的第一个一组, bb 的 2→n−12\to n-1 个一组,然后 cc 最后一个一组。然后我们发现这样答案是 33 ,所以我们得到答案最多是 33 ,所以只用寻找答案是否会 ≤2\le 2 。
  • 浪费了太多时间,然后我就没再继续想了。
  • 我直接果断拿掉 50pts50pts ,再写了一个 abab 性质分然后就走了,估分将近 80pts80pts 。此时已经过去 1.5h1.5h 。

评测结果

考时过后我和黄思瀚都能估分大概 80pts80pts ,然而他拿到了 80pts80pts ,而我只能得到 50pts50pts 。
原因很简单,性质分挂掉了。
漏掉一个判断!!!
考后我看过了题解,
个人感觉如果还有时间应给可以再多想想可能能想到正解。
考时思路已经接近正解了,估计 25min25min 左右就能再得 50pts50pts 。

解法

这里推荐 LBYLBY 的思路。
首先 a1,cna_1,c_n 必选。
所以我们找到一个 i  (2≤i≤n−1)i\ \ (2\le i\le n-1) 使得 bi≠a1b_i \neq a_1 ,我们找到最小的 ii 。
然后我们找 jj 做为 b,cb,c 分界点。
我们根据 a1,bi,cja_1,b_i,c_j 的关系输出答案。
这是 LBYLBY 最初思路。
于是 ZHLZHL 找到了一个 HackHack 。

代码
1
5
aaaaa
aabca
bbbbb

这时 LBYLBY 的程序就会出现错误(虽然考场他数据满分gg),
原因是 “我们找到最小的 ii 。” 这句话是错误的!!!
我们能够发现,为什么会错误?
因为如果我们找到的 ii ,满足 j∈ci→nj\in c_{i\to n} 都满足 cj=bic_j=b_i 。
对于这种情况我们可以找到两个不同的 pospos ,这样就一定至少存在一个 pospos 满足。
这里挂一下 LBYLBY 被 hackhack 两次后改出的代码:

C++
	cin>>n>>a>>b>>c,a=" "+a,b=" "+b,c=" "+c;
	int pos=0,pos114=0;
	for(int i=1;i<n-1;i++) if(a[1]!=b[i+1]){
		if(pos==0) pos=i;
		else if(b[pos+1]!=b[i+1]){
			pos114=i;
			break;
		}
	}
	if(pos==0){
		for(int i=2;i<n;i++) if(a[1]!=c[i+1]){
			pos=i;
			break;
		}
		if(pos==0) cout<<(a[1]==b[2])+(a[1]==c[n])+(b[2]==c[n])<<"\n";
		else cout<<(a[1]==b[2])+(a[1]==c[pos])+(b[2]==c[pos])<<"\n";
	}
	else{
		int pos2=0;
		for(int i=pos+1;i<n;i++) if(a[1]!=c[i+1] && b[pos+1]!=c[i+1]) pos2=i;
		if(pos2==0){
			if(pos114==0) cout<<(a[1]==b[pos+1])+(a[1]==c[n])+(b[pos+1]==c[n])<<"\n";
			else{
				int pos2=0;
				pos=pos114;
				for(int i=pos+1;i<n;i++) if(a[1]!=c[i+1] && b[pos+1]!=c[i+1]) pos2=i;
				cout<<(a[1]==b[pos+1])+(a[1]==c[n])+(b[pos+1]==c[n])<<"\n";
			}
		}
		else cout<<"0\n";
	}

但是 LBYLBY 最后还是被 HackHack 了。
所以上面的代码不是正确的(但是能过几乎所有的数据)。

T2

深度优先搜索树(dfs)

给定一颗树 TT ,求无重边自环的连通图 GG 数量,使 GG 执行给定 dfsdfs 生成的树与 TT 完全一致,结果模 1e9+71e9+7 。

考时情况

  • 火速看题,直接注意到答案是 22 的倍数。
  • 5050 秒写出快速幂。
  • 经过 5min5min 观察,能够发现答案为 2∑i=1npi%P2^{\sum_{i=1}^{n}p_i}\%P ,其中 P=109+7P=10^9+7 , pip_i 为在 DFSDFS 树中从 ii 到 11 的路径上(不含 11 )的节点编号 <i<i 的节点个数。
  • 很明显可以通过在遍历这颗 DFSDFS 树的途中使用权值线段树进行维护。
  • 5min5min 写出,直接过大小样例。
  • 突然发现我的代码模板中写了 OfastOfast ,果断删掉。直接给我吓个半死。我决定在所有 c++c++ 代码模板中删去 OfastOfast ,以免导致形成习惯。

评测结果

估分 100pts100pts ,得分 100pts100pts 。
这道题的速度与精准度直接帮我在考场从“ 1.5h1.5h 才做一题” 的劣势,打回了“搞定 175pts175pts ”的优势,是本场考时至关重要的一个节点。

题目思路

  • 能够发现答案为 2∑i=1npi%P2^{\sum_{i=1}^{n}p_i}\%P ,其中 P=109+7P=10^9+7 , pip_i 为在 DFSDFS 树中从 ii 到 11 的路径上(不含 11 )的节点编号 <i<i 的节点个数。
  • 很明显可以通过在遍历这颗 DFSDFS 树的途中使用权值线段树进行维护。
  • 这里给出我的代码。
C++
#include<iostream>
#define int long long
using namespace std;
#define FOR(i,x,y)for(register int i=(x);i<=(y);++i)
typedef long long ll;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin),p1==p2)?EOF:*p1++)
constexpr int MAXSIZE=2e5;
char buf[MAXSIZE],*p1,*p2;
inline double Read(){
	double ans=0.0,p=0.1;bool j=1;char c=gc();
	while(c>'9' or c<'0'){if(c=='-')j=0;c=gc();}
	while(c>='0' and c<='9')ans=10.0*ans+c-48,c=gc();
	if(c!='.')return j?ans:-ans;c=gc();
	while(c>='0' and c<='9')ans=ans+p*(c-48),p*=0.1,c=gc();
	return j?ans:-ans;
}
inline string READ(){
	string ret="";char c=gc();
	while(c==' ' or c=='\n')c=gc();
	while(c!=' ' and c!='\n')ret+=c,c=gc();
	return ret;
}
inline int read(){
	int ans=0,j=1;char c=gc();
	while(c>'9' or c<'0'){if(c=='-')j=-1;c=gc();}
	while(c>='0' and c<='9')ans=(ans<<1)+(ans<<3)+(c^48),c=gc();
	return ans*j;
}
inline void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(48+x%10);
}
const int N=2e5+5;
int n,ver[N<<1],ne[N<<1],head[N],all,rt,tot;
inline void ins(int x,int y){
	ver[++all]=y,ne[all]=head[x],head[x]=all;
}
inline int ksm(int a,int b,int p){
	int ret=1;
	while(b){
		if(b&1)ret*=a,ret%=p;
		a*=a,a%=p,b>>=1;
	}
	return ret;
}
struct node{
	int lc,rc,sum;
}tree[N<<3];
void add(int &p,int l,int r,int x,int c){
	if(!p)p=++tot;
	if(l==r){
		tree[p].sum+=c;
		return;
	}
	int mid=l+r>>1;
	if(x<=mid)add(tree[p].lc,l,mid,x,c);
	else add(tree[p].rc,mid+1,r,x,c);
	tree[p].sum=tree[tree[p].lc].sum+tree[tree[p].rc].sum; 
}
int ask(int p,int l,int r,int fl,int fr){
	if(!p or l>r or fl>fr)return 0;
	if(fl<=l and r<=fr)return tree[p].sum;
	int mid=l+r>>1,ret=0;
	if(fl<=mid)ret+=ask(tree[p].lc,l,mid,fl,fr);
	if(fr>mid)ret+=ask(tree[p].rc,mid+1,r,fl,fr);
	return ret;
}
const int mod=1e9+7;
int ans=0;
void dfs(int x,int fa){
	ans+=ask(rt,1,n,1,x-1);
	for(register int i=head[x];i;i=ne[i]){
		int y=ver[i];
		if(y==fa)continue;
		add(rt,1,n,y,1);
		dfs(y,x);
		add(rt,1,n,y,-1);
	}
}
signed main(){
	freopen("dfs.in","r",stdin);
	freopen("dfs.out","w",stdout);
	n=read();
	FOR(i,1,n-1){
		int x=read(),y=read();
		ins(x,y),ins(y,x);
	}
	dfs(1,0);
	write(ksm(2,ans,mod));
	return 0;
}
/*
 5
 1 2
 1 3
 2 4
 2 5
*/

T3

巡逻网络(net)

有固定和可选站点,经 qq 添加可选站点后,选固定 ++ 部分可选站点,求这些点两两曼哈顿距离之和的最大值。

考时情况

  • 15min15min 读完题目,注意到题目关键点在于 m,tm,t 非常小,其次就是有四个部分分档,每档 25pts25pts 。
  • 这道题有一个非常坑的地方,幸好我发现了。就是因为我最后要把所有可选点存在一起,而我的下标又是从 11 开始的,所以数组大小应该 m+q+1m+q+1 ,通常来讲如果数组大小开 2×104+52\times 10^4 +5 可能有问题。
  • 第一档部分分, k=1k=1 ,也就意味着这些点都在一个坐标轴上,所以我们考虑贪心,每一次选择与当前所有点的总距离最大的点选择,然后更新所有点距离其他点的总距离。时间复杂度 O(n×logn)O(n\times log n) 。
  • 第二档部分分, m=2m=2 ,也就意味着我们只选择两个点,而且 q≤5×103q\le 5\times 10^3 ,那就很好了,我直接用 O(q2)O(q^2) 的时间复杂度枚举选的是哪两个点就行了。
  • 第三、四档部分分, m=3/4,k=2,q≤2×104m=3/4,k=2,q\le 2\times 10^4 ,这属于大数据了。借鉴“平面最近点对”,我们直接充分发扬人类智慧。
  • 首先能够发现,如果像 k=1k=1 那样贪心,是可能会出现问题的。
  • 但是通常来讲,较优解在贪心按照总距离最小排序后一定相对靠前。
  • 而我们进行一次贪心需要的时间复杂度为 O(n×logn)O(n\times log n) ,折合题目数据后计算得到约为 3×1053\times 10^5 ,于是我们拥有 计算机一秒处理数据量/单次贪心需要处理数据量计算机一秒处理数据量 / 单次贪心需要处理数据量 次贪心机会,也就是 2×107/3×105=6602\times 10^7/3\times 10^5 = 660 左右。
  • 于是当 m=3m=3 时,我们限制在第一层搜索时按照贪心排序顺序取前 5050 优的答案,在第二层 dfsdfs 时取前 1010 优的答案,在第三层搜索时,贪心一定争取,于是选择排序后最优的解。
  • 同样的道理,我们在 m=4m=4 时,将 4d4d 个阀门分别定为 30,5,2,130,5,2,1 。
  • 我的程序可以在较短时间内得到较优解,同时为了避免时间超过限制,所以我们使用 clock()clock() 卡一下时间,当时间为 990ms990ms 时输出当前最优解并推出程序。
  • 以下是考场代码:
C++
#include<iostream>
#include<time.h>
#include<algorithm>
#define int long long
using namespace std;
#define FOR(i,x,y)for(register int i=(x);i<=(y);++i)
typedef long long ll;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin),p1==p2)?EOF:*p1++)
constexpr int MAXSIZE=2e5;
char buf[MAXSIZE],*p1,*p2;
inline int read(){
	int ans=0,j=1;char c=gc();
	while(c>'9' or c<'0'){if(c=='-')j=-1;c=gc();}
	while(c>='0' and c<='9')ans=(ans<<1)+(ans<<3)+(c^48),c=gc();
	return ans*j;
}
inline void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(48+x%10);
}
inline int get(int x){
	if(x<0)return -x;
	return x;
}
const int N=2e4+10;//坑 
int m,q,k,t,time_start;
namespace Sub1{
    int a[N];//一维固定点 
    struct node{
    	int pos,dis;
    	bool operator<(const node &p)const{
	    	return dis<p.dis;
		}
	}p[N];//可选点 
	int main(){
		int ans=0;
		FOR(i,1,t)a[i]=read();
		FOR(i,1,m)p[i].pos=read();
		FOR(i,m+1,m+q)p[i].pos=read();
		q+=m; 
		FOR(i,1,q)FOR(j,1,t)//与必选点的距离 
			p[i].dis+=get(p[i].pos-a[j]); 
		for(int i=0;i<m;++i){//每次加入最远点 
			//排序
			sort(p+1,p+(q-i)+1);
			//选第q-i个
			ans+=p[q-i].dis;
			//更新距离
			FOR(j,1,q-i-1)p[j].dis+=get(p[j].pos-p[q-i].pos);
		}
		FOR(i,1,t)FOR(j,1,i-1)ans+=get(a[i]-a[j]);
		write(ans); 
		return 0;
	}
}
namespace Sub2{
	struct node{
		int x,y,dis;
	}g[N],a[N];
	int main(){
		int ans=0;
		FOR(i,1,t)g[i].x=read(),g[i].y=read();
		FOR(i,1,m)a[i].x=read(),a[i].y=read();
		FOR(i,m+1,m+q)a[i].x=read(),a[i].y=read();
		q+=m;
		FOR(i,1,q)FOR(j,1,t){
			a[i].dis+=get(a[i].x-g[j].x);
			a[i].dis+=get(a[i].y-g[j].y);
		}
		FOR(i,1,q)FOR(j,1,q){
			if(i==j)continue;
			ans=max(ans,a[i].dis+a[j].dis+get(a[i].x-a[j].x)+get(a[i].y-a[j].y));
		}
		FOR(i,1,t)FOR(j,1,i-1)ans+=get(g[i].x-g[j].x)+get(g[i].y-g[j].y);
		write(ans);
		return 0;
	}
}
namespace Sub3{//充分发扬人类智慧 
    int ans=0;
	struct node{
		int x,y,dis;
		bool operator<(const node &p)const{return dis>p.dis;}
	}g[N],a[N],tmp[6][N];
	void dfs1(int step,int lst,int nowall){//针对m=3 
	    if(clock()-time_start>980){
	    	write(ans);
	    	exit(0);
		}
	    if(step==4){
	    	ans=max(ans,nowall);
	    	return;
		}
		sort(a+1,a+lst+1);
		int mx;
		if(step==1)mx=50;
		else if(step==2)mx=10;
		else mx=1;
		mx=min(mx,lst);
		FOR(i,1,lst)tmp[step][i]=a[i];
		FOR(i,1,mx){
			//选择第i个
			swap(a[i],a[lst]);
			FOR(j,1,lst-1)a[j].dis+=get(a[j].x-a[lst].x)+get(a[j].y-a[lst].y);
			dfs1(step+1,lst-1,nowall+a[lst].dis);
			FOR(j,1,lst)a[j]=tmp[step][j];
		}
	}
	void dfs2(int step,int lst,int nowall){//针对m=4
	    if(clock()-time_start>980){
	    	write(ans);
	    	exit(0);
		}
	    if(step==5){
	    	ans=max(ans,nowall);
	    	return;
		}
		sort(a+1,a+lst+1);
		int mx;
		if(step==1)mx=30;
		else if(step==2)mx=5;
		else if(step==3)mx=2;
		else mx=1;
		mx=min(mx,lst);
		FOR(i,1,lst)tmp[step][i]=a[i];
		FOR(i,1,mx){
			//选择第i个
			swap(a[i],a[lst]);
			FOR(j,1,lst-1)a[j].dis+=get(a[j].x-a[lst].x)+get(a[j].y-a[lst].y);
			dfs2(step+1,lst-1,nowall+a[lst].dis);
			FOR(j,1,lst)a[j]=tmp[step][j];
		}
	}
	int main(){
		FOR(i,1,t)g[i].x=read(),g[i].y=read();
		FOR(i,1,m)a[i].x=read(),a[i].y=read();
		FOR(i,m+1,m+q)a[i].x=read(),a[i].y=read();
		q+=m;
		FOR(i,1,q)FOR(j,1,t){
			a[i].dis+=get(a[i].x-g[j].x);
			a[i].dis+=get(a[i].y-g[j].y);
		}
		if(m==3)dfs1(1,q,0);
		else if(m==4)dfs2(1,q,0);
		else cout<<"我直接自认答案错误\n";
		FOR(i,1,t)FOR(j,1,i-1)ans+=get(g[i].x-g[j].x)+get(g[i].y-g[j].y);
		write(ans);
		return 0;
	}
}
signed main(){
	freopen("net.in","r",stdin);
	freopen("net.out","w",stdout);
	time_start=clock();
    m=read(),q=read(),k=read(),t=read(); 
	if(k==1)Sub1::main();
	else if(q<=5000 and m==2)Sub2::main();
	else Sub3::main();//我们充分发扬人类智慧 
	return 0;
}
/*
3 5 2 3
-82 -37
69 60
-98 27
34 24
73 100
35 30
92 65
-65 66
-81 -87
48 -97
-83 26

2 2 2 3
5 0
7 0
11 0
3 0
6 0
12 0
17 0

2 2 1 2
19
0
3
8
19
17
*/

T4

彩色括号(witch)

给出 33 色括号序列,最少操作次数(翻转括号方向)使总序列、非红、非蓝、非绿子序列均为合法括号序列。

考时情况

  • 考时的时候做到这道题时已经没有太多时间了。
  • 我试图骗分,直接分析性质写了一个 ai≤1a_i\le 1 的性质,然后就提交了我的代码。

测评

估分至少 10pts10pts ,结果真的给我整的一分都没有了。
错误比较细节:
我把:

C++
	if(top)++ans;
	if(tot)++ans;

写成了:

C++
	if(top or tot)++ans;