多倍经验题求助(悬棺)
查看原帖
多倍经验题求助(悬棺)
616964
Adolfo_North楼主2023/8/11 21:52

[APIO/CTSC2007] 数据备份A了,但这题死活A不了,该请空的全清了,也不知道为什么

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+1;
int n,m,ans;
int pre[N],nxt[N],w[N];
bool flag[N];
struct node{
	int first,second;
	bool operator <(node it) const
	{
		return first>it.first;
	} 
};
priority_queue<node> p;
void del(int id){
	nxt[pre[id]]=nxt[id];
	pre[nxt[id]]=pre[id];
	flag[id]=1;
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int T;
	cin>>T;
	while(T--){
		memset(nxt,0,sizeof nxt);
		memset(pre,0,sizeof pre);
		memset(flag,0,sizeof flag);
		while(!p.empty()) p.pop();
		ans=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			cin>>w[i];
			pre[i]=i-1,nxt[i]=i+1;
		}
		long long xx=1e18;
		for(int i=1;i<n;i++) w[i]=w[i+1]-w[i],p.push({w[i],i});
		w[0]=w[n]=xx;
		while(m--){
			while(!p.empty()&&flag[p.top().second]) p.pop();
			node l=p.top();
			p.pop();
			ans+=l.first;
			w[l.second]=w[pre[l.second]]+w[nxt[l.second]]-w[l.second];
			del(pre[l.second]);
			del(nxt[l.second]);
			p.push({w[l.second],l.second});
		}
		cout<<ans<<'\n';
	}
	return 0;
}
2023/8/11 21:52
加载中...