3016: [Usaco2012 Nov]Clumsy Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 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 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

题解:
不错的贪心。

用s记录前面多出来的'('的个数,每次读入'('s++,读入')'s--

s<0时表示没有‘(’还多出')'就需要把')'变成'(',这样s+=2,ans++

最后如果s!=0的时候还要把一半的'('变成')',就是ans+=s/2

想的时候因为思路比较乱,不敢大胆想,所以没想出来T_T

代码:

 #include<cstdio>
int n,s,ans;
char ch;
int main()
{
while (scanf("%c",&ch)!=EOF)
{
if (ch=='\n')break;
if (ch=='(')s++;
if (ch==')')
{
s--;
if (s<){s+=;ans++;}
}
}
ans+=s/;
printf("%d",ans);
}

BZOJ3016: [Usaco2012 Nov]Clumsy Cows的更多相关文章

  1. 3016: [Usaco2012 Nov]Clumsy Cows

    3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 91  Solved: 69[Submi ...

  2. 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...

  3. BZOJ 3016 [Usaco2012 Nov]Clumsy Cows:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3016 题意: 给你一个括号序列,问你至少修改多少个括号,才能使这个括号序列合法. 题解: ...

  4. USACO Clumsy Cows

    洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows 洛谷传送门 JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows JDOJ传送门 Desc ...

  5. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

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

    树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...

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

    从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...

  8. 3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Sub ...

  9. 3018: [Usaco2012 Nov]Distant Pastures

    3018: [Usaco2012 Nov]Distant Pastures Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved: 20[ ...

随机推荐

  1. 求一个数组中第K小的数

    面试南大夏令营的同学说被问到了这个问题,我的第一反应是建小顶堆,但是据他说用的是快排的方法说是O(n)的时间复杂度, 但是后来经过我的考证,这个算法在最坏的情况下是O(n^2)的,但是使用堆在一般情况 ...

  2. Linux+eclipse+gdb调试postgresql源码

    pg内核源码解析课上用的vs调试pg源码, VS用起来确实方便,但是配置调试环境着实有点麻烦.首先得装个windows系统,最好是xp,win7稍微麻烦点:最好使用vs05,08和10也可以,但是比0 ...

  3. js 获取10个不重复随机数

    var arr1 = new Array(); var arr2 = new Array(); for(var i = 0; i<20; i++){ arr1.push(i); } for(va ...

  4. JScript_Test

    Hello SyntaxHighlighter function helloSyntaxHighlighter() { return "hi!"; } function hello ...

  5. (转)awk命令

    转自:http://man.lupaworld.com/content/manage/ringkee/awk.htm#id2874788 整理:Jims of 肥肥世家 <jims.yang@g ...

  6. SpringMVC+highstock实现曲线报表

    最近项目要做曲线报表,之前用的是生成图片然后传到前端,感觉不是很好,在网上找到资料说highstock就可以而且还可以做类似股票的那种实时的曲线,研究的一段时间把项目问题解决了做个总结: 首先把hig ...

  7. iOS动画开发之五——炫酷的粒子效果

    在上几篇博客中,我们对UIView层的动画以及iOS的核心动画做了介绍,基本已经可以满足iOS应用项目中所有的动画需求,如果你觉得那些都还不够炫酷,亦或是你灵光一现,想用UIKit框架写出一款炫酷的休 ...

  8. 忘记了SqlServer的SA密码怎么办

    转自 http://v-consult.be/2011/05/26/recover-sa-password-microsoft-sql-server-2008-r2/ 如果忘记了sa密码,并且wind ...

  9. C#生成Code39(extend)条形码【非条形码字体】

    Code39是条形码的一种.由于编制简单.能够对任意长度的数据进行编码.支持设备广泛等特性而被广泛采用. 能够对任意长度的数据进行编码.其局限在于印刷品的长度和条码阅读器的识别范围. 支持设备广泛.目 ...

  10. out返回值的用法与用途

    static void Main(string[] arr) { Console.WriteLine("请输入用户名"); string uname = Console.ReadL ...