题意:

  给出a数组的排列。求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0。

题解:

  用树状数组倒着求出以每个数为首的递增子序列个数。若总的个数之和小于k则输出-1。

  总的个数可能非常大而k<=1e18。所以要判下上界。

  最后从1~n扫一遍。当前数大于上一个加入答案的数时,若以它为首的递增子序列个数小于k,则用k减去那个个数,否则将这个数加入答案并将k-1(即减去后面不再加数的情况)。

  k = 0时跳出循环。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+;
const ll inf = 1e18+;
int n, tot, pre;
ll k, sum;
int a[N], id[N], ans[N];
ll tre[N], val[N];
ll add(ll x, ll y) { return min(x + y, inf); }
void update(int pos, ll val) {
while(pos > ) {
tre[pos] = add(tre[pos], val);
pos -= pos&(-pos);
}
}
ll query(int pos) {
ll res = ;
while(pos <= n) {
res = add(res, tre[pos]);
pos += pos&(-pos);
}
return res;
}
int main() {
scanf("%d%lld", &n, &k);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
id[i] = a[i];
}
sort(id+, id+n+);
for(int i = n; i >= ; i--) {
int p = lower_bound(id+, id+n+, a[i])-id;
val[i] = query(p+)+;
update(p, val[i]);
}
for(int i = ; i <= n; i++) sum = add(sum, val[i]);
if(sum < k) {
puts("-1");
return ;
}
for(int i = ; i <= n; i++) {
if(k == ) break;
if(a[i] <= a[pre]) continue;
if(val[i] < k) k -= val[i];
else {
ans[++tot] = i;
k--;
pre = i;
}
}
printf("%d\n", tot);
for(int i = ; i < tot; i++) printf("%d ", ans[i]);
printf("%d\n", ans[tot]);
}

2018牛客多校第五场 H.subseq的更多相关文章

  1. 2018牛客多校第五场 E.room

    题意: 一共有n个宿舍,每个宿舍有4个人.给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动. 题解: 对于第一年的每个宿舍,向今年的每种组合连边.流量为1,费用为(4 - 组合中已在该宿 ...

  2. 2019牛客多校第五场H - subsequence 2 拓扑

    H - subsequence 2 题意 要你使用前\(m\)个小写字母构造一个长度为\(n\)的字符串 有\(m*(m-1)/2\)个限制条件: \(c_{1} .c_{2}. len\):表示除去 ...

  3. 牛客多校第五场 H subsequence 2 拓扑排序

    题意: 给你长度最长为1000的字符串,这个字符串中最多有10种字母,每次给你两种字母,输出这两种字母在字符串中的相对位置,问你这个字符串原本是什么样子,如果不存在则输出-1 题解: 把整个字符串看作 ...

  4. 牛客多校第五场 F take

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...

  5. 牛客多校第五场 J:Plan

    链接:https://www.nowcoder.com/acm/contest/143/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  6. 牛客多校第五场-D-inv

    链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...

  7. 牛客多校第五场 F take 期望转化成单独事件概率(模板) 树状数组

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 Kanade has n boxes , the i-th box has p[i] proba ...

  8. 牛客多校第五场 E room 二分图匹配 KM算法模板

    链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...

  9. 字符串dp——牛客多校第五场G

    比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...

随机推荐

  1. svn 撤销 已提交的修改

    1.保证我们拿到的是最新代码:  svn update  假设最新版本号是28.  2.然后找出要回滚的确切版本号:  svn log [something]  假设根据svn log日志查出要回滚的 ...

  2. 如何利用Navicat导入/导出mssql中的数据

    sqlserver,在第一次使用该软件进行"连接"的时候,会提示安装"Microsoft Sqlsever Navicat Client.",这时直接点击&qu ...

  3. vue watch监控对象

    1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console. ...

  4. CSP201412-2:Z字形扫描

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  5. 在几份docx文档中里查找某个值

    import docx, os def readDocx(fileName): doc = docx.Document(fileName) content = '\n'.join([para.text ...

  6. 校招小白机考入坑之从键盘输入java的各种数据类型

    //1.从键盘输入一个整型(其他基本类型类似) Scanner sc =new Scanner(System.in); sc.hasNextInt(); int str1 = sc.nextInt() ...

  7. 56[LeetCode] .Merge Intervals

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  8. Redis+Keepalived高可用方案详细分析

    背景 目前,Redis集群的官方方案还处在开发测试中,未集成到稳定版中.且目前官方开发中的Redis Cluster提供的功能尚不完善(可参考官方网站或http://www.redisdoc.com/ ...

  9. Fafa and the Gates(模拟)

    Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens ...

  10. 《梦断代码Dreaming In Code》阅读笔记(二)

    这段时间一口气读了5章,所以想着现在一块写阅读笔记. 在阅读的这段时间,我一直是晚上进行阅读,很多时候都是读完一看已经一个小时了,效果还不错.闲话不表,说说阅读心得. 关于底层语言或是低级语言,我之前 ...