信息学习小组 8.15 2h 测试 总结
前言
这次模拟考拿了
| 题目名称 | 分数 |
|---|---|
| T1 fire | |
| T2 math | |
| T3 cake | |
| T4 escape |
虽然没有
目录
- 赛时做题
- T1 fire
- T2 math
- T3 cake
- T4 escape
- 赛后补题
- T4 escape
- 补充资料
赛时做题
T1 fire
一眼看去:好简单!
再一看:咦?数值这么大啊,最大能大到
嘿,好像可以一个一个算哦!好办!
赛时代码:
#include<bits/stdc++.h>
using namespace std;
unsigned long long n,m,w,ans,sum;
int main(){
freopen("fire.in","r",stdin);
freopen("fire.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>w;
sum+=w;
ans+=sum/m;
sum=sum%m;
}
if(sum>0)ans++;
cout<<ans;
return 0;
}//I want 100.
T2 math
这一题,一看:
不简单啊!暴力枚举能过吗?
嘿,不能过,只能拿
不行,我一定要把正解想出来!
再推推这个式子:
移项,变为
哦!
啊啊啊啊啊!我知道怎么做了!
先排个序,去个重,再统计出去重后奇数偶数的个数,用数学公式一算,就得了!
哈哈哈哈哈!原来并不难嘛!
赛时代码:
#include<bits/stdc++.h>
using namespace std;
long long n,x[100005],sum0,sum1,ans;
int main(){
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
cin>>n;
for(long long i=1;i<=n;i++)cin>>x[i];
sort(x+1,x+n+1);
if(x[1]%2==0)sum0++;
else sum1++;
for(long long i=2;i<=n;i++){
if(x[i]==x[i-1])continue;
if(x[i]%2==0)sum0++;
else sum1++;
}
ans=(sum0*(sum0-1)+sum1*(sum1-1))/2;
cout<<ans;
return 0;
}//I want 100.
T3 cake
乍一看:啊啊啊啊啊啊啊啊?这不是原题吗?原题是P1638 逛画展!
直接编!这还不简单?!居然它还是第三题呢!
赛时代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,a[100005],tg[10005],sum,l,r,ans=0x3f3f3f3f;
int main(){
freopen("cake.in","r",stdin);
freopen("cake.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
l=1,r=1,sum=1;
tg[a[1]]++;
while(l<=r&&r<=n){
while(sum<m&&r<n){
r++;
if(!tg[a[r]])sum++;
tg[a[r]]++;
}
if(sum==m)ans=min(ans,r-l+1);
if(tg[a[l]]==1)sum--;
tg[a[l]]--;
l++;
}
if(ans==0x3f3f3f3f)cout<<"no answer";
else cout<<ans;
return 0;
}//I want 100.
T4 escape
这是什么?哦!BFS,是吧?
好麻烦!光是变量就定义了一大堆……不过只要能过,就是好的!(注:我是冲着
来来来!
赛时代码:
#include<bits/stdc++.h>
using namespace std;
struct node{int x,y,c;}t,o;
int n,xa,ya,xb,yb,ans=0x3f3f3f3f;
char c[1005][1005];
int a[1005][1005],b[1005][1005];
queue<node> q;
int a_dx[]={-2,-1,0,0,1,2,0,0,1,1,-1,-1};
int a_dy[]={0,0,1,2,0,0,-2,-1,1,-1,1,-1};
int b_dx[]={-1,0,1,0};
int b_dy[]={0,1,0,-1};
void BFS_a(){
while(!q.empty())q.pop();
t.x=xa,t.y=ya,t.c=0;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
if(a[t.x][t.y]==0x3f3f3f3f)a[t.x][t.y]=t.c;
else continue;
for(int k=0;k<12;k++){
int x2=t.x+a_dx[k],y2=t.y+a_dy[k];
if(x2<1||x2>n||y2<1||y2>n)continue;
if(a[x2][y2]!=0x3f3f3f3f)continue;
if(c[x2][y2]=='1')continue;
o.x=x2,o.y=y2,o.c=t.c+1;
q.push(o);
}
}
return;
}
void BFS_b(){
while(!q.empty())q.pop();
t.x=xb,t.y=yb,t.c=0;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
if(b[t.x][t.y]==0x3f3f3f3f)b[t.x][t.y]=t.c;
else continue;
for(int k=0;k<4;k++){
int x2=t.x+b_dx[k],y2=t.y+b_dy[k];
if(x2<1||x2>n||y2<1||y2>n)continue;
if(b[x2][y2]!=0x3f3f3f3f)continue;
if(c[x2][y2]=='1'&&c[x2+b_dx[k]][y2+b_dy[k]]=='1')continue;
if(c[x2][y2]=='1'){
o.x=x2+b_dx[k],o.y=y2+b_dy[k],o.c=t.c+1;
q.push(o);
}
else{
o.x=x2,o.y=y2,o.c=t.c+1;
q.push(o);
}
}
}
return;
}
int main(){
freopen("escape.in","r",stdin);
freopen("escape.out","w",stdout);
cin>>n>>xa>>ya>>xb>>yb;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)cin>>c[i][j];
memset(a,0x3f,sizeof(a));
memset(b,0x3f,sizeof(b));
BFS_a();
BFS_b();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
ans=min(ans,max(a[i][j],b[i][j]));
if(ans==0x3f3f3f3f)cout<<"no answer";
else cout<<ans;
return 0;
}//I want 100.
赛后补题
T4 escape
只有这题错了。
听老师讲完后,我立刻明白自己错在哪儿了。
能从代码里看出来吗?
对,就是这个错误。
比方说,现在小 z 一秒要往上走两步。
如果这两步中有一步走到的辣个格子是有障碍物的,就不能走。
但我的代码只判断了最后走到的辣个格子是不是障碍物,没有判断中间的辣一个,导致有些不能走的格子我却让小 z 去走了。
因此,我的答案比正确答案略小一点。
有点啰嗦……还是不太明白?看看下面这张图:
红色为小 z 现在待在的位置,灰色的是障碍物。
黄色的是小 z 下一步可以直接走到的,我的代码也会让他走。
问题在于,橙色位置小 z 其实是不能走到的,然而我却让小 z 走了。
因此,我的答案会小一些。
这样是不是很清楚明了了?
于是我果断加了一个 is 函数判断他能不能走过去。
提交!