#include <bits/stdc++.h>
#define int long long
using namespace std;
int k;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> k;
if (k > 36) {
cout << -1;
return 0;
}
for (int i = 1; i <= k / 2; i++)
cout << 8;
if (k % 2 == 1) cout << 4;
return 0;
}
CF2007B
题目传送门
题意
给定 n 个数,m 次操作,每次操作:
给所有 l \le a_i \le r 的 a_i 加一或减一
输出 a_i 的最大值
思路
我先放个结论在这:我们不用管数组,只要管最大值。
为了方便表示,设最大值为 maxn。
为什么呢?我们可以分情况讨论。
那么程序中也只要这么分类讨论就行了。
思路
看着非常难,就打了个暴力,然后写错了。 。 。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int kN = 1e5 + 5;
int n, m;
int a[kN];
void work() {
cin >> n >> m;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans = max(ans, a[i]); //维护最大值
}
while (m--) {
char c;
int l, r;
cin >> c >> l >> r;
if (ans < l || ans > r) cout << ans << " "; //直接输出最大值的情况
else cout << (c == '+' ? ++ans : --ans) << " "; //加一或减一
}
cout << "\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
work();
return 0;
}
CF2092B
题目传送门
题意
给定长度为 n 的两个序列 a 和 b,问能不能用以下两种操作让 a 数组全变为 0:
交换 a_i 和 b_{i - 1} 的值
交换 b_i 和 a_{i - 1} 的值
思路
我们知道,把 a 全弄成 0 就是把 1 全弄到 b 里面,那么就可以把 a 看作行 0,b 看作行 1。
所有 $type0$ 的 $1$ 都在行 $1$ 中占据奇数列,$type1$ 占据偶数列,所以 $b$ 中奇数列的位置就是 $\frac{n + 1}{2}$,偶数列就是 $\frac{n}{2}$。
那么最后如果 $type0 \le \frac{n + 1}{2}$ 且 $type1 \le \frac{n}{2}$,则输出 `YES`,否则 `NO`。
### 错因
我去分析 $a$ 和 $b$ 的关系去了。
### 代码
```cpp
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int kN = 2e5 + 5;
int n;
string a, b;
void work() {
cin >> n >> a >> b;
int type0 = 0, type1 = 0;
for (int i = 0; i < n; i++) {
if (a[i] == '1') {
if ((i + 1) % 2 == 0) type0++; //偶数
else type1++; //奇数
}
if (b[i] == '1') {
if ((i + 2) % 2 == 0) type0++; //偶数
else type1++; //奇数
}
}
if (type0 <= (n + 1) / 2 && type1 <= n / 2) cout << "YES\n"; //检查
else cout << "NO\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
work();
return 0;
}
```
# P8197
[题目传送门](https://www.luogu.com.cn/problem/P8197)
### 题意
给定两个长度为 $n$ 的数组 $a$ 和 $b$,问能否把 $a$ 数组中相邻的数交换不超过 $n^2$ 次把 $a$ 变成 $b$。
### 思路
一眼一个“冒泡排序”。
我们知道,冒泡排序的交换次数至多为 $\frac{n \times (n - 1)}{2}$,这个肯定小于 $n^2$,所以 $n^2$ 这个条件我们可以不用考虑。
我们可以将 $a$ 和 $b$ 数组复制一份:$A$ 和 $B$ 数组。
然后我们可以先将$A$ 和 $B$ 进行排序,再让 $A_i$ 和 $B_i$ 进行比较,如果 $A_i \ne B_i$,就输出 `NO`。
那么怎么交换呢?我们可以枚举 $b_i$,然后在 $a$ 中找到相同的元素一个一个交换过来,输出操作次数即可。
### 错因
没想到怎么交换。
### 代码
```cpp
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int kN = 1e3 + 5;
int n;
int a[kN], b[kN], A[kN], B[kN];
void work() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
A[i] = a[i]; //复制
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
B[i] = b[i]; //复制
}
//排序
sort(A + 1, A + 1 + n);
sort(B + 1, B + 1 + n);
//检查
for (int i = 1; i <= n; i++)
if (A[i] != B[i]) {
cout << "NO\n";
return ;
}
cout << "YES\n";
for (int i = 1; i <= n; i++) {
int id = 0;
for (int j = n; j >= i; j--) if (a[j] == b[i]) id = j; //交换位置
for (int j = id; j > i; j--) {
swap(a[j], a[j - 1]); //交换
cout << j - 1 << " " << j << "\n"; //输出
}
}
cout << "0 0\n"; //最后还有个0 0
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
work();
return 0;
}
```