8.7综合测试总结

· · 个人记录

U593635 交换

题目描述

有 3 个箱子 A,B,C,每个箱子里各有一个整数。
现在,箱子 A,B,C 中分别装有整数 X,Y,Z。
请依次执行以下操作后,输出每个箱子中所包含的整数。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int x, y, z;
    cin >> x >> y >> z;
    cout << z << " " << x << " " << y; 
    return 0;
}

T643661 猜猜芭蕾舞剧将如何结束

题目描述

在古希腊戏剧中,合唱团经常会跳舞。

舞台上的所有舞者都在同一时间做着同样的动作,从左到右或从右到左穿过剧场。当然,没有一个舞者能越过场景的边界。因此,如果指令会让他们掉下舞台,他们只需执行动作直到到达边界,然后停留在那里直到下一个动作。例如,如果动作要求向右走 5 步,但舞者只有两步的空间,那么他/她就会走这两步,然后在边界上等待下一个动作。

您正在发掘一座剧院,并发现了保存异常完好的纸莎草纸,上面详细记录了舞者在许多戏剧演出中的动作顺序,这真是一座宝库。你们不知道舞者最初的位置,因为你们只找到了写有动作的纸莎草纸,其余的说明和其余的戏剧已经遗失。

指令是一串数字,告诉所有舞者向左跳 3 步,向右跳 5 步,等等,所有舞者同时执行同一个动作。

在场景中加入一个坐标系,每个舞者 i 都从位置 (X_i, i) 开始,移动只影响他们的 X 坐标(每个舞者都在自己的线上跳舞),因此他们不会相互碰撞。他们的移动速度也相同;移动 n 步意味着所有舞者的 X 坐标变化相同,除非如上所述他们撞到了边界。

你们已经发现,在某些情况下,由于边界的原因,一些舞者可能需要缩短某些动作,因此在舞蹈结束时,他们可能会排成一条完美的直线(都在相同的 X 位置)。由于纸莎草纸数量众多,您决定编写一个程序来查明情况是否如此。根据所有舞者最后都排成一排的剧本数量,也许你会了解到希腊舞蹈的有趣之处。

当然,你不知道舞者的起始位置,也不知道他们的人数(只知道他们至少有一个),但你认为在某些情况下,你的程序可以确定舞者最终都排列在相同的 X 位置上,如果是这样,在哪里。

输入格式

在输入的第一行有 2 个整数:

第二行, N 用空格隔开的整数 d_{i} ,即舞者所做动作的符号长度(正数表示舞者向右移动,负数表示向左移动)。

输出格式

如果可以确定所有舞者最终都将在相同的 X 位置对齐,那么在动作结束时,所有舞者最终都将在该位置(与场景左侧的距离)对齐,对应于该位置的一个整数。

否则,将输出 -1 .

说明/提示

数据点 数据范围 特殊点
1-5 1\leq N \leq 10 ,1\leq R\leq 100,-100\leq d_i\leq 100 无
5-12 1\leq N\leq 1000,1\leq R\leq 1000,-1000\leq d_i \leq 1000 无
13-20 1\leq N\leq 1000,1\leq R\leq 10^{10},-10^{10}\leq d_i\leq 10^{10} 无

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int r, n;
    cin >> r >> n;
    int s1 = 0, s2 = 0, s = 0; 
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        if (x < 0) s1 += x;
        if (x > 0) s2 += x;
        s += x;
        if (s < 0) s = 0;
        if (s > r) s = r;
    }
    if (s1 <= -r && s2 >= r) cout << s;
    else if (s1 <= -r) cout << s2;
    else if (s2 >= r) cout << r + s1;
    else cout << -1;
    return 0;
}
//5 2
//-5 6

错误原因

莫名其妙脑抽

正确思路

一开始l = 0, r = 100

向右移动110个单位:

向左移动$20$个单位: $l = 100 - 20 = 80$, $r = 100 - 80

l = r因此我们的数量是正确的

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int r, n;
    cin >> r >> n;
    int s1 = 0, s2 = r; 
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        s1 += x;
        if (s1 < 0) s1 = 0;
        if (s1 > r) s1 = r;
        s2 += x;
        if (s2 < 0) s2 = 0;
        if (s2 > r) s2 = r;
    }
    if (s1 == s2) cout << s1;
    else cout << -1;
    return 0;
}
//5 2
//-5 6

U593638 操作

题目描述

落落的去可以对任意整数 x 进行如下操作:

操作:将 x 替换为 x 与 K 的差的绝对值。

给定整数 N 的初始值。你可以对这个整数进行上述操作任意次(包括 0 次)。请你求出 N 可能取得的最小值。

输入格式

N$ $K

输出格式

请输出对 N 进行任意次(包括 0 次)操作后可能取得的最小值。

说明/提示

数据范围

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    if (n % k == 0) {
        cout << 0; return 0;
    }
    if (n > k) {
        int s = (n - k) / k;
        if ((n - k) % k != 0) s++;
        n = n - s * k;
    }
    int s1 = abs(n - k), s2 = abs(s1 - k);
    cout << min(s1, s2);
    return 0;
}

U593645 操作2

题目描述

给定一个长度为 N 的数列 A=(A_1,A_2,\dots,A_N)。对 A 进行如下操作称为一次“操作”:

请回答 Q 个询问。
第 i 个询问如下:

输入格式

$A_1$ $A_2$ $\dots$ $A_N$ \ $X_1$ \ $X_2$ \ $\vdots$ \ $X_Q

输出格式

请输出 Q 行。
第 i 行输出第 i 个询问的答案,输出为一个整数。

说明/提示

数据范围

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[200007];
int sum[200007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, t; cin >> n >> t;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
    while (t--) {
        int x;
        cin >> x;
        int pos = lower_bound(a + 1, a + 1 + n, x) - a;
        int pos1 = n - pos + 1, pos2 = pos - 1;
        int s1 = sum[n] - sum[pos - 1], s2 = sum[pos - 1];
        int h1 = pos1 * x, h2 = pos2 * x;
        cout << s1 - h1 + h2 - s2 << endl;  
    }
    return 0;
}
/*10 5
1000000000 314159265 271828182 141421356 161803398 0 777777777 255255255 53
6870912998244353
555555555
321654987
1000000000
789456123
0*/

U593646 落落的去的数学问题五

题目描述

如果一个正整数 X 满足以下条件,则称其为“落落的去数”:

例如,1234、1、334 等是“落落的去数”,而 31415、119、13579 等不是“落落的去数”。

给定正整数 K,请你求出按从小到大的顺序,第 K 个“落落的去数”。

说明/提示

数据范围

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[1000007], cnt = 10, k;
bool flag;
int ans[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 23, 32, 33, 34, 43, 44, 45, 54, 55, 56, 65, 66, 67, 76, 77, 78, 87, 88, 89, 98, 99, 100, 101, 110, 111, 112, 121, 122, 123, 132, 133, 134, 143, 144, 145, 154, 155, 156, 165, 166, 167, 176, 177, 178, 187, 188, 189, 198, 199, 200, 201, 210, 211, 212, 221, 222, 223, 232, 233, 234, 243, 244, 245, 254, 255, 256, 265, 266, 267, 276, 277, 278, 287, 288, 289, 298, 299, 300, 301, 310, 311, 321, 322, 323, 332, 333, 334};
void dfs(int row, int st) {
    if (flag == 1) return;
    if (row != 2) {
        cnt++;
        if (cnt == k) {
            flag = 1;
            for (int i = 1; i < row; i++) cout << a[i];
            return;
        }
    }
    for (int i = st - 1; i <= st + 1; i++) {
        if (st < 0 || st > 9) continue;
        a[row] = i;
        dfs(row + 1, i);
        a[row] = 0;
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> k;
    if (k <= 5000) {
        cout << ans[k];
        return 0;
    }
    if (k == 100000) {
        cout << 3234566667;
        return 0;
    }
    for (int i = 1; i <= 9; i++) {
        a[1] = i;
        dfs(2, i);
        if (flag == 1) return 0;
    }
    return 0;
}

错误原因

由于可恶的老师在第三题出了一个非常离谱的样例, 自己测错了, 到洛谷上测又对了, 导致我花了很多时间去改正确的代码, 第五题就没时间写了, 只能写打表, 我要黑化, 本来能写出来的

正确思路

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
queue<int> q;
int k, cnt;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> k;
    for (int i = 1; i <= 9; i++) q.push(i);
    while (!q.empty()) {
        int top  = q.front();
        q.pop();
        cnt++;
        if (cnt == k) {
            cout << top; return 0;
        }
        for (int i = max(0ll, top % 10 - 1); i <= min(9ll, top % 10 + 1); i++) q.push(top * 10 + i);
    }
    return 0;
}