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

· · 题解

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

贪心

显然的,我们应该尽可能将夹子放在画布的两端。因为这样可能能让一个夹子支撑两个画布。

构造

先处理每个画布上已经有多少个夹子,如果存在一个画布的夹子数大于 2,则无解。

::::info[警示后人] 如果你RE,请注意处理画布原始夹子数的部分。

应该是这样:

LL now=1;
for(LL i=1;i<=p;i++){
  LL x=read();
  vis[x]=1;
  while(x>a[now].r&&now<n)now++;
  // ...
}

::::

对于每个画布,从其右端点向左枚举夹子放在哪。

如果一个夹子可以被放,则这个夹子所在的位置没有被放其它夹子,且画布已经有的夹子数应小于 2。但是要注意一个夹子可能跨越两个画布。

ACcode

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 1005

inline LL read(){
    char ch=getchar();
    int w=1;
    LL s=0;
    while(ch<'0'||ch>'9'){
        if(ch=='-')w*=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=(s<<3)+(s<<1)+(ch^48);
        ch=getchar();
    }
    return w*s;
}
inline void write(LL n){
    if(n<0)putchar('-'),n=-n;
    if(n>9)write(n/10);
    putchar(n%10+'0');
    return ;
}

struct node{
    LL l,r;
}a[N];
LL cnt[N];
vector<LL>ans;
unordered_map<LL,bool>vis;
int main(){
    LL n=read();
    for(LL i=1;i<=n;i++){
        a[i]={read(),read()};
    }
    LL p=read();
    LL now=1;
    for(LL i=1;i<=p;i++){
        LL x=read();
        vis[x]=1;
        while(x>a[now].r&&now<n)now++;
        if(a[now].l<=x&&x<=a[now].r){
            cnt[now]++;
            if(cnt[now]>2){
                printf("impossible");
                return 0;
            }
        }
        if(a[now+1].l<=x&&x<=a[now+1].r){
            cnt[now+1]++;
            if(cnt[now+1]>2){
                printf("impossible");
                return 0;
            }
        }
        if(a[now-1].l<=x&&x<=a[now-1].r){
            cnt[now-1]++;
            if(cnt[now-1]>2){
                printf("impossible");
                return 0;
            }
        }
    }
    for(LL i=1;i<=n;i++){
        LL pos=a[i].r;
        if(i<n&&cnt[i+1]==2)pos--;
        while(cnt[i]!=2){
            while(vis[pos])pos--;
            cnt[i]++;
            vis[pos]=1;
            if(pos==a[i+1].l)cnt[i+1]++;
            ans.push_back(pos);
        }
    }
    write(ans.size());
    putchar('\n');
    for(LL i=0;i<ans.size();i++){
        write(ans[i]);
        putchar(' ');
    }
    return 0;
}