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. libvirtsAPI

    mongodb远程服务器连接 mongo -uroot -p321 master.puppet.org:27017/admin

  2. 红领:挺进高端 青岛财经日报-htmlmainVerName

    红领:挺进高端 青岛财经日报-htmlmainVerName 红领:挺进高端

  3. java——Arrays.asList()方法

    Arrays.asList() 是将数组作为列表 问题来源于: public class Test { public static void main(String[] args) { int[] a ...

  4. Redis哨兵模式

    Redis-Sentinel redis的哨兵模式 Redis Sentinel 模式简介 Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案 ...

  5. Python进阶(面向对象编程基础)(一)

    鉴于昨天被类和函数折腾得晕头转向,今特把类的知识翻出来温习. 1.定义类并创建实力对象 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ ...

  6. Jmeter接口测试案例实践(一)

    1.1. 接口介绍 本次测试的接口采用内网中的通讯录查询接口进行测试,接口参数如下: 1.2. 使用Jmeter进行接口测试 1.2.1. 打开Jmeter 下载好Jmeter后,双击bin目录下的j ...

  7. 转载:mybatis和hibernate 解析

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  8. oracle触发器实例

    8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2.3 创建替代(INS ...

  9. 路由器WAN口和LAN口详解

    前一阵子做路由器的联网,由于利用了Openwrt操作系统,做起来虽然方便,但是很多原理细节都被忽略了.所以这里再来老生常谈一下wan口和lan口的区别,以及他们之间的工作原理. 首先百度一下,基本知识 ...

  10. VirtualBox虚拟机网络设置

    VirtualBox虚拟机网络设置 测试环境:物理机win10企业版本,VirtaulBox版本5.0.14,虚拟机安装Windows XP及linux系统 想实现虚拟机上网的最简单方式,修改虚拟机网 ...