Codeforces Hello2020 A-D

A 签到,下次应该直接看样例

#include <bits/stdc++.h>
using namespace std;
#define inc(i, l, r) for (int i = l; i <= r; i++)

string s[25], t[25];
int n, m, q, x;

int main() {
    cin >> n >> m;
    inc(i, 0, n - 1) cin >> s[i];
    inc(i, 0, m - 1) cin >> t[i];
    cin >> q;
    while (q--) {
        cin >> x;
        x--;
        cout << s[x % n] << t[x % m] << "\n";
    }
}
View Code

B 找出相连非法的情况。不要想着把开头结尾的数字放一个数组排序,分两个数组,用一个数组的元素去另一个数组查找就很清晰

#include <bits/stdc++.h>
using namespace std;
#define inc(i, l, r) for (int i = l; i <= r; i++)
#define ll long long
#define pii pair<int, int>
#define se second
#define pb push_back

const int maxn = 1e6 + 5;

int top, x;
ll n, res;
int tmp[maxn];
int a[maxn], b[maxn];

int main() {
    cin >> n;
    inc(k, 0, n - 1) {
        cin >> x;
        inc(i, 0, x - 1) cin >> tmp[i];
        int f = 0;
        inc(i, 0, x - 2) if (tmp[i] < tmp[i + 1]) f = 1;
        if (!f) {
            a[top] = tmp[0];
            b[top++] = tmp[x - 1];
        }
    }
    sort(a, a + top);
    inc(i, 0, top - 1) res += upper_bound(a, a + top, b[i]) - a;
    cout << n * n - res;
}
View Code

C 考虑[l, r]组合的贡献,推式子的时候注意到 l - r 在每个项里都有出现,就当做一个整体来

#include <bits/stdc++.h>
using namespace std;
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1e6 + 5;
#define ll long long

ll n, m;
ll jie[maxn], res;

int main() {
    cin >> n >> m;
    jie[0] = 1;
    inc(i, 1, 250010) jie[i] = jie[i - 1] * i % m;
    inc(i, 0, n - 1) res =
        (res + (n - i) * (n - i) % m * jie[i + 1] % m * jie[n - 1 - i] % m) % m;
    cout << res % m;
}
View Code

D 不会做,看题解。

题意等价与,每两场讲座,在A地是否冲突和在B地是否冲突必须一样。维护一个A的时间序列,ai在当前时间 t 时,对应的bi也要在,然后判断bi是否全部都冲突。

#include <bits/stdc++.h>
using namespace std;
#define inc(i, l, r) for (int i = l; i <= r; i++)

const int maxn = 1e6 + 5;

int n, a[4][maxn];

struct Event {
    int t, a, b, f;
    bool operator<(const Event &rhs) {
        if (t == rhs.t) return f < rhs.f;
        return t < rhs.t;
    }
} eve[maxn];

bool judge(int *a1, int *b1, int *a2, int *b2) {
    multiset<int> beg, end;
    for (int i = 0; i < n; i++) {
        eve[2 * i] = {a1[i], a2[i], b2[i], 0};
        eve[2 * i + 1] = {b1[i], a2[i], b2[i], 1};
    }
    sort(eve, eve + 2 * n);
    inc(i, 0, 2 * n - 1) {
        if (eve[i].f == 0) {
            if (!beg.empty()) {
                if (eve[i].b < *beg.rbegin() || eve[i].a > *end.begin())
                    return false;
            }
            beg.insert(eve[i].a);
            end.insert(eve[i].b);
        } else {
            beg.erase(beg.find(eve[i].a));
            end.erase(end.find(eve[i].b));
        }
    }
    return true;
}

int main() {
    cin >> n;
    inc(i, 0, n - 1) inc(j, 0, 3) cin >> a[j][i];
    if (judge(a[0], a[1], a[2], a[3]) && judge(a[2], a[3], a[0], a[1]))
        cout << "YES";
    else
        cout << "NO";
}
View Code

猜你喜欢

转载自www.cnblogs.com/hs-zlq/p/12152844.html