Kanade has an array a[1…n] , she define that an array b[1…m] is good if and only if it satisfy the following conditions: $1<=b[i]<=n b[i]<b[i+1] $for every i between 1 and m-1 a[b[i]] < a[b[i+1]] for every i between 1 and m-1 m>0 Now you need to find the k-th smallest lexicographically good array.
输入描述: The first line has two integer n,k
The second line has n integer a[i]
输出描述: If there is no solution, just only output -1, else output two lines, the first line has an integer m, the second line has m integer b[i]
备注: 1<=n <= 10^5
1<=k<=10^(18)
1<=a[i]<=10^9
示例 1 输入 3 2 1 2 3 输出 2 1 2
示例 2 输入 3 1000000000 1 2 3 输出 -1
以a为坐标,先离散化,然后从后往前做,这样就可以找到当前下表最大的可以到第几排列。
如果大于等于k就把他放进去,或者直接减掉。
比如说1,5 那么5的时候就是1,1的时候就是2,如果k==1的时候,那么f[1]=2,把1放进去,k–,k=0,就跳出,答案就是1,k=2的时候f[1]=2,k–,k=1,f[2]=1,就把1,2放进去。K=3的时候,f[1]比k小,那么就不放了,k-=2,k=1,f[2]=1,然后就把2放进去,答案就是2
因为这个是小于,所以查找的时候需要加一,然后在add的时候如果比k大要记成k+1,否则最后一个样例会超过ll的范围。。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2000000;
ll a[maxn+5],f[maxn+5],c[maxn+5],tmp[maxn+5],ans[maxn+5];
int cnt;
ll n,k;
ll lowbit(ll x)
{
return x&(-x);
}
void add(ll x,ll val)
{
for(ll i=x;i>=1;i-=lowbit(i))
{
c[i]+=val;
if(c[i]>k)
c[i]=k+1;
}
}
ll query(ll x)
{
ll ans=0;
for(ll i=x;i<=cnt;i+=lowbit(i))
{
ans+=c[i];
if(ans>k)
return k+1;
}
return ans;
}
int main()
{
scanf("%lld%lld",&n,&k);
for(ll i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
tmp[i]=a[i];
}
sort(tmp+1,tmp+1+n);
cnt=unique(tmp+1,tmp+1+n)-(tmp+1);
for(int i=1;i<=n;i++)
a[i]=lower_bound(tmp+1,tmp+1+cnt,a[i])-tmp;
for(int i=n;i>=1;i--)
{
f[i]=query(a[i]+1)+1;
if(f[i]>k)
f[i]=k+1;
add(a[i],f[i]);
}
int ctans=0,maxn=0;
for(int i=1;i<=n;i++)
{
if(a[i]>maxn)
{
if(f[i]>=k)
ans[++ctans]=i,k--,maxn=a[i];
else
k-=f[i];
}
if(k<=0)
break;
}
if(k>0)
{
printf("-1\n");
return 0;
}
printf("%d\n",ctans);
for(int i=1;i<=ctans;i++)
printf("%d%c",ans[i],i==ctans?'\n':' ');
return 0;
}