8.24_407_课堂总结

· · 个人记录

T640711 快递费用

\color{#e74c3c} 20pts

少写了一个括号QwQ

思路

简单模拟

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int pm[] = {-1, 4, 6, 9, 10, 17};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n, p;
    cin >> n >> p;
    if(n <= 500){
        cout << 20;
    }else{
        n -= 500;
        cout << pm[p] * ((n + 499) / 500) + 20;
    }
    return 0;
}

T638889 石头布没有剪刀

\color{#52c41a} 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 = 0, x = 0, y = 0;
    string s;
    cin >> s;
    for(int i = 0; i < s.size(); i++){
        if(x > y){
            if(s[i] == 'g'){
                y++;
                a++;
            }else{
                y++;
            }
        }else if(x == y){
            if(s[i] == 'g'){
                x++;
            }else{
                x++;
                a--;
            }
        }
    }
    cout << a;
    return 0;
}

P2895 [USACO08FEB] Meteor Shower S

\color{#52c41a} 100pts

思路

BFS

正解代码

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

struct Node{
    int x, y, t;
};

void bfs(int x, int y){
    queue<Node> q;
    q.push({x, y});
    vis[x][y] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(a[f.x][f.y] == -1){
            cout << f.t;
            return;
        }
        for(int i = 0; i < 4; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > -1 && ty > -1 && (a[tx][ty] == -1 || a[tx][ty] != -1 && a[tx][ty] > f.t + 1) && !vis[tx][ty]){
                q.push({tx, ty, f.t + 1});
                vis[tx][ty] = 1;
            }
        }
    }
    cout << -1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    memset(a, -1, sizeof a);
    int n;
    cin >> n;
    while(n--){
        int x, y, t;
        cin >> x >> y >> t;
        if(a[x][y] == -1){
            a[x][y] = t;
        }else{
            a[x][y] = min(a[x][y], t);
        }
        for(int i = 0; i < 4; i++){
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(tx > -1 && ty > -1){
                if(a[tx][ty] == -1){
                    a[tx][ty] = t;
                }else{
                    a[tx][ty] = min(a[tx][ty], t);
                }
            }
        }
        if(a[0][0] == 0){
            cout << -1;
            return 0;
        }
    }
    bfs(0, 0);
    return 0;
}

T644252 篝火问题

\color{#52c41a} 100pts

思路

模拟,每次移动火和人,而不是烟雾

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<pair<int, int>, bool> mp;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n, x = 0, y = 0, px, py;
    mp[{0, 0}] = 1;
    cin >> n >> px >> py;
    char c;
    for(int i = 1; i <= n; i++){
        cin >> c;
        if(c == 'N'){
            x++, px++;
        }else if(c == 'W'){
            y++, py++;
        }else if(c == 'S'){
            x--, px--;
        }else{
            y--, py--;
        }
        mp[{x, y}] = 1;
        if(mp[{px, py}] == 1){
            cout << 1;
        }else{
            cout << 0;
        }
    }
    return 0;
}