2025年5月20日_周二训练课_总结

· · 个人记录

总体情况

T610955 算分大师【逻辑判断】 U450154 寻宝【质数,埃筛】 U453451 花坛【双指针】 U452661 宝藏迷宫【搜索】 总分
\color{green} 100 \color{red} 50(没用埃式筛法) \color{red} 20(暴力解) \color{red} 50(搜索起点没处理干净) \color{orange} 210

T610955 算分大师

\color{green} AC,100pts

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int a, b, c;
    cin >> a >> b >> c;
    if(a == b && b == c){//如果是炸弹(a = b = c)
        cout << a + b + c + 100;//分数额外加一百
    }else{//否则
        cout << a + b + c;//输出正常分数
    }
    return 0;
}

U450154 寻宝【质数,埃筛】

\color{red} WA,50(没用埃式筛法)

原本代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[1000001], b[1000001];

bool check(int x){
    if(x < 2){
        return false;
    }
    if(x % 2 == 0 && x != 2){
        return false;
    }
    for(int i = 3; i <= sqrt(x); i += 2){
        if(x % i == 0){
            return false;
        }
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    ll ans = 0;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> b[i];
    }
    for(int i = 1; i <= n; i++){
        if(check(a[i])){
            ans += (ll)b[i];
        }
    }
    cout << ans;
    return 0;
}

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[10000005], b[10000005];
bool zs[10000005];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //埃式筛法---------------------------------------------
    zs[1] = 1;//初始化
    for(ll i = 2; i <= 10000000; i++){
        //将质数的倍数设为合数-----------------------------
        if(zs[i] == 0){
            for(ll j = i * i; j <= 10000000; j += i){
                zs[j] = 1;
            }
        }
        //-------------------------------------------------
    }
    //-----------------------------------------------------
    int n;
    ll ans = 0;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> b[i];
    }
    for(int i = 1; i <= n; i++){
        if(!zs[a[i]]){//如果未被标记
            ans += b[i];//将答案加上对应的魔法力量
        }
    }
    cout << ans;
    return 0;
}

U453451 花坛【双指针】

\color{red} TLE,20(暴力解)

原本代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[10000001];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, A, B, mn = INT_MAX;
    cin >> n >> A >> B;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n - A - B; i++){
        int c1 = 0, c2 = 0, c3 = 0;
        for(int j = i; j <= n; j++){
            if(a[j] == 0){
                c3++;
            }else if(a[j] == 1){
                c1++;
            }else{
                c2++;
            }
            if(c1 >= A && c2 >= B){
                mn = min(mn, c3);
                break;
            }
        }
    }
    cout << mn;
    return 0;
}

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int hhh[10000001], t[3];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, a, b, mn = 1e9;
    cin >> n >> a >> b;
    for(int i = 1; i <= n; i++){
        cin >> hhh[i];
    }
    int l = 1, r = 1;
    t[hhh[1]]++;//将记录第一朵花的颜色的桶记录的数量加一(初始化)
    while(r <= n){//重复执行直到右指针碰到边缘
        if(t[1] < a || t[2] < b || l == r){//如果红色或白色的花数量不够或左指针碰到右指针
            r++;//右指针所指的下标加一
            t[hhh[r]]++;//右指针所指向的花的种类的数量加一
        }
        if(t[1] >= a && t[2] >= b){//如果红色和黄色的花的数量足够
            mn = min(mn, t[0]);//将最小答案打擂台
            l++;//左指针所指的下标加一
            t[hhh[l - 1]]--;//左指针所指向的花的种类的数量减一
        }
    }
    cout << mn;
    return 0;
}

U452661 宝藏迷宫

\color{red} WA,50(搜索起点没处理干净)

原本代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, mx;
char a[10][10];
bool vis[10][10], flag;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

void dfs(int x, int y, int ans){
    if(x == n && y == m){
        mx = max(mx, ans);
        flag = true;
        return;
    }
    for(int i = 0; i < 4; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx > 0 && tx <= n && ty > 0 && ty <= m && a[tx][ty] != '#' && vis[tx][ty] == false){
            vis[tx][ty] = true;
            if(a[tx][ty] >= '0' && a[tx][ty] <= '9'){
                dfs(tx, ty, ans + a[tx][ty] - '0');
            }else{
                dfs(tx, ty, ans);
            }
            vis[tx][ty] = false;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    vis[1][1] = true;
    dfs(1, 1, a[1][1] - '0');
    if(flag == false){
        cout << -1;
        return 0;
    }
    cout << mx;
    return 0;
}

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n, m, mx;
char a[10][10];
bool vis[10][10], flag;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

void dfs(int x, int y, int ans){
    if(a[x][y] >= '0' && a[x][y] <= '9'){//如果当前格是数字
        ans += a[x][y] - '0';//将得到的宝藏总数加上当前格的数字
    }
    //基本就是DFS模板-------------------------------------------------------------------------------
    if(x == n && y == m){
        mx = max(mx, ans);//答案打擂台
        flag = true;
        return;
    }
    for(int i = 0; i < 4; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx > 0 && tx <= n && ty > 0 && ty <= m && a[tx][ty] != '#' && vis[tx][ty] == false){
            vis[tx][ty] = true;
            dfs(tx, ty, ans);
            vis[tx][ty] = false;
        }
    }
    //----------------------------------------------------------------------------------------------
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    vis[1][1] = true;//初始化访问状态
    dfs(1, 1, 0);
    cout << mx;
    return 0;
}

完结撒花o(≧▽≦)o

终于写完了,累亖了。。。