当前位置:网站首页>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;
}边栏推荐
- Burp Suite-第五章 如何使用Burp Target
- 2022/7/17 exam summary
- The difference between throw and throws?
- One click deployment lamp and LNMP architecture
- awk作业
- I am 35 years old.
- CentOS install mysql5.7
- General Dao interface design
- JSP built-in object (implicit object)
- Apple's tough new rule: third-party payment also requires a percentage, and developers lose a lot!
猜你喜欢

The idea of stack simulating queue

SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据

BGP的基本配置

Ten thousand words long article | deeply understand the architecture principle of openfeign

【 fastjson1.2.24反序列化漏洞原理代码分析】

Team members participate in 2022 China multimedia conference

If the thread crashes, why doesn't it cause the JVM to crash? What about the main thread?

咱就是来聊聊并发编程的三大核心问题。

Vscode utility shortcut

Burp Suite-第四章 SSL和Proxy高级选项
随机推荐
C# 获取选择文件信息
Date and time function of MySQL function summary
The difference between FileInputStream and bufferedinputstream
The first ide overlord in the universe, replaced...
JSP built-in object (implicit object) -- input / output object
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
vscode 实用快捷键
2022/7/7 exam summary
I am 35 years old.
General Dao interface design
C# WinForm中PreviewKeyDown、KeyDown、KeyPress、KeyUp区别与联系
Brief introduction to XML
What are the differences between FileInputStream and bufferedinputstream?
数组的介绍--Array
正则表达式作业
第三天作业
SPSS uses kmeans, two-stage clustering and RFM model to study the behavior law data of borrowers and lenders in P2P network finance
BGP的基本配置
2022-024arts: Longest valid bracket
Flex three column layout
