当前位置:网站首页>B title: razlika priority queue approach
B title: razlika priority queue approach
2022-07-26 08:18:00 【Fanshui 682】
subject
My solution is to use the priority queue to reduce the complexity when calculating the minimum later . The point is to simulate the smallest difference , So all the difference push Go to the priority queue , And then one by one “ Probe ”, If you meet someone who is not inside pop In this way, it can be simplified into O(n), Although it looks like a double cycle .
#include <bits/stdc++.h>
#define int long long
#define CIO std::ios::sync_with_stdio(false)
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define nep(i, r, l) for (int i = r; i >= l; i--)
#define pii pair<int,int>
using namespace std;
const int N=2e6+5;
int a[N];
int d[N];
priority_queue<pii,vector<pii>,greater<pii> > q;
int pqd[N];
void work(){
int n,k;
cin>>n>>k;
rep(i,1,n){
cin>>a[i];
}
sort(a+1,a+n+1);
d[1]=0;
rep(i,2,n){
d[i]=a[i]-a[i-1];
}
int cnt=0;
int sum=0,mi=0,xiao=INT_MAX,mii=INT_MAX;
rep(i,2,n-k){
q.push({d[i],i});
}
rep(i,2,k+1){
sum=a[i+n-k-1-1]-a[i-1];
q.push({d[i+n-k-1],i+n-k-1});
while (!q.empty()){
if (!(i<=q.top().second&&q.top().second<=i+n-k-1)){
q.pop();
}
else{
xiao=q.top().first;
break;
}
}
// cout<<sum<<" "<<xiao<<endl;
mii=min(mii,sum+xiao);
}
cout<<mii;
}
signed main(){
CIO;
work();
return 0;
}边栏推荐
- An empirical study on urban unemployment in Guangxi (Macroeconomics)
- 日常一记(11)--word公式输入任意矩阵
- Share high voltage ultra low noise LDO test results
- Function default parameters, arrow functions, and remaining parameters in ES6 - explanation
- How WPS sets page headers page by page
- JSP built-in object (implicit object) -- input / output object
- 线程崩了,为什么不会导致 JVM 崩溃呢?如果是主线程呢?
- flex三列布局
- vscode国内的镜像服务器加速
- Exam summary on July 13, 2022
猜你喜欢

Beauty naked chat for a while, naked chat over the crematorium!

Pycharm code specification tool flake8

JSP implicit object -- scope
![[fastjson1.2.24 deserialization vulnerability principle code analysis]](/img/14/8f6a75fe5f06c19eeff9c7204979c3.png)
[fastjson1.2.24 deserialization vulnerability principle code analysis]

Strtus2历史漏洞复现

The most complete network: detailed explanation of six constraints of MySQL

2022-07-13 group 5 Gu Xiangquan's learning notes day06

基础乐理 节奏联系题,很重要

How WPS sets page headers page by page

Burp Suite-第九章 如何使用Burp Repeater
随机推荐
Burp Suite - Chapter 2 burp suite proxy and browser settings
Burp suite Chapter 6 how to use burp spider
Day 3 homework
2022-07-09 group 5 Gu Xiangquan's learning notes day02
[June 29, 2022] examination summary
关于期刊论文所涉及的一些概念汇编+期刊查询方法
Reading and writing properties file
咱就是来聊聊并发编程的三大核心问题。
Common Oracle functions
SPSS uses kmeans, two-stage clustering and RFM model to study the behavior law data of borrowers and lenders in P2P network finance
Burp Suite-第八章 如何使用Burp Intruder
99 multiplication table and inverted triangle 99 multiplication table
Spotty music data client_ ID account
第三天作业
2022-07-08 group 5 Gu Xiangquan's learning notes day01
Basic configuration of BGP
Summary of traversal methods of list, set, map, queue, deque and stack
BGP --- 边界网关协议
Awk operation
The difference between equals() and = =
