牛客多校5H subseq 【线段树】

链接:https://www.nowcoder.com/acm/contest/143/H
来源:牛客网
 

题目描述

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. 1<=b[i]<=n

  2. b[i]<b[i+1] for every i between 1 and m-1

  3. a[b[i]] < a[b[i+1]] for every i between 1 and m-1

  4. 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]

题意:给定一个a数组,求其第k大严格递增的下标字典序的序列。例如3 2 4 5  序列排序为1; 13;134;2;23;234;3;34;4;

分析:可以通过线段树维护出以每个数作为子序列的首个字符的递增子序列的个数,设为num[i]。维护过程,依次把最大值的序列个数加入到更新到线段树中,然后求次大值是可以通过查询其右边的和,因为是从大到小依次加入,所以每次查询时,比其先加入的肯定比它大。然后即可通过对比k和num[i]的大小来求出这个子序列。

#include<bits/stdc++.h>
#define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
using namespace std;
typedef long long LL;
const LL INF=1e18;
const int N=5e5+10;
LL tree[N<<2];
int b[N],ans[N];
LL num[N];
struct node
{
    int val,in;
    bool operator<(const node &c)
    {
        if(val==c.val)
            return in<c.in;
        return val>c.val;
    }
}a[N];
void build(int rt,int l,int r)
{
    if(l==r)return ;
    int mid=(l+r)>>1;
    build(Lson);
    build(Rson);
}
void update(int x,LL val,int rt,int l,int r)
{
    if(l==r)
    {
        tree[rt]=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(x<=mid)
        update(x,val,Lson);
    else
        update(x,val,Rson);
    tree[rt]=min(INF,tree[rt<<1]+tree[rt<<1|1]);
}
LL query(int x,int y,int rt,int l,int r)
{
    if(y<l||x>r)return 0ll;
    if(x<=l&&r<=y)
        return tree[rt];
    int mid=(l+r)>>1;
    return min(INF,query(x,y,Lson)+query(x,y,Rson));
}
int main()
{
    int n;
    LL k;
    scanf("%d%lld",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].val);
        b[i]=a[i].val;
        a[i].in=i;
    }
    build(1,1,n);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        num[a[i].in]=query(a[i].in,n,1,1,n)+1;

        update(a[i].in,num[a[i].in],1,1,n);
    }
    //for(int i=1;i<=n;i++)printf("%lld ",num[i]);printf("\n");
    int top,maxx=-1;
    top=0;
    for(int i=1;i<=n;i++)
    {
        if(k==0)break;
        if(b[i]<=maxx)continue;
        if(k<=num[i])
        {
            ans[++top]=i;
            maxx=b[i];
            k--;
        }
        else
            k-=num[i];
    }
    if(k!=0||top==0)
        printf("-1\n");
    else
    {
        printf("%d\n",top);
        for(int i=1;i<=top;i++)
            printf("%d%c",ans[i]," \n"[i==top]);
    }
    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值