猎奇!为什么我的代码会厌氧?
  • 板块灌水区
  • 楼主huangboming
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/12/8 22:09
  • 上次更新2024/12/9 17:19:58
查看原帖
猎奇!为什么我的代码会厌氧?
782985
huangboming楼主2024/12/8 22:09

此代码加O2的话的6个测试点会错,不加O2的话就A了。 逆天,谁知道为什么? (P10478 生日礼物)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool f[1000005];
ll fl=10,l[1000005],r[1000005];
ll m,n,a,sum[1000005],o,ans,x,zs; 
priority_queue<pair<ll,ll>, vector<pair<ll,ll> >, greater<pair<ll,ll> > > q;
int main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a);
		if(a>=0){
			if(fl!=0){
				l[o]=o-1;
				r[o]=o+1;
				zs++;
				fl=0;
				o++;
			}
		}
		else{
			if(fl!=1){
				l[o]=o-1;
				r[o]=o+1;
				o++;
				fl=1;
			}
		}
		sum[o]+=a;
	} 
	o-=(sum[o]<0);
	for(int i=1;i<=o;i++){
		if(sum[i]>0)ans+=sum[i];
		q.push({abs(sum[i]),i});
	}
	r[0]=1;
	l[o+1]=o;
	sum[0]=1e9;
	sum[o+1]=1e9;
	while(zs>m) {
		while(f[q.top().second])
			q.pop();
		x=q.top().second;
		q.pop();
		if((!l[x]||r[x]==o+1)&&sum[x]<0)
			continue;
		zs--;
		ans-=abs(sum[x]);
		sum[x]+=sum[l[x]]+sum[r[x]];
		f[l[x]]=1;
		f[r[x]]=1;
		l[x]=l[l[x]];
		r[x]=r[r[x]];
		r[l[x]]=l[r[x]]=x;
		q.push({abs(sum[x]),x});
	}
	printf("%lld",ans);
	return 0;
}
2024/12/8 22:09
加载中...