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

· · 题解

一道构造题。

思路

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

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

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

最终构造序列:l 加上 \lfloor c \div 2 \rfloor 加上 n-(l+r+c) 加上 c-\lfloor c \div 2 \rfloor 加上 r。

IMPOSSIBLE 的特判方法

l 序列的构造方法

r 序列的构造方法

c 和 凑长度 的构造方法

定义 mid = c \div 2。

特判

时间复杂度

## 代码 ```cpp line-numbers //#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; } ```