跳到正文

题解

题解:AT_abc407_e [ABC407E] Most Valuable Parentheses

Evolve_约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年5月27日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟
获赞
2

版本

  1. v1当前

思路

这道题的关键在于这个括号序列在什么情况下列是正确的,可以发现这个序列的每个前缀中的左括号数量都是要大于等于右括号数量的,既然要大于等于那么第一个肯定是左括号,如果想让这个序列继续满足条件那么之后每两个括号就要选一个左括号,此时就会发现每次只能选那些东西,而 ii 选小的 jj 选大的和 ii 选大的 jj 选小的是相同的,此时就可以用贪心的做法,每过一个位置就把那个位置的值加入,在选择时就只要选择一个没被选择的最大值即可。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define endl '\n'
using namespace std;
int n,a[1000001],sg;
void solve(){
	priority_queue<int>pq;
	sg=0;
	cin>>n;
	for(int i=1;i<=n*2;i++)cin>>a[i];
	for(int i=1;i<=n*2;i+=2){
		if(i==1)pq.push(a[i]);
		else{
			pq.push(a[i]);
			pq.push(a[i-1]);
		}
		sg+=pq.top();
		pq.pop();
	}
	cout<<sg<<endl;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T=1;
	cin>>T;
	while(T--)solve();
	return 0;
}
原文
存档评论3 条抓取于