“蔚来杯“2022牛客暑期多校训练营9 补题

Longest Increasing Subsequence

题意: 构造一个1,2,…,n 的排列,使其 恰好有 m 个不同的最长上升子序列。

#include<bits/stdc++.h>
using namespace std;
#define int long long 
#pragma GCC optimize(3)
#define re register int
typedef pair<int,int>PII;
#define pb push_back
#define mb pop_back
#define debug(a) cout<<a<<' ';
#define fer(i,a,b) for(re i=a;i<=b;i++)
#define der(i,a,b) for(re i=a;i>=b;i--)
const int N = 110;
int a[N];
void cf(){
    int n;
    cin>>n;
    int bit = 0;
    int nn=n;
    while(nn){
        bit++;
        nn>>=1;
    }
    int need=bit-1;
    int st=need*2+1;
    nn=n;
    int sum=0;
    for(int i=need-1;i>=0;i--){
        if((nn>>i)&1){
            a[i]=need-i-sum;
            sum+=a[i];
        }
        else a[i]=0;
    }
    cout<<sum+st<<endl;
    for(int i=0;i<need;i++){
        for(int j=1;j<=a[i];j++)cout<<st<<' ',st++;
        cout<<(i+1)*2<<' '<<(i+1)*2-1<<' ';
    }
    cout<<st<<endl;
}
signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int _=1;
    cin>>_;
    while(_--){
        cf();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值