8.7综合测试总结
U593635 交换
题目描述
有
现在,箱子
请依次执行以下操作后,输出每个箱子中所包含的整数。
- 交换箱子
A 和箱子B 的内容; - 交换箱子
A 和箱子C 的内容。
正确代码
#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 猜猜芭蕾舞剧将如何结束
题目描述
在古希腊戏剧中,合唱团经常会跳舞。
舞台上的所有舞者都在同一时间做着同样的动作,从左到右或从右到左穿过剧场。当然,没有一个舞者能越过场景的边界。因此,如果指令会让他们掉下舞台,他们只需执行动作直到到达边界,然后停留在那里直到下一个动作。例如,如果动作要求向右走
您正在发掘一座剧院,并发现了保存异常完好的纸莎草纸,上面详细记录了舞者在许多戏剧演出中的动作顺序,这真是一座宝库。你们不知道舞者最初的位置,因为你们只找到了写有动作的纸莎草纸,其余的说明和其余的戏剧已经遗失。
指令是一串数字,告诉所有舞者向左跳
在场景中加入一个坐标系,每个舞者
你们已经发现,在某些情况下,由于边界的原因,一些舞者可能需要缩短某些动作,因此在舞蹈结束时,他们可能会排成一条完美的直线(都在相同的
当然,你不知道舞者的起始位置,也不知道他们的人数(只知道他们至少有一个),但你认为在某些情况下,你的程序可以确定舞者最终都排列在相同的
输入格式
在输入的第一行有
第二行,
输出格式
如果可以确定所有舞者最终都将在相同的 X 位置对齐,那么在动作结束时,所有舞者最终都将在该位置(与场景左侧的距离)对齐,对应于该位置的一个整数。
否则,将输出
说明/提示
| 数据点 | 数据范围 | 特殊点 |
|---|---|---|
| 无 | ||
| 无 | ||
| 无 |
错误代码
#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 和r ,l 和r 分别代表任意两个位置, 当然l \ne r , 这里我把l 定成第一个位置,r 定成最后一个位置直接模拟l, r 得位置 - 最后判断, 如果
l = r , 就输出l 和r 中任意一个;否则, 输出-1 - 我们可以验证一下:
一开始
向右移动
正确代码
#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 操作
题目描述
落落的去可以对任意整数
操作:将
给定整数
输入格式
输出格式
请输出对
说明/提示
数据范围
-
0 \leq N \leq 10^{18} -
1 \leq K \leq 10^{18} - 输入均为整数。
正确代码
#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
题目描述
给定一个长度为
- 首先,选择一个满足
1 \le i \le N 的整数i 。 - 然后,从以下两种操作中任选一种执行:
- 将
A_i 加1 。 - 将
A_i 减1 。
- 将
请回答
第
- 通过任意次数(包括
0 次)的“操作”,将A 的所有元素都变为X_i 时,所需的最小“操作”次数是多少?
输入格式
输出格式
请输出
第
说明/提示
数据范围
- 所有输入均为整数。
-
1 \le N, Q \le 2 \times 10^5 -
0 \le A_i \le 10^9 -
0 \le X_i \le 10^9
正确代码
#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 用十进制(不含前导零)表示时,任意相邻的两个数字的差的绝对值不超过1 。
例如,
给定正整数
说明/提示
数据范围
-
1 \leq K \leq 10^5 - 输入均为整数。
错误代码
#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;
}
错误原因
由于可恶的老师在第三题出了一个非常离谱的样例, 自己测错了, 到洛谷上测又对了, 导致我花了很多时间去改正确的代码, 第五题就没时间写了, 只能写打表, 我要黑化, 本来能写出来的
正确思路
- 我们可以用一个队列
queue<int> q;, 先把1 到9 入队 - 接着我们
while 循环, 只有队列非空(!q.empty()), 我们可以先取出队首记作top , 在让队首出队我们可以用cnt 记录这是第几个落落的去数, 这里cnt++, 如果cnt == k, 就输出top , 然后return 0;, 我们可以直接在top 后面加上1 到9 , 在入队 - 核心代码:
queue<int> q; int cnt = 0; 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); }
正确代码
#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;
}