P3056 [USACO12NOV]笨牛Clumsy Cows

题目描述

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:

)(())(((())))

给出一个偶数长度的括号序列,问最少修改多少个括号可以使其平衡。

输入输出格式

输入格式:

  • Line 1: A string of parentheses of even length at most 100,000 characters.

输出格式:

  • Line 1: A single integer giving the minimum number of parentheses that must be toggled to convert the string into a balanced string.

输入输出样例

输入样例#1:

())(
输出样例#1:

2

说明

The last parenthesis must be toggled, and so must one of the two middle right parentheses.

陷入dp的泥潭无法自拔,然后终于在题解中看到了一个真·贪心。



其实可以这样贪:只需保证每一个前缀左括号数目大于等于右括号的数目即可。

这样的话有两种贪法(但其实是一种而已?)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> const int MAXN = 100000 + 10; char str[MAXN];
int n;
int cnt;
int num; int main()
{
freopen("data.txt", "r", stdin);
scanf("%s", str + 1);
n = strlen(str + 1);
for(int i = 1;i <= n;i ++)
{
if(str[i] == '(' )num ++;
else num--;
if(num < 0)cnt++,num+=2;
}
printf("%d", cnt + num/2);
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> const int MAXN = 100000 + 10; char str[MAXN];
int n;
int cnt;
int num; int main()
{
freopen("data.txt", "r", stdin);
scanf("%s", str + 1);
n = strlen(str + 1);
for(int i = 1;i <= n;i ++)
{
if(str[i] == '(' )num ++;
else if(str[i] == ')' && num > 0)num--;
else cnt++,num++;
}
printf("%d", cnt + num/2);
return 0;
}

【P3056】【USACO12NOV】笨牛Clumsy Cows的更多相关文章

  1.  洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows

    P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...

  2. USACO Clumsy Cows

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

  3. BZOJ3016: [Usaco2012 Nov]Clumsy Cows

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

  4. 3016: [Usaco2012 Nov]Clumsy Cows

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

  5. [Swift]LeetCode1006. 笨阶乘 | Clumsy Factorial

    Normally, the factorial of a positive integer n is the product of all positive integers less than or ...

  6. LeetCode竞赛题:笨阶乘(我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。)

    通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.相反,我们设计了一个笨 ...

  7. 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]

    题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...

  8. 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...

  9. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

随机推荐

  1. C#可扩展编程之MEF(五):MEF高级进阶

      好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较 ...

  2. LintCode刷题笔记-- A+B problem

    标签: 位运算 描述 Write a function that add two numbers A and B. You should not use + or any arithmetic ope ...

  3. jeecms vue-cli项目结构详解

    Vue-cli是vue官方出品的快速构建单页应用的脚手架,如果你是初次尝试Vue,不建议使用,推荐你使用普通引入javascript文件的方式进行学习,如果你已经有vue基础那么就可以用vue-cli ...

  4. Lint found fatal errors while assembling a release target问题的解决方案

    此问题发生在编译为 release 版本时,出现错误提示如下: Lint found fatal errors while assembling a release target. To procee ...

  5. css 超出两行省略号,超出一行省略号

    参考:https://www.cnblogs.com/yangguojin/p/10301981.html 超出一行省略: p{ white-space:nowrap; overflow:hidden ...

  6. PKU OJ A Bug's life

    http://bailian.openjudge.cn/tm2018/G/ #include <iostream> #include <vector> #include < ...

  7. Leetcode240. Search a 2D Matrix II搜索二维矩阵2

    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...

  8. Redis Replication & Sentinel

    实践目标: Redis Replication 一主:192.168.1.104 双从:192.168.1.101 192.168.1.103 Sentinel:192.168.1.102 系统环境: ...

  9. fileinput使用心得

    下咋以及一些具体使用过程就不叙述了,简单说一下使用时候需要注意的几点 1.在js中封装好的fileinput函数 /* * 初始化fileInput控件(第一次初始化) * type 不同类别 * i ...

  10. 不同目录cookie共享的问题解决 cookie不同页面访问不到的问题

    一般设置cookie的方法是setcookie(key, value, expire),参数分别的意思是建.值.过期时间,这里是大众的默认设置方法,但是忽略了一个问题,setcookie还有path与 ...