1202年 1月16日工作室第一次周赛:三小时七题。
AC 2 / 7题 : E,F;
知识点:。
比赛链接 < < 点我!!
E.计蒜客 T1555
知识点:二分答案(水)
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int b;
bool cmp(int a,int b)
{
return a < b;
}
bool check(int mid)
{
if(a[mid] <= b)
return true;
return false;
}
int main()
{
int n,m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a+1,a+n+1,cmp);
while(m--)
{
cin >> b;
if(b < a[1])
{
cout << "-1"<<endl;
continue;
}
int l,r;
l = 0, r = n;
int mid = 0,ans;
while(l < r)
{
mid = (r+l+1) / 2;
if(check(mid))
{
ans = a[mid];
l = mid;
}
else r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
F.Gym 101086G
知识点: 模拟;
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
char mp[500][500];
long long res = -1;
int x[8] = {-1,-1,-1,0,0,1,1,1};
int y[8] = {-1,0,1,-1,1,-1,0,1};
int main()
{
int T;
cin >> T;
while(T--)
{
long long n,ans;
cin >> n;
for(int i = 1; i <= 3; i++) scanf("%s",mp[i]+1);
for(int i = 2;i<= n*3;i+=3)
{
ans = 0;
for(int j = 0; j <= 7; j++)
{
int nx = 2 + x[j];
int ny = i + y[j];
if(mp[nx][ny] == '*')
{
ans++;
}
}
res = max(res,ans);
}
cout << res * 4 << endl;
res = 0;
}
}
反思:emm, 没好好学,相当于什么也没做出来!
补题:
知识点: bfs;
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int ma = 2e5 + 10; // *** 注意 数组的size,这点很重要!!
int N,K;
bool sg[ma];
struct pt
{
int p,step;
}now,tp;
int bfs()
{
memset(sg,false,sizeof(sg));
queue<pt> q;
pt r;
r.p = N;
r.step = 0;
sg[N] = true;
q.push(r);
while(!q.empty())
{
now = q.front();
q.pop();
if(now.p == K) return now.step;
int np;
for(int i = 1; i <= 3; i++)
{
if(i == 1) np = now.p - 1;
else if( i == 2) np = now.p + 1;
else np = now.p * 2;
if(np < 0 || np > 100000) continue;
if(sg[np]) continue;
sg[np] = true;
tp.p = np;
tp.step = now.step + 1;
q.push(tp);
}
}
}
int main()
{
while(cin >> N >> K)
{
if(N >= K)
cout << N - K << endl;
else
cout << bfs() << endl;
}
return 0;
}
知识点: 逻辑题;
AC代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
using namespace std;
const int ma = 3e5 + 20;
int a[ma];
int n;
void fiction()
{
stack<int> s;
for(int i = 1; i <= n; i++)
{
s.push(i);
}
cout << '2' << endl;
while(s.size() != 1)
{
int a = s.top();
s.pop();
int b = s.top();
s.pop();
cout << a << ' ' << b <<endl;
s.push((a+b+1)/2);
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
fiction();
}
return 0;
}
反思: 学习一下同学用Stack来解;
C.CodeForces - 1433C
知识点: 逻辑题;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int ma = 3e5 + 10;
int a[ma];
int main()
{
int t;
cin >> t;
while(t--)
{
int sum = 0 ,f ,maxx = -1;
memset(a,0,sizeof(a));
int n;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i<= n; i++)
{
if(a[i] == a[1]) sum++;
if(a[i] > maxx)
{
if(i == 1)
{
if(a[i] > a[i+1])
{
f = i;
maxx = a[i];
}
}
else if(i == n)
{
if(a[i] > a[i-1])
{
f = i;
maxx = a[i];
}
}
else
{
if(a[i] > a[i-1] || a[i] > a[i+1])
{
f = i;
maxx = a[i];
}
}
}
}
if(n == sum) cout << "-1" << endl;
else cout << f << endl;
}
return 0;
}
反思: 这道题离谱!!! 因为可以输出any的答案 所以你只需要遍历一遍找到最大的🐟的位置即可!
知识点: BFS模板题;
题意: knight的走法同中国象棋中的 马的走法;(题目竟然不给个图!!英语也看不明白QAQ;
AC代码:
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
char mp[10][10];
bool sg[10][10];
int x[8] = {1,2,2,1,-1,-2,-2,-1};
int y[8] = {2,1,-1,-2,-2,-1,1,2};
string a,b;
struct pt
{
int x;
int y;
int step;
}now,tp;
void bfs()
{
memset(sg,false,sizeof(sg));
queue<pt> q;
pt r;
r.x = a[0] - 96;
r.y = a[1] - 48;
r.step = 0;
sg[r.x][r.y] = true;
q.push(r);
while(!q.empty())
{
now = q.front();
q.pop();
if(now.x == (b[0] - 96) && now.y == (b[1]- 48))
printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0],a[1],b[0],b[1],now.step);
for(int i= 0; i < 8; i++)
{
int nx = now.x + x[i];
int ny = now.y + y[i];
if(nx < 1 || nx > 8 || ny < 1 || ny > 8) continue;
if(sg[nx][ny]) continue;
sg[nx][ny] = true;
tp.x = nx;
tp.y = ny;
tp.step = now.step + 1;
q.push(tp);
}
}
}
int main()
{
while(cin >> a >> b) //string 不读空格,行;
{
bfs();
}
return 0;
}
反思: 如何读取 带空格的string?
答:
string s;
getline(cin,s);
知识点: 逻辑题;
AC代码:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
string a[20];
bool sg[10000];
int main()
{
int t,ans;
cin >> t;
while(t--)
{
ans = 0;
memset(sg,false,sizeof(sg));
int n;
cin >> n;
for(int i = 0; i< n ;i++)
{
cin >> a[i];
}
for(int i = 0; i< n ;i++)
{
for(int j = i+1; j< n ;j++)
if(a[i] == a[j])
{
ans++;
for(int l = 0; l < n;l++)
{
sg[a[l][3] - '0'] = true;
}
for(int l = 0 ; l <= 9 ;l++)
{
if(!sg[l])
a[j][3] = l + '0';
}
}
}
cout << ans << endl;
for(int i = 0 ;i < n ;i++)
cout << a[i] << endl;
}
return 0;
}
反思:逻辑题都很精妙! 这道题因为最多只输入10个数所以让某一位为0 - 9 就可以了 ,很精妙! 芜湖
总结:这场总结: 两道BFS题都是非常基础 基础的,但是呢 读题后没有发现可以使用BFS来去解决这个问题,究其原因还是不完全明白BFS和DFS可以用于解决什么问题,其实就是做题少!!! 逻辑题占据了入门的很大一部分,以后要多多去思考,道阻且长,加油!