跳到正文

个人记录

9.10 <409>总结

SEF_711约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年9月12日
收录
2026年9月22日
更新
2026年11月5日
篇幅
约 9 分钟

版本

  1. v1当前

前言

考试时的精神状态我就不说了,考完看到成绩之后我直接红温了 分数实在难看

CF934B

题目传送门

题意

给定一个整数 kk,问不超过 101810^{18} 的正整数 nn(满足有 kk 个环)。

思路

真的太简单了。

肯定先用 88 凑,因为那样肯定会让数字更短。
最后用个 44、66、99 凑都行(不能是 00,因为如果 kk 是 11 的话,你不就炸了)。

不难发现,若 k>36k > 36,无解。

错因

我不知道咋了交了个以前样例都没过的代码上去了。 。 。

代码

代码实在简单,不加注释了。

C++
#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

题目传送门

题意

给定 nn 个数,mm 次操作,每次操作:

  • 给所有 l≤ai≤rl \le a_i \le r 的 aia_i 加一或减一
  • 输出 aia_i 的最大值

思路

我先放个结论在这:我们不用管数组,只要管最大值。
为了方便表示,设最大值为 maxnmaxn。

为什么呢?我们可以分情况讨论。

  1. maxn<lmaxn < l:如果 maxnmaxn 都小于 ll 了,那么说明所有值都小于 ll。故输出 maxnmaxn。
  2. maxn>rmaxn > r:如果 maxn>rmaxn > r,我们不妨设 maxn=r+1maxn = r + 1,那么其他数的最大值顶多是 rr,那么我们发现,这个第二大值吃撑了也只能是和 maxnmaxn 相等。故输出 maxnmaxn。
  3. l≤maxn≤rl \le maxn \le r:那么所有数都会随着 maxnmaxn 加 11 或减 11。故输出 maxn+1maxn + 1 或 maxn−1maxn - 1。

那么程序中也只要这么分类讨论就行了。

思路

看着非常难,就打了个暴力,然后写错了。 。 。

代码

C++
#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

题目传送门

题意

给定长度为 nn 的两个序列 aa 和 bb,问能不能用以下两种操作让 aa 数组全变为 00:

  • 交换 aia_i 和 bi−1b_{i - 1} 的值
  • 交换 bib_i 和 ai−1a_{i - 1} 的值

思路

我们知道,把 aa 全弄成 00 就是把 11 全弄到 bb 里面,那么就可以把 aa 看作行 00,bb 看作行 11。

aa 和 bb 中的 11,所在的行 + 所在的列在操作(这里指交换 aa 和 bb 的数字)之后不改变奇偶性,所以我们可以用 type0type0 统计行 + 列为偶数的数量,type1type1 统计行 + 列为奇数的数量。

所有 type0type0 的 11 都在行 11 中占据奇数列,type1type1 占据偶数列,所以 bb 中奇数列的位置就是 n+12\frac{n + 1}{2},偶数列就是 n2\frac{n}{2}。

那么最后如果 type0≤n+12type0 \le \frac{n + 1}{2} 且 type1≤n2type1 \le \frac{n}{2},则输出 YES,否则 NO。

错因

我去分析 aa 和 bb 的关系去了。

代码

C++
#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

题目传送门

题意

给定两个长度为 nn 的数组 aa 和 bb,问能否把 aa 数组中相邻的数交换不超过 n2n^2 次把 aa 变成 bb。

思路

一眼一个“冒泡排序”。

我们知道,冒泡排序的交换次数至多为 n×(n−1)2\frac{n \times (n - 1)}{2},这个肯定小于 n2n^2,所以 n2n^2 这个条件我们可以不用考虑。

我们可以将 aa 和 bb 数组复制一份:AA 和 BB 数组。
然后我们可以先将AA 和 BB 进行排序,再让 AiA_i 和 BiB_i 进行比较,如果 Ai≠BiA_i \ne B_i,就输出 NO。

那么怎么交换呢?我们可以枚举 bib_i,然后在 aa 中找到相同的元素一个一个交换过来,输出操作次数即可。

错因

没想到怎么交换。

代码

C++
#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;
}
原文