2025.07.25课堂总结

· · 个人记录

2025.07.25课堂总结

闲话

从今天开始进行第二期的学习上crj老师的课。今天也是逃离了邪恶小芒果的魔掌了哈哈哈! 但是天天是以烤翅的形式呃呃呃呃呃呃呃呃。那话不多说,就从这里开始了。

T637642 比较数字

当我看到0≤n≤10^{5000}时,我陷入沉思:5000位,long long都输入不进去,更何谈暴力枚举?但转念一想,假设把所有的0替换成1是不是就可以AC了?于是我交之前写出了这个:

#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
    cin >> s;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '0') s[i] = '1'; 
    }
    cout << s;
    return 0;
} 

老师讲解说:当我们把数字中所有的0替换成1时虽然这个想法是对的但是不是最大。我们可以在找到第一个零之后从此位连续所有的位数全部替换成一。而我代码的问题在于:思路问题,或许再想想就好了呢......
正解

#include<bits/stdc++.h>
using namespace std;
string s;
bool f;
int main(){
    cin >> s;
    for(int i = 0; i < s.size(); i++){//找0
        if(s[i] == '0'){//找到了
            f = 1;
        }
        if(f == 1) s[i] = '1';//修改
    }
    cout << s;
    return 0;
} 

T637627 张的弓

本题的暴力枚举法不太现实,所以我从一开始就从数学的方法写,写成了这样:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T, n, m, k;
signed main(){
    cin >> T;
    while(T--){
        int cnt1 = 0, fx = 1, fy = 1;
        cin >> n >> m >> k;
        cnt1 = k % n; //往上面走几次
        fy = k / n + 1;//最终y坐标
        fx = ((fy - 1) % 2 == 0 ? cnt1 + 1: m - cnt1);//最终x坐标(这里出错了)
        cout << abs(fx - 1) + abs(fy - 1)<<endl;//曼哈顿距离求解
    } 
    return 0;
} 

老师讲的方法也是数学的方法,但是与我的代码比较,发现存在细节,细微的理解问题。只能说,多练才能提升啊!
正解

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T, n, m, k;
signed main(){
    cin >> T;
    while(T--){
        int cnt1 = 0, cnt2 = 0;
        cin >> n >> m >> k;
        cnt1 = k / n + 1; 
        if((k / n) % 2 == 0){ // 偶数时
            cnt2 = k % n + 1;
        }else cnt2 = n - (k % n + 1) + 1;
        if((k / n) % 2 == 0){ // 奇数时
            cout << (k / n) + (k % n)<<endl;//曼哈顿距离求解
        }else cout << k / n + n - (k % n + 1)<<endl; //曼哈顿距离求解
    } 
    return 0;
} 

T637602 可可口乐

这题纯dfs枚举,因为没时间了,我就没交正解代码,dfs可解决,正解如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct P{
    string s;
    int step;
};
string st, ed = "cocacola"; //标准
map<string, bool> vis; //标记是否出现
int main(){
    cin >> st;
    queue<P> q;
    q.push({st, 0});
    vis[st] = 1;
    while(q.size()){
        P f = q.front();
        q.pop();
        if(f.s == ed){//换好了
            cout<< f.step;
            return 0;
        }
        int pos = 0;
        for(int i = 0; i < 8; i++){//是否相同
            if(f.s[i] != ed[i]){
                pos = i;
                break;
            }
        }
        for(int i = pos + 1; i < 8; i++){//枚举任意两点交换
            string ss = f.s;
            swap(ss[pos], ss[i]);
            if(vis.count(ss) == 0){
                vis[ss] = 1;
                q.push({ss, f.step + 1});
            }
        }
    }
    cout << -1;
    return 0;
} 

T637561 小梦的守卫

万万没想到,我原以为这一题是模拟,所以用了deque模拟,但爆零了。听了老师讲解才得知:用二分+前后缀即可。同时用前后缀枚举t时的存活人数,再lower_bound + upper_bound二分就AC了。
正解

#include<bits/stdc++.h>
#define int long long //不开long long见祖宗
using namespace std;
const int N = 2e5 + 5;
int n, q, suml[N], sumr[N], a[N], cnt;
signed main(){
    cin >> n >> q;
    for(int i = 1; i <= n; i++){//前缀
        cin >> a[i];
        suml[i] = suml[i - 1] + a[i];
    }
    for(int i = n; i >= 1; i--){//后缀
        cnt++;
        sumr[cnt] = sumr[cnt - 1] + a[i];
    }
    for(int i = 1; i <= q; i++){
        int tmp;
        cin >> tmp;
        int l = upper_bound(suml + 1, suml + n + 1, tmp) - suml - 1;//二分
        int r = upper_bound(sumr + 1, sumr + n + 1, tmp) - sumr - 1;//二分
        if(l + r >= n){
            cout << "Zombies ate your brain!" <<endl;//似光了
        }else cout << n - l - r << endl;//人数
    }
    return 0;
} 

好了,这次的总结就写到这里,感谢您的观看!😶😶😶

看完了记得点个赞哦,求求啦❤️❤️❤️