codeforces deltix round summer 2021C题
  • 板块学术版
  • 楼主brnhbrnh
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/8/30 14:41
  • 上次更新2023/11/4 08:33:20
查看原帖
codeforces deltix round summer 2021C题
152183
brnhbrnh楼主2021/8/30 14:41

仅向参加了8月29日该比赛的人求助 以下是原题 William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to you as a sequence of positive integers c1,c2,…,cn where ci is the number of consecutive brackets "(" if i is an odd number or the number of consecutive brackets ")" if i is an even number.

For example for a bracket sequence "((())()))" a corresponding sequence of numbers is [3,2,1,3].

You need to find the total number of continuous subsequences (subsegments) [l,r] (l≤r) of the original bracket sequence, which are regular bracket sequences.

A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not.

Input The first line contains a single integer n (1≤n≤1000), the size of the compressed sequence.

The second line contains a sequence of integers c1,c2,…,cn (1≤ci≤109), the compressed sequence.

Output Output a single integer — the total number of subsegments of the original bracket sequence, which are regular bracket sequences.

It can be proved that the answer fits in the signed 64-bit integer data type.

我在第13个点卡住了,现在网上也还没有题解。 我的思路是这样的,首先用正常的方法判断合法区间,然后用一个数字存住前面的合法区间个数,当有合法区间连在一起则答案加上前面的合法区间,代码如下。

有无大佬讲下不同的思路:)

#include <bits/stdc++.h>
#define ri register int
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char s=getchar();
	while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
	while(isdigit(s)){x=x*10+s-'0';s=getchar();}
	return x*f;
}
const int maxn=1010;
int n=0,a[maxn],fr=0,t=0,p=0,tp=0;
ll ans=0;
int main(){
	n=read();
	if(n==1){
		printf("0");
		return 0;
	}
	for(ri i=1;i<=n;i++){
		a[i]=read();if(i==n){break;} 
		a[++i]=read();
		if(a[i-1]>a[i]){
			fr=i-1;
			ans+=a[i];a[i-1]-=a[i];a[i]=0;
			p++;
		}
		else if(a[i-1]<a[i]){
			ans+=a[i-1];a[i]-=a[i-1];a[i-1]=0;
			if(fr){
				ans+=p;
				if(a[fr]<a[i]){
					ans+=a[fr];a[i]-=a[fr];a[fr]=0;
					ans+=t;
					t=0;fr=0;p=0;
				}
				else if(a[fr]>a[i]){
					ans+=a[i];a[fr]-=a[i];a[i]=0;
					p++;
				}
				else{
					ans+=a[fr];ans+=t;a[fr]=0;a[i]=0;
					p=0;t++;fr=0;tp=i;
				}
			}
			else{
				t=0;p=0;tp=0;
			}
		}
		else{
			ans+=a[i];
			if(tp>=fr) ans+=t;
			ans+=p;
			t++;tp=i;
		}
	}
	printf("%lld",ans);
	return 0;
}

2021/8/30 14:41
加载中...