1508B - Almost Sorted (Codeforces Round #715 (Div. 1) )
题意
定义几乎升序的置换,这样的置换满足对于所有的 i , i + 1 ∈ n i, i + 1 \in n i,i+1∈n ,都有 a i + 1 ≥ a i − 1 a_{i + 1} \geq a_i - 1 ai+1≥ai−1 。给定 n , k n, k n,k ,给出长度为 n n n 、字典序第 k k k 大的几乎升序的置换。
思路
参考:cf1509E - Almost Sorted_juraws juraws姐姐的代码w
姐姐写的很好所以我不再写一遍了(嗯
表白姐姐(嗯
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
stack<int> st;
vector<ll> ans;
int main()
{
int T;
cin >> T;
while(T--)
{
while(!st.empty())
st.pop();
ans.clear();
ll n, k;
cin >> n >> k;
if(n < 64 && k > (1ll << (n - 1)))
{
cout << "-1" << endl;
continue;
}
k = (k - 1) * 2;
for(int i = 0; i < n; i++)
{
st.push(k & 1);
k >>= 1;
}
int cnt = 0;
while(!st.empty())
{
cnt++;
if(!st.top())
{
ans.push_back(cnt);
}
st.pop();
}
ll pre = 0;
for(auto i : ans)
{
for(int j = i; j > pre; j--)
{
cout << j << ' ';
}
pre = i;
}
cout << endl;
}
return 0;
}