Time Limit: 10 Sec Memory Limit: 128 MB

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

解:

期望:100  实际:20

暴力实现有问题。读题不认真,没有取模。太菜了。

我的想法:从小括号开始扫,每次将对应包含的一个括号集团消除掉

不知道哪里写挂了。

为什么我总是这么弱呢?

正解:分治。

预处理出每个左括号所对应的右括号的所在位置。

然后对于整个问题,分治,用搜索得到部分解,然后更新答案。

 #include<iostream>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
const ll mod=12345678910ll;
int n,s[],top,pos[];
ll dfs(int l,int r)
{
ll nw=,p=pos[l];
if(l+==p) nw=(nw+)%mod;
else nw=(nw+*dfs(l+,p-)%mod)%mod;
if(p+<=r) nw=(nw+dfs(p+,r)%mod)%mod;
return nw;
}
int main()
{
scanf("%d",&n);
for(int i=,x;i<=n;++i)
{
scanf("%d",&x);
if(x==) s[++top]=i;
else if(top) pos[s[top--]]=i;
}
printf("%lld",dfs(,n));
return ;
}

[USACO2011 Feb]Best Parenthesis的更多相关文章

  1. BZOJ3300: [USACO2011 Feb]Best Parenthesis

    3300: [USACO2011 Feb]Best Parenthesis Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 89  Solved: 42 ...

  2. BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟

    Description Recently, the cows have been competing with strings of balanced  parentheses and compari ...

  3. 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...

  4. B3300 [USACO2011 Feb]Best Parenthesis 模拟

    这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<c ...

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

    康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...

  6. BZOJ2274: [Usaco2011 Feb]Generic Cow Protests

    2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solve ...

  7. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  8. 3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 82  Solved: 49[Submit ...

  9. 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

    2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 97 ...

随机推荐

  1. 013-elasticsearch5.4.3【五】-搜索API【二】term术语查询-termQuery、rangeQuery、existsQuery、prefixQuery、wildcardQuery、regexpQuery、fuzzyQuery

    一.概述 虽然全文查询将在执行之前分析查询字符串,但Term级查询将根据存储在倒排索引中的确切术语进行操作. 这些查询通常用于结构化数据,如keyword.数字,日期和枚举,而不是全文字段.或者,它们 ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第3节 Random类_10-练习一_生成1-n之间

    生成随即数的时候,总是会短一个,因为它是从0开始的 这是左闭右闭区间. 循环100次输出的结果

  3. SpringMVC学习01——HelloSpringMvc Demo

    HelloWorldController.java文件 package com.su.controller; import org.springframework.stereotype.Control ...

  4. ETCD分布式锁实现选主机制(Golang实现)

    ETCD分布式锁实现选主机制(Golang) 为什么要写这篇文章 做架构的时候,涉及到系统的一个功能,有一个服务必须在指定的节点执行,并且需要有个节点来做任务分发,想了半天,那就搞个主节点做这事呗,所 ...

  5. Python笔记(二十八)_魔法方法_迭代器

    迭代器用于遍历容器中的数据,但它不是容器,它是一个实现了__next__方法的对象 与迭代器相关的内置函数: iter(): 将一个对象转换成一个迭代器 next(): 访问迭代器中的下一个变量,直到 ...

  6. 【ABAP系列】SAP WEB GUI的实现,SAP在网页中使用

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP WEB GUI的实现,SAP ...

  7. memset, fill 对bool,int 赋值的效率

    memset对bool型变量赋false比对int型变量赋0快了10倍 fill对bool型变量赋false和对int型变量赋0效率一样 fill对int型变量赋0比memset对int型变量赋0慢了 ...

  8. xshell输入字母空格间距变大

    按一下shift+空格(全角/半角转换的快捷键,引起的问题)

  9. STL关联式容器之set\map ----以STL源码为例

    关联式容器的特征:所用元素都会根据元素的键值自动被排序. set STL 中的关联式容器低层数据结构为红黑树,其功能都是调用低层数据结构中提供的相应接口. set元的元素不会像map那样同时拥有键(k ...

  10. [19/05/06-星期一] JDBC(Java DataBase Connectivity,java数据库连接)_基本知识

    一.概念 JDBC(Java Database Connectivity)为java开发者使用数据库提供了统一的编程接口,它由一组java类和接口组成.是java程序与数据库系统通信的标准API. J ...