cf494A Treasure
Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful.
Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters.
Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.
The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.
If there is no way of replacing '#' characters which leads to a beautiful string print - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with.
If there are several possible answers, you may output any of them.
(((#)((#)
1
2
()((#((#(#()
2
2
1
#
-1
(#)
-1
|s| denotes the length of the string s.
题意是要把#替换成1个以上的")",使得"("和“)”的个数相等,且对于s的任意一个前缀,“)“的个数不大于”)“的个数。
因为个数相等,所以分配给#的”)“的个数之和是确定的。
然后很显然的贪心是前k-1个#都只分配一个")",最后一个#多分配一点")"使”(“和”)"的个数相等就好了
于是变成模拟题了
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void write(LL a)
{
if (a<0){printf("-");a=-a;}
if (a>=10)write(a/10);
putchar(a%10+'0');
}
char ch[100010];
int a[100010];
int s[100010];
int len,x,y,z,lst;
int main()
{
scanf("%s",ch);
for (int i=1;ch[i-1];i++)
{
if (ch[i-1]=='(')a[i]=1,x++;
if (ch[i-1]==')')a[i]=-1,y++;
if (ch[i-1]=='#')a[i]=0,z++,lst=i;
len=i;
}
if (x-y<z)
{
printf("-1");
return 0;
}
for (int i=1;i<=len;i++)
{
s[i]=s[i-1];
if (a[i]!=0)s[i]+=a[i];
else if (i==lst)s[i]-=x-(y+z-1);
else s[i]--;
if (s[i]<0)
{
printf("-1");
return 0;
}
}
for (int i=1;i<z;i++)
printf("1\n");
printf("%d\n",x-y-(z-1));
}
cf494A Treasure的更多相关文章
- Windows Phone 8.1低功耗蓝牙开发-Nokia Treasure Tag
1. 引言 上一篇文章<Windows 8.1 低功耗蓝牙开发>讲述了如何在Windows 8.1平台上创建低功耗蓝牙应用,并且以TI的Sensor Tag为例,给出了代码步骤和演示.其实 ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- poj 2594 Treasure Exploration (二分匹配)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 6558 Accepted: 2 ...
- hdu 5446 Unknown Treasure Lucas定理+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- POJ 1473 There's Treasure Everywhere!
题目链接 小小的模拟一下. #include <cstdio> #include <cstring> #include <string> #include < ...
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- POJ2594 Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8193 Accepted: 3358 Description Have ...
- zoj Treasure Hunt IV
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
随机推荐
- 远程连接mysql
win系统下,连接别人的mysql或者让别人链接自己的mysql: 打开命令行cmd 进入mysql: mysql -u root -p mysql>use mysql; mysql>s ...
- 关于XMLEncoder和XMLDecoder
我们用XMLEncoder和XMLDecoder来序列化和反序列化一个类. 我觉得需要注意的是,我们在new一个对象的时候,XMLEncoder本身默认的是类中无参的构造函数,我今儿在实现的时候,老是 ...
- phpstorm xdebug调试设置样式
由于本人比较喜欢白底的phpstorm开发环境,所以XDEBUG断点调试,F8时默认是没有背景色的,phpstorm中的设置方式如下 files -> settings -> Editor ...
- Java基础知识强化51:经典排序之桶排序(BucketSort)
1. 首先说明三点: (1)桶排序是稳定的 (2)桶排序是常见排序里最快的一种,比快排还要快…大多数情况下 (3)桶排序非常快,但是同时也非常耗空间,基本上是最耗空间的一种排序算法 2. 桶排序的分析 ...
- linux内存机制
~# free -m total used free shared buffers cachedMem: 16086 8579 7507 0 152 800 ...
- Android/Linux boot time优化
基于analyze_boot.py分析Android/Linux的kernel boot时间 1.修改HiKey的BoardConfig.mk文件,使能initcall_debug,增加dmesg b ...
- 【转】 LINQ To SQL 语法及实例大全
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...
- iframe顶部跳转跨域问题
$("#button").on("click", function () { // top.location.locatio ...
- IDEA - Project files cannot be watched (are they under network mount?)
在64位Linux系统上使用IDEA时遇到如下问题,启动时警告信息External file changes sync may be slow Project files cannot be watc ...
- C# 解析嵌套的json文件.
概述 今天我同学问我如何转换json文件,没处理过,网上搜了一下,json转excel的很少,反过来倒是有许多人写了工具. json文件的结构大致是这样的: {, , }, , "type& ...