求助双倍经验崩了
查看原帖
求助双倍经验崩了
614725
masonpop楼主2022/10/2 17:35

这份代码在不加多次测试时在P3620ACP3620AC,为什么加了多测清空后WAWA?

#include <bits/stdc++.h>
using namespace std;
#define int long long
int t;
#define debug puts("fuck")
const int maxn=5e5+10;
struct node{
	int pre,nxt,val;//双向链表维护 
}a[maxn];
struct nOdE{
	int val,idx;
	bool operator<(const nOdE &o)const {
		return val>o.val;
	}
};
priority_queue<nOdE> q;
int n,k,now,last,ans;
bool vis[maxn];
inline void change(int x)//去pre[x],nxt[x],x,加上pre[x]+nxt[x]-x
{
	a[x].pre=a[a[x].pre].pre;
	a[x].nxt=a[a[x].nxt].nxt;
	a[a[x].pre].nxt=x;
	a[a[x].nxt].pre=x;
} 

signed main()
{
	scanf("%lld",&t);
	while(t--)
	{
		ans=0;
		memset(vis,0,sizeof(vis));
		while(!q.empty())q.pop();//清空 
		scanf("%lld%lld",&n,&k);
		scanf("%lld",&last);
		memset(a,0,sizeof(a));
		for(int i=2;i<=n;i++)
		{
			scanf("%lld",&now);
			a[i].val=now-last;//加入
			last=now;
			a[i].pre=i-1;
			a[i].nxt=i+1;
			q.push((nOdE){a[i].val,i});//加入 
		}
		a[0].val=a[n].val=LONG_MAX;//注意越界 
		for(int i=1;i<=k;i++)
		{
			while(vis[q.top().idx])q.pop();//排除
			nOdE cur=q.top();
			q.pop();
			ans+=cur.val;
			vis[a[cur.idx].pre]=1;vis[a[cur.idx].nxt]=1;//左右排除
			a[cur.idx].val=a[a[cur.idx].pre].val+a[a[cur.idx].nxt].val-a[cur.idx].val;//更新权值
			q.push((nOdE){a[cur.idx].val,cur.idx});
			change(cur.idx);
		}
		printf("%lld\n",ans);
	}
	
	return 0;
}
2022/10/2 17:35
加载中...