题解:CF1508B Almost Sorted
题意:
给你一个
做法:
首先考虑如何判无解:考虑现在有一个长度为
接下来发现题意可以转化成选一些区间做翻转,所以考虑翻转的端点。假如把划分打到二进制里,对于一个空隙让这个点划分就是
玩一下样例可以发现一个重要性质。
| s | 划分 | 翻转后序列 |
|---|---|---|
| 00 | [1][2][3] | 1 2 3 |
| 01 | [1][2,3] | 1 3 2 |
| 10 | [1,2][3] | 2 1 3 |
| 11 | [1,2,3] | 3 2 1 |
翻转后序列的划分方案在满足条件的排列中的字典序排名也就是
证明:
设划分方案二进制是
这里再感性理解一下,一共
所以直接对
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,k;
int qpow(int a,int b){
int res=1;
while(b){
if(b&1) res=res*a;
a=a*a;
b>>=1;
if(res>k){
return 1e18;
}
}
return res;
}
vector<int>c[100005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
stack<bool>s;
cin>>n>>k;
int cntt=qpow(2,n-1);
if(cntt<k&&n<=62){
cout<<"-1\n";
continue;
}
int cnt=0;
k--;
for(int i=1,p=1;i<=n;i=p+1,p=i){
while((k>>(n-p-1))&1&&n-p<=62) p++;
for(int j=p;j>=i;j--){
cout<<j<<" ";
}
}
cout<<"\n";
}
return 0;
}