T637969 转变
题目描述
给你一个长度为 的序列 。 你正好进行了 次下面的操作:
- 移除 的初始元素,并在 的尾部追加一个 。
打印操作后 中的所有元素。
正确代码
#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 真实社交
题目描述
落落的去竞选漂亮国总统失败啦,他的竞争对手 “懂王” 经营着一个社交媒体软件 “真实社交”,从用户 到用户 共有 个用户。在 “真实社交” 中,用户可以关注或取消关注其他用户。
自 “真实社交” 推出以来,已进行了 次操作。 /- 操作由三个整数 、 和 表示,其含义如下:
- 如果是 :表示用户 跟随用户 。如果用户 在执行此操作时已经关注了用户 ,则不会进行任何更改。
- 如果 :则表示用户 取消关注用户 。如果用户 在执行此操作时没有关注用户 ,则不会进行任何更改。
- 如果 :则表示要求您确定用户 和 是否相互关注。如果用户 正在关注用户 和用户 正在关注用户 ,则需要打印 “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分。
正确思路
-
由于暴力会超时, 因此必须优化这三个操作。
-
我们可以定义与, 分别记录操作的最后执行时间与操作的最后执行的。
-
还可以定义, 记录每个元素最后的更新时间。
-
操作1:
可以直接用前面定义的两个变量和。
记录操作1的最后执行时间 :, 也就是当前执行循环的次数;
记录操作1的最后执行的k :, 可以直接等于输入的。
代码如下:
if (op == 1) {//操作1
int k; cin >> k;
lasttime = t;//记录操作1的最后执行时间
lastk = k;//记录操作1的最后执行的k
}-
操作2:
操作2要分三种情况:
- , 那么加上就好了, 也就是。
- , 直接等于就好了, 没有任何多余操作。
- , 和第一张情况一样加上就好了。
别忘了!!!还要更新最后更新的时间, 。
哦, 对了!还有判断是一定要用 , 否则会出现进入多种情况的事情!
代码如下:
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一样要分三种情况:
- , 那么直接输出即可
- , 就要输出
- , 就要输出
代码如下:
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.
2.
-
我们还可以知道:
相交对数区间总对数不相交区间对数, 所以统计出不相交的区间对数即可。
:统计有多少区间的左端点大于当前区间的右端点。
使用数组存储所有的左端点, 然后排序, 然后二分查找第一个的位置,
:统计有多少区间的右端点小于当前区间的左端点。
使用数组存储所有的右端点, 然后排序, 然后二分查找第一个的位置,
核心代码如下:
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;
}