8.28总结
- NOIP 考试题
- 与绵阳中学高二、本校高考联考
- 联考排名 20/41 , 本校排名 10/23 。
- 失误比较严重
题目
T1
剪切字符串(lcp)
多组测试,每组给 个长度为 的字符串,选 ,把三个字符串分成三段,求三段两两最长公共前缀长度和的最小值。
考时情况
- 只得了 ,真服了。
- 八点开考,开始看 。
- 花费 看完题目。
- 看数据范围,先看部分分。
- 如果 ,我甚至都不知道这档部分分是给谁的。
- 如果 ,这是最朴素的暴力,直接枚举 ,再暴力验证。
- 我发现最大的 能到达 ,所以是不是可能解法会带 或者 。
- 考虑是否会带 ,然后我发现不能进行分块,于是叉掉。
- 考虑是否会带 ,我想到我们可以用 的时间复杂度预处理字符串匹配哈希,对于每一个 ,我们用 的时间复杂度获取从 开始的 字符串与从 开始的 字符串的最大匹配长度,记为 ,所以 的答案贡献为 。
- 然后我就一直试图用同样的思路解决 , ,于是在这里浪费了 。
- 突然发现如果我让 的第一个一组, 的 个一组,然后 最后一个一组。然后我们发现这样答案是 ,所以我们得到答案最多是 ,所以只用寻找答案是否会 。
- 浪费了太多时间,然后我就没再继续想了。
- 我直接果断拿掉 ,再写了一个 性质分然后就走了,估分将近 。此时已经过去 。
评测结果
考时过后我和黄思瀚都能估分大概 ,然而他拿到了 ,而我只能得到 。
原因很简单,性质分挂掉了。
漏掉一个判断!!!
考后我看过了题解,
个人感觉如果还有时间应给可以再多想想可能能想到正解。
考时思路已经接近正解了,估计 左右就能再得 。
解法
这里推荐 的思路。
首先 必选。
所以我们找到一个 使得 ,我们找到最小的 。
然后我们找 做为 分界点。
我们根据 的关系输出答案。
这是 最初思路。
于是 找到了一个 。
1
5
aaaaa
aabca
bbbbb这时 的程序就会出现错误(虽然考场他数据满分gg),
原因是 “我们找到最小的 。” 这句话是错误的!!!
我们能够发现,为什么会错误?
因为如果我们找到的 ,满足 都满足 。
对于这种情况我们可以找到两个不同的 ,这样就一定至少存在一个 满足。
这里挂一下 被 两次后改出的代码:
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";
}但是 最后还是被 了。
所以上面的代码不是正确的(但是能过几乎所有的数据)。
T2
深度优先搜索树(dfs)
给定一颗树 ,求无重边自环的连通图 数量,使 执行给定 生成的树与 完全一致,结果模 。
考时情况
- 火速看题,直接注意到答案是 的倍数。
- 秒写出快速幂。
- 经过 观察,能够发现答案为 ,其中 , 为在 树中从 到 的路径上(不含 )的节点编号 的节点个数。
- 很明显可以通过在遍历这颗 树的途中使用权值线段树进行维护。
- 写出,直接过大小样例。
- 突然发现我的代码模板中写了 ,果断删掉。直接给我吓个半死。我决定在所有 代码模板中删去 ,以免导致形成习惯。
评测结果
估分 ,得分 。
这道题的速度与精准度直接帮我在考场从“ 才做一题” 的劣势,打回了“搞定 ”的优势,是本场考时至关重要的一个节点。
题目思路
- 能够发现答案为 ,其中 , 为在 树中从 到 的路径上(不含 )的节点编号 的节点个数。
- 很明显可以通过在遍历这颗 树的途中使用权值线段树进行维护。
- 这里给出我的代码。
#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)
有固定和可选站点,经 添加可选站点后,选固定 部分可选站点,求这些点两两曼哈顿距离之和的最大值。
考时情况
- 读完题目,注意到题目关键点在于 非常小,其次就是有四个部分分档,每档 。
- 这道题有一个非常坑的地方,幸好我发现了。就是因为我最后要把所有可选点存在一起,而我的下标又是从 开始的,所以数组大小应该 ,通常来讲如果数组大小开 可能有问题。
- 第一档部分分, ,也就意味着这些点都在一个坐标轴上,所以我们考虑贪心,每一次选择与当前所有点的总距离最大的点选择,然后更新所有点距离其他点的总距离。时间复杂度 。
- 第二档部分分, ,也就意味着我们只选择两个点,而且 ,那就很好了,我直接用 的时间复杂度枚举选的是哪两个点就行了。
- 第三、四档部分分, ,这属于大数据了。借鉴“平面最近点对”,我们直接充分发扬人类智慧。
- 首先能够发现,如果像 那样贪心,是可能会出现问题的。
- 但是通常来讲,较优解在贪心按照总距离最小排序后一定相对靠前。
- 而我们进行一次贪心需要的时间复杂度为 ,折合题目数据后计算得到约为 ,于是我们拥有 次贪心机会,也就是 左右。
- 于是当 时,我们限制在第一层搜索时按照贪心排序顺序取前 优的答案,在第二层 时取前 优的答案,在第三层搜索时,贪心一定争取,于是选择排序后最优的解。
- 同样的道理,我们在 时,将 个阀门分别定为 。
- 我的程序可以在较短时间内得到较优解,同时为了避免时间超过限制,所以我们使用 卡一下时间,当时间为 时输出当前最优解并推出程序。
- 以下是考场代码:
#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)
给出 色括号序列,最少操作次数(翻转括号方向)使总序列、非红、非蓝、非绿子序列均为合法括号序列。
考时情况
- 考时的时候做到这道题时已经没有太多时间了。
- 我试图骗分,直接分析性质写了一个 的性质,然后就提交了我的代码。
测评
估分至少 ,结果真的给我整的一分都没有了。
错误比较细节:
我把:
if(top)++ans;
if(tot)++ans;写成了:
if(top or tot)++ans;