原题链接

题意:给出一个打乱顺序的序列,问是否能构造出一个括号匹配的字符串。每个数字为此前读取到的左括号数减去右括号数。

分析:有左括号开始构造,不够的话就找右括号。注意特殊情况待处理。详情看代码

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<climits>
#include<ctype.h>
#include<set>
#include<map>
#define pi acos(-1.0)
#define mem(a) memset(a,0,sizeof(a))
#define mems(a,b) memset((a),(b),sizeof(a))
#define ll long long
#define ull unsigned long long
//#define LOCAL
#define ls root<<1
#define rs root<<1|1
#define Ls root<<1,l,mid
#define Rs root<<1|1,mid+1,r
using namespace std;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
const int mod=1e9+;
map<int,int>mp;
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
int kase=;
while(t--){
int n;
scanf("%d",&n);
mp.clear();
for(int i=;i<n;i++) scanf("%d",&a[i]),mp[a[i]]++;
printf("Case %d: ",++kase);
if((n&)&&!mp.count()){
puts("invalid");
continue;
}
mp[]--;
int pos=,flag=;
for(int i=;i<n&&!flag;i++){
if(mp[pos+]&&mp.count(pos+)){
pos++;
mp[pos]--;
}else if(mp[pos-]&&mp.count(pos-)){
pos--;
mp[pos]--;
}
else flag=;
}
if(flag&&pos){
puts("invalid");
continue;
}
mp.clear();
for(int i=;i<n;i++) mp[a[i]]++;
printf("(");
pos=;
for(int i=;i<n;i++){
if(mp[pos+]&&mp.count(pos+)){
printf("(");
pos++;
mp[pos]--;
}else if(mp[pos-]&&mp.count(pos-)){
pos--;
mp[pos]--;
printf(")");
}
}
puts("");
}
return ;
}

UVALive - 7637 E - Balanced String(构造)的更多相关文章

  1. UVaLive 7637 Balanced String (构造)

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

  2. uva live 7637 Balanced String (贪心)

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

  3. codeforces 709D D. Recover the String(构造)

    题目链接: D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. java的String构造对象的几种方法以及内存运行过程

    String类创建对象的方法可以分为以下三种 1.String a = "123"; 2.String b = new String("123"); 3.Str ...

  5. AIM Tech Round 3 (Div. 1) B. Recover the String 构造

    B. Recover the String 题目连接: http://www.codeforces.com/contest/708/problem/B Description For each str ...

  6. UVaLive 6588 && Gym 100299I (贪心+构造)

    题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i  在pos 位置, ...

  7. LeetCode 1234. Replace the Substring for Balanced String

    原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...

  8. AtCoder Grand Contest 032 B - Balanced Neighbors——构造

    题意 B - Balanced Neighbors 给定一个整数 $N$($3\leq N \leq 100$),构造一个顶点编号为 $1...N$ 的无向图,需满足如下两个条件: 简单图且连通 存在 ...

  9. 【leetcode】1234. Replace the Substring for Balanced String

    题目如下: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string i ...

随机推荐

  1. JHipster - Generate your Spring Boot + Angular/React applications!

    JHipster - Generate your Spring Boot + Angular/React applications!https://www.jhipster.tech/

  2. Gradle vs Maven Comparison

    https://gradle.org/maven-vs-gradle/ Maven需要搭建本地仓库:Gradle直接集成了仓库. ??? 从Maven转移到Gradle,对于如何使用类似于Maven的 ...

  3. What is the best Java email address validation method?

    https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method htt ...

  4. activiti-explorer disable demo

    https://community.alfresco.com/thread/203012-activiti-explorer engine.properties # demo data propert ...

  5. hive数据查询

    Fetch task 丢弃了mapreduce的作业的繁重任务,查询方便简单 1.第一种方式 2.linux命令行 3.地3中

  6. ubuntu 环境 celery配置全解

    继续尝试没有时间弄明白的技术. celery官方文档地址:http://docs.celeryproject.org/en/stable/getting-started/introduction.ht ...

  7. 什么样的 GitHub 才适合放简历上?

    为什么 GitHub 可以加分? 很多招聘描述上面都会备注 GitHub 是加分项,那么为什么它是加分项呢? 停,如果看到这里你还不知道 GitHub 是什么,可以看一下 Phodal的手记:http ...

  8. const,static,volatile关键字的作用

    const关键字: 1.欲阻止一个变量被改变,可使用const,在定义该const变量时,需先初始化,以后就没有机会改变他了: 2.对指针而言,可以指定指针本身为const,也可以指定指针所指的数据为 ...

  9. python 模块之-re

    就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹 ...

  10. C# 对象与JSON字符串互相转换的三种方式

    C# 对象与JSON字符串互相转换的三种方式 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 关于内存对象和JSON字符串的相互转换, ...