7.28综合测试总结

· · 个人记录

T637969 转变

题目描述

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

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

正确代码

#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 真实社交

题目描述

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

自 "真实社交" 推出以来,已进行了 Q 次操作。 i /- (1\leq i\leq Q) 操作由三个整数 op_i 、 A_i 和 B_i 表示,其含义如下:

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

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

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

正确代码

#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 落落的去维护数组

题目描述

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

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

输入格式

第一行一个数 n。

接下来 n 个数,表示 a 数组。

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

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

输出格式

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

错误代码

#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分。

正确思路

代码如下:

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;//和上面的操作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 交叉区间

题目描述

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

说明/提示

数据范围

对于 100\% 的数据:

2 \leq n \leq 5 \times 10^5$ $,0 \leq l_i < r_i \leq 10^9$ $,1 \leq i < j \leq n

错误代码

#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;

核心代码如下:

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;
}