题目链接:https://codeforces.ml/contest/1556/problem/C
思路:由于n只有1000,可以枚举每组左括号向右最多能匹配到多少组ans。具体细节见代码,想不通的地方画画图就迎刃而解了。
ac代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
typedef long long LL;
LL a[N];
int n;
int main(){
ios::sync_with_stdio(false);
cin >> n;
LL ans = 0;
for(int i = 1;i <= n;i++)cin >> a[i];
if(n&1)n--;
for(int i = 1;i <= n;i += 2){
LL mn = a[i];
LL sum = 0;
for(int j = i + 1;j <= n;j++){
if(j&1)sum+=a[j];
else{
LL f = a[j];
LL kase = min(sum,f);
if(kase){
f-=kase;
sum-=kase;
if(sum==0)ans++;
}
if(f){
LL cnt = min(mn,f);
mn-=cnt,f-=cnt;
ans+=cnt;
}
if(f)break;
}
}
}
cout << ans << endl;
return 0;
}