"蔚来杯"2022牛客暑期多校训练营2 G
挺典的,应该在某个 CF 做到过。
暴力发现,答案显然 \(\lceil \sqrt{n}\ \rceil\),然后就按这个分块就好了。
#include <bits/stdc++.h>
//#define int long long
using namespace std;
// 2 4 5 1 3
// 2 4 6 1 3 5
//
int n;
void solve() {
cin>>n;
if(n==1) {
cout<<"1\n";
} else if(n==2) {
cout<<"1 2\n";
} else if(n==3) {
cout<<"1 3 2\n";
} else if(n==4) {
cout<<"2 1 4 3\n";
} else {
int qwq=(int)(ceil(sqrt(n)));
int qaq=n-(n/qwq)*qwq;
for(int i=n-qaq+1;i<=n;i++) cout<<i<<' ';
for(int i=n/qwq;i>=1;i--) {
for(int j=(i-1)*qwq+1;j<=i*qwq;j++) cout<<j<<' ';
}
cout<<'\n';
}
}
signed main() {
cin.tie(0); ios::sync_with_stdio(false);
int T; cin>>T; while(T--) solve();
return 0;
}
// 2 4 1 3
//2 4 6 7 1 3 5
//2 4 6 1 3 5 7
//2 4 1 3
// 2 4 5 1 3
// 2 4 1 3 5