为什么要将0,n放入二叉堆中?
就是这两行
rep(i,0,n)
Insert(i);
我觉得应该是这样
rep(i,1,n-1)
Insert(i);
为什么下面那种会WA啊
code
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define min3(xxx,yyy,zzz) min(min(xxx,yyy),zzz)
#define max3(xxx,yyy,zzz) max(max(xxx,yyy),zzz)
#define pii pair<int,int>
#define fi first
#define se second
#define rep(variable,leftrange,rightrange) for(int variable=leftrange;variable<=rightrange;++variable)
#define Rep(variable,leftrange,rightrange) for(int variable=leftrange;variable>=rightrange;--variable)
#define lb long double
#define mem(x,y) memset(x,y,sizeof x)
#define sq(x) ((x)*(x))
#define ss stable_sort
#define rs random_shuffle
#define gi greater<int>
#define vi vector<int>
template <typename T>inline void read(T &t)
{int c=getchar();t=0;while(!isdigit(c))c=getchar();while(isdigit(c))t=(t<<3)+(t<<1)+c-48,c=getchar();}
template <typename T,typename... Args> inline void read(T& t, Args&... args){read(t);read(args...);}
template <typename T> inline void wrt(T x)
{if(0<=x&&x<10) {putchar(x+'0');return ;}wrt(x/10);putchar(x%10+'0');}
template <typename T> inline void wrt(T x,char c) {wrt(x);putchar(c);}
const int N=2e5+1 ;
int n,a[N],L[N],R[N],pos[N];
#define fa (x>>1)
#define sl (x<<1)
#define sr (x<<1|1)
int heap[N],siz=0;
bool cmph(int x,int y){return a[x]<a[y];}
void Up(int x){while(x>1) {if(!cmph(heap[fa],heap[x])) swap(pos[heap[x]],pos[heap[fa]]),swap(heap[x],heap[fa]),x=fa;else break;}}
void Insert(int x){heap[++siz]=x;pos[x]=siz;Up(siz);}
int Top(){return heap[1];}
void Down(int x){while(sl<=siz){if(sr<=siz && !cmph(heap[sl],heap[sr]))
{if(!cmph(heap[x],heap[sr])) swap(pos[heap[x]],pos[heap[sr]]),swap(heap[x],heap[sr]),x=sr;else break;}
else {if(!cmph(heap[x],heap[sl])) swap(pos[heap[x]],pos[heap[sl]]),swap(heap[x],heap[sl]),x=sl;else break;} } }
void Pop(){pos[heap[siz]]=1; heap[1]=heap[siz--];Down(1);}
void Remove(int x){pos[heap[siz]]=x; heap[x]=heap[siz--];Up(x),Down(x);}
int k;
int main()
{
read(n,k);
rep(i,1,n)
read(a[i]);
rep(i,1,n-1)
a[i]=a[i+1]-a[i];
a[0]=1e9;
a[n]=1e9;
rep(i,0,n)
L[i]=i-1,R[i]=i+1;
rep(i,0,n)
Insert(i);
int ans = 0;
rep(i,1,k)
{
int c = Top();
Pop();
ans += a[c] ;
a[c]=a[L[c]]+a[R[c]]-a[c];
Insert(c);
Remove(pos[L[c]]);
Remove(pos[R[c]]);
R[L[L[c]]] = c;
L[R[R[c]]] = c;
L[c] = L[L[c]] ;
R[c] = R[R[c]] ;
}
cout<<ans;
return 0;
}