BZOJ3300: [USACO2011 Feb]Best Parenthesis

3300: [USACO2011 Feb]Best Parenthesis

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 89  Solved: 42
[Submit][Status]

Description

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. 
如果,平衡字符串“A”的得分是是S(A),那么字符串“(A)”得分是2*S(A) ; 
如果,“A”,“B” 得分分别是S(A)和S(B),那么平衡字符串“AB”得分为S(A)+S(B) 
例如:s("(())()") =s("(())")+s("()") = 2*s("()")+1 = 2*1+1 = 3.

Input

* 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 ‘)‘ 
第1行:N,平衡字符串长度 
第2至N+1行:Linei+1 整数0或1,0代表字符‘(’,1代表‘)’

Output

* Line 1: The score of the string. Since this number can get quite 
large, output the score modulo 12345678910. 
计算字符串得分,结果对12345678910取模

Sample Input

6
0
0
1
1
0
1
INPUT DETAILS:

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

Sample Output

3

HINT

Source

Silver

题解:

题意不明。。。没有说清每个左括号能不能都匹配到右括号。。。导致我想了半天。。。

本来想在栈里面直接算出答案来的,结果发现我算不出来。。。

然后去写dfs,发现还是很好写的。。。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 100000+1000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 12345678910
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
29     return x*f;
30 }
31 int p[maxn],n,top,sta[maxn];
32 inline ll dfs(int l,int r)
33 {
34     if(r==l+1)return 1;
35     ll tmp=0;
36     for(int i=l+1;i<r;i=p[i]+1)tmp+=2*dfs(i,p[i]),tmp%=mod;
37     return tmp;
38 }
39 int main()
40 {
41     freopen("input.txt","r",stdin);
42     freopen("output.txt","w",stdout);
43     n=read();
44     for1(i,n)
45     {
46         int x=read();
47         if(!x)sta[++top]=i;
48         else p[sta[top--]]=i;
49     }
50     ll ans=0;
51     for(int i=1;i<n;i=p[i]+1)ans+=dfs(i,p[i]),ans%=mod;
52     printf("%lld\n",ans);
53     return 0;
54 }

挖个坑,以后看看能不能想出在栈里直接搞定的方法。

时间: 2024-08-06 17:13:47

BZOJ3300: [USACO2011 Feb]Best Parenthesis的相关文章

B3300 [USACO2011 Feb]Best Parenthesis 模拟

这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<cmath> #include<ctime> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define d

3301: [USACO2011 Feb] Cow Line

3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 82  Solved: 49[Submit][Status][Discuss] Description The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer Joh

【BZOJ】【3301】【USACO2011 Feb】Cow Line

康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 序列->序号:(康托展开) 对于每个数a[i],数比它小的数有多少个在它之前没出现,记为b[i],$ans=1+\sum b[i]* (n-i)!$ 序号->序列:(逆康托展开) 求第x个排列所对应的序列,先将x-1,然后对于a[i],$\left\floor \frac{x}{(n-i)!} \right\floor $即为在它之后出现的比它小的数的个数,所以从小到大数一下有几个没出现的数,就知道a[

bzoj3301: [USACO2011 Feb] Cow Line

新姿势康托展开.. ------------------------------------ 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的-- 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:(康托展开) 对于每个数a[i],数比它小的数有多少个在它之前没出现,记为b[i],ans=1+∑b[i]?(n?i)!ans=1+∑b[i]?(n?i)! 序号->序列:(逆康托展开) 求第x个排列所对应的序列,先将x-

小结:模拟

技巧及注意: 细节方面十分重要,你要想啊,敢出模拟题一定有出模拟题的原因-出题人一定会放很多坑给你,所以在码之前一定要先有大概的思路框架,然后在写代码中思考 比如说这题:[BZOJ]3300: [USACO2011 Feb]Best Parenthesis(模拟) 一开始我没注意边界问题,所以一直爆wa.这个问题不要再犯,做一切模拟题也好,先码完后不要急着编译,先静态查错,最后觉得没有错误后再编译也不迟. 还有,过了样例不要以为过了,一些边界没处理好的话大数据不是wa就是re,所以最好自己能造多

BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整( dp )

最优的做法最后路面的高度一定是原来某一路面的高度. dp(x, t) = min{ dp(x - 1, k) } + | H[x] - h(t) | ( 1 <= k <= t ) 表示前 i 个路面单调不递减, 第 x 个路面修整为原来的第 t 高的高度. 时间复杂度O( n³ ). 令g(x, t) = min{ dp(x, k) } (1 <= k <= t), 则转移O(1), g() 只需在dp过程中O(1)递推即可, 总时间复杂度为O( n² ) 然后单调不递增也跑一遍

Parenthesis

G - Parenthesis Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Description Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th question is whether P remains balanced after p ai and

bzoj3943[Usaco2015 Feb]SuperBull*

bzoj3943[Usaco2015 Feb]SuperBull 题意: n头牛进行锦标赛,每场比赛的好看程度是两头牛的编号异或和,并总有一方被淘汰.求安排比赛(可以决定比赛胜负)可以得到的最大总好看程度是多少.n≤2000 题解: 先求出牛两两之间的异或和,然后发现可以把比赛看做连边,且共有n-1场比赛,所以求最大生成树就行了.神犇们用的都是Prim,蒟蒻不会,用Kruscal结果时间排倒数. 代码: 1 #include <cstdio> 2 #include <cstring>

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset