6.【2024初三年前集训测试1】

���

qwq

2024初三年前集训测试1

T1学说话

100pts

  • 水题,秒了。
  • 单词被下划线分隔开,对于每个单词来说,只要记录最长的单词长度,用 tot 临时记录,遇到下划线就清零, ans 记录最大值,最后输出即可。

代码

#include<bits/stdc++.h>
#define int long long
#define N (1000010)
#define sort stable_sort
using namespace std;
namespace IO
{
#define ll long long
const int MAX=1<<25;
char buf[MAX],*p1=buf,*p2=buf;
char obuf[MAX],*o=obuf;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin),p1==p2)?EOF:*p1++)
//template<typename T>
//inline T read()
inline int read()
{
int x=0;bool f=1;
char c=gc();
for(;c<48||c>57;c=gc())if(c=='-')f=0;
for(;c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c^48);
return f?x:~x+1;
}
void print(ll x){if(x>9)print(x/10);*o++=(x%10)+'0';}
void pit(ll x){if(x<0)*o++='-',x=~x+1;print(x);}
void write(ll x,char end){pit(x);*o++=end;}
void flush(){fwrite(obuf,o-obuf,1,stdout);}
#undef ll
}
using IO::read;using IO::write;using IO::flush;using std::complex;
inline int min(int x,int y){return y&((y-x)>>31)|x&(~(y-x)>>31);}
inline int max(int x,int y){return x&((y-x)>>31)|y&(~(y-x)>>31);}
inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
long long n,m;
void init_set()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
#ifdef ONLINE_JUDGE
freopen("word.in","r",stdin);
freopen("word.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
}
int l,r,xx,yy,zz,lss;
string s;
int tot,ans;
signed main()
{
init_set();
cin>>s;
int len(s.size());
for(int i(0);i<len;++i)
{
tot=0;
while(s[i]!='_'&&i<len)++tot,++i;
ans=max(ans,tot);
}
write(ans,' ');
flush();
return 0;
}

T2膜拜大佬

0pts100pts

  • 哈希水题,把每位大佬的哈希值存起来,时间复杂度 O(|L|) ,约为 O(n2) 。当询问时,前两个字符串显然无用,对最后一个字符串求哈希值,如果有一个哈希可以匹配到,看做两个字符串相同 输出 Yes 后跳出查找,否则输出 No 。时间复杂度为 O(n2)
  • 可是如果你不会用哈希怎么办呢?map 大法好,假如用了 map ,复杂度比哈希还要低,为 O(nlog(n)) 。所以喊出那句话 map 大法好!!!
     i  j qwq

代码

哈希

#include<bits/stdc++.h>
#define int long long
#define N (1000010)
#define sort stable_sort
using namespace std;
namespace IO
{
#define ll long long
const int MAX=1<<25;
char buf[MAX],*p1=buf,*p2=buf;
char obuf[MAX],*o=obuf;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin),p1==p2)?EOF:*p1++)
//template<typename T>
//inline T read()
inline int read()
{
int x=0;bool f=1;
char c=gc();
for(;c<48||c>57;c=gc())if(c=='-')f=0;
for(;c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c^48);
return f?x:~x+1;
}
void print(ll x){if(x>9)print(x/10);*o++=(x%10)+'0';}
void pit(ll x){if(x<0)*o++='-',x=~x+1;print(x);}
void write(ll x,char end){pit(x);*o++=end;}
void flush(){fwrite(obuf,o-obuf,1,stdout);}
#undef ll
}
using IO::read;using IO::write;using IO::flush;using std::complex;
inline int min(int x,int y){return y&((y-x)>>31)|x&(~(y-x)>>31);}
inline int max(int x,int y){return x&((y-x)>>31)|y&(~(y-x)>>31);}
inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
long long n,m;
void init_set()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
#ifdef ONLINE_JUDGE
freopen("dalao.in","r",stdin);
freopen("dalao.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
}
int l,r,xx,yy,zz,lss;
string s;
int tot,ans;
unsigned __int128 has[100010],p[100010],qhs;
bool flag=0;
signed main()
{
init_set();
cin>>n;
p[0]=1,p[1]=131;
for(int i(2);i<=10000;++i)p[i]=p[i-1]*p[1];
for(int i(1);i<=n;++i)
{
cin>>s;
int len(s.size());
for(int j(0);j<len;++j)
has[i]=has[i]*p[1]+s[j];
}
cin>>m;
for(int i(1);i<=m;++i)
{
cin>>s>>s>>s;
flag=0;
int len(s.size());
qhs=0;
for(int j(0);j<len;++j)qhs=qhs*p[1]+s[j];
for(int j(1);j<=n;++j)
if(qhs==has[j]){flag=true,puts("Yes");break;}
if(!flag)puts("No");
}
flush();
return 0;
}
$map$
```cpp
#include<bits/stdc++.h>
#define int long long
#define N (1000010)
#define sort stable_sort
using namespace std;
namespace IO
{
#define ll long long
const int MAX=1<<25;
char buf[MAX],*p1=buf,*p2=buf;
char obuf[MAX],*o=obuf;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin),p1==p2)?EOF:*p1++)
//template<typename T>
//inline T read()
inline int read()
{
int x=0;bool f=1;
char c=gc();
for(;c<48||c>57;c=gc())if(c=='-')f=0;
for(;c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c^48);
return f?x:~x+1;
}
void print(ll x){if(x>9)print(x/10);*o++=(x%10)+'0';}
void pit(ll x){if(x<0)*o++='-',x=~x+1;print(x);}
void write(ll x,char end){pit(x);*o++=end;}
void flush(){fwrite(obuf,o-obuf,1,stdout);}
#undef ll
}
using IO::read;using IO::write;using IO::flush;using std::complex;
inline int min(int x,int y){return y&((y-x)>>31)|x&(~(y-x)>>31);}
inline int max(int x,int y){return x&((y-x)>>31)|y&(~(y-x)>>31);}
inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
long long n,m;
void init_set()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
#ifdef ONLINE_JUDGE
freopen("dalao.in","r",stdin);
freopen("dalao.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
}
int l,r,xx,yy,zz,lss;
string s;
int tot,ans;
map<string,int>hs;
bool flag=0;
signed main()
{
init_set();
cin>>n;
for(int i(1);i<=n;++i)
{
cin>>s;
hs[s]=i;
}
cin>>m;
for(int i(1);i<=m;++i)
{
cin>>s>>s>>s;
if(hs.find(s)!=hs.end())puts("Yes");
else puts("No");
}
flush();
return 0;
}

T3走迷宫

80pts100pts

  • 假如当时没有把无解情况注释掉,就是 100pts 了。
  • 很容易看出来这道题是个搜索,但是也有人用 SPFA AC 了。
  • 如果用搜索,显然需要用 BFS 而不是 DFS ,因为要求最优路线, BFS 第一个搜到的解一定是最优解,而 DFS 就做不到。
  • 但是蒟蒻一开始把 BFS 打成了 DFS 也就浪费了半个小时 qwq
  • 由于可以有传送门,先记录上每个传送门匹配的位置。之后就能快速传送。
  • BFS 要用队列,而 DFS 一般是用函数递归。用结构体存坐标和步数,在第一个队列中放入起始点,初始步数为 0 。之后 while(ht) ,每次从队头取值,加入四周有可以达到的点或未走过的点,就进行 BFS ,将新的点加入队尾。如果最终也没有搜到答案,可以在一开始将答案赋为极大值,找到答案就更新,未找到答案就输出 1
  • 对于传送门(字母)来说,可以进行特判,走到字母上直接传送到另一个字母上。

代码

#include<bits/stdc++.h>
#define int long long
#define N (1000010)
#define sort stable_sort
using namespace std;
namespace IO
{
#define ll long long
const int MAX=1<<25;
char buf[MAX],*p1=buf,*p2=buf;
char obuf[MAX],*o=obuf;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin),p1==p2)?EOF:*p1++)
//template<typename T>
//inline T read()
inline int read()
{
int x=0;bool f=1;
char c=gc();
for(;c<48||c>57;c=gc())if(c=='-')f=0;
for(;c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c^48);
return f?x:~x+1;
}
void print(ll x){if(x>9)print(x/10);*o++=(x%10)+'0';}
void pit(ll x){if(x<0)*o++='-',x=~x+1;print(x);}
void write(ll x,char end){pit(x);*o++=end;}
void flush(){fwrite(obuf,o-obuf,1,stdout);}
#undef ll
}
using IO::read;using IO::write;using IO::flush;using std::complex;
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
long long n,m;
void init_set()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
#ifdef ONLINE_JUDGE
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
}
int l,r,x,y,xx,yy,zz,lss,begx,begy,ans(0x7f7f7f7f);
struct aa{int x,y;}e[100][5];
struct bb{int x,y,stp;}q[5000010];
bool pd[510][510];
char cap[510][510],c[510][510];
signed main()
{
init_set();
cin>>n>>m;
for(int i(1);i<=n;++i)
for(int j(1);j<=m;++j)
{
cin>>c[i][j];
if(c[i][j]!='#')pd[i][j]=1;
if(c[i][j]>='A'&&c[i][j]<='Z')
{
if(e[c[i][j]-'A'][0].x!=0)
e[c[i][j]-'A'][1]={i,j};
else e[c[i][j]-'A'][0]={i,j};
}
if(c[i][j]=='@')begx=i,begy=j;
}
int t(0),h(0),stp(0);
q[0]={begx,begy,0};
for(;h<=t;)
{
x=q[h].x,y=q[h].y;stp=q[h].stp;++h;
if(!pd[x][y]||x<1||x>n||y<1||y>m)continue;
pd[x][y]=0;
if(c[x][y]=='='){ans=stp,cout<<stp<<'\n';break;}
if(c[x+1][y]>='A'&&c[x+1][y]<='Z')
{
if(e[c[x+1][y]-'A'][0].x==x+1&&e[c[x+1][y]-'A'][0].y==y)
q[++t]=(bb){e[c[x+1][y]-'A'][1].x,e[c[x+1][y]-'A'][1].y,stp+1};
else
q[++t]=(bb){e[c[x+1][y]-'A'][0].x,e[c[x+1][y]-'A'][0].y,stp+1};
}
else q[++t]=(bb){x+1,y,stp+1};
if(c[x-1][y]>='A'&&c[x-1][y]<='Z')
{
if(e[c[x-1][y]-'A'][0].x==x-1&&e[c[x-1][y]-'A'][0].y==y)
q[++t]=(bb){e[c[x-1][y]-'A'][1].x,e[c[x-1][y]-'A'][1].y,stp+1};
else
q[++t]=(bb){e[c[x-1][y]-'A'][0].x,e[c[x-1][y]-'A'][0].y,stp+1};
}
else q[++t]=(bb){x-1,y,stp+1};
if(c[x][y-1]>='A'&&c[x][y-1]<='Z')
{
if(e[c[x][y-1]-'A'][0].x==x&&e[c[x][y-1]-'A'][0].y==y-1)
q[++t]=(bb){e[c[x][y-1]-'A'][1].x,e[c[x][y-1]-'A'][1].y,stp+1};
else
q[++t]=(bb){e[c[x][y-1]-'A'][0].x,e[c[x][y-1]-'A'][0].y,stp+1};
}
else q[++t]=(bb){x,y-1,stp+1};
if(c[x][y+1]>='A'&&c[x][y+1]<='Z')
{
if(e[c[x][y+1]-'A'][0].x==x&&e[c[x][y+1]-'A'][0].y==y+1)
q[++t]=(bb){e[c[x][y+1]-'A'][1].x,e[c[x][y+1]-'A'][1].y,stp+1};
else
q[++t]=(bb){e[c[x][y+1]-'A'][0].x,e[c[x][y+1]-'A'][0].y,stp+1};
}
else q[++t]=(bb){x,y+1,stp+1};
}
if(ans==0x7f7f7f7f)puts("-1"),exit(0);
flush();
return 0;
}

T4鸭子游戏

20pts

  • 本来感觉有思路,但是还是假了……如果想到差分就很好做了。
  • 一开始以为可以设定一个高度,之后暴力判断使所有高度都为这个高度需要多少步。然后又觉得可以三分,但是最后没打出来。
  • 正解:差分。
  • 首先看这样一组数据 1 3 5 3 1 差分数组为 0 2 2 2 2 ,用人脑模拟得出,第一步将 24 的高度 2 ,之后把 3 的高度 2

原数组:

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

差分数组:

1 2 3 4 5
0 2 2 -2 -2
0 0 2 -2 0
0 0 0 0 0
  • 看出规律了吗?肯定看出来了。 每次修改就是将差分数组正负匹配。如对 24 的修改,事实上就是在将 25 的差分数组正负匹配,对 3 的修改,就是对 3 4 的差分数组正负匹配,之后变成 0
  • 所有最后也就只需要去记录正的差分和负的差分的和,之后输出其中最大的即可。

代码

#include<bits/stdc++.h>
#define int long long
#define N (1000010)
#define sort stable_sort
using namespace std;
namespace IO
{
#define ll long long
const int MAX=1<<25;
char buf[MAX],*p1=buf,*p2=buf;
char obuf[MAX],*o=obuf;
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin),p1==p2)?EOF:*p1++)
//template<typename T>
//inline T read()
inline int read()
{
int x=0;bool f=1;
char c=gc();
for(;c<48||c>57;c=gc())if(c=='-')f=0;
for(;c>=48&&c<=57;c=gc())x=(x<<3)+(x<<1)+(c^48);
return f?x:~x+1;
}
void print(ll x){if(x>9)print(x/10);*o++=(x%10)+'0';}
void pit(ll x){if(x<0)*o++='-',x=~x+1;print(x);}
void write(ll x,char end){pit(x);*o++=end;}
void flush(){fwrite(obuf,o-obuf,1,stdout);}
#undef ll
}
using IO::read;using IO::write;using IO::flush;using std::complex;
inline int min(int x,int y){return y&((y-x)>>31)|x&(~(y-x)>>31);}
inline int max(int x,int y){return x&((y-x)>>31)|y&(~(y-x)>>31);}
inline void swap(int &x,int &y){int tmp=x;x=y;y=tmp;}
long long n,m;
void init_set()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
#ifdef ONLINE_JUDGE
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
}
int l,r,x,y,xx,yy,zz,lss,las,ans,res,kop;
int a[2000010];
int cf[2000010];
signed main()
{
init_set();
n=read();
las=read();
for(int i(2);i<=n;++i)
{
lss=read();
kop=lss-las;
if(kop>0)ans+=kop;
else res-=kop;
las=lss;
}
write(max(ans,res),' ');
flush();
return 0;
}

qwq
image

posted @   minecraft114514  阅读(22)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示