【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 ...
随机推荐
- JavaScript 数组(Array)方法(二)
forEach ES5新增的方法,Arr.forEach((value, index,array)=>{}); let arr=['a','b','c']; arr.forEach((val,i ...
- iOS之UIGraphics.h方法简介
// // UIGraphics.h // UIKit // // Copyright (c) 2005-2017 Apple Inc. All rights reserved. // #import ...
- samba初级使用记录
首先安利一下什么是samba: Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在 ...
- hibernate查询timestamp条件
参考https://blog.csdn.net/zuozuoshenghen/article/details/50540661 Mysql中Timestamp字段的格式为yyyy-MM-dd HH-m ...
- mysql limit 偏移量过大效率解决方式 转贴
原文地址:https://www.jianshu.com/p/f8d81df7ab28 SELECT * FROM product , ) limit SELECT * FROM product a ...
- 11_数据降维PCA
1.sklearn降维API:sklearn. decomposition 2.PCA是什么:主成分分析 本质:PCA是一种分析.简化数据集的技术. 目的:是数据维数压缩,尽可能降低原数据的维数(复杂 ...
- Undertow服务器基础分析 - XNIO
阅读更多 我们从名字上就能看出这是一个NIO思想为基础的IO框架,X是指这个框架可以有多种实现,我们可以从代码库 https://github.com/xnio 中发现一个项目xnio-native, ...
- C# GDI+编程(二)
常用的绘图函数 DrawArc绘制一个弧形 示例:graphics.DrawArc(pen,,,,,,) 倒数第二个参数,表示起始度数,最后一个参数是弧形的跨越度数.比如起始度数是90,跨越度数是12 ...
- css 阴影设置box-shadow
box-shadow: 1px 2px 5px 6px #888888 inset 1px: 必须,水平阴影的位置, 负值为左 2px: 必须,垂直阴影的位置, 负值为上; 5px: 可选,模糊距离; ...
- Leetcode959. Regions Cut By Slashes由斜杠划分区域
在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /.\ 或空格构成.这些字符会将方块划分为一些共边的区域. (请注意,反斜杠字符是转义的,因此 \ 用 &quo ...