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 ...
随机推荐
- 普及C组第三题(8.13)
2334. [NOIP普及组T2]战斗 (File IO): input:fight.in output:fight.out 时间限制: 1000 ms 空间限制: 524288 KB 开始贴图:. ...
- PMP概略学习上--基本思想和概念
1 前言 花了10天左右的时间,对PMP(Project Management Professional,项目管理专业人士)考试认证做了一个概略学习.此次学习的目的是整体了解项目管理知识,并不是以考试 ...
- 一起了解 .Net Foundation 项目 No.3
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. AutoMapper Au ...
- jquery--获取input checkbox是否被选中以及渲染checkbox选中状态
jquery获取checkbox是否被选中 html <div><input type="checkbox" id="is_delete" n ...
- Linux06——安装JDK、Tomcat、Eclipse
一.安装JDK(具体解压命令在Linux02中) ①将JDK解压到opt目录下(opt就是文件夹) ②配置环境变量 vim /etc/profile JAVA_HOME=/opt/jdk1.8.0 ...
- extern "C" 与函数重载
前言 如果向要在一个文件中使用另一个文件中的变量,不能在头文件中定义全局变量,因为被多个文件包含后会导致编译出错,并且静态的static变量,只能在本文件内使用,这时候就可以使用extern关键字. ...
- django项目日志
接口中,写一条日志: 日志工具文件如下:
- [爬坑日记] 安卓模拟器1903蓝屏 没开hyper-v
先说解决方案: 我在升级完1903之后短短几个小时蓝屏了两次 还是不同原因,不由得开始怀疑这个版本的稳定性,随后发现只有启动安卓模拟器的时候必然蓝屏(还有一次蓝屏实在安装驱动的时候) 经过百度得知需要 ...
- Codeforces Round #622 (Div. 2)C2
题意 N长度为500000以内,一个数字两边的数字不能都比他高,最多高一边 求他最大sum.叙述有问题,直接看样例 3 10 6 8 因为6左右都比他高,选择10 6 6或者6 6 8,sum明显前 ...
- Go流程结构(for)
一.程序的流程结构 程序的流程控制结构一共有三种:顺序结构,选择结构,循环结构. 顺序结构:代码从上向下逐行的执行 选择结构:条件满足,某些代码才会被执行.0-1次 if语句,switch语句 循环结 ...