2018牛客多校第五场 H.subseq
题意:
给出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的更多相关文章
- 2018牛客多校第五场 E.room
题意: 一共有n个宿舍,每个宿舍有4个人.给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动. 题解: 对于第一年的每个宿舍,向今年的每种组合连边.流量为1,费用为(4 - 组合中已在该宿 ...
- 2019牛客多校第五场H - subsequence 2 拓扑
H - subsequence 2 题意 要你使用前\(m\)个小写字母构造一个长度为\(n\)的字符串 有\(m*(m-1)/2\)个限制条件: \(c_{1} .c_{2}. len\):表示除去 ...
- 牛客多校第五场 H subsequence 2 拓扑排序
题意: 给你长度最长为1000的字符串,这个字符串中最多有10种字母,每次给你两种字母,输出这两种字母在字符串中的相对位置,问你这个字符串原本是什么样子,如果不存在则输出-1 题解: 把整个字符串看作 ...
- 牛客多校第五场 F take
链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...
- 牛客多校第五场 J:Plan
链接:https://www.nowcoder.com/acm/contest/143/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 牛客多校第五场-D-inv
链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...
- 牛客多校第五场 F take 期望转化成单独事件概率(模板) 树状数组
链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 Kanade has n boxes , the i-th box has p[i] proba ...
- 牛客多校第五场 E room 二分图匹配 KM算法模板
链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...
- 字符串dp——牛客多校第五场G
比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...
随机推荐
- iOS 库 开发小结
1.基本用法 定义类,导出头文件,注意头文件,库文件的search path 2.加载资源 - 使用主工程的文件,耦合性太强 - 封装到NSBundle中 NSBundle可以封装xib storyb ...
- 使用postman实现半自动化
前些日子项目要上一个活动,其中有一个功能是幸运大转盘,用户可以随机抽奖,奖品有多种满减券及多种商品,但是奖品都是有抽中概率的,且有的商品还设置有库存,所以测试点便是测试抽奖的概率和库存.接下来拆分一下 ...
- TPO-15 C2 Performance on a biology exam
TPO-15 C2 Performance on a biology exam 第 1 段 1.Listen to part of a conversation between a Student a ...
- Linux搭建mysql、apache、php服务总结
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2018 年 04 月 22 日 原地址:https://niaobulashi.com/archives/linux-mysql-apache- ...
- python 终极篇 --- django 路由系统
URL配置 基本格式: from django.conf.urls import url urlpattern ...
- Python3 标准库:os
1.重命名 import os os.rename('test.txt','x.txt') #重命名文件或目录 import os os.renames('a/123.txt','a/b/h.txt' ...
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- Codeforces 96D Volleyball(最短路径)
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't b ...
- Python中的namespace package
在Python 3.3之前,一个目录想被当成package被导入,必须包含__init__.py文件:而在Python 3.3及以后的版本中,__init__.py文件可以不需要,直接使用import ...
- Median Weight Bead(最短路—floyed传递闭包)
Description There are N beads which of the same shape and size, but with different weights. N is an ...