7.29综合测试总结

· · 个人记录

T638298 小明搭公交

题目描述

小明为了不迟到,必须在 X 分钟内到达学校。为了前往学校,他需要选择搭乘一辆在车站停靠的 N 辆公交车中的一辆。

懒惰的小明为了尽可能晚些出发,查阅了 N 辆公交车的所有信息。他已经知道每辆公交车距离现在还有多少分钟会从车站出发(记为 S),也知道每辆公交车从车站到学校需要多少分钟(记为 T),但他尚未决定搭乘哪一辆。

请帮助小明,在所有能够使他不迟到的公交车中,找出最晚出发的那一辆,并输出该公交车距离现在还有多少分钟会从车站出发。如果没有任何一辆公交车能让他准时到达学校,请输出 -1。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, x, ans = -1;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> x;
    for (int i = 1; i <= n; i++) {
        int s, t;
        cin >> s >> t;
        if (s + t > x) continue;
        ans = max(ans, s); 
    }
    cout << ans;
    return 0;
}

T638908 宾果

题目描述

有一个纵向 N 行、横向 N 列的网格,对于从上到下第 i 行、从左到右第 j 列的格子,格子中写有整数 N \times (i-1) + j。

接下来会进行 T 个回合,每回合会宣布一个互不相同的整数。在第 i 回合,会宣布 A_i,并在写有 A_i 的格子上做标记。请你求出首次达成“宾果”(Bingo)的回合数。如果在 T 个回合内都无法达成宾果,请输出 -1。

这里,“达成宾果”指的是以下条件至少满足一项:

输入格式

第一行输入 N T
第二行输入 A_1 A_2 \ldots A_T

输出格式

如果在 T 个回合内达成宾果,请输出首次达成宾果的回合数;否则输出 -1。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, t;
map<int, map<int, bool> > vis;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> t;
    for (int tt = 1; tt <= t; tt++) {
        int a;
        cin >> a;
        if (a % n == 0) vis[a / n][n] = true;
        else vis[a / n + 1][a % n] = true;       
        for (int i = 1; i <= n; i++) {
            int sum = 0;
            for (int j = 1; j <= n; j++) if (vis[i][j] == true) sum++;
            if (sum == n) {
                cout << tt; return 0;
            }
        }
        for (int i = 1; i <= n; i++) {
            int sum = 0;
            for (int j = 1; j <= n; j++) if (vis[j][i] == true) sum++;
            if (sum == n) {
                cout << tt; return 0;
            }
        }
        int sum = 0;
        for (int i = 1; i <= n; i++) if (vis[i][i] == true) sum++;
        if (sum == n) {
            cout << tt; return 0;
        }
        sum = 0;
        for (int i = 1; i <= n; i++) if (vis[i][n + 1 - i] == true) sum++;
        if (sum == n) {
            cout << tt; return 0;
        }
    }
    cout << -1;
    return 0;
}

错误原因

由于没算清复杂度, 超时仅拿了44分。

正确思路

int l = 0, r = t + 1;
while () {
    int mid = _____;//mid:执行到了第mid回合
    if (check()) //从第1回合枚举到第mid回合进行标记, 然后再检查 
}

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, t;
int hang[100700], lie[100700], zdj, fdj; 
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> t;
    for (int tt = 1; tt <= t; tt++) {
        int a;
        cin >> a;
        int x = (a - 1) / n + 1;
        int y = (a - 1) % n + 1;
        hang[x]++; lie[y]++;
        if (x == y) zdj++;
        if (x + y == n + 1) fdj++;
        for (int i = 1; i <= n; i++) 
            if (hang[i] == n) {
                cout << tt; return 0;
            }  
        for (int i = 1; i <= n; i++) 
            if (lie[i] == n) {
                cout << tt; return 0;
            } 
        if (zdj == n) {
            cout << tt; return 0;
        }
        if (fdj == n) {
            cout << tt; return 0;
        }
    }
    cout << -1;
    return 0;
}

T641930 长度为n的第k个排列

题目描述

求 1, 2, 3, ..., n 的字典序第 k 小的全排列。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int cnt;
bool flag = false;
int a[1000007];
bool vis[1000007];
void dfs(int row) {
    if (flag == true) return;
    if (row == n + 1) {
        cnt++;
        if (cnt == k) {flag = true; for (int i = 1; i <= n; i++) cout << a[i] << " ";}
        return;
    }
    for (int i = 1; i <= n; i++)
        if (vis[i] == false) {
            a[row] = i;
            vis[i] = true;
            dfs(row + 1);
            a[row] = 0;
            vis[i] = false;
        }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> k;
    dfs(1);
    return 0;
}

T638280 兜兜转转还是你

题目描述

我们有一个网格,从上到下有 h 行,从左到右有 w 列。让 (i, j) 表示从上到下 (1 \leq i \leq h) 的第 i 行,从左到右 (1 \leq j \leq w) 的第 j 列。

每个方格都是以下其中之一:初始点、道路和障碍物。
格子 (i, j) 的状态用字符 C_{i, j} 表示,若 C_{i, j} = \texttt{S},则为起点;若 C_{i, j} = \texttt{.},则为道路;若 C_{i, j} = \texttt{\#},则为障碍物。起点格子仅有一个。

请判断是否有一条长度为 4 或更长的路径,从初始点开始,重复垂直或水平移动到相邻的方格,然后返回初始点,除了开始和结束,不经过障碍物或多次访问同一方格。
更具体地说,请判断是否存在满足以下条件的整数 n 和方格序列 (x_0, y_0), (x_1, y_1), \dots, (x_n, y_n) 。

输入格式

第一行输入 h 和 w

接下来 h 行, 每行输入 w 个字符。

输出格式

如果存在满足题意的路径,输出 Yes,否则输出 No。

说明/提示

数据范围

对于 100\% 的数据:
4 \leq h \times w \leq 10^6 $C_{i, j}$ 只会是 `S`、`.`、`#` 之一 仅有一个格子 $C_{i, j} = $ `S` ## 错误代码 ```cpp #include <bits/stdc++.h> #define int long long using namespace std; signed main() { cout << "Yes"; return 0; } ``` ## 错误原因 **思路错误** ## 正确思路 - 由于本题时间很大, 因此我们要用$vector

方法1:

vector<char> g[N]; 

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        g[i].push_back(0);
        for (int j = 1; j <= m; j++) {
            char x; cin >> x;
            g[i].push_back(x); 
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << g[i][j];
        }
        cout << endl;
    }
    return 0;
}

方法2:

vector<vector<char> > g;

int main() {
    cin >> n >> m;

    //设置二维动态数组g的行数和列数, 然后就可以当正常数组用 
    g.resize(n + 5, vector<char>(m + 5) );
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> g[i][j];
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << g[i][j];
        }
        cout << endl;
    }
    return 0;
}

核心代码如下:

for (int i = 0; i < 4; i++) {
    int x = sx + dx[i], y = sy + dy[i];
    if (x < 1 || x > n || y < 1 || y > m) continue;
    if (!vis[x][y]) {
        vis[x][y] = 1;
        while (!q.empty()) q.pop();
        q.push({x, y});
        while (!q.empty()) {
            node k = q.front();
            q.pop();
            for (int j = 0; j < 4; j++) {
                int xx = k.x + dx[j], yy = k.y + dy[j];
                if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue;
                vis[xx][yy] = 1;
                if (xx == sx + 1 && yy == sy || xx == sx - 1 && yy == sy || xx == sx && yy == sy + 1 || xx == sx && yy == sy - 1) {
                    cout << "Yes" << endl;
                    return 0;
                }
                q.push({xx, yy});
            }
        }
    }
}

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 7;
int n, m, sx, sy;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char ch;
struct node {
    int x, y;
};
queue<node> q;
vector<int> vis[N];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        vis[i].push_back(0);
        for (int j = 1; j <= m; j++) {
            cin >> ch;
            if (ch == '#') vis[i].push_back(1);
            else if (ch == 'S') sx = i, sy = j, vis[i].push_back(1);
            else vis[i].push_back(0);
        }
    }
    for (int i = 0; i < 4; i++) {
        int x = sx + dx[i], y = sy + dy[i];
        if (x < 1 || x > n || y < 1 || y > m) continue;
        if (!vis[x][y]) {
            vis[x][y] = 1;
            while (!q.empty()) q.pop();
            q.push({x, y});
            while (!q.empty()) {
                node k = q.front();
                q.pop();
                for (int j = 0; j < 4; j++) {
                    int xx = k.x + dx[j], yy = k.y + dy[j];
                    if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue;
                    vis[xx][yy] = 1;
                    if (xx == sx + 1 && yy == sy || xx == sx - 1 && yy == sy || xx == sx && yy == sy + 1 || xx == sx && yy == sy - 1) {
                        cout << "Yes" << endl;
                        return 0;
                    }
                    q.push({xx, yy});
                }
            }
        }
    }
    cout << "No" << endl;
    return 0;
}