整数删除
添加链接描述
这题真是让人很头大,这题的写法是优先队列+模拟链表
优先队列
也就是堆,可以在logn的时间复杂度下就找到最小值,就是堆排序中的那个堆,下面讲解几个优先队列的常见用法
1.最普通的数值用法 (默认大根堆,从大到小输出)
对于大根堆我们可以理解less 就是越来越少,所以我们需要用<比较
小根堆事greater就是越来越大,所以我们需要>比较
#include <bits/stdc++.h>
using namespace std;
int N;
int main()
{
cin>>N;
//第一个参数 队列中的类型,第二个是实现的集合,一般就是vector,最后一个参数指定大根堆或者小根堆
//默认大根堆,less<int> greater<int> 大根堆
priority_queue<int> pq;
for(int i=1;i<=N;i++){
int temp;
cin>>temp;
pq.push(temp);
}
//输出
while(!pq.empty()){
//拿到值
int a =pq.top();
pq.pop();
cout<<a<<" ";
}
return 0;
}
小根堆
#include <bits/stdc++.h>
using namespace std;
int N;
int main()
{
cin>>N;
//第一个参数 队列中的类型,第二个是实现的集合,一般就是vector,最后一个参数指定大根堆或者小根堆
//小根堆的参数
priority_queue<int,vector<int>,greater<int>> pq;
for(int i=1;i<=N;i++){
int temp;
cin>>temp;
pq.push(temp);
}
//输出
while(!pq.empty()){
//拿到值
int a =pq.top();
pq.pop();
cout<<a<<" ";
}
return 0;
}
自定义类型
从上面的例子可以看出,在c++的底层,其实就是需要比较运算符< >来比较当前值的大小,从而进行排序,但对于自定义的类型,我们的堆是无法判断那个值是更大或者更小的,所以对于这种新定义的类型,我们需要重载运算符< >
#include <bits/stdc++.h>
using namespace std;
int N;
typedef struct node{
int index;
int value;
node(int i,int v){
this->index=i;
this->value=v;
}
bool operator < (const node &b) const{
return this->value <b.value;
}
}node;
int main()
{
cin>>N;
//第一个参数 队列中的类型,第二个是实现的集合,一般就是vector,最后一个参数指定大根堆或者小根堆
//小根堆的参数
priority_queue<node,vector<node>> pq;
for(int i=1;i<=N;i++){
int a;
cin>>a;
node temp(i,a);
pq.push(temp);
}
//输出
while(!pq.empty()){
//拿到值
node a =pq.top();
pq.pop();
cout<<a.value<<" ";
}
return 0;
}
首先这题我们需要用优先队列,每一次按照logn的时间复杂度拿到最大和最小值,其次我们需要pre,nxt数组来模仿链表实现O(1)的删除复杂度。并且由于优先队列只记得最初的值,所以我们需要一个buffer区来控制我们是否有缓存然后来更新缓存。
#include <bits/stdc++.h>
using namespace std;
int N,K;
//数组模拟链表 0号节点是头结点!
vector<int> pre;
vector<int> nxt;
vector<long long> Vnum(1,0);
vector<long long> buffer;
typedef struct node{
int index;
long long value;
node(int i,int value){
this->index =i;
this->value=value;
}
//重载运算符 要根据是要小根堆还是大根堆重载不同的运算符
bool operator >(const node &b) const{
return this->value >b.value;
}
}node;
void pr(){
for(int i=nxt[0];i<=N;i=nxt[i]){
cout<<Vnum.at(i)<<" ";
}
}
int main()
{
// 请在此输入您的代码
cin>>N>>K;
//一个堆,可以在logn的时间复杂度下找到最值 默认是大根堆
//大根堆:最大值在最上面,但是这题是找最小值,所以我们要找最小值 要小根堆
priority_queue<node,vector<node>,greater<node>> pq;
//为了不出bug这里建议开大一点
pre.assign(N*2,0);
nxt.assign(N*2,0);
buffer.assign(N*2,0);
//自定义结构体
for(int i=1;i<=N;i++){
int temp;
cin>>temp;
node Node(i,temp);
//放入元素
pq.push(Node);
Vnum.push_back(temp);
pre[i]=i-1;
nxt[i]=i+1;
}
//头结点的下一个节点赋值为1
nxt[0] =1;
pre[N+1]=N;
//循环k次
while(!pq.empty()){
//找到最小值
node temp=pq.top();
pq.pop();
if(buffer[temp.index]!=0){
//如果缓冲区有内容,说明当前不可以进行操作,并且无效 需要更新数据之后跳过该循环
Vnum.at(temp.index)+=buffer.at(temp.index);
node tempN(temp.index,Vnum[temp.index]);
pq.push(tempN);
buffer.at(temp.index)=0;
}else{
//把最小值加到两边的节点上
int preIndex=pre.at(temp.index);
int nxtIndex=nxt.at(temp.index);
//更新他俩的缓冲区,这两个地方需要加上值
buffer.at(preIndex)+= Vnum.at(temp.index);
buffer.at(nxtIndex)+= Vnum.at(temp.index);
//删除
nxt.at(preIndex) = nxt.at(temp.index);
pre.at(nxtIndex) = pre.at(temp.index);
K--;
}
if(K==0) break;
}
//将缓冲区的所有值加上
for(int i=1;i<=N;i++){
Vnum[i]+=buffer[i];
}
pr();
return 0;
}