Problem Statement

Given are a permutation P=(P_1,P_2,\ldots,P_N)P=(P1​,P2​,…,PN​) of (1,2,\ldots,N)(1,2,…,N) and a positive integer KK.

For each i=K,K+1,\ldots,Ni=K,K+1,…,N, find the following.

  • The KK-th greatest value among the first ii terms of PP.

Constraints

  • 1 \leq K \leq N \leq 5 \times 10^51≤K≤N≤5×105
  • (P_1,P_2,\ldots,P_N)(P1​,P2​,…,PN​) is a permutation of (1,2,\ldots,N)(1,2,…,N).
  • All values in input are integers

解法:

这就是1个TOPK问题。

java的最小堆、最大堆可以使用PriorityQueue<Integer>

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.TreeSet; public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N,K;
String[] words = reader.readLine().split("\\s+");
N = Integer.parseInt(words[0]);
K = Integer.parseInt(words[1]); int[] P = new int[N];
words = reader.readLine().split("\\s+");
for(int i=0;i<N;i++){
P[i] = Integer.valueOf(words[i]);
}
PriorityQueue<Integer> pq = new PriorityQueue<>(K);
for(int i =0;i<K;i++){
pq.add(P[i]);
}
StringBuilder sb = new StringBuilder();
sb.append(pq.peek()+"\n");
for(int i=K;i<N;i++){
int val = P[i];
if( val > pq.peek()){
pq.poll();
pq.add(val);
}
sb.append(pq.peek()+"\n");
// System.out.println(pq.peek());
}
System.out.println(sb);
reader.close();
} }

[https://atcoder.jp/contests/abc234/tasks/abc234_d Prefix K-th Max] 最小堆实现的更多相关文章

  1. Atcoder Regular 098 区间Pre=Xor Q询问区间连续K去最小值最小极差

    C 用scanf("%s")就会WA..不知道为什么 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset ...

  2. AtCoder Beginner Contest 134-E - Sequence Decomposing

    (https://atcoder.jp/contests/abc134/tasks/abc134_e) 题意:找出最小个数的最长上升子序列 思路:找出最长上升子序列的最小个数,只需要找出每个最小上升子 ...

  3. ARC085E MUL

    https://atcoder.jp/contests/arc085/tasks/arc085_c 题目大意 略 解法 最小割即可. 直接建图有负边,但是因为我们知道最后在割上的边数一定为 \(N\) ...

  4. Atcoder E - Knapsack 2 (01背包进阶版 ex )

    E - Knapsack 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...

  5. Atcoder Beginner Contest 121 D - XOR World(区间异或和)

    题目链接:https://atcoder.jp/contests/abc121/tasks/abc121_d 题目很裸(Atcoder好像都比较裸 就给一个区间求异或和 n到1e12 肯定不能O(n) ...

  6. AtCoder Beginner Contest 120 D - Decayed Bridges(并查集)

    题目链接:https://atcoder.jp/contests/abc120/tasks/abc120_d 题意 先给m条边,然后按顺序慢慢删掉边,求每一次删掉之后有多少对(i,j)不连通(我应该解 ...

  7. Atcoder D - Knapsack 1 (背包)

    D - Knapsack 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...

  8. atcoder B - Frog 2 (DP)

    B - Frog 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...

  9. atcoder A - Frog 1(DP)

    A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...

  10. AtCoder Grand Contest 031 B - Reversi

    https://atcoder.jp/contests/agc031/tasks/agc031_b B - Reversi Time Limit: 2 sec / Memory Limit: 1024 ...

随机推荐

  1. 双层nn逻辑回归BP过程的另外一种解释

    双层nn逻辑回归 吴恩达的双层nn逻辑回归的一些符号说明比较复杂导致视频难以理解,这里简单做一个说明 注意:阅读下面内容需要具备基本的矩阵求导知识 下面是手推过程,有空在进行修改

  2. 使用ethtool排查网卡速率问题

    今天去现场帮一个客户排查备份网络速率问题. 用户期望是万兆的速率,但实际上目前只有千兆,因为目前上面运行着数据库,且数据量较大,千兆的备份网络速率不能满足用户备份数据库的时长要求. 首先,确认备份网络 ...

  3. ListView改变行高的技巧

    改变 ListView 的行高 (Line Height) (cjc,2009.6.2) 改变 ListView 的行高 (Line Height) (cjc,2009.6.2) ListView在R ...

  4. Trino-登录WebUI页面报错,日志中提示:404 Not Foud. (Zookeeper占用8080端口,与Trino的端口冲突)

    问题描述 启动Trino客户端执行show catalogs时报错:Error starting query at http://localhost:8080/v1/statement returne ...

  5. Codeforces Round #847 (Div. 3) A-G

    比赛链接 A 题意 判断输入字符串与 \(\pi\) 的最长前缀匹配,不超过 \(30\) 位. 题解 知识点:模拟. 抄样例最后一个 \(30\) 都正确的,直接匹配. 时间复杂度 \(O(1)\) ...

  6. NC24870 [USACO 2009 Dec G]Video Game Troubles

    题目链接 题目 题目描述 Farmer John's cows love their video games! FJ noticed that after playing these games th ...

  7. mysql 外键索引入门介绍,为什么工作中很少有人使用?

    背景 以前工作学习中,一直被告诫不要使用外键,所以也没有仔细整理过. 这里记录一下笔记. 外键 是什么? MySQL 的外键(Foreign Key)是一种关系型数据库中用于建立表与表之间关联关系的重 ...

  8. Oracle 10gR2新SQL提示——opt_param

    [English] 搜索Internet 搜索 HelloDBABA 首页 English 中文 技术文档 文章 案例 产品及下载 产品 >> FyDB OraTracer FySafe ...

  9. 一键部署Home Assistant ubuntu 20.4.3 树莓派3b+脚本

      树莓派3b+安装好 Ubuntu Server 20.04.3 LTS 32bit 后即可适用此脚本,其他版本树莓派/系统可能需要微调脚本*为方便一些未知/已知错误排查 脚本存在冗余部分,足够了解 ...

  10. C# EnumWindows示例代码

    代码开箱即用,唯一需要处理的就是要提供一个进程的pid. using System; using System.Collections.Generic; using System.Linq; usin ...