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. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第2节 匿名对象_6-匿名对象的说明

    没有名字的对象,叫做匿名对象 新建一个Person类 把赵又廷赋值交给匿名对象Person里面的成员变量name 想调用里面的ShowName的话 还需要再定义一个匿名对象. 第三个对象是一个全新的. ...

  2. 测开之路九十九:js函数、事件、window窗体对象

    函数:function 函数名(参数列表) 事件 单击:onclick()表单提交:onsubmit()鼠标经过:onmouseover()值改表时:onchange() window窗体对象转跳:w ...

  3. dataframe中的数据类型及转化

    1 float与str的互化 import pandas as pd import numpy as np df = pd.DataFrame({'a':[1.22, 4.33], 'b':[3.44 ...

  4. [Forward]C++ Properties - a Library Solution

    orig url: https://accu.org/index.php/journals/255 roperties are a feature of a number of programming ...

  5. 前端借助接口获取ip地址

    <script language="javascript" src="http://www.codefans.net/ajaxjs/jquery1.3.2.js&q ...

  6. js-用判断音乐或图片是否加载完成的方式来控制页面的现实

    判断页面加载,加完完成后,内容页显示,加载条隐藏 百度搜索方法很多,大多都是: document.onreadystatechange = function() //当页面加载状态改变的时候执行fun ...

  7. [USACO 2008 Jan. Silver]架设电话线 —— 最短路+二分

    一道图论的最短路题.一开始连最短路都没想到,可能是做的题太少了吧,完全没有思路. 题目大意: FJ的农场周围分布着N根电话线杆,任意两根电话线杆间都没有电话线相连.一共P对电话线杆间可以拉电话线,第i ...

  8. 关于服务器无法在已发送http表头之后设置状态问题

    Response.ClearHeaders()方法 ClearHeaders方法只删除头信息,而不删除Response显示输出信息. this.Response.BufferOutput = true ...

  9. java项目中,针对缓存问题的处理方式【接口中的处理方式】

    1.在service包中,分别建立了关于缓存的一系列的接口.类等,封装到一个工具包中: 临时缓存的接口(代码部分): package com.tools; import java.util.Date; ...

  10. JDK8 parallelStream性能测试

    https://blog.csdn.net/u011870280/article/details/80700993 public static void main(String[] args) {lo ...