【P3056】【USACO12NOV】笨牛Clumsy Cows
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.
输入输出样例
())(
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的更多相关文章
- 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows
P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...
- USACO Clumsy Cows
洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows 洛谷传送门 JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows JDOJ传送门 Desc ...
- BZOJ3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 52[Submi ...
- 3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 91 Solved: 69[Submi ...
- [Swift]LeetCode1006. 笨阶乘 | Clumsy Factorial
Normally, the factorial of a positive integer n is the product of all positive integers less than or ...
- LeetCode竞赛题:笨阶乘(我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。)
通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.相反,我们设计了一个笨 ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
随机推荐
- PHP接收数据数据包的几个方式
PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型. php获取post参数的几种方式 1.$_POST['paramName'] 只能接收Co ...
- CreateProcess函数详解及示例
WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程运行指定的可执行文件. 函数原型: BOOL CreateProcess ( LPCTSTR lpApplic ...
- 基于SpringBoot+AntDesign的快速开发平台,JeecgBoot 2.0.2 版本发布
Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. 强大的代 ...
- Charles抓包(http/https请求)
Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装官网下载安装Charles:https://www.charlesproxy.com/download/当然由于国情可以使用破 ...
- ftp文件上传下载命令
介绍:从本地以用户wasqry登录的机器1*.1**.21.67上通过ftp远程登录到ftp服务器上,登录用户名是lte****,以下为使用该连接做的实验. 查看远程ftp服务器上用户lte**** ...
- AutoMapper简介
先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说白了就是一个对象,只不过里边全是数据而已. 为什么要用DTO? 1.DTO更注重数据,对领域对 ...
- __autoreleasing 修饰符
将对象赋值给附有__autoreleasing 修饰符的变量等同于ARC 无效时调用对象的autorelease方法.我们通过以下源代码来看一下. @autoreleasepool { id __au ...
- XSS“从1到0”
时隔半年终于也应该更新了,之前说的每周更新也因为懒散这个借口变得遥遥无期.之所以叫这个标题,是在Freebuf上看到一篇文章,开头作者问到:“网上大多的文章标题都是XXX从0开始,可我们到底什么时候能 ...
- Redis源码解析:22sentinel(三)客观下线以及故障转移之选举领导节点
八:判断实例是否客观下线 当前哨兵一旦监测到某个主节点实例主观下线之后,就会向其他哨兵发送"is-master-down-by-addr"命令,询问其他哨兵是否也认为该主节点主观下 ...
- 实战课堂 | DMS企业版教你用一条SQL搞定跨实例查询
背景 数据管理DMS企业版提供了安全.高效地管理大规模数据库的服务.面对多元的数据库实例,为了更方便地查询被“散落”在各个地方的业务数据,我们在DMS企业版中提供了跨数据库实例查询服务. 什么是跨实例 ...