跳到正文

题解

题解:P16764 [GKS 2020 #E] High Buildings

ZBH2015约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年11月27日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 9 分钟
题目
P16764

版本

  1. v1当前

一道构造题。

思路

  • 根据题目所说,一栋楼 xx 是可见的,当且仅当在 xx 左侧没有严格比 xx 高的楼,这段话的意思就是等高不遮挡。
  • 定义 ll 为 Andre 看到的楼但 Sule 没看到的楼的个数。
  • 定义 rr 为 Sule 看到的楼但 Andre 没看到的楼的个数。

很显然,l=a−cl=a-c,r=b−cr=b-c。

构造的序列为 l+c+rl+c+r。

但是如果序列长度 <n<n 我们可以把 cc 一分为二,中间插入 n−(l+r+c)n-(l+r+c) 个 11 来凑长度。

最终构造序列:ll 加上 ⌊c÷2⌋\lfloor c \div 2 \rfloor 加上 n−(l+r+c)n-(l+r+c) 加上 c−⌊c÷2⌋c-\lfloor c \div 2 \rfloor 加上 rr。

IMPOSSIBLE 的特判方法

  • 当 l+r+c>nl+r+c > n 时,无法构造出来。
  • 当 a=b=c=1a = b = c = 1 且 n≥2n \ge 2 时,无解。

ll 序列的构造方法

  • 按照 11 到 ll 的高度升序填充即可。

rr 序列的构造方法

  • 按照 rr 到 11 的高度降序填充即可。

cc 和 凑长度 的构造方法

定义 mid=c÷2mid = c \div 2。

  • 先构造 midmid 个 nn 高度的楼。
  • 再构造 n−(l+r+c)n-(l+r+c) 个 11 高度的楼。
  • 最后再构造 c−midc-mid 个 nn 高度的楼。

特判

  • 当 c=1c = 1 且 r=0r = 0 时,只有一栋最高楼。
  • 当 l≤1l \le 1 时,必须抬到 22 才能挡住,否则正常构造。
  • 构造 ll 以后,先将 n−(l+r+c)n-(l+r+c) 个 11 塞进来,再放一个 nn 堵住。

时间复杂度

O(Tn)O(Tn),可以过。

代码

C++
//#pragma GCC optimize("Ofast,unroll-loops")
#pragma g++ optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define ot float
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define in __int128
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=1e2+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
int n,a,b,c;
string s="IMPOSSIBLE";
int ans[N];
int pos=0;
inline void pre(){
    pos=0;
}
inline void input(){
    n=read(),a=read(),b=read(),c=read();
    int l=a-c;
    int r=b-c;
    if((l+r+c)>n||(a==1&&b==1&&c==1&&n>1)){
        ws(s);
        return ;
    }
    int need=n-(l+r+c);
    if(need>0&&c==1&&r==0){
        if(l==1) ans[++pos]=2;
        else F(i,1,l,1) ans[++pos]=i;
        F(i,1,need,1) ans[++pos]=1;
        ans[++pos]=n;
        F(i,1,n,1) write(ans[i]),pc(' ');
        return;
    }
    F(i,1,l,1) ans[++pos]=i;
    if(need&&c>=2){
        int mid=c/2;
        F(i,1,mid,1) ans[++pos]=n;
        F(i,1,need,1) ans[++pos]=1;
        F(i,mid+1,c,1) ans[++pos]=n;
    }else{
        F(i,1,c,1) ans[++pos]=n;
        F(i,1,need,1) ans[++pos]=1;
    }
    F(i,1,r,1) ans[++pos]=n-i;
    F(i,1,n,1) write(ans[i]),pc(' ');
}
inline void output(){
    
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    //ios::sync_with_stdio(0);
    //cin.tie(0);cin.tie(0);
    string ___="Case #";
    int _=read();
    F(__,1,_,1){
        ws(___);
        write(__);
        pc(':');
        pc(' ');
        solve();
        pc('\n');
    }
    flush();
    return 0;
}
原文