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. 容器安全与EDR的异同

    以Docker为代表的容器技术,直接运行于宿主机操作系统内核,因此对于容器安全,很多人会有着这样的疑问:EDR(Endpoint Detection and Response)等主机安全方案,能否直接 ...

  2. 机器学习(二)数据处理&相似/异性度量

    机器学习(二)数据处理&相似/异性度量 https://woaielf.github.io/2017/03/17/dm-2/ 2017-03-17 ZOE    数据科学  机器学习/数据挖掘 ...

  3. IDEA如何像Eclipse打开多个项目?

    简述: 不能采用open方式,得采用 import module方式 (多个项目,可以不再同一个根目录下,真正的类似eclipse方式打开多个项目) 具体操作步骤: 1.选择一个maven项目,右键选 ...

  4. RQNOJ--2 开心的金明(01背包)

    题目:http://www.rqnoj.cn/problem/2 分析:这个题目每一种物品都是有"选"或"不选"两种情况. 属于01背包问题.物品的价格相当于背 ...

  5. springmvc-高级参数绑定-映射-异常-json数据交互-拦截器

    1.1. 高级参数绑定 1.1.1. 复制工程 把昨天的springmvc-web工程复制一份,作为今天开发的工程 复制工程,如下图: 粘贴并修改工程名为web2,如下图: 工程右键点击,如下图: 修 ...

  6. Activiti流程实例管理

    1.启动流程 在完成了流程定义部署后,就要启动流程实例了. /** * 1 启动流程 * 当流程到达一个节点时,会在act_ru_execution表中产生1条数据 * 如果当前节点是用户任务节点,这 ...

  7. string字符串 获取指定位置范围的子字符串

    string   str1="12345678";   str1.Substring(0,4);其中0表示要取得字符串的起始位置,4就是要取得字符串的长度  结果是 "1 ...

  8. sql里面插入语句insert后面的values关键字可省略

    插入到表名(列值)后跟一个查询语句的话就代表值,简单的说就是后面select select出来的值就是要插入的值,即  insert into tb(字段名一,字段名二)select 字段名一,字段名 ...

  9. 牛客网暑期ACM多校训练营(第一场)菜鸟补题QAQ

    签到题 J Different Integers(树状数组) 题目大意:给一个长为n的数组,每一个询问给两个数字i, j ,询问1~i, j~n这两个区间中有多少不同的数字,真的像是莫队裸题,但是两个 ...

  10. windows API 第八篇 _tcsicmp _stricmp _wcsicmp _mbsicmp

    这些函数都是比较字符串小写的,忽略大写,出入的字符串都将按照小写比较Perform a lowercase comparison of strings. 函数原型: int _stricmp( con ...