跳到正文

个人记录

7.28综合测试总结

#SUSPENDED#约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 15 分钟

版本

  1. v1当前

T637969 转变

题目描述

给你一个长度为 nn 的序列 A=(a1,a2,…,aN)A = (a_1, a_2, \dots, a_N) 。 你正好进行了 kk 次下面的操作:

  • 移除 AA 的初始元素,并在 AA 的尾部追加一个 00 。

打印操作后 AA 中的所有元素。

正确代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int a[107];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	if (k >= n) for (int i = 1; i <= n; i++) cout << 0 << " ";
	else {
		for (int i = k + 1; i <= n; i++) cout << a[i] << " ";
		for (int i = 1; i <= k; i++) cout << 0 << " ";
	}
	return 0;
}
/*
3 2
2 7 8

8 0 0
*/

/*
3 4
9 9 9

0 0 0
*/

/*
9 5
1 2 3 4 5 6 7 8 9

6 7 8 9 0 0 0 0 0
*/

T638034 真实社交

题目描述

落落的去竞选漂亮国总统失败啦,他的竞争对手 “懂王” 经营着一个社交媒体软件 “真实社交”,从用户 11 到用户 NN 共有 NN 个用户。在 “真实社交” 中,用户可以关注或取消关注其他用户。

自 “真实社交” 推出以来,已进行了 QQ 次操作。 ii /- (1≤i≤Q)(1\leq i\leq Q) 操作由三个整数 opiop_i 、 AiA_i 和 BiB_i 表示,其含义如下:

  • 如果是 opi=1op_i = 1 :表示用户 AiA_i 跟随用户 BiB_i 。如果用户 AiA_i 在执行此操作时已经关注了用户 BiB_i ,则不会进行任何更改。
  • 如果 opi=2op_i = 2 :则表示用户 AiA_i 取消关注用户 BiB_i 。如果用户 AiA_i 在执行此操作时没有关注用户 BiB_i ,则不会进行任何更改。
  • 如果 opi=3op_i = 3 :则表示要求您确定用户 AiA_i 和 BiB_i 是否相互关注。如果用户 AiA_i 正在关注用户 BiB_i 和用户 BiB_i 正在关注用户 AiA_i ,则需要打印 “Yes”,否则打印 “No”。

服务启动时,没有用户关注任何用户。

打印所有操作的正确答案,这样 opi=3op_i = 3 按 ii 升序排列。

对于每个 opi=3op_i = 3,输出结果。

正确代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, q;
map<int, map<int, bool> > mp;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> q;
	while (q--) {
		int op, a, b;
		cin >> op >> a >> b;
		if (op == 1) mp[a][b] = true;
		else if (op == 2) mp[a][b] = false;
		else {
			if (mp[a][b] == true && mp[b][a] == true) cout << "Yes\n";
			else cout << "No\n";
		}
	}
	return 0;
}

T638156 落落的去维护数组

题目描述

落落的去是一个无情的数组维护员,负责维护一个长度为 nn 的数组 aa,且 落落的去 会三种操作:

  • op=1op = 1,表示将 aa 数组全部元素替换成 kk。
  • op=2op = 2,表示先输入 ii, 然后让 ai=ai+ka_i = a_i + k。
  • op=3op = 3,表示查询 aia_i 的值。

对于每个 opi=3op_i = 3,输出结果。

输入格式

第一行一个数 nn。

接下来 nn 个数,表示 aa 数组。

第三行一个数 qq。qq 表示操作次数。

接下来 qq 行,每行表示一次操作:

  • opi=1op_i = 1,则再读入一个数 kk。
  • opi=2op_i = 2,则再读入两个数 ii,kk。
  • opi=3op_i = 3,则再读入一个数 ii。

输出格式

对于每个 opi=3op_i = 3,输出结果。

错误代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, q, s;
int a[200007];
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	cin >> q;
	while (q--) {
		int op;
		cin >> op;
		if (op == 1) {
			int k; cin >> k;
			for (int i = 1; i <= n; i++) a[i] = k;
		}
		else if (op == 2) {
			int i, k; cin >> i >> k;
			a[i] += k;
		}
		else  {
			int i; cin >> i;
			cout << a[i] << endl;
		}
	}
	return 0;
}

错误原因

由于写得是暴力, 超时了, 只拿了74分。

正确思路

  • 由于暴力会超时, 因此必须优化这三个操作。

  • 我们可以定义lasttimelasttime与lastklastk, 分别记录操作11的最后执行时间与操作11的最后执行的kk。

  • 还可以定义cnt[N]cnt[N], 记录每个元素最后的更新时间。

  • 操作1:

    可以直接用前面定义的两个变量lasttimelasttime和lastklastk。

    记录操作1的最后执行时间 :lasttime=tlasttime = t, 也就是当前执行循环的次数;

    记录操作1的最后执行的k :lastk=klastk = k, lastklastk可以直接等于输入的kk。

代码如下:

C++
if (op == 1) {//操作1
	int k; cin >> k;
	lasttime = t;//记录操作1的最后执行时间
    lastk = k;//记录操作1的最后执行的k
}
  • 操作2:

    操作2要分三种情况:

    1. lasttime==0lasttime == 0, 那么a[i]a[i]加上kk就好了, 也就是a[i]+=ka[i] += k。
    2. cnt[i]<lasttimecnt[i] < lasttime, a[i]a[i]直接等于kk就好了, 没有任何多余操作。
    3. cnt[i]>lasttimecnt[i] > lasttime, 和第一张情况一样a[i]a[i]加上kk就好了。

    别忘了!!!还要更新最后更新的时间, cnt[i]=tcnt[i] = t。

    哦, 对了!还有ifif判断是一定要用elseelse ifif, 否则会出现进入多种情况的事情!

代码如下:

C++
else if (op == 2) {//操作2
	int i, k; cin >> i >> k;
	if (lasttime == 0) a[i] += k;
	else if (cnt[i] < lasttime) a[i] = k;//注意!!!一点要写else if, 而不是if, 上面说过原因
	else if (cnt[i] > lasttime) a[i] += k;//这里和上面一样
	cnt[i] = t;//更新最后更新的时间
}
  • 操作3:

    和操作2一样要分三种情况:

    1. lasttime==0lasttime == 0, 那么直接输出a[i]a[i]即可
    2. cnt[i]<lasttimecnt[i] < lasttime, 就要输出 lastklastk
    3. cnt[i]>lasttimecnt[i] > lasttime, 就要输出 a[i]+lastka[i] + lastk

代码如下:

C++
else  {//操作3
	int i; cin >> i;
	if (lasttime == 0) cout << a[i] << endl;
	else if (cnt[i] < lasttime) cout << lastk << endl;//和上面的操作2, 同理
	else if (cnt[i] > lasttime) cout << a[i] + lastk << endl;//这里不多说了, 懂得的懂
}

正确代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, q;
int lasttime, lastk;//最后执行的时间与最后执行的k
int a[200007];
int cnt[200007];//记录每个元素最后更新的时间
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	cin >> q;
	for (int t = 1; t <= q; t++) {
		int op;
		cin >> op;//先输入op, 判断是那个操作, 再执行对应操作的内容
		if (op == 1) {//操作1
			int k; cin >> k;
			lasttime = t;//记录操作1的最后执行时间
            lastk = k;//记录操作1的最后执行的k
		}
		else if (op == 2) {//操作2
			int i, k; cin >> i >> k;
			if (lasttime == 0) a[i] += k;
			else if (cnt[i] < lasttime) a[i] = k;//注意!!!一点要写else if, 而不是if
			else if (cnt[i] > lasttime) a[i] += k;//这里和上面一样
			cnt[i] = t;//更新最后更新的时间
		}
		else  {//操作3
			int i; cin >> i;
			if (lasttime == 0) cout << a[i] << endl;
			else if (cnt[i] < lasttime) cout << lastk << endl;//和上面两个else if, 同理
			else if (cnt[i] > lasttime) cout << a[i] + lastk << endl;//这里不多说了, 懂得的懂
		}
	}
	return 0;
}

T638430 交叉区间

题目描述

给你 nn 个实数区间。第 ii (1≤i≤n)(1 \leq i \leq n) 个区间是 [li,ri][l _ i, r _ i] 。求有多少对 (i,j),(1≤i<j≤n)(i, j),(1 \leq i < j \leq n) 中,ii 和 jj 两个区间相交。

说明/提示

数据范围

对于 100%100\% 的数据: 2≤n≤5×1052 \leq n \leq 5 \times 10^5 ,0≤li<ri≤109,0 \leq l_i < r_i \leq 10^9 ,1≤i<j≤n,1 \leq i < j \leq n

错误代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, ans, mr, cnt;
struct node {
	int l, r;
}a[500005];
bool cmp(node x, node y) {
	return x.l < y.l;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i].l >> a[i].r;
	sort(a + 1, a + 1 + n, cmp);
	mr = a[1].r;
	if (n == 2 && mr == 2) {cout << 0; return 0;}
	if (n == 3 && mr == 6) {cout << 3; return 0;}
	bool flag = 1;
	for (int i  = 2; i <= n; i++) {
		mr = max(mr, a[i - 1].r);
		if (a[i].l < mr) cnt++, flag = 0;
		else {
			ans = max(ans, cnt); cnt = 0; mr = a[i].r;
		}
	}
	if (flag == 1) cout << 0;
	else cout << ans + 1;
	return 0;
}

错误原因

思路错误

正确思路

  • 首先我们可以很轻松的想到暴力的代码

核心代码如下:

C++
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
	cin >> a[i].l >> a[i].r;
}
for (int i = 1; i < n; i++) {
	for (int j = i + 1; j <= n; j++) {
		if (a[j].r < a[i].l || a[j].l > a[i].r) continue;
		ans++;
	}
}
cout << ans;
  • 我们可以知道如果两个区间不相交, 有一下两种情况:

    1.lj>ril_j > r_i

    2.rj<lir_j < l_i

  • 我们还可以知道:

    相交对数==区间总对数n∗(n−1)除以2−n * (n - 1) 除以 2 - 不相交区间对数, 所以统计出不相交的区间对数即可。

    ll>ril_l > r_i:统计有多少区间的左端点大于当前区间的右端点。

    使用aa数组存储所有的左端点, 然后排序, 然后二分查找第一个>ri> r_i的位置pospos, sum+=n−pos+1sum += n - pos + 1

    rj<lir_j < l_i:统计有多少区间的右端点小于当前区间的左端点。

    使用bb数组存储所有的右端点, 然后排序, 然后二分查找第一个>=li>= l_i的位置pospos, sum+=pos−1sum += pos - 1

核心代码如下:

C++
for (int i = 1; i <= n; i++) {
    int pos1 = upper_bound(a + 1, a + 1 + n, sum[i].r) - a;
    int pos2 = lower_bound(b + 1, b + 1 + n, sum[i].l) - b;
    ans += n - pos1 + 1; ans += pos2 - 1;
}
ans /= 2;
cout << n * (n - 1) / 2 - ans;

正确代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int ans;
struct node {
	int l, r;
}sum[1000007];
int a[1000007], b[1000007];
signed main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> sum[i].l >> sum[i].r;
        a[i] = sum[i].l; b[i] = sum[i].r;
    }
    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + n);
    for (int i = 1; i <= n; i++) {
        int pos1 = upper_bound(a + 1, a + 1 + n, sum[i].r) - a;
        int pos2 = lower_bound(b + 1, b + 1 + n, sum[i].l) - b;
        ans += n - pos1 + 1; ans += pos2 - 1;
    }
    ans /= 2;
    cout << n * (n - 1) / 2 - ans;
	return 0;
}
原文