求验可能的正确性
查看原帖
求验可能的正确性
551428
ainivolAGEM楼主2024/10/29 16:49

rt,考场上想的,民间数据过了,但是没有太明确的正确性证明?(代码应该题解区最短)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+4;
const int A=1e6+4;
ll t,n,a[N],b[N];
ll ans,lst[A],dp[N];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		cin>>n;
		memset(lst,0,sizeof(lst));
		memset(b,0,sizeof(b));
		ans=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]==a[i-1]){
				n-=(--i)&1|1;
				ans+=a[i];
			}
		}
		for(int i=1;i<=n;i++){
			if(!lst[a[i]]){
				lst[a[i]]=i;
			}else{
				b[i]=lst[a[i]];
				lst[a[i]]=i;
			}
		}
		for(int i=1;i<=n;i++){
			if(b[i]){
				dp[i]=max(dp[i-1],dp[b[i]+1]+a[i]);
			}else{
				dp[i]=dp[i-1];
			}
		}
		cout<<dp[n]+ans<<'\n';
	}
	return 0;
}
2024/10/29 16:49
加载中...