3016: [Usaco2012 Nov]Clumsy Cows

3016: [Usaco2012 Nov]Clumsy Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 91  Solved: 69
[Submit][Status][Discuss]

Description

Bessie the cow is trying to type a balanced string of parentheses into her new laptop, but she is sufficiently clumsy (due to her large hooves) that she keeps mis-typing characters. Please help her by computing the minimum number of characters in the string that one must reverse (e.g., changing a left parenthesis to a right parenthesis, or vice versa) so that the string would become balanced. There are several ways to define what it means for a string of parentheses to be "balanced". Perhaps the simplest definition is that there must be the same total number of (‘s and )‘s, and for any prefix of the string, there must be at least as many (‘s as )‘s. For example, the following strings are all balanced:

()

(())

()(()())

while these are not:

)(

())(

((())))

问题描述

给定长度为n的一个括号序列,每次修改可以修改一个位置的括号,若这个括号为’(‘,则修改为’)’,若这个括号为’)’,则修改为’(‘,问最少修改多少个使得原括号序列合法。

其中:

①     ()是合法的;

②     若A是合法的,则(A)是合法的;

③     若AB都是合法的,则AB是合法的。

Input

一个长度为n个括号序列。

Output

最少的修改次数。

Sample Input

())(

Sample Output

2

样例说明
修改为()(),其中红色部分表示修改的括号。

数据范围
100%的数据满足:1 <= n <= 100,000。

HINT

Source

Silver

题解:一个很神奇的模拟,感觉自己水水哒

 1 /**************************************************************
 2     Problem: 3016
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:72 ms
 7     Memory:220 kb
 8 ****************************************************************/
 9
10 var ch:char;s,ans:longint;
11 begin
12      while not(eoln) do
13            begin
14                 read(ch);
15                 if ch=‘(‘ then inc(s) else
16                    begin
17                         dec(s);
18                         if s<0 then
19                            begin
20                                 inc(s,2);
21                                 inc(ans);
22                            end;
23                    end;
24            end;
25      ans:=ans+s div 2;
26      writeln(ans);
27 end.
时间: 2024-12-30 03:50:00

3016: [Usaco2012 Nov]Clumsy Cows的相关文章

【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows 贪心 单调栈

Description Bessie the cow is trying to type a balanced string of parentheses into her new laptop, but she is sufficiently clumsy (due to her large hooves) that she keeps mis-typing characters. Please help her by computing the minimum number of chara

BZOJ3016: [Usaco2012 Nov]Clumsy Cows

3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 52[Submit][Status] Description Bessie the cow is trying to type a balanced string of parentheses into her new laptop, but she is sufficiently clumsy (due to h

3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Submit][Status][Discuss] Description Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(

BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )

树形dp..水 ------------------------------------------------------------------------ #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define clr( x , c )

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 252  Solved: 185[Submit][Status] Description 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号为C1和C2的奶牛

BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ---------------------------------------------------------------------------- #include<bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; ++i) #define clr(x, c) memset(x, c, sizeof(x)) #define forea

BZOJ3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Submit][Status] Description Farmer John's N cows (1 <= N <= 50,000) are grazing along a one-dimensional fence. Cow i is standing at location x(i) and has

bzoj2060[Usaco2010 Nov]Visiting Cows 拜访奶牛*

bzoj2060[Usaco2010 Nov]Visiting Cows 拜访奶牛 题意: 给棵树,要求如果取了某个节点就不能取与它相邻的节点,问最多可取几个节点.树的大小≤50000. 题解: 树形dp.令f[i][0]不取i节点,f[i][1]为取i节点,则方程为f[i][0]=sum(max(f[j][0],f[j][1]+1)),f[i][1]=sum(f[j][0]),j为i的子节点.最后答案为max(f[1][0],f[1][1]+1).注意不要漏了那个“+1”. 代码: 1 #in

【BZOJ 2060】 [Usaco2010 Nov]Visiting Cows 拜访奶牛

2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 257 Solved: 190 [Submit][Status][Discuss] Description 经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号