P3015 [USACO11FEB]Best Parenthesis 【模拟】

题目描述

Recently, the cows have been competing with strings of balanced parentheses and comparing them with each other to see who has the best one.

Such strings are scored as follows (all strings are balanced): the string '()' has score 1; if 'A' has score s(A) then '(A)' has score 2*s(A); and if 'A' and 'B' have scores s(A) and s(B), respectively, then 'AB' has score s(A)+s(B). For example, s('(())()') = s('(())')+s('()') = 2*s('()')+1 = 2*1+1 = 3.

Bessie wants to beat all of her fellow cows, so she needs to calculate the score of some strings. Given a string of balanced parentheses of length N (2 <= N <= 100,000), help Bessie compute its score.

给定一个只包含左右括号的字符串,得分规则如下:

如果一对括号内没有括号,那么这对括号的得分为1;如果两对括号互不包含(即并列存在),那这两对括号的得分相加;如果括号内包含一对括号,那么这个括号的得分纪为内部括号序列的得分*2。

例如:对于这样一个字符串:"() ()",两对括号并列存在,则得分为1+1=2;

而对于这样一个字符串:"(())",最外层的括号内层包含一对括号,则得分为2*1=2.

Bessie想击败所有同事的牛,所以她需要计算某个字符串的评分。给定一个长度为n、只包含括号的字符串(2≤N≤100000),计算其得分帮助Bessie。

输入格式

* Line 1: A single integer: N

* Lines 2..N + 1: Line i+1 will contain 1 integer: 0 if the ith character of the string is '(', and 1 if the ith character of the string is ')'

输出格式

* Line 1: The score of the string. Since this number can get quite large, output the score modulo 12345678910.

输入输出样例

输入 #1<button class="copy&#45;btn lfe&#45;form&#45;sz&#45;middle" data&#45;v&#45;370e72e2="" data&#45;v&#45;52f2d52f="" type="button">复制</button>
6 
0 
0 
1 
1 
0 
1 
输出 #1<button class="copy&#45;btn lfe&#45;form&#45;sz&#45;middle" data&#45;v&#45;370e72e2="" data&#45;v&#45;52f2d52f="" type="button">复制</button>
3 

说明/提示

This corresponds to the string "(())()".

As discussed above.

输出答案要mod12345678910

 

思路

  存储两个合法括号序列间有多少对括号计算即可。

  能 % 就 %, 真就wa穿嗷。

  另外题意解释的不是很清楚,给一组数据帮助理解下

  14

  0 0 0 1 1 1 0 0 0 1 1 0 1 1

  输出应该是 4 + 2 * ( 2 + 1 ) = 10 

 

CODE

 

 1 #include <bits/stdc++.h>
 2 #define dbg(x) cout << #x << "=" << x << endl
 3 #define eps 1e-8
 4 #define pi acos(-1.0)
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 
 9 const int inf = 0x3f3f3f3f;
10 
11 template<class T>inline void read(T &res)
12 {
13     char c;T flag=1;
14     while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
15     while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
16 }
17 
18 namespace _buff {
19     const size_t BUFF = 1 << 19;
20     char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
21     char getc() {
22         if (ib == ie) {
23             ib = ibuf;
24             ie = ibuf + fread(ibuf, 1, BUFF, stdin);
25         }
26         return ib == ie ? -1 : *ib++;
27     }
28 }
29 
30 int qread() {
31     using namespace _buff;
32     int ret = 0;
33     bool pos = true;
34     char c = getc();
35     for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
36         assert(~c);
37     }
38     if (c == '-') {
39         pos = false;
40         c = getc();
41     }
42     for (; c >= '0' && c <= '9'; c = getc()) {
43         ret = (ret << 3) + (ret << 1) + (c ^ 48);
44     }
45     return pos ? ret : -ret;
46 }
47 
48 const LL MOD = 12345678910;
49 
50 int n;
51 
52 int a[1000007];
53 
54 int main()
55 {
56     //freopen("data.txt", "r", stdin);
57     scanf("%d",&n);
58     LL ans = 0;
59     LL sum = 0, cnt = 0;
60     for ( int i = 1; i <= n; ++i ) {
61         scanf("%d",&a[i]);
62     }
63     for ( int i = 1; i <= n; ++i ) {
64         if(a[i] == 0) {
65             cnt++;
66         }
67         else {
68             cnt--;
69             //dbg(cnt);
70             if(!a[i-1]) {
71                 LL temp = 1;
72                 for ( int j = 1; j <= cnt; ++j ) {
73                     temp *= 2;
74                     temp %= MOD;
75                 }
76                 sum += temp;
77                 sum %= MOD;
78                 //dbg(sum);
79             }
80         }
81     }
82     cout << sum % MOD << endl;
83     return 0;
84 }
View Code

 

 

 

#include  < bits/stdc++.h >
#define  dbg (x ) cout  << #x  <<  " = "  << x  << endl
#define  eps  1e - 8
#define  pi  acos (- 1.0 )

using  namespace  std ;
typedef  long  long LL ;

const  int inf  =  0x3f3f3f3f ;

template < class  T > inline  void  read ( T  &res )
{
     char c ;T flag = 1 ;
     while ((c = getchar ())< ' 0 ' ||c > ' 9 ' ) if (c == ' - ' )flag =- 1 ;res =c - ' 0 ' ;
     while ((c = getchar ())>= ' 0 ' &&c <= ' 9 ' )res =res * 10 +c - ' 0 ' ;res *=flag ;
}

namespace   _buff   {
     const   size_t  BUFF  =   1   <<   19 ;
     char  ibuf [ BUFF ],   * ib  =  ibuf ,   * ie  =  ibuf ;
     char   getc ()   {
         if   ( ib  ==  ie )   {
            ib  =  ibuf ;
            ie  =  ibuf  +   fread ( ibuf ,   1 ,  BUFF ,  stdin );
         }
         return  ib  ==  ie  ?   - 1   :   * ib ++;
     }
}

int  qread ()  {
     using  namespace  _buff ;
     int ret  =  0 ;
     bool pos  =  true;
     char c  =  getc ();
     for  (;  (<  ' 0 '  || c  >  ' 9 ' )  && c  !=  ' - ' ; c  =  getc ())  {
         assert (~ c );
     }
     if  (==  ' - ' )  {
        pos  =   false;
        c  =   getc ();
     }
     for  (; c  >=  ' 0 '  && c  <=  ' 9 ' ; c  =  getc ())  {
        ret  =   ( ret  <<   3 )   +   ( ret  <<   1 )   +   ( ^   48 );
     }
     return pos  ? ret  :  -ret ;
}

const LL MOD  =  12345678910 ;

int n ;

int a [ 1000007 ];

int  main ()
{
    //freopen("data.txt", "r", stdin);
     scanf ( " %d " ,&n );
    LL ans  =  0 ;
    LL sum  =  0 , cnt  =  0 ;
     for  (  int i  =  1 ; i  <= n ;  ++)  {
         scanf ( " %d " ,&a [ i ]);
     }
     for  (  int i  =  1 ; i  <= n ;  ++)  {
         if (a [ i ]   ==   0 )   {
            cnt ++;
         }
         else   {
            cnt --;
            //dbg(cnt);
             if (!a [ i - 1 ])   {
                LL temp  =   1 ;
                 for   (   int  j  =   1 ;  j  <=  cnt ;   ++ )   {
                    temp  *=   2 ;
                    temp  %=  MOD ;
                 }
                sum  +=  temp ;
                sum  %=  MOD ;
                //dbg(sum);
             }
         }
     }
    cout  << sum  % MOD  << endl ;
     return  0 ;
}
全部评论

相关推荐

牛客77743221...:做一段时间,公司出钱送你去缅甸和泰国旅游
点赞 评论 收藏
分享
想run的马里奥在学...:这个学历帮你扫平百分之80的障碍,投就完了,这会找不到就等3月暑期一样能找到
点赞 评论 收藏
分享
01-14 10:23
已编辑
湖南师范大学 计调
太久没更新,前几天看到一条评论,说“牛客就是当年那群做题区毕业了开始找工作还收不住那股味”的群体。字里行间透着居高临下的评判,不是,他该不会以为自己很幽默?很犀利吧?作为在牛客混了不算短日子的用户,我感到的不只是被冒犯,更是一种深刻的悲哀——这种以“松弛感”为名,对另一种生存策略的轻蔑,颇有一种自己考不上大学早早出来混社会,嘲笑考上大学的人是书呆子,然后大言不惭地说:死读书有什么用,人脉和资源才是硬道理。我不知道说这个话的人,手头究竟握着多少真正管用的人脉与资源,也不知道他这么傲慢地说出“那股味”的时候,是站在哪一个巨人的肩膀上,才能如此“松弛从容”地俯视众生,还能品评出别人身上“没收住”的余...
淬月星辉:这种评论把正常的努力扭曲成卷😂,说白了就是自己不努力,看着身边努力的人一个个都事业有成了,自己的心里开始不平衡了,就发这种酸言酸语。牛客可以说是我用过那么多平台里社区氛围最好的论坛了,用了大半年了,基本上没见过有人吵架的,都是在互帮互助提建议,帮忙看简历的,帮忙选offer的,帮忙指点学习路线的,分享工作经验和趣事的,我觉得这才是互联网该有的样子。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务