第一题 抛硬币
![在这里插入图片描述]()
输出:0.5
第二题 求零点
![在这里插入图片描述]()
盲猜有单调性 ,盲猜二分,
答案:1.849016
注意精度
第三题 棋盘放置
![在这里插入图片描述]()
勾勾画画 ,答案 14
第四题 突破障碍
![在这里插入图片描述]()
B组模拟赛最后一题
bfs 每次找代价最小的点扩展,最后扩展出答案,用堆来找最小的代价,发现代价要么0要么1,用deque也可以整
官方题解是deque,用堆也可以过
堆代码
#include<iostream>
#include<algorithm>
#include <queue>
#include<string>
using namespace std;
typedef pair<int,int>pll;
struct node
{
int x,y;
int w;
bool operator>(const node & t)const{
return w>t.w;
}
node(int _x,int _y,int _w)
{
x=_x;
y=_y;
w=_w;
}
};
const int N=300;
char g[N][N];
int n,m;
pll st,ed;
int book[N][N];
void bfs()
{
priority_queue<node,vector<node>,greater<node> >q;
q.push(node(st.first,st.second,0));
book[st.first][st.second]=1;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
node t=q.top();
while(q.size())
{
node t=q.top();
q.pop();
if(t.x == ed.first && t.y == ed.second)
{
cout<<t.w<<endl;
return ;
}
for(int i=0;i<4;i++)
{
int a=t.x+dx[i];
int b=t.y+dy[i];
if(a<0 || a>=n || b<0 || b>=m) continue;
if(book[a][b]) continue;
book[a][b]=1;
int distemp=g[a][b]=='#';
q.push(node(a,b,t.w+distemp));
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>g[i];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(g[i][j]=='S')
{
st.first=i,st.second=j;
}
if(g[i][j]=='T')
{
ed.first=i,ed.second=j;
}
}
bfs();
return 0;
}
第五题 歌手
![在这里插入图片描述]()
刚开始写是排列组合,排列不出来,就整暴力dfs了
代码
#include<iostream>
using namespace std;
int a[7]={0,3,1,2,2,1};
int res;
void dfs(int u,int state)
{
if(u==10)
{
res++;
return ;
}
for(int i=1;i<=5;i++)
{
if(i==state) continue;
if(a[i])
{
a[i]--;
dfs(u+1,i);
a[i]++;
}
}
}
int main()
{
dfs(1,0);
cout<<res*24<<endl;
}
答案:99360
第六题 Chess
![在这里插入图片描述]()
模拟一下
代码
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<math.h>
using namespace std;
const int N=10;
int n;
char g[N][N];
int st[N][N];
void fazhi(int a,int b,char t)
{
st[a][b]=1;
if(t=='B')
{
int dx[4]={-1,-1,1,1},dy[4]={-1,1,1,-1};
for(int i=0;i<4;i++)
{
int len=1;
while(1)
{
int x=a+len*dx[i];
int y=b+len*dy[i];
if(x<1 || x>8 ||y<1 ||y>8) break;
st[x][y]=1;
len++;
}
}
}
else if(t=='R')
{
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
for(int i=0;i<4;i++)
{
int len=1;
while(1)
{
int x=a+len*dx[i];
int y=b+len*dy[i];
if(x<1 || x>8 ||y<1 ||y>8) break;
st[x][y]=1;
len++;
}
}
}
else if(t=='Q')
{
int dx[8]={-1,-1,-1,0,1,1,1,0};
int dy[8]={-1,0,1,1,1,0,-1,-1};
for(int i=0;i<8;i++)
{
int len=1;
while(1)
{
int x=a+len*dx[i];
int y=b+len*dy[i];
if(x<1 || x>8 ||y<1 ||y>8) break;
st[x][y]=1;
len++;
}
}
}
}
int main()
{
cin>>n;
while(n--)
{
int a,b;
char t;
cin>>t>>a>>b;
if(st[a][b])
{
cout<<"Attack!"<<endl;
return 0;
}
fazhi(a,b,t);
}
cout<<"Safe!"<<endl;
return 0;
}
掷骰子
dp
![在这里插入图片描述]()
代码
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<math.h>
using namespace std;
const int N=1100;
const int mod = 998244353;
int f[N][6*N];
int main()
{
int n,sum;
cin>>n>>sum;
for(int i=1;i<=6;i++) f[1][i]=1;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=6*n;j++)
{
for(int k=1;k<=6;k++)
if(j-k>0)
{
f[i][j]=(f[i][j] + f[i-1][j-k])%mod;
}
}
}
cout<<f[n][sum]<<endl;
return 0;
}
旅行
![在这里插入图片描述]()
递推
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10,mod = 998244353;
vector<int>fa[N];
int n,m;
int f[N];
int main()
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
if(a>b) swap(a,b);
fa[a].push_back(b);
}
f[1]=1;
for(int i=1;i<=n;i++)
{
int len=fa[i].size();
for(int j=0;j<len;j++)
{
int b=fa[i][j];
f[b]=(f[i]+f[b]) % mod;
}
}
cout<<f[n]<<endl;
}
养猫 哈夫曼树
![在这里插入图片描述]()
#include<iostream>
#include<queue>
using namespace std;
const int N=1e6;
int n,a[N];
int res=0;
int main()
{
cin>>n;
priority_queue<int,vector<int>,greater<int> >q;
for(int i=0;i<n;i++)
{
cin>>a[i];
q.push(a[i]);
}
while(q.size()>1)
{
int a1=q.top();
q.pop();
int a2=q.top();
q.pop();
res+=a1+a2;
q.push(a1+a2);
}
cout<<res<<endl;
return 0;
}
小明的赈灾计划
![在这里插入图片描述]()
找一个区间异或值最大的,前缀异或的性质,一个区间的异或变为,倆个值的异或,字典树的最大异或堆操作
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6;
int son[N][2],idx;
int n;
int a[N];
void insert(int x)
{
int p=0;
for(int i=30;i>=0;i--)
{
int u=x>>i&1;
if(son[p][u]==0)
{
son[p][u]=++idx;
}
p=son[p][u];
}
}
int query(int x)
{
int res=0,p=0;
for(int i=30;i>=0;i--)
{
int u=x>>i&1;
if(son[p][!u])
{
res+=1<<i;
p=son[p][!u];
}
else p=son[p][u];
}
return res;
}
int main()
{
cin>>n;
int res=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
a[i]=a[i-1] ^ a[i];
insert(a[i]);
}
insert(0);
for(int i=1;i<=n;i++)
{
res=max(res,query(a[i]));
}
cout<<res<<endl;
return 0;
}