7.28综合测试总结
T637969 转变
题目描述
给你一个长度为
你正好进行了
- 移除
A 的初始元素,并在A 的尾部追加一个0 。
打印操作后
正确代码
#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 真实社交
题目描述
落落的去竞选漂亮国总统失败啦,他的竞争对手 "懂王" 经营着一个社交媒体软件 "真实社交",从用户
自 "真实社交" 推出以来,已进行了
- 如果是
op_i = 1 :表示用户A_i 跟随用户B_i 。如果用户A_i 在执行此操作时已经关注了用户B_i ,则不会进行任何更改。 - 如果
op_i = 2 :则表示用户A_i 取消关注用户B_i 。如果用户A_i 在执行此操作时没有关注用户B_i ,则不会进行任何更改。 - 如果
op_i = 3 :则表示要求您确定用户A_i 和B_i 是否相互关注。如果用户A_i 正在关注用户B_i 和用户B_i 正在关注用户A_i ,则需要打印 "Yes",否则打印 "No"。
服务启动时,没有用户关注任何用户。
打印所有操作的正确答案,这样
对于每个
正确代码
#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 落落的去维护数组
题目描述
落落的去是一个无情的数组维护员,负责维护一个长度为
对于每个
输入格式
第一行一个数
接下来
第三行一个数
接下来
输出格式
对于每个
错误代码
#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分。
正确思路
-
由于暴力会超时, 因此必须优化这三个操作。
-
我们可以定义
lasttime 与lastk , 分别记录操作1 的最后执行时间与操作1 的最后执行的k 。 -
还可以定义
cnt[N] , 记录每个元素最后的更新时间。 -
操作1:
可以直接用前面定义的两个变量
lasttime 和lastk 。记录操作1的最后执行时间 :
lasttime = t , 也就是当前执行循环的次数;记录操作1的最后执行的k :
lastk = k ,lastk 可以直接等于输入的k 。
代码如下:
if (op == 1) {//操作1
int k; cin >> k;
lasttime = t;//记录操作1的最后执行时间
lastk = k;//记录操作1的最后执行的k
}
-
操作2:
操作2要分三种情况:
别忘了!!!还要更新最后更新的时间,
cnt[i] = t 。哦, 对了!还有
if 判断是一定要用else if , 否则会出现进入多种情况的事情! -
代码如下:
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一样要分三种情况:
-
-
cnt[i] < lasttime$, 就要输出 $lastk -
cnt[i] > lasttime$, 就要输出 $a[i] + lastk
-
代码如下:
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;//这里不多说了, 懂得的懂
}
正确代码
#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 交叉区间
题目描述
给你
说明/提示
数据范围
对于
错误代码
#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;
}
错误原因
思路错误
正确思路
- 首先我们可以很轻松的想到暴力的代码
核心代码如下:
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.
l_j > r_i 2.
r_j < l_i -
我们还可以知道:
相交对数
= 区间总对数n * (n - 1) 除以 2 - 不相交区间对数, 所以统计出不相交的区间对数即可。使用$a$数组存储所有的左端点, 然后排序, 然后二分查找第一个$> r_i$的位置$pos$, $sum += n - pos + 1 使用$b$数组存储所有的右端点, 然后排序, 然后二分查找第一个$>= l_i$的位置$pos$, $sum += pos - 1
核心代码如下:
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;
正确代码
#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;
}