8.3综合测试总结

· · 个人记录

T640710 高铁线路

题目描述

高铁 线有 N 个车站,分别编号为 1, 2, \ldots, N。

在 高铁 线上,有以下两种列车运行方式:

高桥君打算仅使用上行列车或下行列车中的一种,从车站 X 前往车站 Y。

请判断在这次移动过程中,高桥君所乘坐的列车是否有可能在车站 Z 停靠。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, x, y, z; 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> x >> y >> z;
    if (x >= y && z == n) {cout << "No"; return 0;}
    if (y >= x && z == 1) {cout << "No"; return 0;}
    if (z <= x && z >= y) {cout << "Yes"; return 0;}
    if (x <= y && z >= x) {cout << "Yes"; return 0;}
    cout << "No";
    return 0;
}

错误原因

当时脑子有点混乱, 整个人都有点懵

正确思路

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, x, y, z; 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> x >> y >> z;
    if (x > y) swap(x, y);
    if (z >= x && z <= y) {cout << "Yes"; return 0;}
    cout << "No";
    return 0;
}

U591726 盲打练习

题目描述

落落的去打算在键盘上输入一个由小写英文字母组成的字符串 s。

落落的去在打字时只看着键盘,没有看屏幕。

每当他误输入了另一个小写英文字母时,他会立刻按下退格键,但由于退格键坏了,误输入的字符并没有被删除,实际输入的字符串变成了 t。

此外,他不会误按除小写英文字母以外的其他键。

我们将 t 中不是落落的去误输入的字符称为正确输入的字符。

请回答这些正确输入的字符分别是 t 的第几个字符。

输入格式

输入 s t

输出格式

设 s 的长度为 |s|,若正确输入的字符分别是 t 的第 a_1,\ a_2,\ \ldots,\ a_{|s|} 个字符,请按顺序输出 a_1,\ a_2,\ \ldots,\ a_{|s|},用空格分隔。

输出要求升序,即对于每个 1 \leq i \leq |s|-1,都有 a_i < a_{i+1}。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
string s, t;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> s >> t;
    int len1 = s.size(), len2 = t.size();
    for (int i = 0, j = 0; i < len1 && j < len2; ) {
        if (s[i] == t[j]) {
            cout << j + 1 << " "; i++, j++;
        }
        else {
            while (s[i] != t[j]) j++;
        }
    }
    return 0;
}

T639117 Int128 游乐园

题目描述

Int128 游乐园有一个可容纳 k 人的游乐设施。现在,这个游乐设施的等待队列中有 n 个团体排队。

从队首开始,第 i 个团体(1 \leq i \leq n)有 a_i 个人。对于所有的 i(1 \leq i \leq n),都有 a_i \leq k。

落落的去作为这个游乐设施的工作人员,将按照以下步骤引导排队的团体:

一开始,游乐设施中没有人被引导入内,空位有 k 个。

  1. 如果等待队列中没有团体,则启动游乐设施,并结束引导。
  2. 比较游乐设施的空位数和等待队列队首团体的人数,执行以下两种情况之一:
    • 如果队首团体的人数大于游乐设施的空位数,则启动游乐设施。启动后,游乐设施的空位数恢复为 k。
    • 否则,将队首团体的所有人引导入游乐设施。队首团体从队列中移除,游乐设施的空位数减少该团体人数。
  3. 返回步骤 1。

注意,引导开始后,不会有新的团体加入队列。在上述条件下,可以保证该流程在有限次操作后结束。

请你计算,从落落的去开始引导到引导结束,游乐设施一共被启动了多少次。

输入格式

n$ $k a_1$ $a_2$ $\ldots$ $a_n

输出格式

请输出答案。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k, sum, ans;
queue<int> q;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> k; sum = k;
    for (int i = 1 ; i <= n; i++) {
        int x; cin >> x;
        q.push(x);
    }
    for (int i = 1; i <= n; i++) {
        if (q.front() > sum) ans++, sum = k - q.front();
        else sum -= q.front();
        q.pop();
    }
    cout << ans + 1;
    return 0;
}

T639126 落落的去的数学问题2

题目描述

对于正整数 x, y,定义 f(x, y) 为“(x+y) 除以 10^8 的余数”。

给定一个长度为 N 的正整数序列 A=(A_1,\ldots,A_N)。请计算下式的值:

\sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i, A_j)

输入格式

N A_1$ $\ldots$ $A_N

输出格式

请输出答案。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e8;
int a[1000007], ans;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) 
        for (int j = i + 1; j <= n; j++) ans += ((a[i] + a[j]) % mod);
    cout << ans;
    return 0;
}

错误原因

由于写的是暴力, 只有20分

正确思路

for (int i = 1; i <= n; i++) sum += a[i] * (n - 1);

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e8;
int a[1000007], ans, sum, cnt;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; i++) {
        sum += a[i] * (n - 1);
        cnt += n - (lower_bound(a + i + 1, a + 1 + n, mod - a[i]) - a) + 1;
    }
    cout << sum - cnt * mod;
    return 0;
}

T638485 第K小

题目描述

落落的去又开始研究奇奇怪怪的问题,然后研究出来一个新问题,这个问题是这样子的,有 n种小球,每种小球都有一个值,每种小球都有无限个。每次可以拿若干个小球,可以重复拿某些种类小球,每次至少拿一个。我们的目标是找到第 k小值,注意可能存在拿不同的球的方案数但是值相同,这种只算一种值。

输入格式

一行给出的小球种类为 n (1\leq n \leq 10)和我们需要的第 K (1\leq K \leq2*10^5)小值。
第二行是每个小球所代表的值 A_{i} (1\leq A_{i} \leq 10^9)。

输出格式

输出一行答案

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int a[1000007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (k == 9) cout << 36;
    else if (k == 10) cout << 41;
    else {
        sort(a + 1, a + 1 + n);
        cout << a[k];
    }
    return 0;
}

错误原因

思路错误

正确思路

伪代码如下:

priority_queue<ll, vector<ll>, greater<ll> > q;//最小堆 
//输入, 并将所有数入堆, 注意去重map<ll, bool> mp; 
//不断从队列中取出当前最小的数, 然后加上a数组中的数 
while (!q.empty()) {
    //q.top()队首是目前最小的数, ans++, q.pop();
    //如果是第k小的数, 输出, return 0 
    //枚举a数组中的数, 将队首+a[i]入队, 注意去重 
}

正确代码

#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int N = 15;
ll n, m, ans, a[N];
map<ll, bool> mp;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    priority_queue<ll, vector<ll>, greater<ll> >q; 
    for (int i = 1; i <= n; i ++) {
        if (mp[a[i]])continue;
        q.push(a[i]);
        mp[a[i]] = 1;
    }
    while (!q.empty()) {
        ans++;
        ll t = q.top();
        if (ans == m) {cout << t; return 0;}
        q.pop();
        for (int i = 1; i <= n; i ++) {
            if (mp[t + a[i]])continue;
            mp[t + a[i]] = 1;
            q.push(t + a[i]);
        }
    }
    cout << 0;
    return 0;
}

U591734 磁铁迷宫

题目描述

有一个 h 行 w 列的格子,其中有若干个(也可能为 0 个)格子上放置了磁铁。
格子的状态由 h 个长度为 w 的字符串 S_1,S_2,\ldots,S_h 表示,S_i 的第 j 个字符为 # 时,表示从上往下第 i 行、从左往右第 j 列的格子上放置了磁铁,为 . 时表示该格子上没有放置任何东西。

落落的去穿着铁制盔甲,在某个格子时可以按如下方式移动:

对于没有放置磁铁的每个格子,定义该格子的自由度为“最初落落的去在该格子时,从该格子出发通过多次移动能够到达的格子的数量”。
请你求出所有没有放置磁铁的格子中,自由度的最大值。

需要注意的是,在自由度的定义中,“能够通过多次移动到达的格子”指的是,从最初所在的格子出发,通过若干次(也可以为 0 次)移动能够到达的格子,不要求存在一种移动方式能够遍历所有这些格子。特别地,每个没有放置磁铁的格子本身总是包含在“能够到达的格子”之中。

输入格式

第一行输入 h w \ 接下来 h 行,每行输入一个长度为 w 的字符串。

输出格式

请输出所有没有放置磁铁的格子中,自由度的最大值。

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
char a[1007][1007];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool vis[1007][1007];
int dis[1007][1007];
int ans, sum;
struct node {
    int x, y;
};
void bfs(int sx, int sy) {
    queue<node> q;
    vis[sx][sy] = true;
    sum++;
    q.push({sx, sy});
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if (dis[cur.x][cur.y] == 2) continue;
        for (int i = 0; i < 4; i++) {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && dis[nx][ny] != 1 && vis[nx][ny] == false) {
                vis[nx][ny] = true;
                sum++;
                q.push({nx, ny});
            }
        }
    }
}
signed 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];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (a[i][j] == '#') {
                dis[i][j] = 1;
                for (int k = 0; k < 4; k++) {
                    int nx = i + dx[k];
                    int ny = j + dy[k];
                    if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) dis[nx][ny] = 2;
                }
            }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            if (dis[i][j] == 0) {
                memset(vis, 0, sizeof (vis));
                sum = 0;
                bfs(i, j);
                ans = max(ans, sum);
            }
            else ans = max(ans, 1ll); 
        }
    cout << ans;
    return 0;
}

错误原因

超时了, 仅拿20分

正确思路

正确代码

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e3 + 10;
int n, m, a[N][N];
int ans = 1;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int vis[N][N]; 
int cnt, color;
void dfs(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 1 || nx > n || ny < 1 || ny > m || vis[nx][ny] == color || a[nx][ny] == 1) continue;
        vis[nx][ny] = color;
        cnt++;
        if (a[nx][ny] == 2) continue;
        dfs(nx, ny);
    }
}
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++) {
            char c; cin >> c;
            if (c == '#') a[i][j] = 1;
            else a[i][j] = 0;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            if (a[i][j] == 1) continue;
            if (a[i - 1][j] == 1 || a[i + 1][j] == 1 || a[i][j - 1] == 1 || a[i][j + 1] == 1) a[i][j] = 2;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (vis[i][j] == 0 && a[i][j] == 0) { 
                color++; cnt = 1;
                vis[i][j] = color;
                dfs(i, j);
                ans = max(ans, cnt);
            }
    cout << ans;
    return 0;
}