题意:一共有$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)(主席树)的更多相关文章

  1. 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) 思路:我们想一下如果题目说的是最 ...

  2. 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 ...

  3. 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 ...

  4. 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, ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy

    //因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. 记录 shell学习过程(9)正则表达式 转自树明聊运维

    正则表达式 正则表达式介绍 特殊字符 POSIX特殊字符 一.正则表达式介绍 正则表达式是一种文本模式匹配,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符") ...

  2. ICPC2019 亚洲区域赛 南京站

    蒟蒻终于打完了人生的第一场ICPC了. 终榜去星后rank36,AG,和AU差几十罚时了. 虽有遗憾但总体也是正常发挥了. 不愿再去对比赛做什么回顾,甚至很不愿去想.很多题已经在能力之外,即便是平常熟 ...

  3. path is not a working copy directory

    svn: 'G:\chengXu\2017_Year\JFW\Easy7\WebRoot' is not a working copy directory 解决方法: (1)原因:eclipse把sr ...

  4. 2 request的get和post方法

    requests的get方法 1 在百度里面查询关键字的方法,并获取带百度当前页面 import requests keywords = input('请输入>>>').strip( ...

  5. layui下select禁止点击

    layui下拉选择框select禁止点击_设置禁用_设置不可操作的实现方法 直接上代码: <form class="layui-form"> <!-- 提示:如果 ...

  6. 2.8 (显示、隐式、线程休眠) selenium 等待方式 ❀

    http://blog.csdn.net/pf20050904/article/details/20052485 http://www.cnblogs.com/hellokitty1/p/629584 ...

  7. 百炼OJ - 1004 - 财务管理

    题目链接:http://bailian.openjudge.cn/practice/1004/ 思路 求和取平均... #include <stdio.h> int main() { fl ...

  8. Linux上查看当前系统各内存分区信息

    命令 ulimit -a -a 查看所有信息,同理,也可以例如 ulimit -s 只查看栈占内存信息

  9. C++四种初始化方式

    1. 直接初始化直接调用与实参匹配的构造函数,形式如“T t(u)”.2. 拷贝初始化:复制初始化首先使用指定构造函数创建一个临时对象,然后用复制构造函数将那个临时对象复制到正在创建的对象”,形式如“ ...

  10. Git的基本使用 -- 历史版本、版本回退

    查看提交的日志(历史版本) git log 不能查看已删除的commit记录 git reflog 可以查看所有分支的所有操作记录,包括已删除的commit记录 版本回退 git reset --ha ...