【贪心】答案错误,求调
查看原帖
【贪心】答案错误,求调
539344
CJ_Fu楼主2022/7/14 17:36

rt,样例2、3不过

#include<iostream>
#include<queue>
#include<climits>
#include<algorithm>
typedef long long ll;
#define p(x,y) (c){x,y}
#define tp tmp.p
#define tw tmp.w
#define qp q.top()
#define qtp q.top().p
#define qow q.top().w
#define qey q.empty()
#define qop q.pop();
using namespace std;
const int maxn=500003;
struct c{
	ll w,p;
	bool operator<(c x)const{return w>x.w;}
};
priority_queue<c>q;
int n,k;
ll ans;
ll a[maxn],b[maxn],l[maxn],r[maxn],f[maxn];
int main(){
	cin>>n>>k;
	cin>>a[0];
	k--;
	for(int i=1;i<=k;i++){
		cin>>a[i];
//		cout<<a[i]<<endl;
	}
	sort(a,a+n+1);
	for(int i=1;i<=k;i++){
		b[i]=abs(a[i]-a[i-1]);
		q.push(p(b[i],i));
//		cout << b[i] << " " << qtp << endl;
		l[i]=i-1;
		r[i]=i+1;
	}
	b[0]=LONG_LONG_MAX>>1;
	l[1]=0;r[k]=0;
	q.push(p(b[0],0));
	while(n--){
		c tmp=qp;
		qop;
		if(f[tp]){
			n++;
			continue;
		}
		f[l[tp]]=f[r[tp]]=1;
		ans+=b[tp];
//		cout << tp << " " << b[tp] << endl;
		b[tp]=b[l[tp]]+b[r[tp]]-b[tp];
		q.push(p(b[tp],tp));
		l[tp]=l[l[tp]];
		r[tp]=r[r[tp]];
		r[l[tp]]=tp;
		l[r[tp]]=tp;
	}
	cout<<ans;
	return 0;
}
2022/7/14 17:36
加载中...