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
0
0
1
1
0
1
INPUT DETAILS:
This corresponds to the string "(())()".
Sample Output
HINT
Source
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 100000+1000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 12345678910
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int p[maxn],n,top,sta[maxn];
inline ll dfs(int l,int r)
{
if(r==l+)return ;
ll tmp=;
for(int i=l+;i<r;i=p[i]+)tmp+=*dfs(i,p[i]),tmp%=mod;
return tmp;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)
{
int x=read();
if(!x)sta[++top]=i;
else p[sta[top--]]=i;
}
ll ans=;
for(int i=;i<n;i=p[i]+)ans+=dfs(i,p[i]),ans%=mod;
printf("%lld\n",ans);
return ;
}
挖个坑,以后看看能不能想出在栈里直接搞定的方法。
BZOJ3300: [USACO2011 Feb]Best Parenthesis的更多相关文章
- BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟
Description Recently, the cows have been competing with strings of balanced parentheses and compari ...
- 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)
http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...
- B3300 [USACO2011 Feb]Best Parenthesis 模拟
这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<c ...
- [USACO2011 Feb]Best Parenthesis
Time Limit: 10 Sec Memory Limit: 128 MB Description Recently, the cows have been competing with stri ...
- 【BZOJ】【3301】【USACO2011 Feb】Cow Line
康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...
- BZOJ2274: [Usaco2011 Feb]Generic Cow Protests
2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solve ...
- BZOJ3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 67 Solved: 39[Submit ...
- 3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 82 Solved: 49[Submit ...
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 138 Solved: 97 ...
随机推荐
- Vagrant 部署python开发环境
Vagrant简介 Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境.它使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境. 在Windows下 ...
- IIS rewrite映射规则语法格式
IIS rewrite映射规则语法格式,特殊符号:&请用& amp;代替,否则异常. <configuration> <system.webServer> &l ...
- spring aop原理分析
持续更新... aop跟java代理模式有关. java.lang.reflect.Proxy java.lang.reflect.InvocationHandler 工厂模式用到java反射. ao ...
- asp.net+Sqlserver 通过存储过程读取数据
Sqlserver代码 创建存储过程如下: /*根据父id获取类别总数*/ IF EXISTS (SELECT name FROM sysobjects WHERE name = N'getsite ...
- MSSQL 查询统计某状态出现的次数及累计时间
1.问题来源 最近客户需要统计某个设备,某状态,在某一个时间段内出现的次数,并计算累计出现的时间. 数据源如下: 现在如果要统计UPSCTSTA状态为D出现的次数(同一状态,连续出现的认为是一次),并 ...
- iptables阻止服务器被攻击
下列规则将会阻止来自某一特定IP范围内的数据包,因为该IP地址范围被管理员怀疑有大量恶意攻击者在活动: # iptables -t filter -A INPUT -s 123.456.789.0/ ...
- C# linq的学习及使用
linq可以对多种数据源和对象进行查询,如数据库.数据集.XML文档.数组等. 通过对linq的使用,可以减少代码量并优化检索操作. LINQ关键字 from 指定数据源和范围变量 where 根据布 ...
- C/C++中的++a和a++
代码: #include <iostream> #include <cstdio> using namespace std; int main(){ ; (++a)+=a; / ...
- Linux 从 sar 到 sar2html 的认识
这些变形的工具.诸如:淘宝的Tsar.ksar.sar2html....等.都是通过抓取 sar的数据 所以在最终呈现的数据上变化不大.只是展现的手段和效果不一样而已 sar 是帮助我们 ...
- PHPCMS v9修改栏目或者单页没有权限
问题: 普通管理员没有权限修改栏目,只有超级管理员才有权限修改,凡是非超级管理员,设置栏目权限后只能添加栏目,不能对栏目进行修改和删除操作,提示您没有权限操作该项这使系统的一个BUG. 一.对于普 ...