ABC-407-E - Most Valuable Parentheses(最有价值的括号) · znzryb
## 思路讲解
和这道题目比较像,但又不完全一样。
[CF-1011-D. Serval and Kaitenzushi Buffet](/2025/05/08/20250508-3164f2cd)
套路那比较像,但不完全一样。
其实最重要的就是要找到一个贪心结构,我们这边发现,倒过来遍历,然后一直选左括号,一旦右括号不够了,这个时候就要选一个左括号变为右括号。又因为前面的左括号
```cpp
ROF(i,N-1,1){
ans+=A[i];
pq.push(i);
balance--;
if(balance<0){ // 倒过来遍历,结果发现右括号数量不够了,这个时候必须牺牲一个左括号了
ans-=A[pq.top()];
// #ifdef LOCAL
// debug(pq.top());
// debug(A[pq.top()]);
// #endif
pq.pop();
balance+=2;
}
}
```
## AC代码
[https://atcoder.jp/contests/abc407/submissions/66140638](https://atcoder.jp/contests/abc407/submissions/66140638)
::: 源代码
```cpp
// Problem: E - Most Valuable Parentheses
// Contest: AtCoder - AtCoder Beginner Contest 407
// URL: https://atcoder.jp/contests/abc407/tasks/abc407_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define getFro(vec) (vec.empty()?0:vec.front())
#define getBac(vec) (vec.empty()?0:vec.front())
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define DEBUG(variable) \
do { \
std::cerr << #variable << ":"; \
for (const auto& elem : variable) { \
std::cerr << elem << " "; \
} \
std::cerr << "\n"; \
} while (0)
#define uniVec(var) \
do { \
sort(var.begin(),var.end());\
var.resize(unique(var.begin(),var.end())-var.begin());\
} while (0)
#define debugN(var,N) \
do{ \
std::cerr<<#var<<":"; \
FOR(i,1,N){ \
std::cerr<<var[i]<<" "; \
} \
std::cerr<<"\n"; \
}while(0)
#define debugMap(variable) \
do { \
std::cerr << #variable << ":\n"; \
for (const auto& pair : variable) { \
std::cerr << " " << pair.first << " => " << pair.second << "\n"; \
} \
std::cerr << std::endl; \
} while (0)
#define lson(var) (var<<1)
#define rson(var) ((var<<1)+1)
#define vecMxx(vec,x) (vec.end()-lower_bound(all(vec),x)) // vec中大于x的数有几个
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr3;
typedef array<ll,2> arr2;
typedef double DB;
typedef long double LD;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e18)+3;
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;
ll N,M,Q,X,K,lT,A[MAXN];
struct cmp{
bool operator()(ll a,ll b){
return A[a]>A[b];
}
};
/*
*/
inline void solve(){
cin>>N;
N=2*N;
FOR(i,1,N){
cin>>A[i];
}
ll ans=0,balance=1;
priority_queue<ll,vector<ll>,cmp> pq;
ROF(i,N-1,1){
ans+=A[i];
pq.push(i);
balance--;
if(balance<0){ // 倒过来遍历,结果发现右括号数量不够了,这个时候必须牺牲一个左括号了
ans-=A[pq.top()];
// #ifdef LOCAL
// debug(pq.top());
// debug(A[pq.top()]);
// #endif
pq.pop();
balance+=2;
}
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>lT;
while(lT--){
solve();
}
// solve();
return 0;
}
/*
AC
https://atcoder.jp/contests/abc407/submissions/66140638
*/
```
:::
## 心路历程(WA,TLE,MLE……)