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 ...
随机推荐
- js 时间延迟
dojosetTimeout(dojo.hitch(this, function(){ this.onClickCount(); }), 3000); 普通应用在js中,延迟执行函数有两种,setTi ...
- 自定义jstl标签*
原文链接:https://www.it610.com/article/442039.htm 步骤如下: 1.写tld文档:用来指定标签的名字,标签库等. 2.写标签处理器类. 3.配置到web.xml ...
- C++类几种初始化的顺序
首先给段代码: class A{ public: ; A():x(){cout<<"A(): x="<<x<<endl;} A(int a):x ...
- OPC通信原理在数采中的应用
OPC通信原理在数采中的应用 OPC是Object Linking and Embedding(OLE)for Process Control的缩写,它是微软公司的对象链接和嵌入技术在过程控制方面的应 ...
- OmniGraffle原型案例 | 某APP产品原型PDF文件分享之二
1.从 App 首页进入商城 App底部 Tab有社区.商城,我们点击「商城」进入「乐宠商城」,下面简称商城.商城采用的是经典的宫格式导航设计(还有普通列表式.和瀑布流列表式.或两者皆有等),主要有搜 ...
- 详解C++11智能指针
前言 C++里面的四个智能指针: auto_ptr, unique_ptr,shared_ptr, weak_ptr 其中后三个是C++11支持,并且第一个已经被C++11弃用. C++11智能指针介 ...
- String.valueOf()和toString()的区别
1.String.valueOf(): Object obj=null; String str=""; str=String.valueOf(obj); //str=obj.toS ...
- 第四十一篇 入门机器学习——Numpy的基本操作——聚合操作
No.1. 对向量元素求和使用np.sum,也可以使用类似big_array.sum()的方式 No.2. 对向量元素求最小值使用np.min,求最大值使用np.max,也可以使用类似big_arra ...
- C++-HDU1166-敌兵布阵[数据结构][树状数组]
单点修改+区间查询=树状数组 空间复杂度O(n) 时间复杂度O(mlogn) #include <set> #include <map> #include <cmath& ...
- python UI自动化生成BeautifulReport测试报告并保存截图
前面已经写过利用BeautifulReport生成测试报告,那么接下来讲讲如何在测试报告里面保存截图 首先需要在测试用例中定义一个截图的方法: # 截图方法 """ os ...