Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3)

题目传送门

题意:

给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列

思路:

这么考虑:

如果是左括号

1)整个序列左括号个数比右括号多 2

2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数

3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2

(这里这么想,把左变成右,左-1,右+1)

右括号也是这样

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],pre[N],post[N];
char s[N];
int n;
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s",s+1);
        int x=0;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            if(s[i]==‘(‘) x++;
            else x--;
            a[i]=x;
        }
       pre[0]=N,post[n]=N;
       for(int i=1;i<n;i++) pre[i]=min(pre[i-1],a[i]);
       for(int i=n-1;i>=0;i--) post[i]=min(post[i+1],a[i]);
       int ans=0;
       if(x!=-2&&x!=2)
       {
           printf("0\n");
       }
       else{
       for(int i=1;i<=n;i++)
       {
           if(s[i]==‘(‘)
           {
               if(pre[i-1]>=0&&post[i]>=2&&x==2) ans++;
           }
           else if(pre[i-1]>=0&&post[i]>=-2&&x==-2) ans++;
       }
        printf("%d\n",ans);
       }
    }
}

原文地址:https://www.cnblogs.com/zhgyki/p/10336488.html

时间: 2024-07-30 14:03:23

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)的相关文章

# Codeforces Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Description Input Output Sample Input 51 2 1 2 1 Sample Output 1 1 1 2 2 题解:这个题有做慢了,这种题做慢了和没做出来区别不大... 读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那

Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 1  - (b-1).那么我们可以从这一点入口来进行解题.. mod (x, b) = 1 时, x  =  b + 1, 2b + 1, 3b + 1..... a * b + 1. mod (x , b) = 2 时, x =  2b + 2, 4b + 2, 6b + 2, ..... 2a * b

Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x,3操作是将下标为k的数变为x. 注意成段更新的时候,遇到一个区间的最大值还小于x的话就停止更新. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

Codeforces Round #588 (Div. 2) D. Marcin and Training Camp(思维)

链接: https://codeforces.com/contest/1230/problem/D 题意: Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. L

Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)

传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad. Let's consider the following algorithm of generating a

Codeforces Round #354 (Div. 2) B. Pyramid of Glasses (模拟+思维)

原题请戳这里 题意: 将杯子摆成杨辉三角状,即顶层1个杯子,第二层2个杯子,……第N层N个杯子. 每一秒能倒满1个杯子,每次一个杯子满了,酒会平分得流入它下面支撑它的两个杯子中. 如下图所示.1 ≤ n ≤ 10, 0 ≤ t ≤ 10 000. 分析:由于n很小,所以直接模拟此过程,其实也是一个递推的过程. 注意,如果递推的时候没有递推到n+1层,那么最后统计的时候是统计>=1的个数而不是==1的个数, 因为当酒能倒满的杯子大于n层杯子即n*(n+1)/2<t时,递推过程终止于n层,不能向下

Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to