题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5659

题意:

  你有一个只包含"(" 和 ")" 的串,每一个位置有个数值,这个数值是当前的左括号-右括号的值。

  例:()() 数值就是1010。

  给你一个打乱了的数值,要你构造出字典序最小的字符串。

题解:

  因为左括号比右括号小,所以我们要尽量的选择左括号,选择左括号会使得数值增大,如果这个数值不能继续增大了我们就只能选择右括号。

  记得要初始化

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
int a[maxn];
int num[maxn];
int last[maxn];
int ans[maxn];
void solve()
{
ms(num, );
ms(last, );
int n;scanf("%d", &n);
for(int i = ;i<n;i++) scanf("%d", &a[i]);
for(int i = ;i<n;i++){
if(a[i]<){
printf("invalid");return;
}
}
for(int i = ;i<n;i++){
num[a[i]]++;
}
last[]=;
for(int i=;i<=n;i++){
if(num[last[i-]+]){
last[i] = last[i-]+;
ans[i] = ;
num[last[i-]+]--;
}
else if(num[last[i-]-]){
last[i] = last[i-]-;
ans[i] = ;
num[last[i-]-]--;
}
else{
printf("invalid");return;
}
}
if(ans[n]==){
for(int i = ;i<=n;i++)
if(ans[i]==) printf("(");
else printf(")");
}else{
printf("invalid");return;
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// IOS
int t;scanf("%d", &t);
int cnt = ;
while(t--){
printf("Case %d: ", cnt++);
solve();
printf("\n");
}
return ;
}

uva live 7637 Balanced String (贪心)的更多相关文章

  1. UVaLive 7637 Balanced String (构造)

    题意:给定一个括号的序列,原先的序列是碰到左括号加1,碰到右括号减1,然后把序列打乱,让你找出字典序最小的一个答案. 析:直接从第一个括号判断就好了,优先判断左括号,如果不行就加右括号. 代码如下: ...

  2. 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题

    https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...

  3. Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)

    题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...

  4. UVALive - 7637 E - Balanced String(构造)

    原题链接 题意:给出一个打乱顺序的序列,问是否能构造出一个括号匹配的字符串.每个数字为此前读取到的左括号数减去右括号数. 分析:有左括号开始构造,不够的话就找右括号.注意特殊情况待处理.详情看代码 # ...

  5. UVA 10123 No Tipping (物理+贪心+DFS剪枝)

    Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm, it will ...

  6. AIM Tech Round (Div. 2) B. Making a String 贪心

    B. Making a String 题目连接: http://codeforces.com/contest/624/problem/B Description You are given an al ...

  7. uva 993 Product of digits (贪心 + 分解因子)

      Product of digits  For a given non-negative integer number N , find the minimal natural Q such tha ...

  8. UVA - 11292 Dragon of Loowater 贪心

    贪心策略:一个直径为X的头颅,应该让雇佣费用满足大于等于X且最小的骑士来砍掉,这样才能使得花费最少. AC代码 #include <cstdio> #include <cmath&g ...

  9. hdu6299 Balanced Sequence 贪心

    题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...

随机推荐

  1. JAVA获取当前系统时间System.currentTimeMillis()以及获取运行时间

    System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMi ...

  2. 获取客户机MAC地址 根据IP地址 获取机器的MAC地址 / 获取真实Ip地址

    [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref ...

  3. thinkPHP三级城市联动

    html+js: <!doctype html> <html lang="en"> <head> <meta charset=" ...

  4. Sublime Text 3 安装及汉化操作

    Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等.还可自定义键绑定,菜单和工具栏.Sublime Text 的主要功能包括:拼写检查,书签,完整的 ...

  5. 搜索专题: HDU1429胜利大逃亡

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. FFmpeg SDK开发模型之中的一个:解码器

    简单介绍 本例解说了怎样使用ffmpeg SDK解码媒体文件: 參考源代码是ffmpeg 自带的apiexample.c 一.源代码#include <stdlib.h>#include ...

  7. Mac 切换bash zsh等shell

    现在假设大家都安装了iTerm2,我们先把bash切换成zsh,使用命令行如下: chsh -s /bin/zsh 执行命令后,会让你输入电脑的密码,输入即可.完成后,需要完全退出iTerm2,再次进 ...

  8. Spark Streaming整合logstash + Kafka wordCount

    1.安装logstash,直接解压即可 测试logstash是否可以正常运行 bin/logstash -e 'input { stdin { } } output { stdout {codec = ...

  9. Python webdriver调用Chrome报错

    报错信息如下: selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to b ...

  10. es6 async和await

    es7 async和await ,作为genertor函数语法糖,在使用上比generator函数方便的,Generator 函数就是一个封装的异步任务,或者说是异步任务的容器.异步操作需要暂停的地方 ...