8.17_407_课堂总结

· · 个人记录

T637969 转变

\color{#52c41a} 100pts

思路

简单队列,pop操作k次,再push(0)操作k次

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    queue<int> q;
    int n, k;
    cin >> n >> k;
    while(n--){
        int x;
        cin >> x;
        q.push(x);
    }
    while(k--){
        q.pop();
        q.push(0);
    }
    while(q.size()){
        cout << q.front() << " ";
        q.pop();
    }
    return 0;
}

T638034 真实社交

\color{#f39c11} 50pts

正解思路

用一个pair来绑定x, y而不是用二维bool数组

正解代码

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

T638156 落落的去维护数组

\color{#52c41a} 100pts

思路

map加模拟

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<int, bool> mp;
ll a[200001];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n, q, k = -1;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    cin >> q;
    while(q--){
        int op;
        cin >> op;
        if(op == 1){
            cin >> k;
            mp.clear();
        }else if(op == 2){
            int i, x;
            cin >> i >> x;
            if(k != -1){
                if(mp[i]){
                    a[i] += x;
                }else{
                    a[i] = x;
                    mp[i] = true;
                }
            }else{
                a[i] += x;
            }
        }else{
            int i;
            cin >> i;
            if(k == -1){
                cout << a[i] << "\n";
            }else{
                if(mp[i]){
                    cout << k + a[i] << "\n";
                }else{
                    cout << k << "\n";
                }
            }
        }
    }
    return 0;
}

T638430 交叉区间

\color{#f39c11} 40pts

正解思路

换一个方式,找出不交叉的序列数(用二分),再用总序列对数 - 不交叉的序列对数 = 交叉的序列对数

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll b[500001], c[500001];

struct Node{
    ll l, r;
} a[500001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i].l >> a[i].r;
        b[i] = a[i].l;
        c[i] = a[i].r;
    }
    sort(b + 1, b + n + 1);
    sort(c + 1, c + n + 1);
    ll ans = 0;
    for(int i = 1; i <= n; i++){
        ll L = upper_bound(b + 1, b + n + 1, a[i].r) - b;
        ll n1 = n - L + 1;
        ll R = lower_bound(c + 1, c + n + 1, a[i].l) - c;
        ll n2 = R - 1;
        ans += n1 + n2;
    }
    cout << (ll)(n * (n - 1) / 2) - ans / 2;
    return 0;
}