1512E Permutation by Sum
等差数列求和边界 判定是否存在
由123…r-l+1 从后往前每个数不断放大,直到到达s
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve(){
int n,l,r,s;
cin>>n>>l>>r>>s;
if ((1+r-l+1)*(r-l+1)/2>s||(2*n-r+l)*(r-l+1)/2<s){
cout<<"-1\n";
return;
}
vector<int> a(n+1);
vector<bool> sym(n+1);
s-=(1+r-l+1)*(r-l+1)/2;
for (int i=l;i<=r;i++){
a[i]=i-l+1;
sym[i-l+1]=1;
}
for (int i=r;i>=l;i--){
if (!s)break;
for (int j=n;j>=1;j--){
if (sym[j]||j-a[i]>s)continue;
// cout<<i<<" "<<a[i]<<" "<<j<<"\n";
sym[a[i]]=0;
sym[j]=1;
s-=j-a[i];
a[i]=j;
break;
}
// cout<<s<<"kkk\n";
}
for (int i=1;i<=n;i++){
if (a[i])continue;
for (int j=1;j<=n;j++){
if (sym[j])continue;
sym[j]=1;
a[i]=j;
break;
}
}
for (int i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--){
solve();
}
return 0;
}