题解:P17001 [NWERC 2019] 晾画绳 / Canvas Line

· · 题解

因为每张画布必须恰好被两个夹子夹住,所以读入现有的之后,判断每张画布上的夹子数量。如果某块画布的夹子数已经超过两个,直接判定无解。

为了让夹子数量最少,先在所有画布边界处,且两块画布的夹子数都少于两个,尚未放置夹子的地方夹,这样一个夹子用于两块画布。同时,更新每块画布的夹子数量。

如果有画布还缺夹子,就在这块画布没有夹子的地方夹,直到布满两个。画布宽度至少 10 厘米,因此总有空余的地方。

::::info[代码]

// author: luogu @ OvO_frsf
// time: 2026.7.6
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define err() assert(0)
#define debug(x) cerr << "debug: " << #x << " = " << x << endl
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define repp(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define perr(i, a, b) for (int i = (a); i > (b); i--)
inline int read(){
    int res = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9'){ if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9'){ res = res * 10 + c - '0'; c = getchar(); }
    return res * f;
}

// Define Variables & Structures & Functions & Others Here.

const int N = 2e3 + 10;
struct line{
    int l, r;
}a[N];
bool cmp(line a, line b){
    return a.l < b.l;
}
unordered_map<int, bool> f;
int n, p, x[N], val[N], ans, cnt[N];
void solve(){
    n = read();
    rep(i, 1, n)
        a[i].l = read(), a[i].r = read();
    sort(a + 1, a + n + 1, cmp);
    p = read();
    rep(i, 1, p) x[i] = read(), f[x[i]] = 1;
    sort(x + 1, x + p + 1);
    rep(i, 1, p)
        rep(j, 1, n)
            if (x[i] >= a[j].l && x[i] <= a[j].r)
                cnt[j]++;
    rep(i, 1, n)
        if (cnt[i] > 2){
            puts("impossible");
            return ;
        }
    rep(i, 2, n){
        if (a[i - 1].r == a[i].l && cnt[i - 1] < 2 && cnt[i] < 2 && !f[a[i - 1].r]){
            ans++;
            val[ans] = a[i - 1].r;
            cnt[i - 1]++; cnt[i]++;
            f[a[i - 1].r] = 1;
        }
    }
    rep(i, 1, n)
        if (cnt[i] == 0){
            for (int j = 1, c = 0; c < 2 && j <= 10; j++)
                if (!f[a[i].l + j]){
                    f[a[i].l + j] = 1;
                    c++;
                    val[++ans] = a[i].l + j;
                }
        }
        else if (cnt[i] == 1){
            for (int j = 1, c = 0; c < 1 && j <= 10; j++)
                if (!f[a[i].l + j]){
                    f[a[i].l + j] = 1;
                    c++;
                    val[++ans] = a[i].l + j;
                }
        }
    sort(val + 1, val + ans + 1);
    printf("%lld\n", ans);
    rep(i, 1, ans) printf("%lld ", val[i]);
}
signed main(){
    int t = 1;
    while (t--) solve();
    return 0;
}

::::