Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) - D2. Optimal Subsequences (Hard Version)(主席树)
题意:一共有$n$个数,$m$次询问,每次询问包括$k、pos$两个数,需要你从这$n$个数里面找出$k$个数,使得他们的总和最大,如果有多种情况,找出序号字典序最小的一组,然后输出这个序列中第$pos$个数的值。
思路:根据贪心的思想,把这$n$个数按大到小排个序$($相同大小按下标从小到大$)$,再按照每个元素的下标依次放入主席树中,对于每次询问,查找前$k$个数中第$pos$大的数,但这个查找到的值是下标,再把这个下标对应到数值即可,体现在代码中的$b[]$数组中。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector> using namespace std; const int N = ; struct date {
int val, pos;
}; struct node {
int l, r, sum;
}; int n, q, cnt, root[N], b[N];
date a[N];
node tree[ * N]; bool cmp(date one, date two)
{
if (one.val != two.val) return one.val > two.val;
return one.pos < two.pos;
} void update(int l, int r, int &x, int y, int pos)
{
tree[++cnt] = tree[y];
tree[cnt].sum++, x = cnt;
if (l == r) return;
int mid = (l + r) / ;
if (mid >= pos) update(l, mid, tree[x].l, tree[y].l, pos);
else update(mid + , r, tree[x].r, tree[y].r, pos);
} int query(int l, int r, int x, int y, int k)
{
if (l == r) return l;
int mid = (l + r) / ;
int sum = tree[tree[y].l].sum - tree[tree[x].l].sum;
if (sum >= k) return query(l, mid, tree[x].l, tree[y].l, k);
else return query(mid + , r, tree[x].r, tree[y].r, k - sum);
} int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%d", &a[i].val);
a[i].pos = i;
}
sort(a + , a + n + , cmp);
for (int i = ; i <= n; i++) {
b[a[i].pos] = a[i].val;
update(, n, root[i], root[i - ], a[i].pos);
}
scanf("%d", &q);
while (q--) {
int k, pos;
scanf("%d%d", &k, &pos);
printf("%d\n", b[query(, n, root[], root[k], pos)]);
}
return ;
}
Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) - D2. Optimal Subsequences (Hard Version)(主席树)的更多相关文章
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3
A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学
F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和
E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心
D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造
C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心
B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) pos ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题
A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy
//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box
#include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem
//只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...
随机推荐
- [BZOJ4310] 跳蚤 - 后缀数组,二分,ST表
[BZOJ4310] 跳蚤 Description 首先,他会把串分成不超过 \(k\) 个子串,然后对于每个子串 \(S\) ,他会从 \(S\) 的所有子串中选择字典序最大的那一个,并在选出来的 ...
- 数据库 oracle 函数
static OracleConnection mQracleConnecting = null; public static OracleConnection QracleConnecting { ...
- C++——程序的结构
1.作用域和可见性 1.1 函数原型中的参数其作用域仅在()内.因此参数名称可有可无,但是参数类型需要声明. 1.2 块作用域 在块中声明的标识符其作用域自声明处起,限于块中. 1.3 类作用域 类作 ...
- (转)数据索引BTree
.B-tree 转自:http://blog.csdn.net/hbhhww/article/details/8206846 B-tree又叫平衡多路查找树.一棵m阶的B-tree (m叉树)的特性如 ...
- C short类型的内存分析
#include<stdio.h> #include<limits.h> void main(){ //printf("short%d, int%d, long%d ...
- 【算法学习记录-排序题】【PAT A1012】The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- layui之表单验证
这篇文章的表单验证我只是随便记录下,望各位看官理解 1.排序 验证 html代码 <div class="layui-form-item"> <label cla ...
- 如何查看mac多少位的操作系统?
1.点击工具栏左上角点击 (苹果Logo)标志,关于本机 --> 更多信息 --> 系统报告 -->(左侧栏中)软件 (有的电脑是没有的例如第一张图) 2. 输入命令 una ...
- beego 页面布局
模板 this.Layout = "admin/layout.html" this.TplName = "admin/list.html" 在layout.ht ...
- wamp配置本地多站点。
' 进入C:\wamp64\wamp64\bin\apache\apache2.4.37\conf\http.conf 首先确保httpd-vhosts.conf扩展文件引入进来了,部分版本默认是不引 ...