题解:P11890 [XRCOI Round 1] A. 相聚相逢本无意

· · 题解

好神金的一道题目,在场上被创到了。

按照 x 将要求升序排列,再处理每个要求。

设 A 中最大元素为 k,特别的,当 A 为空时,规定 k=-1。

考虑以下四种情况:

如果你的思路很清晰了,下面的代码也不难写出了。(其实很难)

#include <algorithm>
#include <cstdio>
using namespace std;
int read() {
    int v = 0, f = 1;
    char c = getchar();
    while (c < '0' || '9' < c) {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while ('0' <= c && c <= '9') {
        v = v * 10 + c - '0';
        c = getchar();
    }
    return v * f;
}
const int N = 2e5 + 5, M = 100 + 5;
int n = 0, m;
struct Node {
    int x, y;
} a[M];
bool cmp(const Node &x, const Node &y) { return x.x < y.x; }
int s[N], mx = -1;
bool zero = false, flag = false;
int main() {
    m = read();
    for (int i = 1; i <= m; i++)
        a[i].x = read(), a[i].y = read();
    sort(a + 1, a + m + 1, cmp);
    for (int i = 1; i <= m; i++) {
        if (a[i].x == 0) {
            if (a[i].y == 0)
                continue;
            for (int j = 1; j <= a[i].y; j++)
                s[++n] = 1;
            mx = 1;
            zero = true;
            continue;
        }
        if (zero) {
            if (a[i].y == 0)
                continue;
            puts("-1");
            return 0;
        }
        if (a[i].y == 0) {
            flag = true;
            continue;
        }
        if (flag) {
            puts("-1");
            return 0;
        }
        for (int j = mx + 1; j < a[i].x - 1; j++)
            s[++n] = j;
        for (int j = 1; j <= a[i].y; j++)
            s[++n] = a[i].x - 1;
        mx = a[i].x - 1;
    }
    printf("%d\n", n);
    for (int i = 1; i <= n; i++)
        printf("%d ", s[i]);
    return 0;
}